# NAG Library Function Documentnag_ode_bvp_fd_lin_gen (d02gbc)

## 1  Purpose

nag_ode_bvp_fd_lin_gen (d02gbc) solves a general linear two-point boundary value problem for a system of ordinary differential equations using a deferred correction technique.

## 2  Specification

#include <nag.h>
#include <nagd02.h>
void  nag_ode_bvp_fd_lin_gen (Integer neq,

void  (*fcnf)(Integer neq, double x, double f[], Nag_User *comm),

void  (*fcng)(Integer neq, double x, double g[], Nag_User *comm),
double a, double b, double c[], double d[], double gam[], Integer mnp, Integer *np, double x[], double y[], double tol, Nag_User *comm, NagError *fail)

## 3  Description

nag_ode_bvp_fd_lin_gen (d02gbc) solves the linear two-point boundary value problem for a system of neq ordinary differential equations in the interval $\left[a,b\right]$. The system is written in the form

$y ′ = F x y + g x$  (1)
and the boundary conditions are written in the form

$Cy a + Dy b = γ$  (2)
Here $F\left(x\right)$, $C$ and $D$ are neq by neq matrices, and $g\left(x\right)$ and $\gamma $ are neq component vectors. The approximate solution to (1) and (2) is found using a finite difference method with deferred correction. The algorithm is a specialisation of that used in the function nag_ode_bvp_fd_nonlin_gen (d02rac) which solves a nonlinear version of (1) and (2). The nonlinear version of the algorithm is described fully in Pereyra (1979).
You need to supply an absolute error tolerance and may also supply an initial mesh for the construction of the finite difference equations (alternatively a default mesh is used). The algorithm constructs a solution on a mesh defined by adding points to the initial mesh. This solution is chosen so that the error is everywhere less than your tolerance and so that the error is approximately equidistributed on the final mesh. The solution is returned on this final mesh.
If the solution is required at a few specific points then these should be included in the initial mesh. If, on the other hand, the solution is required at several specific points, then you should use the interpolation functions provided in Chapter e01 if these points do not themselves form a convenient mesh.

## 4  References

Pereyra V (1979) PASVA3: An adaptive finite-difference Fortran program for first order nonlinear, ordinary boundary problems Codes for Boundary Value Problems in Ordinary Differential Equations. Lecture Notes in Computer Science (eds B Childs, M Scott, J W Daniel, E Denman and P Nelson) 76 Springer–Verlag

## 5  Arguments

1:     neq – IntegerInput
On entry: the number of equations; that is neq is the order of system (1).
Constraint: ${\mathbf{neq}}\ge 2$.
2:     fcnf – function, supplied by the userExternal Function
fcnf must evaluate the matrix $F\left(x\right)$ in (1) at a general point $x$.
The specification of fcnf is:

void  fcnf (Integer neq, double x, double f[], Nag_User *comm)
1:     neq – IntegerInput
On entry: the number of differential equations.
2:     x – doubleInput
On entry: the value of the independent variable $x$.
3:     f[${\mathbf{neq}}×{\mathbf{neq}}$] – doubleOutput
On exit: the $\left(i,j\right)$th element of the matrix $F\left(x\right)$, for $i,j=1,2,\dots ,{\mathbf{neq}}$ where ${F}_{ij}$ is set by ${\mathbf{f}}\left[\left(i-1\right)×{\mathbf{neq}}+\left(j-1\right)\right]$. (See Section 9 for an example.)
4:     comm – Nag_User *
Pointer to a structure of type Nag_User with the following member:
p – Pointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ should be cast to the required type, e.g., struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument comm below.)
3:     fcng – function, supplied by the userExternal Function
fcng must evaluate the vector $g\left(x\right)$ in (1) at a general point $x$.
The specification of fcng is:

void  fcng (Integer neq, double x, double g[], Nag_User *comm)
1:     neq – IntegerInput
On entry: the number of differential equations.
2:     x – doubleInput
On entry: the value of the independent variable $x$.
3:     g[neq] – doubleOutput
On exit: the $\mathit{i}$th element of the vector $g\left(x\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{neq}}$. (See Section 9 for an example.)
4:     comm – Nag_User *
Pointer to a structure of type Nag_User with the following member:
p – Pointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ should be cast to the required type, e.g., struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument comm below.)
If you do not wish to supply fcng the actual argument fcng must be the NAG defined null function pointer NULLFN.
4:     a – doubleInput
On entry: the left-hand boundary point, $a$.
5:     b – doubleInput
On entry: the right-hand boundary point, $b$.
Constraint: ${\mathbf{b}}>{\mathbf{a}}$.
6:     c[${\mathbf{neq}}×{\mathbf{neq}}$] – doubleInput/Output
7:     d[${\mathbf{neq}}×{\mathbf{neq}}$] – doubleInput/Output
8:     gam[neq] – doubleInput/Output
On entry: the arrays c and d must be set to the matrices $C$ and $D$ in (2). gam must be set to the vector $\gamma $ in (2).
On exit: the rows of c and d and the components of gam are re-ordered so that the boundary conditions are in the order:

