d02 Chapter Contents
d02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_ode_ivp_rk_interp (d02pxc)

## 1  Purpose

nag_ode_ivp_rk_interp (d02pxc) is a function to compute the solution of a system of ordinary differential equations using interpolation anywhere on an integration step taken by nag_ode_ivp_rk_onestep (d02pdc).

## 2  Specification

 #include #include
void  nag_ode_ivp_rk_interp (Integer neq, double twant, Nag_SolDeriv request, Integer nwant, double ywant[], double ypwant[],
 void (*f)(Integer neq, double t, const double y[], double yp[], Nag_User *comm),
Nag_ODE_RK *opt, Nag_User *comm, NagError *fail)

## 3  Description

nag_ode_ivp_rk_interp (d02pxc) and its associated functions (nag_ode_ivp_rk_setup (d02pvc), nag_ode_ivp_rk_onestep (d02pdc), nag_ode_ivp_rk_reset_tend (d02pwc), 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.
nag_ode_ivp_rk_onestep (d02pdc) computes the solution at the end of an integration step. Using the information computed on that step nag_ode_ivp_rk_interp (d02pxc) computes the solution by interpolation at any point on that step. It cannot be used if ${\mathbf{method}}=\mathrm{Nag_RK_7_8}$ was specified in the call to setup function nag_ode_ivp_rk_setup (d02pvc).

## 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.
Constraint: ${\mathbf{neq}}\ge 1$.
2:     twantdoubleInput
On entry: the value of the independent variable, $t$, where a solution is desired.
3:     requestNag_SolDerivInput
On entry: determines whether the solution and/or its first derivative are computed as follows:
• ${\mathbf{request}}=\mathrm{Nag_Sol}$ – compute approximate solution only;
• ${\mathbf{request}}=\mathrm{Nag_Der}$ – compute approximate first derivative of the solution only;
• ${\mathbf{request}}=\mathrm{Nag_SolDer}$ – compute both approximate solution and first derivative.
Constraint: ${\mathbf{request}}=\mathrm{Nag_Sol}$, $\mathrm{Nag_Der}$ or $\mathrm{Nag_SolDer}$.
4:     nwantIntegerInput
On entry: the number of components of the solution to be computed. The first nwant components are evaluated.
Constraint: $1\le {\mathbf{nwant}}\le {\mathbf{neq}}$.
5:     ywant[nwant]doubleOutput
On exit: an approximation to the first nwant components of the solution at twant when specified by request.
6:     ypwant[nwant]doubleOutput
On exit: an approximation to the first nwant components of the first derivative of the solution at twant when specified by request.
7:     ffunction, supplied by the userExternal 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}$. It must be the same procedure as supplied to nag_ode_ivp_rk_onestep (d02pdc).
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.)
8:     optNag_ODE_RK *Input/Output
On entry: the structure of type Nag_ODE_RK as output from nag_ode_ivp_rk_onestep (d02pdc). You must not change this structure.
On exit: some members of opt are changed internally.
9:     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.
10:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, ${\mathbf{nwant}}=〈\mathit{\text{value}}〉$ while ${\mathbf{neq}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{neq}}\le {\mathbf{nwant}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument request had an illegal value.
NE_INT_ARG_LT
On entry, ${\mathbf{nwant}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nwant}}\ge 1$.
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_MISSING_CALL
Previous call to nag_ode_ivp_rk_onestep (d02pdc) has not been made, hence nag_ode_ivp_rk_interp (d02pxc) must not be called.
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_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) 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_interp (d02pxc). This is not permitted.
NE_RK_PX_METHOD
Interpolation is not available with ${\mathbf{method}}=\mathrm{Nag_RK_7_8}$. Either use ${\mathbf{method}}=\mathrm{Nag_RK_2_3}$ or $\mathrm{Nag_RK_4_5}$ for which interpolation is available. Alternatively use nag_ode_ivp_rk_reset_tend (d02pwc) to make nag_ode_ivp_rk_onestep (d02pdc) step exactly to the points where you want output.

## 7  Accuracy

The computed values will be of a similar accuracy to that computed by nag_ode_ivp_rk_onestep (d02pdc).

None.

## 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. nag_ode_ivp_rk_onestep (d02pdc) is used to integrate the problem one step at a time and nag_ode_ivp_rk_interp (d02pxc) is used to compute the first component of the solution and its derivative at intervals of length $\pi /8$ across the range whenever these points lie in one of those integration steps. We use a moderate order Runge–Kutta method (${\mathbf{method}}=\mathrm{Nag_RK_4_5}$) with tolerances ${\mathbf{tol}}=\text{1.0e−3}$ and ${\mathbf{tol}}=\text{1.0e−4}$ 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 (d02pxce.c)

None.

### 9.3  Program Results

Program Results (d02pxce.r)