# NAG CL Interfaced02pec (ivp_​rkts_​range)

## 1Purpose

d02pec solves an initial value problem for a first-order system of ordinary differential equations using Runge–Kutta methods.

## 2Specification

 #include
void  d02pec (
 void (*f)(double t, Integer n, const double y[], double yp[], Nag_Comm *comm),
Integer n, double twant, double *tgot, double ygot[], double ypgot[], double ymax[], Nag_Comm *comm, Integer iwsav[], double rwsav[], NagError *fail)
The function may be called by the names: d02pec or nag_ode_ivp_rkts_range.

## 3Description

d02pec and its associated functions (d02pqc, d02ptc and d02puc) solve an initial value problem for a first-order system of ordinary differential equations. The functions, based on Runge–Kutta methods and derived from RKSUITE (see Brankin et al. (1991)), integrate
 $y′=ft,y given yt0=y0$
where $y$ is the vector of $\mathit{n}$ solution components and $t$ is the independent variable.
d02pec is designed for the usual task, namely to compute an approximate solution at a sequence of points. You must first call d02pqc to specify the problem and how it is to be solved. Thereafter you call d02pec repeatedly with successive values of twant, the points at which you require the solution, in the range from tstart to tend (as specified in d02pqc). In this manner d02pec returns the point at which it has computed a solution tgot (usually twant), the solution there (ygot) and its derivative (ypgot). If d02pec 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, respectively).
In the call to d02pqc you can specify either the first step size for d02pec to attempt or that it computes automatically an appropriate value. Thereafter d02pec estimates an appropriate step size for its next step. This value and other details of the integration can be obtained after any call to d02pec by a call to d02ptc. The local error is controlled at every step as specified in d02pqc. If you wish to assess the true error, you must set ${\mathbf{errass}}=\mathrm{Nag_ErrorAssess_on}$ in the call to d02pqc. This assessment can be obtained after any call to d02pec by a call to d02puc.
For more complicated tasks, you are referred to functions d02pfc, d02prc and d02psc, all of which are used by d02pec.

## 4References

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

## 5Arguments

1: $\mathbf{f}$function, supplied by the user External Function
f must evaluate the functions ${f}_{i}$ (that is the first derivatives ${y}_{i}^{\prime }$) for given values of the arguments $t$, ${y}_{i}$.
The specification of f is:
 void f (double t, Integer n, const double y[], double yp[], Nag_Comm *comm)
