d02bhf integrates a system of first-order ordinary differential equations over an interval with suitable initial conditions, using a Runge–Kutta–Merson method, until a user-specified function of the solution is zero.
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 function $g(x,y)$ specified by you, to determine an interval where it changes sign. The position of this sign change is then determined accurately by interpolating for the solution and its derivative. It is assumed that $g(x,y)$ is a continuous function of the variables, so that a solution of $g(x,y)=0$ can be determined by searching for a change in sign in $g(x,y)$.
The accuracy of the integration and, indirectly, of the determination of the position where $g(x,y)=0$, is controlled by tol.
For a description of Runge–Kutta methods and their practical implementation see Hall and Watt (1976).
4References
Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford
5Arguments
1: $\mathbf{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 $g(x,y)=0.0$ unless an error has occurred, when it contains the value of $x$ at the error. In particular, if $g(x,y)\ne 0.0$ anywhere on the range x to xend, it will contain xend on exit.
2: $\mathbf{xend}$ – Real (Kind=nag_wp)Input
On entry: the final value of the independent variable $x$.
If ${\mathbf{xend}}<{\mathbf{x}}$ on entry, integration proceeds in a negative direction.
3: $\mathbf{n}$ – IntegerInput
On entry: $\mathit{n}$, the number of differential equations.
Constraint:
${\mathbf{n}}>0$.
4: $\mathbf{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}}$.
On exit: the computed values of the solution at the final point $x={\mathbf{x}}$.
5: $\mathbf{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 $g(x,y)=0.0$.
d02bhf 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 $g(x,y)=0.0$ 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 d02bhf 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 d02bhf with ${\mathbf{tol}}={10.0}^{-p}$ and ${\mathbf{tol}}={10.0}^{-p-1}$ 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={\mathbf{x}}$ to the position where $g(x,y)=0.0$ (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, tol is returned with its sign changed. To check results returned with ${\mathbf{tol}}<0.0$, d02bhf should be called again with a positive value of tol whose magnitude is considerably smaller than that of the previous call.
6: $\mathbf{irelab}$ – IntegerInput
On entry: determines 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:
$\mathit{est}\le {\mathbf{tol}}\times \mathrm{max}\phantom{\rule{0.125em}{0ex}}\{\epsilon ,\left|{y}_{1}\right|,\left|{y}_{2}\right|,\dots ,\left|{y}_{\mathit{n}}\right|\}$, where $\epsilon $ is machine precision.
If the appropriate condition is not satisfied, the step size is reduced and the solution 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, set ${\mathbf{irelab}}=1$ on entry, whereas if the error requirement is in terms of the number of correct significant digits, set ${\mathbf{irelab}}=2$. Where there is no preference in the choice of error test, ${\mathbf{irelab}}=0$ will result in a mixed error test. It should be borne in mind that the computed solution will be used in evaluating $g(x,y)$.
Constraint:
${\mathbf{irelab}}=0$, $1$ or $2$.
7: $\mathbf{hmax}$ – Real (Kind=nag_wp)Input
On entry: if ${\mathbf{hmax}}=0.0$, no special action is taken.
If ${\mathbf{hmax}}\ne 0.0$, a check is made for a change in sign of $g(x,y)$ at steps 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 $g(x,y)$ are expected within a distance $h$, say, of each other, a suitable value for hmax might be ${\mathbf{hmax}}=h/2$. If only one change of sign in $g(x,y)$ is expected on the range x to xend, the choice ${\mathbf{hmax}}=0.0$ is most appropriate.
8: $\mathbf{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}}$.
In the description of the arguments of d02bhf below, $\mathit{n}$ denotes the value of n in the call of d02bhf.
1: $\mathbf{x}$ – Real (Kind=nag_wp)Input
On entry: $x$, the value of the argument.
2: $\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.
3: $\mathbf{f}\left(*\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 d02bhf is called. Arguments denoted as Input must not be changed by this procedure.
Note:fcn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02bhf. If your code inadvertently does return any NaNs or infinities, d02bhf is likely to produce unexpected results.
9: $\mathbf{g}$ – real (Kind=nag_wp) Function, supplied by the user.External Procedure
g must evaluate the function $g(x,y)$ at a specified point.
In the description of the arguments of d02bhf below, $\mathit{n}$ denotes the value of n in the call of d02bhf.
1: $\mathbf{x}$ – Real (Kind=nag_wp)Input
On entry: $x$, the value of the independent variable.
2: $\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the value of
${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
g must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02bhf is called. Arguments denoted as Input must not be changed by this procedure.
Note:g should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02bhf. If your code inadvertently does return any NaNs or infinities, d02bhf is likely to produce unexpected results.
10: $\mathbf{w}({\mathbf{n}},7)$ – Real (Kind=nag_wp) arrayWorkspace
11: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-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{irelab}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: $0\le {\mathbf{irelab}}\le 2$.
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}>0$.
On entry, ${\mathbf{tol}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=2$
The value of tol, $\u27e8\mathit{\text{value}}\u27e9$, is too small for the function to make any further progress across the integration range. Current value of $x=\u27e8\mathit{\text{value}}\u27e9$.
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 9 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 $g(x,y)$ changes sign has been located up to the point $x={\mathbf{x}}$.
${\mathbf{ifail}}=3$
The value of tol, $\u27e8\mathit{\text{value}}\u27e9$, is too small for the function to take an initial step.
${\mathbf{ifail}}=4$
No change in sign of the function $g(x,y)$ was detected in the integration range.
${\mathbf{ifail}}=5$
An internal error has occurred in this routine. Check the routine call and any array sizes. If the call is correct then please contact NAG for assistance.
${\mathbf{ifail}}=6$
A serious error occurred in a call to the internal integrator.
The error code internally was $\u27e8\mathit{\text{value}}\u27e9$. Please contact NAG.
${\mathbf{ifail}}=7$
Unexpected internal error in call to interpolation routine.
The interpolation routine returned error flag $\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
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 accuracy depends on tol, on the mathematical properties of the differential system, on the position where $g(x,y)=0.0$ 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 accuracy may also be restricted by the properties of $g(x,y)$. You should try to code g without introducing any unnecessary cancellation errors.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
d02bhf is not threaded in any implementation.
9Further Comments
The time taken by d02bhf depends on the complexity and mathematical properties of the system of differential equations defined by fcn, the complexity of g, on the range, the position of the solution and the tolerance. There is also an overhead of the form $a+b\times \mathit{n}$ where $a$ and $b$ are machine-dependent computing times.
For some problems it is possible that d02bhf will return ${\mathbf{ifail}}={\mathbf{4}}$ because of inaccuracy of the computed values y, leading to inaccuracy in the computed values of $g(x,y)$ used in the search for the solution of $g(x,y)=0.0$. This difficulty can be overcome by reducing tol sufficiently, and if necessary, by choosing hmax sufficiently small. If possible, you should choose xend well beyond the expected point where $g(x,y)=0.0$; for example make $|{\mathbf{xend}}-{\mathbf{x}}|$ about $50\%$ larger 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 at xend, then inaccuracy is not a likely source of error.
If d02bhf 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 d02bhf 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 well-behaved 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 d02bhf, 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 compute in very small steps in $x$ (internally to d02bhf) 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 d02ejf which uses a Backward Differentiation Formula method. To determine whether a problem is stiff, d02pef may be used.
For well-behaved 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 d02bhf is not sufficiently general, you should consider d02pff. 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.
d02bhf can also be used to solve an equation involving $x$, ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and the derivatives of ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$.
For example in Section 10, d02bhf is used to find a value of ${\mathbf{x}}>0.0$ where ${\mathbf{y}}\left(1\right)=0.0$. It could instead be used to find a turning-point of ${y}_{1}$ by replacing the function $g(x,y)$ in the program by:
Real (kind=nag_wp) Function g(x,y)
Real (kind=nag_wp) x,y(3),f(3)
Call fcn(x,y,f)
g = f(1)
Return
End
This routine is only intended to locate the first zero of $g(x,y)$. If later zeros are required, you are strongly advised to construct your own more general root-finding routines as discussed above.
10Example
This example finds the value ${\mathbf{x}}>0.0$ at which $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. We expect the solution ${\mathbf{x}}\simeq 7.3$ and so we set ${\mathbf{xend}}=10.0$ to avoid determining the solution of $y=0.0$ too near the end of the range of integration. The initial values and range are read from a data file.