NAG Library Function Document
nag_ode_ivp_rk_range (d02pcc)
1 Purpose
nag_ode_ivp_rk_range (d02pcc) is a function for solving the initial value problem for a first order system of ordinary differential equations using Runge–Kutta methods.
2 Specification
#include <nag.h> 
#include <nagd02.h> 
void 
nag_ode_ivp_rk_range (Integer neq,
void 
(*f)(Integer neq,
double t,
const double y[],
double yp[],
Nag_User *comm),


double twant,
double *tgot,
double ygot[],
double ypgot[],
double ymax[],
Nag_ODE_RK *opt,
Nag_User *comm,
NagError *fail) 

3 Description
nag_ode_ivp_rk_range (d02pcc) and its associated functions (
nag_ode_ivp_rk_setup (d02pvc),
nag_ode_ivp_rk_errass (d02pzc) ) solve the initial value problem for a first order system of ordinary differential equations. The functions, based on Runge–Kutta methods and derived from RKSUITE (
Brankin et al. (1991)) integrate
where
$y$ is the vector of
neq solution components and
$t$ is the independent variable.
This function is designed for the usual task, namely to compute an approximate solution at a sequence of points. You must first call
nag_ode_ivp_rk_setup (d02pvc) to specify the problem and how it is to be solved. Thereafter you call nag_ode_ivp_rk_range (d02pcc) repeatedly with successive values of
twant, the points at which you require the solution, in the range from
tstart to
tend (as specified in
nag_ode_ivp_rk_setup (d02pvc)). In this manner nag_ode_ivp_rk_range (d02pcc) returns the point at which it has computed a solution
tgot (usually
twant), the solution there
ygot and its derivative
ypgot. If nag_ode_ivp_rk_range (d02pcc) encounters some difficulty in taking a step toward
twant, then it returns the point of difficulty
tgot and the solution and derivative computed there
ygot and
ypgot.
In the call to
nag_ode_ivp_rk_setup (d02pvc) you can specify the first step size for nag_ode_ivp_rk_range (d02pcc) to attempt or that it compute automatically an appropriate value. Thereafter nag_ode_ivp_rk_range (d02pcc) estimates an appropriate step size for its next step. This value and other details of the integration can be obtained after any call to nag_ode_ivp_rk_range (d02pcc) by examining the contents of the structure
opt, see
Section 5. The local error is controlled at every step as specified in
nag_ode_ivp_rk_setup (d02pvc). If you wish to assess the true error, you must set
${\mathbf{errass}}=\mathrm{Nag\_ErrorAssess\_on}$ in the call to
nag_ode_ivp_rk_setup (d02pvc). This assessment can be obtained after any call to nag_ode_ivp_rk_range (d02pcc) by a call to the function
nag_ode_ivp_rk_errass (d02pzc).
4 References
Brankin R W, Gladwell I and Shampine L F (1991) RKSUITE: A suite of Runge–Kutta codes for the initial value problems for ODEs SoftReport 91S1 Southern Methodist University
5 Arguments
 1:
$\mathbf{neq}$ – IntegerInput

On entry: the number of ordinary differential equations in the system to be solved.
Constraint:
${\mathbf{neq}}\ge 1$.
 2:
$\mathbf{f}$ – function, supplied by the userExternal Function

f must evaluate the first derivatives
${y}_{i}^{\prime}$ (that is the functions
${f}_{i}$) for given values of the arguments
$t,{y}_{i}$.
The specification of
f is:
void 
f (Integer neq,
double t,
const double y[],
double yp[],
Nag_User *comm)


 1:
$\mathbf{neq}$ – IntegerInput

On entry: the number of differential equations.
 2:
$\mathbf{t}$ – doubleInput

On entry: the current value of the independent variable, $t$.
 3:
$\mathbf{y}\left[{\mathbf{neq}}\right]$ – const doubleInput

On entry: the current values of the dependent variables, ${y}_{i}$, for $i=1,2,\dots ,{\mathbf{neq}}$.
 4:
$\mathbf{yp}\left[{\mathbf{neq}}\right]$ – doubleOutput

On exit: the values of ${f}_{i}$, for $i=1,2,\dots ,{\mathbf{neq}}$.
 5:
$\mathbf{comm}$ – Nag_User *

