# NAG FL Interfaced02gbf (bvp_​fd_​lin_​gen)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine d02gbf ( a, b, n, tol, fcnf, fcng, c, d, gam, mnp, x, y, np, w, lw, iw, liw,
 Integer, Intent (In) :: n, mnp, lw, liw Integer, Intent (Inout) :: np, ifail Integer, Intent (Out) :: iw(liw) Real (Kind=nag_wp), Intent (In) :: a, b, tol Real (Kind=nag_wp), Intent (Inout) :: c(n,n), d(n,n), gam(n), x(mnp) Real (Kind=nag_wp), Intent (Out) :: y(n,mnp), w(lw) External :: fcnf, fcng
#include <nag.h>
 void d02gbf_ (const double *a, const double *b, const Integer *n, const double *tol, void (NAG_CALL *fcnf)(const double *x, double f[]),void (NAG_CALL *fcng)(const double *x, double g[]),double c[], double d[], double gam[], const Integer *mnp, double x[], double y[], Integer *np, double w[], const Integer *lw, Integer iw[], const Integer *liw, Integer *ifail)
The routine may be called by the names d02gbf or nagf_ode_bvp_fd_lin_gen.

## 3Description

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′=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 $\mathit{n}×\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.
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

## 5Arguments

1: $\mathbf{a}$Real (Kind=nag_wp) Input
On entry: $a$, the left-hand boundary point.
2: $\mathbf{b}$Real (Kind=nag_wp) Input
On entry: $b$, the right-hand boundary point.
Constraint: ${\mathbf{b}}>{\mathbf{a}}$.
3: $\mathbf{n}$Integer Input
On entry: the number of equations; that is $\mathit{n}$ is the order of system (1).
Constraint: ${\mathbf{n}}\ge 2$.
4: $\mathbf{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 |ui(xj)| .$
Constraint: ${\mathbf{tol}}>0.0$.
5: $\mathbf{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:
Fortran Interface
 Subroutine fcnf ( x, f)
 Real (Kind=nag_wp), Intent (In) :: x Real (Kind=nag_wp), Intent (Out) :: f(*)
 void fcnf (const double *x, double f[])
In the description of the arguments of d02gbf below, $\mathit{n}$ denotes the actual value of n in the call of d02gbf.
1: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: $x$, the value of the independent variable.
2: $\mathbf{f}\left(*\right)$Real (Kind=nag_wp) array Output
On exit: ${\mathbf{f}}\left(\mathit{n}×\left(\mathit{i}-1\right)+\mathit{j}\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}$.
fcnf must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02gbf is called. Arguments denoted as Input must not be changed by this procedure.
Note: fcnf should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02gbf. If your code inadvertently does return any NaNs or infinities, d02gbf is likely to produce unexpected results.
6: $\mathbf{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:
Fortran Interface
 Subroutine fcng ( x, g)
 Real (Kind=nag_wp), Intent (In) :: x Real (Kind=nag_wp), Intent (Out) :: g(*)
 void fcng (const double *x, double g[])
In the description of the arguments of d02gbf below, $\mathit{n}$ denotes the actual value of n in the call of d02gbf.
1: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: $x$, the value of the independent variable.
2: $\mathbf{g}\left(*\right)$Real (Kind=nag_wp) array Output
On exit: the $\mathit{i}$th element of the vector $g\left(x\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
fcng must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02gbf is called. Arguments denoted as Input must not be changed by this procedure.
Note: fcng should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02gbf. If your code inadvertently does return any NaNs or infinities, d02gbf is likely to produce unexpected results.
7: $\mathbf{c}\left({\mathbf{n}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
8: $\mathbf{d}\left({\mathbf{n}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
9: $\mathbf{gam}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/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:
1. (i)conditions on $y\left(a\right)$ only;
2. (ii)condition involving $y\left(a\right)$ and $y\left(b\right)$; and
3. (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: $\mathbf{mnp}$Integer Input
On entry: the maximum permitted number of mesh points.
Constraint: ${\mathbf{mnp}}\ge 32$.
11: $\mathbf{x}\left({\mathbf{mnp}}\right)$Real (Kind=nag_wp) array Input/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=x(1) (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: $\mathbf{y}\left({\mathbf{n}},{\mathbf{mnp}}\right)$Real (Kind=nag_wp) array Output
On exit: the approximate solution $z\left(x\right)$ satisfying (3), on the final mesh, that is
 $y(j,i)=zj(xi), 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: $\mathbf{np}$Integer Input/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: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
15: $\mathbf{lw}$Integer Input
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: $\mathbf{iw}\left({\mathbf{liw}}\right)$Integer array Workspace
17: $\mathbf{liw}$Integer Input
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: $\mathbf{ifail}$Integer Input/Output
This routine uses an ifail input value codification that differs from the normal case to distinguish between errors and warnings (see Section 4 in the Introduction to the NAG Library FL Interface).
On entry: ifail must be set to one of the values below to set behaviour on detection of an error; these values have no effect when no error is detected. The behaviour relate to whether or not program execution is halted and whether or not messages are printed when an error or warning is detected.
ifail Execution Error Printing Warning Printed
$\phantom{00}0$ halted No No
$\phantom{00}1$ continue No No
$\phantom{0}10$ halted Yes No
$\phantom{0}11$ continue Yes No
$100$ halted No Yes
$101$ continue No Yes
$110$ halted Yes Yes
$111$ continue Yes Yes
For environments where it might be inappropriate to halt program execution when an error is detected, the value $1$, $11$, $101$ or $111$ is recommended. If the printing of messages is undesirable, then the value $1$ is recommended. Otherwise, the recommended value is $110$. When the value $\mathbf{1}$, $\mathbf{11}$, $\mathbf{101}$ or $\mathbf{111}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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$
On entry, ${\mathbf{a}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{b}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{b}}>{\mathbf{a}}$.
On entry, ${\mathbf{liw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{liw}}\ge {\mathbf{mnp}}×\left(2×{\mathbf{n}}+1\right)+{\mathbf{n}}$; that is, $⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{lw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lw}}\ge {\mathbf{mnp}}×\left(3×{{\mathbf{n}}}^{2}+5×{\mathbf{n}}+2\right)+3×{{\mathbf{n}}}^{2}+5×{\mathbf{n}}$; that is, $⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{mnp}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mnp}}\ge 32$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{np}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{np}}=0$ or ${\mathbf{np}}\ge 4$.
On entry, ${\mathbf{np}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{mnp}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{np}}\le {\mathbf{mnp}}$.
On entry, ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tol}}>0.0$.
On entry: ${\mathbf{a}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{x}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{a}}={\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<\cdots <{\mathbf{x}}\left({\mathbf{np}}\right)={\mathbf{b}}\text{, }{\mathbf{np}}\ge 4$.
On entry: ${\mathbf{b}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{x}}\left({\mathbf{np}}\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{a}}={\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<\cdots <{\mathbf{x}}\left({\mathbf{np}}\right)={\mathbf{b}}\text{, }{\mathbf{np}}\ge 4$.
The sequence x is not strictly increasing. For $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{x}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{x}}\left(i+1\right)=⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=2$
c is identically zero; n conditions are set in d.
At least one condition must be on the left. ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
d is identically zero; n conditions are set in c.
At least one condition must be on the right. ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
More than n columns of the n by $2×{\mathbf{n}}$ 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}}⟩$.
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.
${\mathbf{ifail}}=3$
A finer mesh is required for the accuracy requested; that is, mnp is not large enough.
Newton iteration has reached round-off level.
If desired accuracy has not been reached, tol is too small for this problem and this machine precision.
The Newton iteration has failed to converge.
This could be due to there being too few points in the initial mesh or to the initial approximate solution being too inaccurate. If this latter reason is suspected or you cannot make changes to prevent this error, you should use the routine with a continuation facility instead.
${\mathbf{ifail}}=4$
A serious error occurred in a call to the internal integrator.
The error code internally was $⟨\mathit{\text{value}}⟩$. Please contact NAG.
${\mathbf{ifail}}=5$
At least one row of the n by $2×{\mathbf{n}}$ 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}}⟩$.
At least one row of the n by $2×{\mathbf{n}}$ 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 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:
1. (i)at least one row of the $\mathit{n}×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
2. (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) ) .$
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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.

## 8Parallelism and Performance

d02gbf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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 unit numbers on which this output is to appear by calls of x04aaf (for error messages) or x04abf (for monitoring information) – see Section 10 for an example. Otherwise the default unit 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.

## 10Example

This example solves 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}$.
Note the call to x04abf prior to the call to d02gbf.

### 10.1Program Text

Program Text (d02gbfe.f90)

### 10.2Program Data

Program Data (d02gbfe.d)

### 10.3Program Results

Program Results (d02gbfe.r)