d02 Chapter Contents
d02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_ode_ivp_rk_onestep (d02pdc)

## 1  Purpose

nag_ode_ivp_rk_onestep (d02pdc) is a one-step function for solving the initial value problem for a first order system of ordinary differential equations using Runge–Kutta methods.

## 2  Specification

 #include #include
void  nag_ode_ivp_rk_onestep (Integer neq,
 void (*f)(Integer neq, double t, const double y[], double yp[], Nag_User *comm),
double *tnow, double ynow[], double ypnow[], Nag_ODE_RK *opt, Nag_User *comm, NagError *fail)

## 3  Description

nag_ode_ivp_rk_onestep (d02pdc) and its associated functions (nag_ode_ivp_rk_setup (d02pvc), nag_ode_ivp_rk_reset_tend (d02pwc), nag_ode_ivp_rk_interp (d02pxc) and 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
 $y ′ = f t,y given y t 0 = y 0$
where $y$ is the vector of neq solution components and $t$ is the independent variable.
This function is designed to be used in complicated tasks when solving systems of ordinary differential equations. You must first call nag_ode_ivp_rk_setup (d02pvc) to specify the problem and how it is to be solved. Thereafter you (repeatedly) call nag_ode_ivp_rk_onestep (d02pdc) to take one integration step at a time from tstart in the direction of tend (as specified in nag_ode_ivp_rk_setup (d02pvc)). In this manner nag_ode_ivp_rk_onestep (d02pdc) returns an approximation to the solution ynow and its derivative ypnow at successive points tnow. If nag_ode_ivp_rk_onestep (d02pdc) encounters some difficulty in taking a step, the integration is not advanced and the function returns with the same values of tnow, ynow and ypnow as returned on the previous successful step. nag_ode_ivp_rk_onestep (d02pdc) tries to advance the integration as far as possible subject to passing the test on the local error and not going past tend. In the call to nag_ode_ivp_rk_setup (d02pvc) you can specify either the first step size for nag_ode_ivp_rk_onestep (d02pdc) to attempt or that it compute automatically an appropriate value. Thereafter nag_ode_ivp_rk_onestep (d02pdc) 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_onestep (d02pdc) 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_onestep (d02pdc) by a call to the subroutine nag_ode_ivp_rk_errass (d02pzc).
If you want answers at specific points there are two ways to proceed:
The more efficient way is to step past the point where a solution is desired, and then call nag_ode_ivp_rk_interp (d02pxc) to get an answer there. Within the span of the current step, you can get all the answers you want at very little cost by repeated calls to nag_ode_ivp_rk_interp (d02pxc). This is very valuable when you want to find where something happens, e.g., where a particular solution component vanishes. You cannot proceed in this way with ${\mathbf{method}}=\mathrm{Nag_RK_7_8}$.
The other way to get an answer at a specific point is to set tend to this value and integrate to tend. nag_ode_ivp_rk_onestep (d02pdc) will not step past tend, so when a step would carry it past, it will reduce the step size so as to produce an answer at tend exactly. After getting an answer there (${\mathbf{tnow}}={\mathbf{tend}}$), you can reset tend to the next point where you want an answer, and repeat. tend could be reset by a call to nag_ode_ivp_rk_setup (d02pvc), but you should not do this. You should use nag_ode_ivp_rk_reset_tend (d02pwc) because it is both easier to use and much more efficient. This way of getting answers at specific points can be used with any of the available methods, but it is the only way with ${\mathbf{method}}=\mathrm{Nag_RK_7_8}$. It can be inefficient. Should this be the case, the code will bring the matter to your attention.

## 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 91-S1 Southern Methodist University

## 5  Arguments

1:     neqIntegerInput
On entry: the number of ordinary differential equations in the system to be solved.
Constraint: ${\mathbf{neq}}\ge 1$.
2:     ffunction, 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:     neqIntegerInput
On entry: the number of differential equations.
2:     tdoubleInput
On entry: the current value of the independent variable, $t$.
3:     y[neq]const doubleInput
On entry: the current values of the dependent variables, ${y}_{i}$ for $i=1,2,\dots ,{\mathbf{neq}}$.
4:     yp[neq]doubleOutput
On exit: the values of ${f}_{i}$ for $i=1,2,\dots ,{\mathbf{neq}}$.
5:     commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
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:     tnowdouble *Output
On exit: the value of the independent variable $t$ at which a solution has been computed.
4:     ynow[neq]doubleOutput
On exit: an approximation to the solution at tnow. The local error of the step to tnow was no greater than permitted by the specified tolerances (see nag_ode_ivp_rk_setup (d02pvc)).
5:     ypnow[neq]doubleOutput
On exit: an approximation to the derivative of the solution at tnow.
6:     optNag_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:
totfcnIntegerOutput
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}$.
stpcstIntegerOutput
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).
wastedoubleOutput
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.
stpsokIntegerOutput
On exit: the number of accepted steps.
hnextdoubleOutput
On exit: the step size the integrator plans to use for the next step.
7:     commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
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.
8:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

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}}=〈\mathit{\text{value}}〉$ but the value given to nag_ode_ivp_rk_setup (d02pvc) was $〈\mathit{\text{value}}〉$.
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_onestep (d02pdc) 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_range (d02pcc). However the actual call was made to nag_ode_ivp_rk_onestep (d02pdc). This is not permitted.
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 tnow. ${\mathbf{tnow}}=〈\mathit{\text{value}}〉$.
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_onestep (d02pdc) would have to use a step size of $〈\mathit{\text{value}}〉$ at current ${\mathbf{t}}=〈\mathit{\text{value}}〉$. This is too small for the machine precision.
NE_RK_PDC_TEND
${\mathbf{tend}}=〈\mathit{\text{value}}〉$ 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_STIFF_PROBLEM
The problem appears to be stiff.
NW_RK_TOO_MANY
Approximately $〈\mathit{\text{value}}〉$ 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.

If nag_ode_ivp_rk_onestep (d02pdc) 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 ynow should be monitored 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.
If nag_ode_ivp_rk_onestep (d02pdc) returns with a non-trivial value of fail (i.e., those not related to an invalid call) then performance statistics are available by examining the structure opt (see Section 5). Furthermore if ${\mathbf{errass}}=\mathrm{Nag_ErrorAssess_on}$ then global error assessment is available 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×\mathbf{stpsok}×\mathbf{stpcst}$ for ${\mathbf{method}}=\mathrm{Nag_RK_4_5}$ or $\mathrm{Nag_RK_7_8}$ and $3×\mathbf{stpsok}×\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_onestep (d02pdc) 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_onestep (d02pdc) 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.

## 9  Example

We solve the equation
 $y ′′ = -y , y 0 = 0 , y ′ 0 = 1$
reposed as
 $y 1 ′ = y 2 y 2 ′ = - y 1$
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−8}$ for each solution component and print the solution at each integration step across the range. We use a medium order Runge–Kutta method (${\mathbf{method}}=\mathrm{Nag_RK_4_5}$) with tolerances ${\mathbf{tol}}=\text{1.0e−4}$ and ${\mathbf{tol}}=\text{1.0e−5}$ in turn so that we may compare the solutions. The value of $\pi$ is obtained by using nag_pi (X01AAC).

### 9.1  Program Text

Program Text (d02pdce.c)

None.

### 9.3  Program Results

Program Results (d02pdce.r)