NAG FL Interface
d02bjf (ivp_rk_zero_simple)
1
Purpose
d02bjf integrates a system of firstorder ordinary differential equations over an interval with suitable initial conditions, using a fixed order Runge–Kutta method, until a userspecified function, if supplied, of the solution is zero, and returns the solution at points specified by you, if desired.
2
Specification
Fortran Interface
Subroutine d02bjf ( 
x, xend, n, y, fcn, tol, relabs, output, g, w, ifail) 
Integer, Intent (In) 
:: 
n 
Integer, Intent (Inout) 
:: 
ifail 
Real (Kind=nag_wp), External 
:: 
g 
Real (Kind=nag_wp), Intent (In) 
:: 
xend, tol 
Real (Kind=nag_wp), Intent (Inout) 
:: 
x, y(n) 
Real (Kind=nag_wp), Intent (Out) 
:: 
w(20*n) 
Character (1), Intent (In) 
:: 
relabs 
External 
:: 
fcn, output 

C Header Interface
#include <nag.h>
void 
d02bjf_ (double *x, const double *xend, const Integer *n, double y[], void (NAG_CALL *fcn)(const double *x, const double y[], double f[]), const double *tol, const char *relabs, void (NAG_CALL *output)(double *xsol, const double y[]), double (NAG_CALL *g)(const double *x, const double y[]), double w[], Integer *ifail, const Charlen length_relabs) 

C++ Header Interface
#include <nag.h> extern "C" {
void 
d02bjf_ (double &x, const double &xend, const Integer &n, double y[], void (NAG_CALL *fcn)(const double &x, const double y[], double f[]), const double &tol, const char *relabs, void (NAG_CALL *output)(double &xsol, const double y[]), double (NAG_CALL *g)(const double &x, const double y[]), double w[], Integer &ifail, const Charlen length_relabs) 
}

The routine may be called by the names d02bjf or nagf_ode_ivp_rk_zero_simple.
3
Description
d02bjf advances the solution of a system of ordinary differential equations
from
$x={\mathbf{x}}$ to
$x={\mathbf{xend}}$ using a fixed order Runge–Kutta method. The system is defined by
fcn, which evaluates
${f}_{i}$ in terms of
$x$ and
$y=\left({y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}\right)$. The initial values of
$y=\left({y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}\right)$ must be given at
$x={\mathbf{x}}$.
The solution is returned via the
output supplied by you and 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$ 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$, is controlled by the arguments
tol and
relabs.
4
References
Shampine L F (1994) Numerical solution of ordinary differential equations Chapman and Hall
5
Arguments

1:
$\mathbf{x}$ – Real (Kind=nag_wp)
Input/Output

On entry: the initial value of the independent variable $x$.
On exit: if
$g$ is supplied by you, it contains the point where
$g\left(x,y\right)=0$, unless
$g\left(x,y\right)\ne 0$ anywhere on the range
x to
xend, in which case,
x will contain
xend (and the error indicator
${\mathbf{ifail}}={\mathbf{6}}$ is set); if
$g$ is not supplied by you it contains
xend. However, if an error has occurred, it contains the value of
$x$ at which the error occurred.

2:
$\mathbf{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:
$\mathbf{n}$ – Integer
Input

On entry: $\mathit{n}$, the number of equations.
Constraint:
${\mathbf{n}}>0$.

4:
$\mathbf{y}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) array
Input/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:
$\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}}$.
The specification of
fcn is:
Fortran Interface
Subroutine fcn ( 
x, y, f) 
Real (Kind=nag_wp), Intent (In) 
:: 
x, y(*) 
Real (Kind=nag_wp), Intent (Inout) 
:: 
f(*) 

C Header Interface
void 
fcn_ (const double *x, const double y[], double f[]) 

C++ Header Interface
#include <nag.h> extern "C" {
void 
fcn_ (const double &x, const double y[], double f[]) 
}


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) array
Input

Note: the dimension,
$\mathit{n}$, of
y is
n as in the call of
d02bjf.
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.

3:
$\mathbf{f}\left(*\right)$ – Real (Kind=nag_wp) array
Output

