D02 Chapter Contents
D02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD02GBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D02GBF solves a general linear two-point boundary value problem for a system of ordinary differential equations, using a deferred correction technique.

## 2  Specification

 SUBROUTINE D02GBF ( A, B, N, TOL, FCNF, FCNG, C, D, GAM, MNP, X, Y, NP, W, LW, IW, LIW, IFAIL)
 INTEGER N, MNP, NP, LW, IW(LIW), LIW, IFAIL REAL (KIND=nag_wp) A, B, TOL, C(N,N), D(N,N), GAM(N), X(MNP), Y(N,MNP), W(LW) EXTERNAL FCNF, FCNG

## 3  Description

D02GBF solves a linear two-point boundary value problem for a system of $\mathit{n}$ ordinary differential equations in the interval [$a,b$]. The system is written in the form
 $y′=Fxy+gx$ (1)
and the boundary conditions are written in the form
 $Cya+Dyb=γ .$ (2)
Here $F\left(x\right)$, $C$ and $D$ are $\mathit{n}$ by $\mathit{n}$ matrices, and $g\left(x\right)$ and $\gamma$ are $\mathit{n}$-component vectors. The approximate solution to (1) and (2) is found using a finite difference method with deferred correction. The algorithm is a specialization of that used in subroutine D02RAF which solves a nonlinear version of (1) and (2). The nonlinear version of the algorithm is described fully in Pereyra (1979).
You 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 routines 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  Parameters

1:     A – REAL (KIND=nag_wp)Input
On entry: $a$, the left-hand boundary point.
2:     B – REAL (KIND=nag_wp)Input
On entry: $b$, the right-hand boundary point.
Constraint: ${\mathbf{B}}>{\mathbf{A}}$.
3:     N – INTEGERInput
On entry: the number of equations; that is $\mathit{n}$ is the order of system (1).
Constraint: ${\mathbf{N}}\ge 2$.
4:     TOL – REAL (KIND=nag_wp)Input
On entry: a positive absolute error tolerance. If
 $a=x1
is the final mesh, $z\left(x\right)$ is the approximate solution from D02GBF and $y\left(x\right)$ is the true solution of equations (1) and (2) then, except in extreme cases, it is expected that
 $z-y≤TOL$ (3)
where
 $u = max 1≤i≤N max 1≤j≤NP uixj .$
Constraint: ${\mathbf{TOL}}>0.0$.
5:     FCNF – SUBROUTINE, supplied by the user.External Procedure
FCNF must evaluate the matrix $F\left(x\right)$ in (1) at a general point $x$.
The specification of FCNF is:
 SUBROUTINE FCNF ( X, F)
 REAL (KIND=nag_wp) X, F(*)
