NAG Library Routine Document
D02LAF
1 Purpose
D02LAF is a routine for integrating a nonstiff system of secondorder ordinary differential equations using Runge–Kutta–Nystrom techniques.
2 Specification
INTEGER 
NEQ, LRWORK, IFAIL 
REAL (KIND=nag_wp) 
T, TEND, Y(NEQ), YP(NEQ), YDP(NEQ), RWORK(LRWORK) 
EXTERNAL 
FCN 

3 Description
Given the initial values
$x,{y}_{1},{y}_{2},\dots ,{y}_{{\mathbf{NEQ}}},{y}_{1}^{\prime},{y}_{2}^{\prime},\dots ,{y}_{{\mathbf{NEQ}}}^{\prime}$ D02LAF integrates a nonstiff system of secondorder differential equations of the type
from
$x={\mathbf{T}}$ to
$x={\mathbf{TEND}}$ using a Runge–Kutta–Nystrom formula pair. The system is defined by
FCN, which evaluates
${f}_{i}$ in terms of
$x$ and
${y}_{1},{y}_{2},\dots ,{y}_{{\mathbf{NEQ}}}$, where
${y}_{1},{y}_{2},\dots ,{y}_{{\mathbf{NEQ}}}$ are supplied at
$x$.
There are two Runge–Kutta–Nystrom formula pairs implemented in this routine. The lower order method is intended if you have moderate accuracy requirements and may be used in conjunction with the interpolation routine
D02LZF to produce solutions and derivatives at userspecified points. The higher order method is intended if you have high accuracy requirements.
In onestep mode the routine returns approximations to the solution, derivative and
${f}_{i}$ at each integration point. In interval mode these values are returned at the end of the integration range. You select the order of the method, the mode of operation, the error control and various optional inputs by a prior call to
D02LXF.
For a description of the Runge–Kutta–Nystrom formula pairs see
Dormand et al. (1986a) and
Dormand et al. (1986b) and for a description of their practical implementation see
Brankin et al. (1989).
4 References
Brankin R W, Dormand J R, Gladwell I, Prince P J and Seward W L (1989) Algorithm 670: A Runge–Kutta–Nystrom Code ACM Trans. Math. Software 15 31–40
Dormand J R, El–Mikkawy M E A and Prince P J (1986a) Families of Runge–Kutta–Nystrom formulae Mathematical Report TPMR 861 Teesside Polytechnic
Dormand J R, El–Mikkawy M E A and Prince P J (1986b) High order embedded Runge–Kutta–Nystrom formulae Mathematical Report TPMR 862 Teesside Polytechnic
5 Arguments
 1: $\mathrm{FCN}$ – SUBROUTINE, supplied by the user.External Procedure

FCN must evaluate the functions
${f}_{i}$ (that is the second derivatives
${y}_{i}^{\prime \prime}$) for given values of its arguments
$x$,
${y}_{1},{y}_{2},\dots ,{y}_{{\mathbf{NEQ}}}$.
The specification of
FCN is:
SUBROUTINE FCN ( 
NEQ, T, Y, F) 
INTEGER 
NEQ 
REAL (KIND=nag_wp) 
T, Y(NEQ), F(NEQ) 

 1: $\mathrm{NEQ}$ – INTEGERInput

On entry: the number of differential equations.
 2: $\mathrm{T}$ – REAL (KIND=nag_wp)Input

On entry: $x$, the value of the argument.
 3: $\mathrm{Y}\left({\mathbf{NEQ}}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NEQ}}$, the value of the argument.
 4: $\mathrm{F}\left({\mathbf{NEQ}}\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: the value of
${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NEQ}}$.
FCN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02LAF is called. Arguments denoted as
Input must
not be changed by this procedure.
 2: $\mathrm{NEQ}$ – INTEGERInput

