NAG Library Routine Document
D02BGF
1 Purpose
D02BGF integrates a system of firstorder ordinary differential equations over an interval with suitable initial conditions, using a Runge–Kutta–Merson method, until a specified component attains a given value.
2 Specification
SUBROUTINE D02BGF ( 
X, XEND, N, Y, TOL, HMAX, M, VAL, FCN, W, IFAIL) 
INTEGER 
N, M, IFAIL 
REAL (KIND=nag_wp) 
X, XEND, Y(N), TOL, HMAX, VAL, W(N,10) 
EXTERNAL 
FCN 

3 Description
D02BGF advances the solution of a system of ordinary differential equations
from
$x={\mathbf{X}}$ towards
$x={\mathbf{XEND}}$ using a Merson form of the Runge–Kutta method. 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), and the values of
${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ must be given at
$x={\mathbf{X}}$.
As the integration proceeds, a check is made on the specified component ${y}_{m}$ of the solution to determine an interval where it attains a given value $\alpha $. The position where this value is attained is then determined accurately by interpolation on the solution and its derivative. It is assumed that the solution of ${y}_{m}=\alpha $ can be determined by searching for a change in sign in the function ${y}_{m}\alpha $.
The accuracy of the integration and, indirectly, of the determination of the position where
${y}_{m}=\alpha $ is controlled by the parameter
TOL.
For a description of Runge–Kutta methods 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 Parameters
 1: X – REAL (KIND=nag_wp)Input/Output
On entry: must be set to the initial value of the independent variable $x$.
On exit: the point where the component
${y}_{m}$ attains the value
$\alpha $ unless an error has occurred, when it contains the value of
$x$ at the error. In particular, if
${y}_{m}\ne \alpha $ anywhere on the range
$x={\mathbf{X}}$ to
$x={\mathbf{XEND}}$, it will contain
XEND on exit.
 2: XEND – REAL (KIND=nag_wp)Input
On entry: the final value of the independent variable
$x$.
If ${\mathbf{XEND}}<{\mathbf{X}}$ on entry integration will proceed in the negative direction.
 3: N – INTEGERInput
On entry: $\mathit{n}$, the number of differential equations.
Constraint:
${\mathbf{N}}>0$.
 4: Y(N) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the initial values of the solution ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$.
On exit: the computed values of the solution at a point near the solution
X, unless an error has occurred when they contain the computed values at the final value of
X.
 5: TOL – REAL (KIND=nag_wp)Input/Output
On entry: must be set to a positive tolerance for controlling the error in the integration and in the determination of the position where
${y}_{m}=\alpha $.
D02BGF has been designed so that, for most problems, a reduction in
TOL leads to an approximately proportional reduction in the error in the solution obtained in the integration. The relation between changes in
TOL and the error in the determination of the position where
${y}_{m}=\alpha $ is less clear, but for
TOL small enough the error should be approximately proportional to
TOL. However, the actual relation between
TOL and the accuracy cannot be guaranteed. You are strongly recommended to call D02BGF 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 results obtained by calling D02BGF with
${\mathbf{TOL}}={10.0}^{p}$ and
${\mathbf{TOL}}={10.0}^{p1}$ if
$p$ correct decimal digits in the solution are required.
Constraint:
${\mathbf{TOL}}>0.0$.
On exit: normally unchanged. However if the range from
X to the position where
${y}_{m}=\alpha $ (or to the final value of
X if an error occurs) 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. To check results returned with
${\mathbf{TOL}}<0.0$, D02BGF should be called again with a positive value of
TOL whose magnitude is considerably smaller than that of the previous call.
 6: HMAX – REAL (KIND=nag_wp)Input
On entry: controls how the sign of
${y}_{m}\alpha $ is checked.
 ${\mathbf{HMAX}}=0.0$
 ${y}_{m}\alpha $ is checked at every internal integration step.
 ${\mathbf{HMAX}}\ne 0.0$
 The computed solution is checked for a change in sign of ${y}_{m}\alpha $ at steps of not greater than $\left{\mathbf{HMAX}}\right$. This facility should be used if there is any chance of ‘missing’ the change in sign by checking too infrequently. For example, if two changes of sign of ${y}_{m}\alpha $ are expected within a distance $h$, say, of each other then a suitable value for HMAX might be ${\mathbf{HMAX}}=h/2$. If only one change of sign in ${y}_{m}\alpha $ is expected on the range X to XEND then ${\mathbf{HMAX}}=0.0$ is most appropriate.
 7: M – INTEGERInput
On entry: the index $m$ of the component of the solution whose value is to be checked.
Constraint:
$1\le {\mathbf{M}}\le {\mathbf{N}}$.
 8: VAL – REAL (KIND=nag_wp)Input
On entry: the value of
$\alpha $ in the equation
${y}_{m}=\alpha $ to be solved for
X.
 9: 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(*), F(*) 

In the description of the parameters of D02BGF below,
$\mathit{n}$ denotes the actual value of
N in the call of D02BGF.
 1: X – REAL (KIND=nag_wp)Input
On entry: $x$, the value of the argument.
 2: Y($*$) – REAL (KIND=nag_wp) arrayInput
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.
 3: F($*$) – 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 D02BGF is called. Parameters denoted as
Input must
not be changed by this procedure.
 10: W(N,$10$) – REAL (KIND=nag_wp) arrayWorkspace
 11: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction 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 parameter, 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}}={\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$
On entry,  ${\mathbf{TOL}}\le 0.0$, 
or  ${\mathbf{N}}\le 0$, 
or  ${\mathbf{M}}\le 0$, 
or  ${\mathbf{M}}>{\mathbf{N}}$. 
 ${\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}}$, or dependence of the error on