In the description of the parameters of D02GBF below, $\mathit{n}$ denotes the actual value of N in the call of D02GBF.
1:     X – REAL (KIND=nag_wp)Input
On entry: $x$, the value of the independent variable.
2:     F($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{F}}\left(\mathit{j},\mathit{i}\right)$ must contain the $\left(\mathit{i},\mathit{j}\right)$th element of the matrix $F\left(x\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$ and $\mathit{j}=1,2,\dots ,\mathit{n}$. (See Section 9 for an example.)
FCNF must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02GBF is called. Parameters denoted as Input must not be changed by this procedure.
6:     FCNG – SUBROUTINE, supplied by the user.External Procedure
FCNG must evaluate the vector $g\left(x\right)$ in (1) at a general point $x$.
The specification of FCNG is:
 SUBROUTINE FCNG ( X, G)
 REAL (KIND=nag_wp) X, G(*)
In the description of the parameters of D02GBF below, $\mathit{n}$ denotes the actual value of N in the call of D02GBF.
1:     X – REAL (KIND=nag_wp)Input
On entry: $x$, the value of the independent variable.
2:     G($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: the $\mathit{i}$th element of the vector $g\left(x\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$. (See Section 9 for an example.)
FCNG must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02GBF is called. Parameters denoted as Input must not be changed by this procedure.
7:     C(N,N) – REAL (KIND=nag_wp) arrayInput/Output
8:     D(N,N) – REAL (KIND=nag_wp) arrayInput/Output
9:     GAM(N) – REAL (KIND=nag_wp) arrayInput/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 reordered 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 routine 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 problems (1) and (2) 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 $\mathit{n}$ for the problem to be properly posed. Any violation of these conditions will lead to an error exit.
10:   MNP – INTEGERInput
On entry: the maximum permitted number of mesh points.
Constraint: ${\mathbf{MNP}}\ge 32$.
11:   X(MNP) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{NP}}\ge 4$ (see NP), the first NP elements must define an initial mesh. Otherwise the elements of $x$ need not be set.
Constraint:
 $A=X1 (4)
On exit: ${\mathbf{X}}\left(1\right),{\mathbf{X}}\left(2\right),\dots ,{\mathbf{X}}\left({\mathbf{NP}}\right)$ define the final mesh (with the returned value of NP) satisfying the relation (4).
12:   Y(N,MNP) – REAL (KIND=nag_wp) arrayOutput
On exit: the approximate solution $z\left(x\right)$ satisfying (3), on the final mesh, that is
 $Yji=zjxi, i=1,2,…,NP​ and ​j=1,2,…,n$
where NP is the number of points in the final mesh.
The remaining columns of Y are not used.
13:   NP – INTEGERInput/Output
On entry: determines whether a default mesh or user-supplied mesh is used.
${\mathbf{NP}}=0$
A default value of $4$ for NP and a corresponding equispaced mesh ${\mathbf{X}}\left(1\right),{\mathbf{X}}\left(2\right),\dots ,{\mathbf{X}}\left({\mathbf{NP}}\right)$ are used.
${\mathbf{NP}}\ge 4$
You must define an initial mesh X as in (4).
On exit: the number of points in the final (returned) mesh.
14:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
15:   LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which D02GBF is called.
Constraint: ${\mathbf{LW}}\ge {\mathbf{MNP}}×\left(3{{\mathbf{N}}}^{2}+5{\mathbf{N}}+2\right)+3{{\mathbf{N}}}^{2}+5{\mathbf{N}}$.
16:   IW(LIW) – INTEGER arrayWorkspace
17:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which D02GBF is called.
Constraint: ${\mathbf{LIW}}\ge {\mathbf{MNP}}×\left(2{\mathbf{N}}+1\right)+{\mathbf{N}}$.
18:   IFAIL – INTEGERInput/Output
For this routine, the normal use of IFAIL is extended to control the printing of error and warning messages as well as specifying hard or soft failure (see Section 3.3 in the Essential Introduction).
On entry: IFAIL must be set to a value with the decimal expansion $\mathit{cba}$, where each of the decimal digits $c$, $b$ and $a$ must have a value of $0$ or $1$.
 $a=0$ specifies hard failure, otherwise soft failure; $b=0$ suppresses error messages, otherwise error messages will be printed (see Section 6); $c=0$ suppresses warning messages, otherwise warning messages will be printed (see Section 6).
The recommended value for inexperienced users is $110$ (i.e., hard failure with all messages printed).
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
One or more of the parameters N, TOL, NP, MNP, LW or LIW is incorrectly set, ${\mathbf{B}}\le {\mathbf{A}}$ or the condition (4) on X is not satisfied.
${\mathbf{IFAIL}}=2$
There are three possible reasons for this error exit to be taken:
 (i) one of the matrices $C$ or $D$ is identically zero (that is, the problem is of initial value and not boundary value type). In this case, ${\mathbf{IW}}\left(1\right)=0$ on exit; (ii) a row of $C$ and the corresponding row of $D$ are identically zero (that is, the boundary conditions are rank deficient). In this case, on exit ${\mathbf{IW}}\left(1\right)$ contains the index of the first such row encountered; and (iii) more than $\mathit{n}$ of the columns of the $\mathit{n}$ by $2\mathit{n}$ matrix $\left[C,D\right]$ are identically zero (that is, the boundary conditions are rank deficient). In this case, on exit ${\mathbf{IW}}\left(1\right)$ contains minus the number of non-identically zero columns.
${\mathbf{IFAIL}}=3$
The routine has failed to find a solution to the specified accuracy. There are a variety of possible reasons including:
 (i) the boundary conditions are rank deficient, which may be indicated by the message that the Jacobian is singular. However this is an unlikely explanation for the error exit as all rank deficient boundary conditions should lead instead to error exits with either ${\mathbf{IFAIL}}={\mathbf{2}}$ or ${\mathbf{5}}$; see also (iv); (ii) not enough mesh points are permitted in order to attain the required accuracy. This is indicated by ${\mathbf{NP}}={\mathbf{MNP}}$ on return from a call to D02GBF. This difficulty may be aggravated by a poor initial choice of mesh points; (iii) the accuracy requested cannot be attained on the computer being used; and (iv) an unlikely combination of values of $Fx$ has led to a singular Jacobian. The error should not persist if more mesh points are allowed.
${\mathbf{IFAIL}}=4$
A serious error has occurred in a call to D02GBF. Check all array subscripts and subroutine parameter lists in calls to D02GBF. Seek expert help.
${\mathbf{IFAIL}}=5$
There are two possible reasons for this error exit which occurs when checking the rank of the boundary conditions by reduction to a row echelon form:
 (i) at least one row of the $\mathit{n}$ by $2\mathit{n}$ matrix $\left[C,D\right]$ is a linear combination of the other rows and hence the boundary conditions are rank deficient. The index of the first such row encountered is given by ${\mathbf{IW}}\left(1\right)$ on exit; and (ii) as (i) but the rank deficiency implied by this error exit has only been determined up to a numerical tolerance. Minus the index of the first such row encountered is given by ${\mathbf{IW}}\left(1\right)$ on exit.
In case (ii) 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 routine.
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 .$

## 7  Accuracy

The solution returned by the routine will be accurate to your tolerance as defined by the relation (3) 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.

The time taken by D02GBF depends on the difficulty of the problem, the number of mesh points (and meshes) used and the number of deferred corrections.
You are strongly recommended to set IFAIL to obtain self-explanatory error messages, and also monitoring information about the course of the computation. You may select the channel numbers on which this output is to appear by calls of X04AAF (for error messages) or X04ABF (for monitoring information) – see Section 9 for an example. Otherwise the default channel numbers will be used, as specified in the Users' Note.
In the case where you wish to solve a sequence of similar problems, the final mesh from solving one case is strongly recommended as the initial mesh for the next.

## 9  Example

This example solves the problem (written as a first-order system)
 $ε y′′+y′=0$
with boundary conditions
 $y0=0, y1=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}$.
Note the call to X04ABF prior to the call to D02GBF.

### 9.1  Program Text

Program Text (d02gbfe.f90)

### 9.2  Program Data

Program Data (d02gbfe.d)

### 9.3  Program Results

Program Results (d02gbfe.r)