(i)    conditions on $y\left(a\right)$ only;
(ii)   condition involving $y\left(a\right)$ and $y\left(b\right)$; and
(iii)  conditions on $y\left(b\right)$ only.
The function will be slightly more efficient if the arrays c, d and gam are ordered in this way before entry, and in this event they will be unchanged on exit.
Note that the boundary conditions must be of boundary value type, that is neither $C$ nor $D$ may be identically zero. Note also that the rank of the matrix $\left[C,D\right]$ must be neq for the problem to be properly posed. Any violation of these conditions will lead to an error exit.
9:     mnp – IntegerInput
On entry: the maximum permitted number of mesh points.
Constraint: ${\mathbf{mnp}}\ge 32$.
10:   np – Integer *Input/Output
On entry: determines whether a default or user-supplied initial mesh is used.
${\mathbf{np}}=0$
np is set to a default value of 4 and a corresponding equispaced mesh ${\mathbf{x}}\left[0\right],{\mathbf{x}}\left[1\right],\dots ,{\mathbf{x}}\left[{\mathbf{np}}-1\right]$ is used.
${\mathbf{np}}\ge 4$
You must define an initial mesh using the array x as described.
Constraint: ${\mathbf{np}}=0$ or $4\le {\mathbf{np}}\le {\mathbf{mnp}}$.
On exit: the number of points in the final (returned) mesh.
11:   x[mnp] – doubleInput/Output
On entry: if ${\mathbf{np}}\ge 4$ (see np above), the first np elements must define an initial mesh. Otherwise the elements of x need not be set.
Constraint:

$a = x[0] < x[1] < ⋯ < x[np-1] = b$  (3)
, for ${\mathbf{np}}\ge 4$
On exit: ${\mathbf{x}}\left[0\right],{\mathbf{x}}\left[1\right],\dots ,{\mathbf{x}}\left[{\mathbf{np}}-1\right]$ define the final mesh (with the returned value of np) satisfying the relation (3).
12:   y[${\mathbf{neq}}×{\mathbf{mnp}}$] – doubleOutput
On exit: the approximate solution ${z}_{j}\left({x}_{i}\right)$ satisfying (4), on the final mesh, that is

$y[j-1×mnp+i-1] = z j x i , i = 1 , 2 , … , np ; ​ j = 1 , 2 , … , neq ,$
where np is the number of points in the final mesh.
The remaining columns of y are not used.
13:   tol – doubleInput
On entry: a positive absolute error tolerance.
If

$a = x 1 < x 2 < ⋯ < x np = b$  (4)
is the final mesh, ${z}_{j}\left({x}_{i}\right)$ is the $j$th component of the approximate solution at ${x}_{i}$, and ${y}_{j}\left({x}_{i}\right)$ is the $j$th component of the true solution of equation (1) (see Section 3) and the boundary conditions, then, except in extreme cases, it is expected that