Note: the dimension,
$\mathit{n}$, of
f is
n as in the call of
d02bjf.
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
d02bjf is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: fcn should not return floatingpoint NaN (Not a Number) or infinity values, since these are not handled by
d02bjf. If your code inadvertently
does return any NaNs or infinities,
d02bjf is likely to produce unexpected results.

6:
$\mathbf{tol}$ – Real (Kind=nag_wp)
Input

On entry: 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$, if
$g$ is supplied.
d02bjf 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
d02bjf 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
d02bjf with
${\mathbf{relabs}}=\text{'D'}$ and with each of
${\mathbf{tol}}={10.0}^{p}$ and
${\mathbf{tol}}={10.0}^{p1}$ where
$p$ correct significant digits are required in the solution,
$y$. The accuracy of the value
$x$ such that
$g\left(x,y\right)=0$ is indirectly controlled by varying
tol. You should experiment to determine this accuracy.
Constraint:
$10.0\times \mathit{machineprecision}{\mathbf{tol}}0.01$.

7:
$\mathbf{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
relabs  ${\tau}_{r}$  ${\tau}_{a}$ 
'M'  tol  1.0 
'A'  ${\epsilon}_{r}$  ${\mathbf{tol}}/{\epsilon}_{r}$ 
'R'  tol  ${\epsilon}_{a}$ 
'D'  tol  ${\epsilon}_{a}$ 
where
${\epsilon}_{r}$ and
${\epsilon}_{a}$ are small machinedependent numbers and
${e}_{i}$ is an estimate of the local error at
${y}_{i}$, computed internally. If the 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,
relabs should be set to 'A' on entry, whereas if the error requirement is in terms of the number of correct significant digits,
relabs should be set to 'R'. If you prefer a mixed error test,
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{'M'}$, $\text{'A'}$, $\text{'R'}$ or $\text{'D'}$.

8:
$\mathbf{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
d02bjf 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,
d02bjf will integrate to
xend with no further calls to
output; if a call to
output is required at the point
${\mathbf{xsol}}={\mathbf{xend}}$,
xsol must be given precisely the value
xend.
The specification of
output is:
Fortran Interface
Subroutine output ( 
xsol, y) 
Real (Kind=nag_wp), Intent (In) 
:: 
y(*) 
Real (Kind=nag_wp), Intent (Inout) 
:: 
xsol 

C Header Interface
void 
output_ (double *xsol, const double y[]) 

C++ Header Interface
#include <nag.h> extern "C" {
void 
output_ (double &xsol, const double y[]) 
}


1:
$\mathbf{xsol}$ – Real (Kind=nag_wp)
Input/Output

On entry: the output value of the independent variable $x$.
On exit: you must set
xsol to the next value of
$x$ at which
output is to be called.

2:
$\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) array
Input

Note: the dimension,
$\mathit{n}$, of
y is
n as in the call of
d02bjf.
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
d02bjf is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: output should not return floatingpoint NaN (Not a Number) or infinity values, since these are not handled by
d02bjf. If your code inadvertently
does return any NaNs or infinities,
d02bjf is likely to produce unexpected results.
If you do not wish to access intermediate output, the actual argument
output
must be the dummy routine
d02bjx. (
d02bjx is included in the NAG Library.)

9:
$\mathbf{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:
Fortran Interface
Real (Kind=nag_wp) 
:: 
g 
Real (Kind=nag_wp), Intent (In) 
:: 
x, y(*) 

C Header Interface
double 
g_ (const double *x, const double y[]) 

C++ Header Interface
#include <nag.h> extern "C" {
double 
g_ (const double &x, const double y[]) 
}


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) array
Input

Note: the dimension,
$\mathit{n}$, of
y is
n as in the call of
d02bjf.
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
d02bjf is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: g should not return floatingpoint NaN (Not a Number) or infinity values, since these are not handled by
d02bjf. If your code inadvertently
does return any NaNs or infinities,
d02bjf is likely to produce unexpected results.
If you do not require the rootfinding option, the actual argument
g must be the dummy routine
d02bjw. (
d02bjw is included in the NAG Library.)

10:
$\mathbf{w}\left(20\times {\mathbf{n}}\right)$ – Real (Kind=nag_wp) array
Workspace