Pointer to a structure of type Nag_User with the following member:
 p – Pointer

On entry/exit: the pointer
$\mathbf{comm}\mathbf{\to}\mathbf{p}$ should be cast to the required type, e.g.,
struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument
comm below.)
 3:
$\mathbf{twant}$ – doubleInput

On entry: the next value of the independent variable, $t$, where a solution is desired.
Constraint:
twant must be closer to
tend than the previous of
tgot (or
tstart on the first call to nag_ode_ivp_rk_range (d02pcc)); see
nag_ode_ivp_rk_setup (d02pvc) for a description of
tstart and
tend.
twant must not lie beyond
tend in the direction of integration.
 4:
$\mathbf{tgot}$ – double *Output

On exit: the value of the independent variable
$t$ at which a solution has been computed. On successful exit with
${\mathbf{fail}}\mathbf{.}\mathbf{code}=\mathrm{NE\_NOERROR}$,
tgot will equal
twant. For nontrivial values of
fail (i.e., those not related to an invalid call of nag_ode_ivp_rk_range (d02pcc)) a solution has still been computed at the value of
tgot but in general
tgot will not equal
twant.
 5:
$\mathbf{ygot}\left[{\mathbf{neq}}\right]$ – doubleInput/Output

On entry: on the first call to nag_ode_ivp_rk_range (d02pcc),
ygot need not be set. On all subsequent calls
ygot must remain unchanged.
On exit: an approximation to the true solution at the value of
tgot. At each step of the integration to
tgot, the local error has been controlled as specified in
nag_ode_ivp_rk_setup (d02pvc). The local error has still been controlled even when
${\mathbf{tgot}}\ne {\mathbf{twant}}$, that is after a return with a nontrivial error.
 6:
$\mathbf{ypgot}\left[{\mathbf{neq}}\right]$ – doubleOutput

On exit: an approximation to the first derivative of the true solution at
tgot.
 7:
$\mathbf{ymax}\left[{\mathbf{neq}}\right]$ – doubleInput/Output

On entry: on the first call to nag_ode_ivp_rk_range (d02pcc),
ymax need not be set. On all subsequent calls
ymax must remain unchanged.
On exit: ${\mathbf{ymax}}\left[i1\right]$ contains the largest value of $\left{y}_{i}\right$ computed at any step in the integration so far.
 8:
$\mathbf{opt}$ – Nag_ODE_RK *

Pointer to a structure of type Nag_ODE_RK as initialized by the setup function
nag_ode_ivp_rk_setup (d02pvc) with the following members:
 totfcn – IntegerOutput

On exit: the total number of evaluations of
$f$ used in the primary integration so far; this does not include evaluations of
$f$ for the secondary integration specified by a prior call to
nag_ode_ivp_rk_setup (d02pvc) with
${\mathbf{errass}}=\mathrm{Nag\_ErrorAssess\_on}$.
 stpcst – IntegerOutput

On exit: the cost in terms of number of evaluations of
$f$ of a typical step with the method being used for the integration. The method is specified by the argument
method in a prior call to
nag_ode_ivp_rk_setup (d02pvc).
 waste – doubleOutput

On exit: the number of attempted steps that failed to meet the local error requirement divided by the total number of steps attempted so far in the integration. A ‘large’ fraction indicates that the integrator is having trouble with the problem being solved. This can happen when the problem is ‘stiff’ and also when the solution has discontinuities in a low order derivative.
 stpsok – IntegerOutput

On exit: the number of accepted steps.
 hnext – doubleOutput

On exit: the step size the integrator plans to use for the next step.
 9:
$\mathbf{comm}$ – Nag_User *

Pointer to a structure of type Nag_User with the following member:
 p – Pointer

On entry/exit: the pointer
$\mathbf{comm}\mathbf{\to}\mathbf{p}$, of type Pointer, allows you to communicate information to and from
f. An object of the required type should be declared, e.g., a structure, and its address assigned to the pointer
$\mathbf{comm}\mathbf{\to}\mathbf{p}$ by means of a cast to Pointer in the calling program, e.g.,
comm.p = (Pointer)&s. The type pointer will be
void * with a C compiler that defines
void * and
char * otherwise.
 10:
$\mathbf{fail}$ – NagError *Input/Output