On entry: the number of secondorder ordinary differential equations to be solved by D02LAF. It must contain the same value as the argument
NEQ used in a prior call to
D02LXF.
Constraint:
${\mathbf{NEQ}}\ge 1$.
 3: $\mathrm{T}$ – REAL (KIND=nag_wp)Input/Output

On entry: the initial value of the independent variable $x$.
Constraint:
${\mathbf{T}}\ne {\mathbf{TEND}}$.
On exit: the value of the independent variable, which is usually
TEND, unless an error has occurred or the code is operating in onestep mode. If the integration is to be continued, possibly with a new value for
TEND,
T must not be changed.
 4: $\mathrm{TEND}$ – REAL (KIND=nag_wp)Input

On entry: the end point of the range of integration. If
${\mathbf{TEND}}<{\mathbf{T}}$ on initial entry, integration will proceed in the negative direction.
TEND may be reset, in the direction of integration, before any continuation call.
 5: $\mathrm{Y}\left({\mathbf{NEQ}}\right)$ – REAL (KIND=nag_wp) arrayInput/Output

On entry: the initial values of the solution ${y}_{1},{y}_{2},\dots ,{y}_{{\mathbf{NEQ}}}$.
On exit: the computed values of the solution at the exit value of
T. If the integration is to be continued, possibly with a new value for
TEND, these values must not be changed.
 6: $\mathrm{YP}\left({\mathbf{NEQ}}\right)$ – REAL (KIND=nag_wp) arrayInput/Output

On entry: the initial values of the derivatives ${y}_{1}^{\prime},{y}_{2}^{\prime},\dots ,{y}_{{\mathbf{NEQ}}}^{\prime}$.
On exit: the computed values of the derivatives at the exit value of
T. If the integration is to be continued, possibly with a new value for
TEND, these values must not be changed.
 7: $\mathrm{YDP}\left({\mathbf{NEQ}}\right)$ – REAL (KIND=nag_wp) arrayInput/Output

On entry: must be unchanged from a previous call to D02LAF.
On exit: the computed values of the second derivative at the exit value of
T, unless illegal input is detected, in which case the elements of
YDP may not have been initialized. If the integration is to be continued, possibly with a new value for
TEND, these values must not be changed.
 8: $\mathrm{RWORK}\left({\mathbf{LRWORK}}\right)$ – REAL (KIND=nag_wp) arrayCommunication Array

This
must be the same argument
RWORK as supplied to
D02LXF. It is used to pass information from
D02LXF to D02LAF, and from D02LAF to both
D02LYF and
D02LZF. Therefore the contents of this array
must not be changed before the call to D02LAF or calling either of the routines
D02LYF and
D02LZF.
 9: $\mathrm{LRWORK}$ – INTEGERInput

On entry: the dimension of the array
RWORK as declared in the (sub)program from which D02LAF is called.
This must be the same argument
LRWORK as supplied to
D02LXF.
 10: $\mathrm{IFAIL}$ – INTEGERInput/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 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
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}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$

Illegal input detected, i.e., one of the following conditions:
 on any call, ${\mathbf{T}}={\mathbf{TEND}}$, or the value of NEQ or LRWORK has been altered;
 on a continuation call, the direction of integration has been changed;
 D02LXF had not been called previously, or the previous call to D02LXF resulted in an error exit.
This error exit can be caused if elements of
RWORK have been overwritten.
 ${\mathbf{IFAIL}}=2$

The maximum number of steps has been attempted. (See argument
MAXSTP in
D02LXF.) If integration is to be continued then you need only reset
IFAIL and call the routine again and a further
MAXSTP steps will be attempted.
 ${\mathbf{IFAIL}}=3$

In order to satisfy the error requirements, the step size needed is too small for the machine precision being used.
 ${\mathbf{IFAIL}}=4$

The code has detected two successive error exits at the current value of $x$ and cannot proceed. Check all input variables.
 ${\mathbf{IFAIL}}=5$

