# NAG Library Routine Document

## 1Purpose

d02psf computes the solution of a system of ordinary differential equations using interpolation anywhere on an integration step taken by d02pff.

## 2Specification

Fortran Interface
 Subroutine d02psf ( n, f,
 Integer, Intent (In) :: n, ideriv, nwant, lwcomm Integer, Intent (Inout) :: iuser(*), iwsav(130), ifail Real (Kind=nag_wp), Intent (In) :: twant Real (Kind=nag_wp), Intent (Inout) :: wcomm(lwcomm), ruser(*), rwsav(32*n+350) Real (Kind=nag_wp), Intent (Out) :: ywant(nwant), ypwant(nwant) External :: f
#include nagmk26.h
 void d02psf_ (const Integer *n, const double *twant, const Integer *ideriv, const Integer *nwant, double ywant[], double ypwant[], void (NAG_CALL *f)(const double *t, const Integer *n, const double y[], double yp[], Integer iuser[], double ruser[]),double wcomm[], const Integer *lwcomm, Integer iuser[], double ruser[], Integer iwsav[], double rwsav[], Integer *ifail)

## 3Description

d02psf and its associated routines (d02pff, d02pqf, d02prf, d02ptf and d02puf) solve the initial value problem for a first-order system of ordinary differential equations. The routines, 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.
d02pff computes the solution at the end of an integration step. Using the information computed on that step d02psf computes the solution by interpolation at any point on that step. It cannot be used if ${\mathbf{method}}=3$ or $-3$ was specified in the call to setup routine d02pqf.

## 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{n}$ – IntegerInput
On entry: $n$, the number of ordinary differential equations in the system to be solved by the integration routine.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathbf{twant}$ – Real (Kind=nag_wp)Input
On entry: $t$, the value of the independent variable where a solution is desired.
3:     $\mathbf{ideriv}$ – IntegerInput
On entry: determines whether the solution and/or its first derivative are to be computed
${\mathbf{ideriv}}=0$
compute approximate solution.
${\mathbf{ideriv}}=1$
compute approximate first derivative.
${\mathbf{ideriv}}=2$
compute approximate solution and first derivative.
Constraint: ${\mathbf{ideriv}}=0$, $1$ or $2$.
4:     $\mathbf{nwant}$ – IntegerInput
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{n}}$.
5:     $\mathbf{ywant}\left({\mathbf{nwant}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: an approximation to the first nwant components of the solution at twant if ${\mathbf{ideriv}}=0$ or $2$. Otherwise ywant is not defined.
6:     $\mathbf{ypwant}\left({\mathbf{nwant}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: an approximation to the first nwant components of the first derivative at twant if ${\mathbf{ideriv}}=1$ or $2$. Otherwise ypwant is not defined.
7:     $\mathbf{f}$ – Subroutine, supplied by the user.External Procedure
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 d02pff.
The specification of f is:
Fortran Interface
 Subroutine f ( t, n, y, yp,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: iuser(*) Real (Kind=nag_wp), Intent (In) :: t, y(n) Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Real (Kind=nag_wp), Intent (Out) :: yp(n)
#include nagmk26.h
 void f (const double *t, const Integer *n, const double y[], double yp[], Integer iuser[], double ruser[])
1:     $\mathbf{t}$ – Real (Kind=nag_wp)Input
On entry: $t$, the current value of the independent variable.
2:     $\mathbf{n}$ – IntegerInput
On entry: $\mathit{n}$, the number of ordinary differential equations in the system to be solved.
3:     $\mathbf{y}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
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)$ – Real (Kind=nag_wp) arrayOutput
On exit: the values of ${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
5:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
6:     $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
f is called with the arguments iuser and ruser as supplied to d02psf. You should use the arrays iuser and ruser to supply information to f.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02psf is called. Arguments denoted as Input must not be changed by this procedure.
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02psf. If your code inadvertently does return any NaNs or infinities, d02psf is likely to produce unexpected results.
8:     $\mathbf{wcomm}\left({\mathbf{lwcomm}}\right)$ – Real (Kind=nag_wp) arrayCommunication Array
On entry: this array stores information that can be utilized on subsequent calls to d02psf.
9:     $\mathbf{lwcomm}$ – IntegerInput
On entry: length of wcomm.
If in a previous call to d02pqf:
• ${\mathbf{method}}=1$ or $-1$ then lwcomm must be at least $1$.
• ${\mathbf{method}}=2$ or $-2$ then lwcomm must be at least ${\mathbf{n}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{n}},5×{\mathbf{nwant}}\right)$.
• ${\mathbf{method}}=3$ or $-3$ then wcomm and lwcomm are not referenced.
10:   $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
11:   $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
iuser and ruser are not used by d02psf, but are passed directly to f and may be used to pass information to this routine.
12:   $\mathbf{iwsav}\left(130\right)$ – Integer arrayCommunication Array
13:   $\mathbf{rwsav}\left(32×{\mathbf{n}}+350\right)$ – Real (Kind=nag_wp) arrayCommunication Array
On entry: these must be the same arrays supplied in a previous call d02pff. They must remain unchanged between calls.
On exit: information about the integration for use on subsequent calls to d02pff, d02psf or other associated routines.
14:   $\mathbf{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, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{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).

## 6Error 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$
${\mathbf{method}}=-3$ or $3$ in setup, but interpolation is not available for this method. Either use ${\mathbf{method}}=-2$ or $2$ in setup or use reset routine to force the integrator to step to particular points.
On entry, a previous call to the setup routine has not been made or the communication arrays have become corrupted, or a catastrophic error has already been detected elsewhere.
You cannot continue integrating the problem.
On entry, ${\mathbf{ideriv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ideriv}}=0$, $1$ or $2$.
On entry, ${\mathbf{lwcomm}}=〈\mathit{\text{value}}〉$, ${\mathbf{n}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nwant}}=〈\mathit{\text{value}}〉$.
Constraint: for ${\mathbf{method}}=-2$ or $2$, ${\mathbf{lwcomm}}\ge {\mathbf{n}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{n}},5×{\mathbf{nwant}}\right)$.
On entry, ${\mathbf{lwcomm}}=〈\mathit{\text{value}}〉$.
Constraint: for ${\mathbf{method}}=-1$ or $1$, ${\mathbf{lwcomm}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$, but the value passed to the setup routine was ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{nwant}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{nwant}}\le {\mathbf{n}}$.
You cannot call this routine after the integrator has returned an error.
You cannot call this routine before you have called the step integrator.
You cannot call this routine when you have specified, in the setup routine, that the range integrator will be used.
${\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.

## 7Accuracy

The computed values will be of a similar accuracy to that computed by d02pff.

## 8Parallelism and Performance

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

None.

## 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. d02pff is used to integrate the problem one step at a time and d02psf 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. A low order Runge–Kutta method (${\mathbf{method}}=-1$) is also used with tolerances ${\mathbf{tol}}=\text{1.0E−4}$ and ${\mathbf{tol}}=\text{1.0E−5}$ in turn so that solutions may be compared.

### 10.1Program Text

Program Text (d02psfe.f90)

### 10.2Program Data

Program Data (d02psfe.d)

### 10.3Program Results

Program Results (d02psfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017