The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
 NE_ALLOC_FAIL

Dynamic memory allocation failed.
 NE_INTERNAL_ERROR

An internal error has occurred in this function. Check the function call
and any array sizes. If the call is correct then please contact
NAG for
assistance.
 NE_MEMORY_FREED

Internally allocated memory has been freed by a call to
nag_ode_ivp_rk_free (d02ppc) without a subsequent call to the setup function
nag_ode_ivp_rk_setup (d02pvc).
 NE_NEQ

The value of
neq supplied is not the same as that given to the setup function
nag_ode_ivp_rk_setup (d02pvc).
${\mathbf{neq}}=\u2329\mathit{\text{value}}\u232a$ but the value given to
nag_ode_ivp_rk_setup (d02pvc) was
$\u2329\mathit{\text{value}}\u232a$.
 NE_NO_SETUP

The setup function
nag_ode_ivp_rk_setup (d02pvc) has not been called.
 NE_PREV_CALL

The previous call to a function had resulted in a severe error. You must call
nag_ode_ivp_rk_setup (d02pvc) to start another problem.
 NE_PREV_CALL_INI

The previous call to the function nag_ode_ivp_rk_range (d02pcc) had resulted in a severe error. You must call
nag_ode_ivp_rk_setup (d02pvc) to start another problem.
 NE_RK_INVALID_CALL

The function to be called as specified in the setup function
nag_ode_ivp_rk_setup (d02pvc) was
nag_ode_ivp_rk_onestep (d02pdc). However the actual call was made to nag_ode_ivp_rk_range (d02pcc). This is not permitted.
 NE_RK_PCC_METHOD

The efficiency of the integration has been degraded. Consider calling the setup function
nag_ode_ivp_rk_setup (d02pvc) to reinitialize the integration at the current point with
${\mathbf{method}}=\mathrm{Nag\_RK\_4\_5}$. Alternatively nag_ode_ivp_rk_range (d02pcc) can be called again to resume at the current point.
 NE_RK_PDC_GLOBAL_ERROR_S

The global error assessment algorithm failed at the start of the integration.
 NE_RK_PDC_GLOBAL_ERROR_T

The global error assessment may not be reliable for
t past
tgot.
${\mathbf{tgot}}=\u2329\mathit{\text{value}}\u232a$.
 NE_RK_PDC_POINTS

More than 100 output points have been obtained by integrating to
tend. They have been sufficiently close to one another that the efficiency of the integration has been degraded. It would probably be (much) more efficient to obtain output by interpolating with
nag_ode_ivp_rk_interp (d02pxc) (after changing to
${\mathbf{method}}=\mathrm{Nag\_RK\_4\_5}$ if you are using
${\mathbf{method}}=\mathrm{Nag\_RK\_7\_8}$).
 NE_RK_PDC_STEP

In order to satisfy the error requirements nag_ode_ivp_rk_range (d02pcc) would have to use a step size of $\u2329\mathit{\text{value}}\u232a$ at current ${\mathbf{t}}=\u2329\mathit{\text{value}}\u232a$. This is too small for the machine precision.
 NE_RK_PDC_TEND

${\mathbf{tend}}=\u2329\mathit{\text{value}}\u232a$ has been reached already. To integrate further with same problem the function
nag_ode_ivp_rk_reset_tend (d02pwc) must be called with a new value of
tend.
 NE_RK_TGOT_EQ_TEND

The call to nag_ode_ivp_rk_range (d02pcc) has been made after reaching
tend. The previous call to nag_ode_ivp_rk_range (d02pcc) resulted in
tgot (
tstart on the first call)
$\text{}={\mathbf{tend}}$. You must call
nag_ode_ivp_rk_setup (d02pvc) to start another problem.
 NE_RK_TGOT_RANGE_TEND

The call to nag_ode_ivp_rk_range (d02pcc) has been made with a
twant that does not lie between the previous value of
tgot (
tstart on the first call) and
tend. This is not permitted.
 NE_RK_TGOT_RANGE_TEND_CLOSE

The call to nag_ode_ivp_rk_range (d02pcc) has been made with a
twant that does not lie between the previous value of
tgot (
tstart on the first call) and
tend. This is not permitted. However
twant is very close to
tend, so you may have meant it to be
tend exactly. Check your program.
 NE_RK_TWANT_CLOSE_TGOT

