NAG Library Routine Document
D02EJF
1 Purpose
D02EJF integrates a stiff system of firstorder ordinary differential equations over an interval with suitable initial conditions, using a variableorder, variablestep method implementing the Backward Differentiation Formulae (BDF), until a userspecified function, if supplied, of the solution is zero, and returns the solution at points specified by you, if desired.
2 Specification
SUBROUTINE D02EJF ( 
X, XEND, N, Y, FCN, PEDERV, TOL, RELABS, OUTPUT, G, W, IW, IFAIL) 
INTEGER 
N, IW, IFAIL 
REAL (KIND=nag_wp) 
X, XEND, Y(N), TOL, G, W(IW) 
CHARACTER(1) 
RELABS 
EXTERNAL 
FCN, PEDERV, OUTPUT, G 

3 Description
D02EJF advances the solution of a system of ordinary differential equations
from
$x={\mathbf{X}}$ to
$x={\mathbf{XEND}}$ using a variableorder, variablestep method implementing the BDF. The system is defined by
FCN, which evaluates
${f}_{i}$ in terms of
$x$ and
${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ (see
Section 5). The initial values of
${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ must be given at
$x={\mathbf{X}}$.
The solution is returned via the
OUTPUT at points specified by you, if desired: this solution is obtained by
${C}^{1}$ interpolation on solution values produced by the method. As the integration proceeds a check can be made on the userspecified function
$g\left(x,y\right)$ to determine an interval where it changes sign. The position of this sign change is then determined accurately by
${C}^{1}$ interpolation to the solution. It is assumed that
$g\left(x,y\right)$ is a continuous function of the variables, so that a solution of
$g\left(x,y\right)=0.0$ can be determined by searching for a change in sign in
$g\left(x,y\right)$. The accuracy of the integration, the interpolation and, indirectly, of the determination of the position where
$g\left(x,y\right)=0.0$, is controlled by the arguments
TOL and
RELABS. The Jacobian of the system
${y}^{\prime}=f\left(x,y\right)$ may be supplied in
PEDERV, if it is available.
For a description of BDF and their practical implementation see
Hall and Watt (1976).
4 References
Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford
5 Arguments
 1: $\mathrm{X}$ – REAL (KIND=nag_wp)Input/Output

On entry: the initial value of the independent variable $x$.
Constraint:
${\mathbf{X}}\ne {\mathbf{XEND}}$.
On exit: if
G is supplied by you,
X contains the point where
$g\left(x,y\right)=0.0$, unless
$g\left(x,y\right)\ne 0.0$ anywhere on the range
X to
XEND, in which case,
X will contain
XEND. If
G is not supplied
X contains
XEND, unless an error has occurred, when it contains the value of
$x$ at the error.
 2: $\mathrm{XEND}$ – REAL (KIND=nag_wp)Input

On entry: the final value of the independent variable. If ${\mathbf{XEND}}<{\mathbf{X}}$, integration will proceed in the negative direction.
Constraint:
${\mathbf{XEND}}\ne {\mathbf{X}}$.
 3: $\mathrm{N}$ – INTEGERInput

On entry: $\mathit{n}$, the number of differential equations.
Constraint:
${\mathbf{N}}\ge 1$.
 4: $\mathrm{Y}\left({\mathbf{N}}\right)$ – REAL (KIND=nag_wp) arrayInput/Output

On entry: the initial values of the solution ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ at $x={\mathbf{X}}$.
On exit: the computed values of the solution at the final point $x={\mathbf{X}}$.
 5: $\mathrm{FCN}$ – SUBROUTINE, supplied by the user.External Procedure

FCN must evaluate the functions
${f}_{i}$ (i.e., the derivatives
${y}_{i}^{\prime}$) for given values of its arguments
$x,{y}_{1},\dots ,{y}_{\mathit{n}}$.
The specification of
FCN is:
SUBROUTINE FCN ( 
X, Y, F) 
REAL (KIND=nag_wp) 
X, Y($n$), F($n$) 

where
$\mathit{n}$ is the value of
N in the call of D02EJF.
 1: $\mathrm{X}$ – REAL (KIND=nag_wp)Input

On entry: $x$, the value of the independent variable.
 2: $\mathrm{Y}\left(\mathit{n}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.
 3: $\mathrm{F}\left(\mathit{n}\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: the value of
${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
FCN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02EJF is called. Arguments denoted as
Input must
not be changed by this procedure.
 6: $\mathrm{PEDERV}$ – SUBROUTINE, supplied by the NAG Library or the user.External Procedure

PEDERV must evaluate the Jacobian of the system (that is, the partial derivatives
$\frac{\partial {f}_{i}}{\partial {y}_{j}}$) for given values of the variables
$x,{y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$.
The specification of
PEDERV is:
SUBROUTINE PEDERV ( 
X, Y, PW) 
REAL (KIND=nag_wp) 
X, Y($n$), PW(*) 

where
$\mathit{n}$ is the value of
N in the call of D02EJF.
 1: $\mathrm{X}$ – REAL (KIND=nag_wp)Input

On entry: $x$, the value of the independent variable.
 2: $\mathrm{Y}\left(\mathit{n}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.
 3: $\mathrm{PW}\left(*\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit:
${\mathbf{PW}}\left(\mathit{n}\times \left(\mathit{i}1\right)+\mathit{j}\right)$ must contain the value of
$\frac{\partial {f}_{\mathit{i}}}{\partial {y}_{\mathit{j}}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$ and $\mathit{j}=1,2,\dots ,\mathit{n}$.
PEDERV must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02EJF is called. Arguments denoted as
Input must
not be changed by this procedure.
If you do not wish to supply the Jacobian, the actual argument
PEDERV must be the
dummy routine D02EJY. (D02EJY is included in the NAG Library.)
 7: $\mathrm{TOL}$ – REAL (KIND=nag_wp)Input/Output

On entry: must be set to a
positive tolerance for controlling the error in the integration. Hence
TOL affects the determination of the position where
$g\left(x,y\right)=0.0$, if
G is supplied.
D02EJF has been designed so that, for most problems, a reduction in
TOL leads to an approximately proportional reduction in the error in the solution. However, the actual relation between
TOL and the accuracy achieved cannot be guaranteed. You are strongly recommended to call D02EJF with more than one value for
TOL and to compare the results obtained to estimate their accuracy. In the absence of any prior knowledge, you might compare the results obtained by calling D02EJF with
${\mathbf{TOL}}={10}^{p}$ and
${\mathbf{TOL}}={10}^{p1}$ if
$p$ correct decimal digits are required in the solution.
Constraint:
${\mathbf{TOL}}>0.0$.
On exit: normally unchanged. However if the range
X to
XEND is so short that a small change in
TOL is unlikely to make any change in the computed solution, then, on return,
TOL has its sign changed.
 8: $\mathrm{RELABS}$ – CHARACTER(1)Input

On entry: the type of error control. At each step in the numerical solution an estimate of the local error,
$\mathit{est}$, is made. For the current step to be accepted the following condition must be satisfied:
where
${\tau}_{r}$ and
${\tau}_{a}$ are defined by
where
$\epsilon $ is a small machinedependent number and
${e}_{i}$ is an estimate of the local error at
${y}_{i}$, computed internally. If the appropriate condition is not satisfied, the step size is reduced and the solution is recomputed on the current step. If you wish to measure the error in the computed solution in terms of the number of correct decimal places, then
RELABS should be set to 'A' on entry, whereas if the error requirement is in terms of the number of correct significant digits, then
RELABS should be set to 'R'. If you prefer a mixed error test, then
RELABS should be set to 'M', otherwise if you have no preference,
RELABS should be set to the default 'D'. Note that in this case 'D' is taken to be 'R'.
Constraint:
${\mathbf{RELABS}}=\text{'A'}$, $\text{'M'}$, $\text{'R'}$ or $\text{'D'}$.
 9: $\mathrm{OUTPUT}$ – SUBROUTINE, supplied by the NAG Library or the user.External Procedure

OUTPUT permits access to intermediate values of the computed solution (for example to print or plot them), at successive userspecified points. It is initially called by D02EJF with
${\mathbf{XSOL}}={\mathbf{X}}$ (the initial value of
$x$). You must reset
XSOL to the next point (between the current
XSOL and
XEND) where
OUTPUT is to be called, and so on at each call to
OUTPUT. If, after a call to
OUTPUT, the reset point
XSOL is beyond
XEND, D02EJF will integrate to
XEND with no further calls to
OUTPUT; if a call to
OUTPUT is required at the point
${\mathbf{XSOL}}={\mathbf{XEND}}$, then
XSOL must be given precisely the value
XEND.
The specification of
OUTPUT is:
SUBROUTINE OUTPUT ( 
XSOL, Y) 
REAL (KIND=nag_wp) 
XSOL, Y($n$) 

where
$\mathit{n}$ is the value of
N in the call of D02EJF.
 1: $\mathrm{XSOL}$ – REAL (KIND=nag_wp)Input/Output

On entry: $x$, the value of the independent variable.
On exit: you must set
XSOL to the next value of
$x$ at which
OUTPUT is to be called.
 2: $\mathrm{Y}\left(\mathit{n}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: the computed solution at the point
XSOL.
OUTPUT must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02EJF is called. Arguments denoted as
Input must
not be changed by this procedure.
If you do not wish to access intermediate output, the actual argument
OUTPUT must be the
dummy routine D02EJX. (D02EJX is included in the NAG Library.)
 10: $\mathrm{G}$ – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure

G must evaluate the function
$g\left(x,y\right)$ for specified values
$x,y$. It specifies the function
$g$ for which the first position
$x$ where
$g\left(x,y\right)=0$ is to be found.
The specification of
G is:
REAL (KIND=nag_wp) 
X, Y($n$) 

where
$\mathit{n}$ is the value of
N in the call of D02EJF.
 1: $\mathrm{X}$ – REAL (KIND=nag_wp)Input

On entry: $x$, the value of the independent variable.
 2: $\mathrm{Y}\left(\mathit{n}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.
G must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02EJF is called. Arguments denoted as
Input must
not be changed by this procedure.
If you do not require the rootfinding option, the actual argument
G must be the
dummy routine D02EJW. (D02EJW is included in the NAG Library.)
 11: $\mathrm{W}\left({\mathbf{IW}}\right)$ – REAL (KIND=nag_wp) arrayWorkspace
 12: $\mathrm{IW}$ – INTEGERInput

On entry: the dimension of the array
W as declared in the (sub)program from which D02EJF is called.
Constraint:
${\mathbf{IW}}\ge \left(12+{\mathbf{N}}\right)\times {\mathbf{N}}+50$.
 13: $\mathrm{IFAIL}$ – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ 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).
6 Error 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{TOL}}\le 0.0$, 
or  ${\mathbf{X}}={\mathbf{XEND}}$, 
or  ${\mathbf{N}}\le 0$, 
or  ${\mathbf{RELABS}}\ne \text{'M'},\text{'A'},\text{'R'},\text{'D'}$, 
or  ${\mathbf{IW}}<\left(12+{\mathbf{N}}\right)\times {\mathbf{N}}+50$. 
 ${\mathbf{IFAIL}}=2$

With the given value of
TOL, no further progress can be made across the integration range from the current point
$x={\mathbf{X}}$. (See
Section 5 for a discussion of this error test.) The components
${\mathbf{Y}}\left(1\right),{\mathbf{Y}}\left(2\right),\dots ,{\mathbf{Y}}\left({\mathbf{N}}\right)$ contain the computed values of the solution at the current point
$x={\mathbf{X}}$. If you have supplied
G, then no point at which
$g\left(x,y\right)$ changes sign has been located up to the point
$x={\mathbf{X}}$.
 ${\mathbf{IFAIL}}=3$

TOL is too small for D02EJF to take an initial step.
X and
${\mathbf{Y}}\left(1\right),{\mathbf{Y}}\left(2\right),\dots ,{\mathbf{Y}}\left({\mathbf{N}}\right)$ retain their initial values.
 ${\mathbf{IFAIL}}=4$

XSOL lies behind
X in the direction of integration, after the initial call to
OUTPUT, if the
OUTPUT option was selected.
 ${\mathbf{IFAIL}}=5$

A value of
XSOL returned by the
OUTPUT lies behind the last value of
XSOL in the direction of integration, if the
OUTPUT option was selected.
 ${\mathbf{IFAIL}}=6$

At no point in the range
X to
XEND did the function
$g\left(x,y\right)$ change sign, if
$g$ was supplied. It is assumed that
$g\left(x,y\right)=0$ has no solution.
 ${\mathbf{IFAIL}}=7$ (C05AZF)

A serious error has occurred in an internal call to the specified routine. Check all subroutine calls and array dimensions. Seek expert help.
 ${\mathbf{IFAIL}}=8$ (D02XKF)

A serious error has occurred in an internal call to the specified routine. Check all subroutine calls and array dimensions. Seek expert help.
 ${\mathbf{IFAIL}}=9$

A serious error has occurred in an internal call to an interpolation routine. Check all (sub)program calls and array dimensions. Seek expert help.
 ${\mathbf{IFAIL}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7 Accuracy
The accuracy of the computation of the solution vector
Y may be controlled by varying the local error tolerance
TOL. In general, a decrease in local error tolerance should lead to an increase in accuracy. You are advised to choose
${\mathbf{RELABS}}=\text{'R'}$ unless you have a good reason for a different choice. It is particularly appropriate if the solution decays.
If the problem is a rootfinding one, then the accuracy of the root determined will depend strongly on $\frac{\partial g}{\partial x}$ and
$\frac{\partial g}{\partial {y}_{\mathit{i}}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$. Large values for these quantities may imply large errors in the root.
8 Parallelism and Performance
D02EJF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
D02EJF 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 implementationspecific information.
If more than one root is required, then to determine the second and later roots D02EJF may be called again starting a short distance past the previously determined roots. Alternatively you may construct your own rootfinding code using
D02NBF (and other routines in
Subchapter D02M–N),
C05AZF and
D02XKF.
If it is easy to code, you should supply
PEDERV. However, it is important to be aware that if
PEDERV is coded incorrectly, a very inefficient integration may result and possibly even a failure to complete the integration (see
${\mathbf{IFAIL}}={\mathbf{2}}$).
10 Example
We illustrate the solution of five different problems. In each case the differential system is the wellknown stiff Robertson problem.
with initial conditions
$a=1.0$,
$b=c=0.0$ at
$x=0.0$. We solve each of the following problems with local error tolerances
$\text{1.0E\u22123}$ and
$\text{1.0E\u22124}$.
(i) 
To integrate to $x=10.0$ producing output at intervals of $2.0$ until a point is encountered where $a=0.9$. The Jacobian is calculated numerically. 
(ii) 
As (i) but with the Jacobian calculated analytically. 
(iii) 
As (i) but with no intermediate output. 
(iv) 
As (i) but with no termination on a rootfinding condition. 
(v) 
Integrating the equations as in (i) but with no intermediate output and no rootfinding termination condition. 
10.1 Program Text
Program Text (d02ejfe.f90)
10.2 Program Data
Program Data (d02ejfe.d)
10.3 Program Results
Program Results (d02ejfe.r)