NAG Library Routine Document
D02BJF
1 Purpose
D02BJF integrates a system of first-order ordinary differential equations over an interval with suitable initial conditions, using a fixed order Runge–Kutta method, until a user-specified function, if supplied, of the solution is zero, and returns the solution at points specified by you, if desired.
2 Specification
SUBROUTINE D02BJF ( |
X, XEND, N, Y, FCN, TOL, RELABS, OUTPUT, G, W, IFAIL) |
INTEGER |
N, IFAIL |
REAL (KIND=nag_wp) |
X, XEND, Y(N), TOL, G, W(20*N) |
CHARACTER(1) |
RELABS |
EXTERNAL |
FCN, OUTPUT, G |
|
3 Description
D02BJF advances the solution of a system of ordinary differential equations
from
to
using a fixed order Runge–Kutta method. The system is defined by
FCN, which evaluates
in terms of
and
. The initial values of
must be given at
.
The solution is returned via the
OUTPUT supplied by you and at points specified by you, if desired: this solution is obtained by
interpolation on solution values produced by the method. As the integration proceeds a check can be made on the user-specified function
to determine an interval where it changes sign. The position of this sign change is then determined accurately by
interpolation to the solution. It is assumed that
is a continuous function of the variables, so that a solution of
can be determined by searching for a change in sign in
. The accuracy of the integration, the interpolation and, indirectly, of the determination of the position where
, is controlled by the parameters
TOL and
RELABS.
4 References
Shampine L F (1994) Numerical solution of ordinary differential equations Chapman and Hall
5 Parameters
- 1: – REAL (KIND=nag_wp)Input/Output
-
On entry: the initial value of the independent variable .
On exit: if
is supplied by you, it contains the point where
, unless
anywhere on the range
X to
XEND, in which case,
X will contain
XEND (and the error indicator
is set); if
is not supplied by you it contains
XEND. However, if an error has occurred, it contains the value of
at which the error occurred.
- 2: – REAL (KIND=nag_wp)Input
-
On entry: the final value of the independent variable. If , integration will proceed in the negative direction.
Constraint:
.
- 3: – INTEGERInput
-
On entry: , the number of equations.
Constraint:
.
- 4: – REAL (KIND=nag_wp) arrayInput/Output
-
On entry: the initial values of the solution at .
On exit: the computed values of the solution at the final point .
- 5: – SUBROUTINE, supplied by the user.External Procedure
-
FCN must evaluate the functions
(i.e., the derivatives
) for given values of its arguments
.
The specification of
FCN is:
SUBROUTINE FCN ( |
X, Y, F) |
REAL (KIND=nag_wp) |
X, Y(*), F(*) |
|
- 1: – REAL (KIND=nag_wp)Input
-
On entry: , the value of the independent variable.
- 2: – REAL (KIND=nag_wp) arrayInput
-
On entry: , for , the value of the variable.
- 3: – REAL (KIND=nag_wp) arrayOutput
-
On exit: the value of
, for .
FCN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02BJF is called. Parameters denoted as
Input must
not be changed by this procedure.
- 6: – 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
, if
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
and with each of
and
where
correct significant digits are required in the solution,
. The accuracy of the value
such that
is indirectly controlled by varying
TOL. You should experiment to determine this accuracy.
Constraint:
.
- 7: – CHARACTER(1)Input
-
On entry: the type of error control. At each step in the numerical solution an estimate of the local error,
, is made. For the current step to be accepted the following condition must be satisfied:
where
and
are defined by
where
and
are small machine-dependent numbers and
is an estimate of the local error at
, 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, 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:
, , or .
- 8: – 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 user-specified points. It is initially called by D02BJF with
(the initial value of
). 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
, then
XSOL must be given precisely the value
XEND.
The specification of
OUTPUT is:
SUBROUTINE OUTPUT ( |
XSOL, Y) |
REAL (KIND=nag_wp) |
XSOL, Y(*) |
|
- 1: – REAL (KIND=nag_wp)Input/Output
-
On entry: the output value of the independent variable .
On exit: you must set
XSOL to the next value of
at which
OUTPUT is to be called.
- 2: – 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 D02BJF is called. Parameters denoted as
Input must
not be changed by this procedure.
If you do not wish to access intermediate output, the actual parameter
OUTPUT
must be the dummy routine D02BJX. (D02BJX is included in the NAG Library.)
- 9: – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
-
G must evaluate the function
for specified values
. It specifies the function
for which the first position
where
is to be found.
The specification of
G is:
REAL (KIND=nag_wp) |
X, Y(*) |
|
where
is the value of
N in the call of D02BJF.
- 1: – REAL (KIND=nag_wp)Input
-
On entry: , the value of the independent variable.
- 2: – REAL (KIND=nag_wp) arrayInput
-
On entry: , for , 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. Parameters denoted as
Input must
not be changed by this procedure.
If you do not require the root-finding option, the actual parameter
G must be the dummy routine D02BJW. (D02BJW is included in the NAG Library.)
- 10: – REAL (KIND=nag_wp) arrayWorkspace
-
- 11: – INTEGERInput/Output
-
On entry:
IFAIL must be set to
,
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
.
When the value is used it is essential to test the value of IFAIL on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
-
On entry, | , |
or | TOL is too small |
or | , |
or | , , or , |
or | . |
-
With the given value of
TOL, no further progress can be made across the integration range from the current point
. (See
Section 9 for a discussion of this error exit.) The components
contain the computed values of the solution at the current point
. If you have supplied
, then no point at which
changes sign has been located up to the point
.
-
TOL is too small for D02BJF to take an initial step.
X and
retain their initial values.
-
XSOL has not been reset or
XSOL lies behind
X in the direction of integration, after the initial call to
OUTPUT, if the
OUTPUT option was selected.
-
A value of
XSOL returned by the
OUTPUT has not been reset or lies behind the last value of
XSOL in the direction of integration, if the
OUTPUT option was selected.
-
At no point in the range
X to
XEND did the function
change sign, if
was supplied. It is assumed that
has no solution.
-
A serious error has occurred in an internal call to an interpolation routine. Check all (sub)program calls and array dimensions. Seek expert help.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.8 in the Essential Introduction for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.7 in the Essential Introduction for further information.
Dynamic memory allocation failed.
See
Section 3.6 in the Essential Introduction 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
unless you have a good reason for a different choice.
If the problem is a root-finding one, then the accuracy of the root determined will depend on the properties of
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 is not threaded by NAG in any implementation.
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 implementation-specific 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 root-finding code using
C05AZF,
D02PFF and
D02PSF.
If D02BJF fails with
, 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
, 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 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 , 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 (internally to D02BJF) to preserve stability. This will exhibit itself by making the computing time excessively long, or occasionally by an exit with . 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
to
starting with values
,
and
. We solve each of the following problems with local error tolerances
and
.
(i) |
To integrate to producing intermediate output at intervals of until a root is encountered where . |
(ii) |
As (i) but with no intermediate output. |
(iii) |
As (i) but with no termination on a root-finding condition. |
(iv) |
As (i) but with no intermediate output and no root-finding termination condition. |
10.1 Program Text
Program Text (d02bjfe.f90)
10.2 Program Data
Program Data (d02bjfe.d)
10.3 Program Results
Program Results (d02bjfe.r)