1: $\mathbf{t}$double Input
On entry: $t$, the current value of the independent variable.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of ordinary differential equations in the system to be solved.
3: $\mathbf{y}\left[{\mathbf{n}}\right]$const double Input
On entry: the current values of the dependent variables, ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
4: $\mathbf{yp}\left[{\mathbf{n}}\right]$double Output
On exit: the values of ${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
5: $\mathbf{comm}$Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to f.
userdouble *
iuserInteger *
pPointer
The type Pointer will be void *. Before calling d02pec you may allocate memory and initialize these pointers with various quantities for use by f when called from d02pec (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02pec. If your code inadvertently does return any NaNs or infinities, d02pec is likely to produce unexpected results.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of ordinary differential equations in the system to be solved.
Constraint: ${\mathbf{n}}\ge 1$.
3: $\mathbf{twant}$double Input
On entry: $t$, the next value of the independent variable where a solution is desired.
Constraint: twant must be closer to tend than the previous value of tgot (or tstart on the first call to d02pec); see d02pqc 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: $t$, the value of the independent variable at which a solution has been computed. On successful exit with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_NOERROR, tgot will equal twant. On exit with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_RK_GLOBAL_ERROR_S, NE_RK_GLOBAL_ERROR_T, NE_RK_POINTS, NE_RK_STEP_TOO_SMALL, NE_STIFF_PROBLEM or NW_RK_TOO_MANY, a solution has still been computed at the value of tgot but in general tgot will not equal twant.
5: $\mathbf{ygot}\left[{\mathbf{n}}\right]$double Input/Output
On entry: on the first call to d02pec, 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 d02pqc. The local error has still been controlled even when ${\mathbf{tgot}}\ne {\mathbf{twant}}$, that is after a return with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_RK_GLOBAL_ERROR_S, NE_RK_GLOBAL_ERROR_T, NE_RK_POINTS, NE_RK_STEP_TOO_SMALL, NE_STIFF_PROBLEM or NW_RK_TOO_MANY.
6: $\mathbf{ypgot}\left[{\mathbf{n}}\right]$double Output
On exit: an approximation to the first derivative of the true solution at tgot.
7: $\mathbf{ymax}\left[{\mathbf{n}}\right]$double Input/Output
On entry: on the first call to d02pec, ymax need not be set. On all subsequent calls ymax must remain unchanged.
On exit: ${\mathbf{ymax}}\left[i-1\right]$ contains the largest value of $\left|{y}_{i}\right|$ computed at any step in the integration so far.
8: $\mathbf{comm}$Nag_Comm *
The NAG communication argument (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
9: $\mathbf{iwsav}\left[130\right]$Integer Communication Array
10: $\mathbf{rwsav}\left[32×{\mathbf{n}}+350\right]$double Communication Array
On entry: these must be the same arrays supplied in a previous call to d02pqc. They must remain unchanged between calls.
On exit: information about the integration for use on subsequent calls to d02pec or other associated functions.
11: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT_CHANGED
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$, but the value passed to the setup function was ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_MISSING_CALL
On entry, a previous call to the setup function has not been made or the communication arrays have become corrupted.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_PREV_CALL
On entry, the communication arrays have become corrupted, or a catastrophic error has already been detected elsewhere. You cannot continue integrating the problem.
NE_PREV_CALL_INI
You cannot call this function after it has returned an error.
You must call the setup function to start another problem.
NE_RK_GLOBAL_ERROR_S
The global error assessment algorithm failed at start of integration.
The integration is being terminated.
NE_RK_GLOBAL_ERROR_T
The global error assessment may not be reliable for times beyond $〈\mathit{\text{value}}〉$.
The integration is being terminated.
NE_RK_INVALID_CALL
You cannot call this function when you have specified, in the setup function, that the step integrator will be used.
NE_RK_POINTS
This function is being used inefficiently because the step size has been reduced drastically many times to obtain answers at many points. Using the order $4$ and $5$ pair method at setup is more appropriate here. You can continue integrating this problem.
NE_RK_STEP_TOO_SMALL
In order to satisfy your error requirements the solver has to use a step size of $〈\mathit{\text{value}}〉$ at the current time, $〈\mathit{\text{value}}〉$. This step size is too small for the machine precision, and is smaller than $〈\mathit{\text{value}}〉$.
NE_RK_TGOT_EQ_TEND
To start a new problem, you will need to call the setup function.
NE_RK_TGOT_RANGE_TEND
twant does not lie in the direction of integration. ${\mathbf{twant}}=〈\mathit{\text{value}}〉$.
twant lies beyond tend (setup) in the direction of integration.
${\mathbf{twant}}=〈\mathit{\text{value}}〉$ and ${\mathbf{tend}}=〈\mathit{\text{value}}〉$.
NE_RK_TGOT_RANGE_TEND_CLOSE
twant lies beyond tend (setup) in the direction of integration, but is very close to tend.
You may have intended ${\mathbf{twant}}={\mathbf{tend}}$.
$\left|{\mathbf{twant}}-{\mathbf{tend}}\right|=〈\mathit{\text{value}}〉$.
NE_RK_TWANT_CLOSE_TGOT
twant is too close to the last value of tgot (tstart on setup).
When using the method of order $8$ at setup, these must differ by at least $〈\mathit{\text{value}}〉$. Their absolute difference is $〈\mathit{\text{value}}〉$.
NE_STIFF_PROBLEM
Approximately $〈\mathit{\text{value}}〉$ function evaluations have been used to compute the solution since the integration started or since this message was last printed. Your problem has been diagnosed as stiff. If the situation persists, it will cost roughly $〈\mathit{\text{value}}〉$ times as much to reach tend (setup) as it has cost to reach the current time. You should probably call functions intended for stiff problems. However, you can continue integrating the problem.
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. However, you can continue integrating the problem.

## 7Accuracy

The accuracy of integration is determined by the arguments tol and thresh in a prior call to d02pqc (see the function document for d02pqc for further details and advice). 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.

## 8Parallelism and Performance

d02pec 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

If d02pec returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_RK_STEP_TOO_SMALL and the accuracy specified by tol and thresh 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 large in magnitude. Successive output values of ygot and ymax should be monitored (or d02pfc 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 integration cannot be continued through a singularity, and analytical treatment may be necessary.
Performance statistics are available after any return from d02pec by a call to d02ptc. If ${\mathbf{errass}}=\mathrm{Nag_ErrorAssess_on}$ in the call to d02pqc, global error assessment is available after a return from d02pec with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_NOERROR, NE_RK_GLOBAL_ERROR_S, NE_RK_GLOBAL_ERROR_T, NE_RK_POINTS, NE_RK_STEP_TOO_SMALL, NE_STIFF_PROBLEM or NW_RK_TOO_MANY by a call to d02puc.
After a failure with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_RK_GLOBAL_ERROR_S, NE_RK_GLOBAL_ERROR_T or NE_RK_STEP_TOO_SMALL each of the diagnostic functions d02ptc and d02puc may be called only once.
If d02pec returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_STIFF_PROBLEM then it is advisable to change to another code more suited to the solution of stiff problems. d02pec will not return with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ 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.

## 10Example

This example solves the equation
 $y′′=-y, y0=0, y′0=1$
reposed as
 $y1′=y2$
 $y2′=-y1$
over the range $\left[0,2\pi \right]$ with initial conditions ${y}_{1}=0.0$ and ${y}_{2}=1.0$. Relative error control is used with threshold values of $\text{1.0e−8}$ for each solution component and compute the solution at intervals of length $\pi /4$ across the range. A low-order Runge–Kutta method (see d02pqc) is also used with tolerances ${\mathbf{tol}}=\text{1.0e−3}$ and ${\mathbf{tol}}=\text{1.0e−4}$ in turn so that the solutions can be compared.