TOL would be lost if further progress across the integration range were attempted (see
Section 8 for a discussion of this error exit). The components
${\mathbf{Y}}\left(1\right),{\mathbf{Y}}\left(2\right),\dots ,{\mathbf{Y}}\left(\mathit{n}\right)$ contain the computed values of the solution at the current point
$x={\mathbf{X}}$. No point at which
${y}_{m}\alpha $ changes sign has been located up to the point
$x={\mathbf{X}}$.
 ${\mathbf{IFAIL}}=3$
TOL is too small for the routine to take an initial step (see
Section 8).
X and
${\mathbf{Y}}\left(1\right),{\mathbf{Y}}\left(2\right),\dots ,{\mathbf{Y}}\left(\mathit{n}\right)$ retain their initial values.
 ${\mathbf{IFAIL}}=4$

At no point in the range
X to
XEND did the function
${y}_{m}\alpha $ change sign. It is assumed that
${y}_{m}\alpha $ has no solution.
 ${\mathbf{IFAIL}}=5$ (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}}=6$
A serious error has occurred in an internal call to an integration routine. Check all subroutine calls and array dimensions. Seek expert help.
 ${\mathbf{IFAIL}}=7$

A serious error has occurred in an internal call to an interpolation routine. Check all (sub)program calls and array dimensions. Seek expert help.
7 Accuracy
The accuracy depends on
TOL, on the mathematical properties of the differential system, on the position where
${y}_{m}=\alpha $ and on the method. It can be controlled by varying
TOL but the approximate proportionality of the error to
TOL holds only for a restricted range of values of
TOL. For
TOL too large, the underlying theory may break down and the result of varying
TOL may be unpredictable. For
TOL too small, rounding error may affect the solution significantly and an error exit with
${\mathbf{IFAIL}}={\mathbf{2}}$ or
${\mathbf{3}}$ is possible.
The time taken by D02BGF depends on the complexity and mathematical properties of the system of differential equations defined by
FCN, on the range, the position of solution and the tolerance. There is also an overhead of the form
$a+b\times \mathit{n}$ where
$a$ and
$b$ are machinedependent computing times.
For some problems it is possible that D02BGF will exit with
${\mathbf{IFAIL}}={\mathbf{4}}$ due to inaccuracy of the computed value
${y}_{m}$. For example, consider a case where the component
${y}_{m}$ has a maximum in the integration range and
$\alpha $ is close to the maximum value. If
TOL is too large, it is possible that the maximum might be estimated as less than
$\alpha $, or even that the integration step length chosen might be so long that the maximum of
${y}_{m}$ and the (two) positions where
${y}_{m}=\alpha $ are all in the same step and so the position where
${y}_{m}=\alpha $ remains undetected. Both these difficulties can be overcome by reducing
TOL sufficiently and, if necessary, by choosing
HMAX sufficiently small. For similar reasons, care should be taken when choosing
XEND. If possible, you should choose
XEND well beyond the point where
${y}_{m}$ is expected to equal
$\alpha $, for example
$\left{\mathbf{XEND}}{\mathbf{X}}\right$ should be made about
$50\%$ longer than the expected range. As a simple check, if, with
XEND fixed, a change in
TOL does not lead to a significant change in
${y}_{m}$ at
XEND, then inaccuracy is not a likely source of error.
If D02BGF fails with
${\mathbf{IFAIL}}={\mathbf{3}}$, then it could be called again with a larger value of
TOL if this has not already been tried. If the accuracy requested is really needed and cannot be obtained with this routine, the system may be very stiff (see below) or so badly scaled that it cannot be solved to the required accuracy.
If D02BGF fails with
${\mathbf{IFAIL}}={\mathbf{2}}$, it is likely that it has been called with a value of
TOL which is so small that a solution cannot be obtained on the range
X to
XEND. This can happen for wellbehaved systems and very small values of
TOL. You should, however, consider whether there is a more fundamental difficulty. For example:
(a) 
in the region of a singularity (infinite value) of the solution, the routine will usually stop with ${\mathbf{IFAIL}}={\mathbf{2}}$, unless overflow occurs first. If overflow occurs using D02BGF, routine D02PFF can be used instead to detect the increasing solution before overflow occurs. In any case, numerical integration cannot be continued through a singularity, and analytical treatment should be considered; 
(b) 
for ‘stiff’ equations, where the solution contains rapidly decaying components the routine will use very small steps in $x$ (internally to D02BGF) to preserve stability. This will usually exhibit itself by making the computing time excessively long, or occasionally by an exit with ${\mathbf{IFAIL}}={\mathbf{2}}$. Merson's method is not efficient in such cases, and you should try the method D02EJF which uses a Backward Differentiation Formula. To determine whether a problem is stiff, D02PEF may be used. 
For wellbehaved systems with no difficulties such as stiffness or singularities, the Merson method should work well for low accuracy calculations (three or four figures). For high accuracy calculations or where
FCN is costly to evaluate, Merson's method may not be appropriate and a computationally less expensive method may be
D02CJF which uses an Adams method.
For problems for which D02BGF is not sufficiently general, you should consider the routines
D02BHF and
D02PFF. Routine
D02BHF can be used to solve an equation involving the components
${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives (for example, to find where a component passes through zero or to find the maximum value of a component). It also permits a more general form of error control and may be preferred to D02BGF if the component whose value is to be determined is very small in modulus on the integration range.
D02BHF can always be used in place of D02BGF, but will usually be computationally more expensive for solving the same problem.
D02PFF is a more general routine with many facilities including a more general error control criterion.
D02PFF can be combined with the rootfinder
C05AZF and the interpolation routine
D02PSF to solve equations involving
${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives.
This routine is only intended to be used to locate the first zero of the function ${y}_{m}\alpha $. If later zeros are required you are strongly advised to construct your own more general rootfinding routines as discussed above.
9 Example
This example finds the value
${\mathbf{X}}>0.0$ where
$y=0.0$, where
$y$,
$v$,
$\varphi $, are defined by
and where at
${\mathbf{X}}=0.0$ we are given
$y=0.5$,
$v=0.5$ and
$\varphi =\pi /5$. We write
$y={\mathbf{Y}}\left(1\right)$,
$v={\mathbf{Y}}\left(2\right)$ and
$\varphi ={\mathbf{Y}}\left(3\right)$ and we set
${\mathbf{TOL}}=\text{1.0E\u22124}$ and
${\mathbf{TOL}}=\text{1.0E\u22125}$ in turn so that we can compare the solutions obtained. We expect the solution
${\mathbf{X}}\simeq 7.3$ and we set
${\mathbf{XEND}}=10.0$ so that the point where
$y=0.0$ is not too near the end of the range of integration. The initial values and range are read from a data file.
9.1 Program Text
Program Text (d02bgfe.f90)
9.2 Program Data
Program Data (d02bgfe.d)
9.3 Program Results
Program Results (d02bgfe.r)