$z j x i - y j x i ≤ tol , i = 1 , 2 , … , np ; ​ j = 1 , 2 , … , neq$  (5)
Constraint: ${\mathbf{tol}}>0.0$.
14:   comm – Nag_User *
Pointer to a structure of type Nag_User with the following member:
p – Pointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$, of type Pointer, allows you to communicate information to and from fcnf and fcng. An object of the required type should be declared, e.g., a structure, and its address assigned to the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ by means of a cast to Pointer in the calling program, e.g., comm.p = (Pointer)&s. The type pointer will be void * with a C compiler that defines void * and char * otherwise.
15:   fail – NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_REAL_ARG_LE
On entry, ${\mathbf{b}}=〈\mathit{\text{value}}〉$ while ${\mathbf{a}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{b}}>{\mathbf{a}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BOUND_COND_COL
More than neq columns of the neq by $2×{\mathbf{neq}}$ matrix $\left[C,D\right]$ are identically zero. i.e., the boundary conditions are rank deficient. The number of non-identically zero columns is $〈\mathit{\text{value}}〉$.
NE_BOUND_COND_LC
At least one row of the neq by $2×{\mathbf{neq}}$ matrix $\left[C,D\right]$ is a linear combination of the other rows, i.e., the boundary conditions are rank deficient. The index of the first such row is $〈\mathit{\text{value}}〉$.
NE_BOUND_COND_MAT
One of the matrices $C$ or $D$ is identically zero, i.e., the problem is of initial value and not of the boundary type.
NE_BOUND_COND_NLC
At least one row of the neq by $2×{\mathbf{neq}}$ matrix $\left[C,D\right]$ is a linear combination of the other rows determined up to a numerical tolerance, i.e., the boundary conditions are rank deficient. The index of first such row is $〈\mathit{\text{value}}〉$. There is some doubt as to the rank deficiency of the boundary conditions. However even if the boundary conditions are not rank deficient they are not posed in a suitable form for use with this function. For example, if

$C = 1 0 1 ε , D = 1 0 1 0 , γ = γ 1 γ 2$
and $\epsilon $ is small enough, this error exit is likely to be taken. A better form for the boundary conditions in this case would be

$C = 1 0 0 1 , D = 1 0 0 0 , γ = γ 1 ε -1 γ 2 - γ 1$
NE_BOUND_COND_ROW
Row $〈\mathit{\text{value}}〉$ of the array c and the corresponding row of array d are identically zero, i.e., the boundary conditions are rank deficient.
NE_CONV_MESH
A finer mesh is required for the accuracy requested; that is mnp is not large enough.
NE_CONV_MESH_INIT
The Newton iteration failed to converge on the initial mesh. This may be due to the initial mesh having too few points or the initial approximate solution being too inaccurate. Try using nag_ode_bvp_fd_nonlin_gen (d02rac).
NE_CONV_ROUNDOFF
Solution cannot be improved due to roundoff error. Too much accuracy might have been requested.
NE_INT_ARG_LT
On entry, ${\mathbf{mnp}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mnp}}\ge 32$.
On entry, ${\mathbf{neq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{neq}}\ge 2$.
NE_INT_RANGE_CONS_2
On entry, ${\mathbf{np}}=〈\mathit{\text{value}}〉$ and ${\mathbf{mnp}}=〈\mathit{\text{value}}〉$. The argument np must satisfy either $4\le {\mathbf{np}}\le {\mathbf{mnp}}$ or ${\mathbf{np}}=0$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_LF_B_MESH
On entry, the left boundary value a, has not been set to ${\mathbf{x}}\left[0\right]$: ${\mathbf{a}}=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left[0\right]=〈\mathit{\text{value}}〉$.
NE_NOT_STRICTLY_INCREASING
The sequence x is not strictly increasing: ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
NE_REAL_ARG_LE
On entry, tol must not be less than or equal to 0.0: ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
NE_RT_B_MESH
On entry, the right boundary value b, has not been set to ${\mathbf{x}}\left[{\mathbf{np}}-1\right]$: ${\mathbf{b}}=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left[{\mathbf{np}}-1\right]=〈\mathit{\text{value}}〉$.

## 7  Accuracy

The solution returned by the function will be accurate to your tolerance as defined by the relation (4) except in extreme circumstances. If too many points are specified in the initial mesh, the solution may be more accurate than requested and the error may not be approximately equidistributed.

## 8  Further Comments

The time taken by the function depends on the difficulty of the problem, the number of mesh points (and meshes) used and the number of deferred corrections.
In the case where you wish to solve a sequence of similar problems, the use of the final mesh from one case is strongly recommended as the initial mesh for the next.

## 9  Example

We solve the problem (written as a first order system)

$ε y ′′ + y ′ = 0$
with boundary conditions

$y 0 = 0 , y 1 = 1$
for the cases $\epsilon ={10}^{-1}$ and $\epsilon ={10}^{-2}$ using the default initial mesh in the first case, and the final mesh of the first case as initial mesh for the second (more difficult) case. We give the solution and the error at each mesh point to illustrate the accuracy of the method given the accuracy request ${\mathbf{tol}}=\text{1.0e−3}$.

### 9.1  Program Text

Program Text (d02gbce.c)

None.

### 9.3  Program Results

Program Results (d02gbce.r)
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 104, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.7770207524299622, "perplexity_flag": "middle"}