NAG Library Routine Document
d02bgf
(ivp_rkm_val_simple)
1
Purpose
d02bgf integrates a system of first-order 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
Fortran Interface
Subroutine d02bgf ( |
x,
xend,
n,
y,
tol,
hmax,
m,
val,
fcn,
w,
ifail) |
Integer, Intent (In) | :: |
n,
m | Integer, Intent (Inout) | :: |
ifail | Real (Kind=nag_wp), Intent (In) | :: |
xend,
hmax,
val | Real (Kind=nag_wp), Intent (Inout) | :: |
x,
y(n),
tol | Real (Kind=nag_wp), Intent (Out) | :: |
w(n,10) | External | :: |
fcn |
|
C Header Interface
#include nagmk26.h
void |
d02bgf_ (
double *x,
const double *xend,
const Integer *n,
double y[],
double *tol,
const double *hmax,
const Integer *m,
const double *val,
void (NAG_CALL *fcn)(
const double *x,
const double y[],
double f[]),
double w[],
Integer *ifail) |
|
3
Description
d02bgf advances the solution of a system of ordinary differential equations
from
towards
using a Merson form of the Runge–Kutta method. The system is defined by
fcn, which evaluates
in terms of
and
(see
Section 5), and the values of
must be given at
.
As the integration proceeds, a check is made on the specified component of the solution to determine an interval where it attains a given value . 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 can be determined by searching for a change in sign in the function .
The accuracy of the integration and, indirectly, of the determination of the position where
is controlled by the argument
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
Arguments
- 1: – Real (Kind=nag_wp)Input/Output
-
On entry: must be set to the initial value of the independent variable .
On exit: the point where the component
attains the value
unless an error has occurred, when it contains the value of
at the error. In particular, if
anywhere on the range
to
, it will contain
xend on exit.
- 2: – Real (Kind=nag_wp)Input
-
On entry: the final value of the independent variable
.
If on entry integration will proceed in the negative direction.
- 3: – IntegerInput
-
On entry: , the number of differential equations.
Constraint:
.
- 4: – Real (Kind=nag_wp) arrayInput/Output
-
On entry: the initial values of the solution .
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: – 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
.
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
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
and
if
correct decimal digits in the solution are required.
Constraint:
.
On exit: normally unchanged. However if the range from
x to the position where
(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
,
d02bgf should be called again with a positive value of
tol whose magnitude is considerably smaller than that of the previous call.
- 6: – Real (Kind=nag_wp)Input
-
On entry: controls how the sign of
is checked.
- is checked at every internal integration step.
- The computed solution is checked for a change in sign of at steps of not greater than . 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 are expected within a distance , say, of each other then a suitable value for hmax might be . If only one change of sign in is expected on the range x to xend then is most appropriate.
- 7: – IntegerInput
-
On entry: the index of the component of the solution whose value is to be checked.
Constraint:
.
- 8: – Real (Kind=nag_wp)Input
-
On entry: the value of
in the equation
to be solved for
x.
- 9: – 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:
Fortran Interface
Subroutine fcn ( |
x,
y,
f) |
Real (Kind=nag_wp), Intent (In) | :: |
x,
y(*) | Real (Kind=nag_wp), Intent (Out) | :: |
f(*) |
|
C Header Interface
#include nagmk26.h
void |
fcn (
const double *x,
const double y[],
double f[]) |
|
In the description of the arguments of
d02bgf below,
denotes the actual value of
n in the call of
d02bgf.
- 1: – Real (Kind=nag_wp)Input
-
On entry: , the value of the argument.
- 2: – Real (Kind=nag_wp) arrayInput
-
On entry: , for , the value of the argument.
- 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
d02bgf 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
d02bgf. If your code inadvertently
does return any NaNs or infinities,
d02bgf is likely to produce unexpected results.
- 10: – Real (Kind=nag_wp) arrayWorkspace
-
- 11: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, 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 | , |
or | , |
or | . |
-
With the given value of
tol, no further progress can be made across the integration range from the current point
, 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
contain the computed values of the solution at the current point
. No point at which
changes sign has been located up to the point
.
-
tol is too small for the routine to take an initial step (see
Section 9).
x and
retain their initial values.
-
At no point in the range
x to
xend did the function
change sign. It is assumed that
has no solution.
- (c05azf)
-
A serious error has occurred in an internal call to the specified routine. Check all subroutine calls and array dimensions. Seek expert help.
-
A serious error has occurred in an internal call to an integration routine. Check all subroutine calls and array dimensions. Seek expert help.
-
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.9 in How to Use the NAG Library and its Documentation for further information.
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.
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 depends on
tol, on the mathematical properties of the differential system, on the position where
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
or
is possible.
8
Parallelism and Performance
d02bgf is not threaded in any implementation.
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
where
and
are machine-dependent computing times.
For some problems it is possible that
d02bgf will exit with
due to inaccuracy of the computed value
. For example, consider a case where the component
has a maximum in the integration range and
is close to the maximum value. If
tol is too large, it is possible that the maximum might be estimated as less than
, or even that the integration step length chosen might be so long that the maximum of
and the (two) positions where
are all in the same step and so the position where
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
is expected to equal
, for example
should be made about
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
at
xend, then inaccuracy is not a likely source of error.
If
d02bgf fails with
, 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
, 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 , 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 (internally to d02bgf) to preserve stability. This will usually exhibit itself by making the computing time excessively long, or occasionally by an exit with . 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 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
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
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 root-finder
c05azf and the interpolation routine
d02psf to solve equations involving
and their derivatives.
This routine is only intended to be used to locate the first zero of the function . If later zeros are required you are strongly advised to construct your own more general root-finding routines as discussed above.
10
Example
This example finds the value
where
, where
,
,
, are defined by
and where at
we are given
,
and
. We write
,
and
and we set
and
in turn so that we can compare the solutions obtained. We expect the solution
and we set
so that the point where
is not too near the end of the range of integration. The initial values and range are read from a data file.
10.1
Program Text
Program Text (d02bgfe.f90)
10.2
Program Data
Program Data (d02bgfe.d)
10.3
Program Results
Program Results (d02bgfe.r)