The call to nag_ode_ivp_rk_range (d02pcc) has been made with a
twant that is not sufficiently different from the last value of
tgot (
tstart on the first call). When using
${\mathbf{method}}=\mathrm{Nag\_RK\_7\_8}$, it must differ by at least
$\u2329\mathit{\text{value}}\u232a$.
 NE_STIFF_PROBLEM

The problem appears to be stiff.
 NW_RK_TOO_MANY

Approximately $\u2329\mathit{\text{value}}\u232a$ function evaluations have been used to compute the solution since the integration started or since this message was last printed.
7 Accuracy
The accuracy of integration is determined by the arguments
tol and
thres in a prior call to
nag_ode_ivp_rk_setup (d02pvc). 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 properties of the differential system.
8 Parallelism and Performance
Not applicable.
If nag_ode_ivp_rk_range (d02pcc) returns with
${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE\_RK\_PDC\_STEP}}$ and the accuracy specified by
tol and
thres is really required then you should consider whether there is a more fundamental difficulty. For example, the solution may contain a singularity. In such a region the solution components will usually be of a large magnitude. Successive output values of
ygot and
ymax should be monitored (or the function
nag_ode_ivp_rk_onestep (d02pdc) should be used since this takes one integration step at a time) with the aim of trapping the solution before the singularity. In any case numerical solution cannot be continued through a singularity, and analytical treatment may be necessary.
Performance statistics are available after any return from nag_ode_ivp_rk_range (d02pcc) by examining the structure
opt, see
Section 5. If
${\mathbf{errass}}=\mathrm{Nag\_ErrorAssess\_on}$ in the call to
nag_ode_ivp_rk_setup (d02pvc), global error assessment is available after any return from nag_ode_ivp_rk_range (d02pcc) (except when the error is due to incorrect input arguments or incorrect set up) by a call to the function
nag_ode_ivp_rk_errass (d02pzc). The approximate extra number of evaluations of
$f$ used is given by
$2\times \mathbf{opt}\mathbf{\to}\mathbf{stpsok}\times \mathbf{opt}\mathbf{\to}\mathbf{stpcst}$ for
${\mathbf{method}}=\mathrm{Nag\_RK\_4\_5}$ or
$\mathrm{Nag\_RK\_7\_8}$ and
$3\times \mathbf{opt}\mathbf{\to}\mathbf{stpsok}\times \mathbf{opt}\mathbf{\to}\mathbf{stpcst}$ for
${\mathbf{method}}=\mathrm{Nag\_RK\_2\_3}$.
After a failure with
${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE\_RK\_PDC\_STEP}}$,
NE_RK_PDC_GLOBAL_ERROR_T or
NE_RK_PDC_GLOBAL_ERROR_S the diagnostic function
nag_ode_ivp_rk_errass (d02pzc) may be called only once.
If nag_ode_ivp_rk_range (d02pcc) returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE\_STIFF\_PROBLEM}}$ then it is advisable to change to another code more suited to the solution of stiff problems. nag_ode_ivp_rk_range (d02pcc) will not return with ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE\_STIFF\_PROBLEM}}$ if the problem is actually stiff but it is estimated that integration can be completed using less function evaluations than already computed.
10 Example
We solve the equation
reposed as
over the range
$\left[0,2\pi \right]$ with initial conditions
${y}_{1}=0.0$ and
${y}_{2}=1.0$. We use relative error control with threshold values of
$\text{1.0e\u22128}$ for each solution component and compute the solution at intervals of length
$\pi /4$ across the range. We use a low order Runge–Kutta method (
${\mathbf{method}}=\mathrm{Nag\_RK\_2\_3}$) with tolerances
${\mathbf{tol}}=\text{1.0e\u22123}$ and
${\mathbf{tol}}=\text{1.0e\u22124}$ in turn so that we may compare the solutions. The value of
$\pi $ is obtained by using
nag_pi (X01AAC).
See also
Section 10 in nag_ode_ivp_rk_errass (d02pzc).
10.1 Program Text
Program Text (d02pcce.c)
10.2 Program Data
None.
10.3 Program Results
Program Results (d02pcce.r)