11:
$\mathbf{ifail}$ – Integer
Input/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 4 in the Introduction to the NAG Library FL Interface 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{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{relabs}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{relabs}}=\text{'M'}$, $\text{'A'}$, $\text{'R'}$ or $\text{'D'}$.
On entry, ${\mathbf{tol}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: $\u2329\mathit{\text{value}}\u232a\le {\mathbf{tol}}<0.01$.
On entry, ${\mathbf{x}}={\mathbf{xend}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{x}}\ne {\mathbf{xend}}$.
 ${\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}}$.
The solution is returned at this point,
${\mathbf{x}}=\u2329\mathit{\text{value}}\u232a$.
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 9 for a discussion of this error exit.) 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$, no point at which
$g\left(x,y\right)$ changes sign has been located up to the point
$x={\mathbf{x}}$.
 ${\mathbf{ifail}}=3$

With the given value of
tol, no initial progress can be made from the starting point of integration provided.
tol is too small for
d02bjf 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$

On the first call to
output,
xsol remained unchanged.
On the first call to
output,
xsol was returned as
$\u2329\mathit{\text{value}}\u232a$, which is inconsistent with
$\left({\mathbf{x}},{\mathbf{xend}}\right)$ —
$\left(\u2329\mathit{\text{value}}\u232a,\u2329\mathit{\text{value}}\u232a\right)$.
 ${\mathbf{ifail}}=5$

On a call to
output,
xsol remained unchanged.
On a call to
output,
xsol was returned as
$\u2329\mathit{\text{value}}\u232a$, which is inconsistent with previous
xsol and
xend —
$\left(\u2329\mathit{\text{value}}\u232a,\u2329\mathit{\text{value}}\u232a\right)$.
 ${\mathbf{ifail}}=6$

No change in sign of the function $g\left(x,y\right)$ was detected in the integration range.
 ${\mathbf{ifail}}=7$

Unexpected internal error in call to interpolation routine.
The interpolation routine returned error flag $\u2329\mathit{\text{value}}\u232a$.
Unexpected internal error in call to zerofinding routine.
The zerofinding routine returned error flag $\u2329\mathit{\text{value}}\u232a$.
 ${\mathbf{ifail}}=8$

Unexpected internal error in call to step integrator.
The step integrator returned error flag $\u2329\mathit{\text{value}}\u232a$.
 ${\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.
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{'D'}$ unless you have a good reason for a different choice.
If the problem is a rootfinding one, then the accuracy of the root determined will depend on the properties of
$g\left(x,y\right)$ and on the values of
tol and
relabs. You should try to code
g without introducing any unnecessary cancellation errors.
8
Parallelism and Performance
d02bjf 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
d02bjf may be called again starting a short distance past the previously determined roots. Alternatively you may construct your own rootfinding code using
c05azf,
d02pff and
d02psf.
If
d02bjf fails with
${\mathbf{ifail}}={\mathbf{3}}$, then it can 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
d02bjf fails with
${\mathbf{ifail}}={\mathbf{2}}$, it is probable 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. Numerical integration cannot be continued through a singularity, and analytic 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 d02bjf) to preserve stability. This will exhibit itself by making the computing time excessively long, or occasionally by an exit with ${\mathbf{ifail}}={\mathbf{2}}$. Runge–Kutta methods are not efficient in such cases, and you should try d02ejf.
10
Example
This example illustrates the solution of four different problems. In each case the differential system (for a projectile) is
over an interval
${\mathbf{x}}=0.0$ to
${\mathbf{xend}}=10.0$ starting with values
$y=0.5$,
$v=0.5$ and
$\varphi =\pi /5$. We solve each of the following problems with local error tolerances
$\text{1.0E\u22124}$ and
$\text{1.0E\u22125}$.

(i)To integrate to $x=10.0$ producing intermediate output at intervals of $2.0$ until a root is encountered where $y=0.0$.

(ii)As (i) but with no intermediate output.

(iii)As (i) but with no termination on a rootfinding condition.

(iv)As (i) but with no intermediate output and no rootfinding termination condition.
10.1
Program Text
10.2
Program Data
10.3
Program Results