The code has detected inefficient use of the integration method. The step size has been reduced by a significant amount too often in order to hit the output points specified by
TEND. (Of the last
$100$ or more successful steps more than
$10\%$ are steps with sizes that have had to be reduced by a factor of greater than a half.)
 ${\mathbf{IFAIL}}=99$
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.
 ${\mathbf{IFAIL}}=399$
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.
 ${\mathbf{IFAIL}}=999$
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 of integration is determined by the arguments
TOL,
THRES and
THRESP in a prior call to
D02LXF. Note that only the local error at each step is controlled by these arguments. The error estimates obtained are not strict bounds but are usually reliable over one step. Over a number of steps the overall error may accumulate in various ways, depending on the system. The code is designed so that a reduction in
TOL should lead to an approximately proportional reduction in the error. You are strongly recommended to call D02LAF with more than one value for
TOL and to compare the results obtained to estimate their accuracy.
The accuracy obtained depends on the type of error test used. If the solution oscillates around zero a relative error test should be avoided, whereas if the solution is exponentially increasing an absolute error test should not be used. For a description of the error test see the specifications of the arguments
TOL,
THRES and
THRESP in routine document
D02LXF.
8 Parallelism and Performance
D02LAF is not thread safe and should not be called from a multithreaded user program. Please see
Section 3.12.1 in How to Use the NAG Library and its Documentation for more information on thread safety.
D02LAF is not threaded in any implementation.
If D02LAF fails with
${\mathbf{IFAIL}}={\mathbf{3}}$ then the value of
TOL may be so small that a solution cannot be obtained, in which case the routine should be called again with a larger value for
TOL. If the accuracy requested is really needed then you should consider whether there is a more fundamental difficulty. For example:
(a) 
in the region of a singularity the solution components will usually be of a large magnitude. D02LAF could be used in onestep mode to monitor the size of the solution with the aim of trapping the solution before the singularity. In any case numerical integration cannot be continued through a singularity, and analytical treatment may be necessary; 
(b) 
if the solution contains fast oscillatory components, the routine will require a very small step size to preserve stability. This will usually be exhibited by excessive computing time and sometimes an error exit with ${\mathbf{IFAIL}}={\mathbf{3}}$. The Runge–Kutta–Nystrom methods are not efficient in such cases and you should consider reposing your problem as a system of firstorder ordinary differential equations and then using a routine from Subchapter D02M–N with the Blend formulae (see D02MVF). 
D02LAF can be used for producing results at short intervals (for example, for tabulation), in two ways. By far the less efficient is to call D02LAF successively over short intervals,
$t+\left(i1\right)\times h$ to
$t+i\times h$, although this is the only way if the higher order method has been selected and precisely
not what it is intended for. A more efficient way,
only for use when the lower order method has been selected, is to use D02LAF in onestep mode. The output values of arguments
Y,
YP,
YDP,
T and
RWORK are set correctly for a call to
D02LZF to compute the solution and derivative at the required points.
10 Example
This example solves the following system (the two body problem)
over the range
$\left[0,20\right]$ with initial conditions
${y}_{1}=1.0\epsilon $,
${y}_{2}=0.0$,
${y}_{1}^{\prime}=0.0$ and
${y}_{2}^{\prime}=\sqrt{\left(\frac{1+\epsilon}{1\epsilon}\right)}$ where
$\epsilon $, the eccentricity, is
$0.5$. The system is solved using the lower order method with relative local error tolerances
$\text{1.0E\u22124}$ and
$\text{1.0E\u22125}$ and default threshold tolerances. D02LAF is used in onestep mode (
${\mathbf{ONESTP}}=\mathrm{.TRUE.}$) and
D02LZF provides solution values at intervals of
$2.0$.
10.1 Program Text
Program Text (d02lafe.f90)
10.2 Program Data
Program Data (d02lafe.d)
10.3 Program Results
Program Results (d02lafe.r)