Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_ivp_rkts_reset_tend (d02pr)

## Purpose

nag_ode_ivp_rkts_reset_tend (d02pr) resets the end point in an integration performed by nag_ode_ivp_rkts_onestep (d02pf).

## Syntax

[iwsav, rwsav, ifail] = d02pr(tendnu, iwsav, rwsav)
[iwsav, rwsav, ifail] = nag_ode_ivp_rkts_reset_tend(tendnu, iwsav, rwsav)

## Description

nag_ode_ivp_rkts_reset_tend (d02pr) and its associated functions (nag_ode_ivp_rkts_onestep (d02pf), nag_ode_ivp_rkts_setup (d02pq), nag_ode_ivp_rkts_interp (d02ps), nag_ode_ivp_rkts_diag (d02pt) and nag_ode_ivp_rkts_errass (d02pu)) 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 (see Brankin et al. (1991)), integrate
 $y′=ft,y given yt0=y0$
where $y$ is the vector of $n$ solution components and $t$ is the independent variable.
nag_ode_ivp_rkts_reset_tend (d02pr) is used to reset the final value of the independent variable, ${t}_{f}$, when the integration is already underway. It can be used to extend or reduce the range of integration. The new value must be beyond the current value of the independent variable (as returned in tnow by nag_ode_ivp_rkts_onestep (d02pf)) in the current direction of integration. It is much more efficient to use nag_ode_ivp_rkts_reset_tend (d02pr) for this purpose than to use nag_ode_ivp_rkts_setup (d02pq) which involves the overhead of a complete restart of the integration.
If you want to change the direction of integration then you must restart by a call to nag_ode_ivp_rkts_setup (d02pq).

## 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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{tendnu}$ – double scalar
The new value for ${t}_{f}$.
Constraint: $\mathrm{sign}\left({\mathbf{tendnu}}-{\mathbf{tnow}}\right)=\mathrm{sign}\left({\mathbf{tend}}-{\mathbf{tstart}}\right)$, where tstart and tend are as supplied in the previous call to nag_ode_ivp_rkts_setup (d02pq) and tnow is returned by the preceding call to nag_ode_ivp_rkts_onestep (d02pf) (i.e., integration must proceed in the same direction as before). tendnu must be distinguishable from tnow for the method and the machine precision being used.
2:     $\mathrm{iwsav}\left(130\right)$int64int32nag_int array
3:     $\mathrm{rwsav}\left(350\right)$ – double array
Note: the communication array rwsav used by the other functions in the suite must be used here however, only the first $350$ elements will be referenced.
These must be the same arrays supplied in a previous call to nag_ode_ivp_rkts_onestep (d02pf). They must remain unchanged between calls.

None.

### Output Parameters

1:     $\mathrm{iwsav}\left(130\right)$int64int32nag_int array
2:     $\mathrm{rwsav}\left(350\right)$ – double array
Note: the communication array rwsav used by the other functions in the suite must be used here however, only the first $350$ elements will be referenced.
Information about the integration for use on subsequent calls to nag_ode_ivp_rkts_onestep (d02pf) or other associated functions.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
On entry, a previous call to the setup function 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, tendnu is not beyond tnow (step integrator) in the direction of integration.
On entry, tendnu is too close to tnow (step integrator).
You cannot call this function after the integrator has returned an error.
You cannot call this function before you have called the setup function.
You cannot call this function before you have called the step integrator.
You cannot call this function when the range integrator has been used.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Not applicable.

None.

## Example

This example integrates a two body problem. The equations for the coordinates $\left(x\left(t\right),y\left(t\right)\right)$ of one body as functions of time $t$ in a suitable frame of reference are
 $x′′=-xr3$
 $y′′=-yr3, r=x2+y2.$
The initial conditions
 $x0=1-ε, x′0=0 y0=0, y′0= 1+ε 1-ε$
lead to elliptic motion with $0<\epsilon <1$. $\epsilon =0.7$ is selected and the system of ODEs is reposed as
 $y1′=y3 y2′=y4 y3′=- y1r3 y4′=- y2r3$
over the range $\left[0,6\pi \right]$. Relative error control is used with threshold values of $\text{1.0e−10}$ for each solution component and compute the solution at intervals of length $\pi$ across the range using nag_ode_ivp_rkts_reset_tend (d02pr) to reset the end of the integration range. A high-order Runge–Kutta method (${\mathbf{method}}=-3$) is also used with tolerances ${\mathbf{tol}}=\text{1.0e−4}$ and ${\mathbf{tol}}=\text{1.0e−5}$ in turn so that the solutions may be compared.
```function d02pr_example

fprintf('d02pr example results\n\n');

% Set initial conditions and input
method = int64(-3);
tstart = 0;
tend   = 6*pi;
ecc    = 0.7;
yinit  = [1-ecc; 0; 0; sqrt((1+ecc)/(1-ecc))];
hstart = 0;
thresh = [1e-10; 1e-10; 1e-10; 1e-10];
n      = int64(4);
npts   = 96;
tol0   =  1.0E-4;
ysave  = zeros(npts+1, n);
tsave  = zeros(npts+1, 1);

% Set control for output
tol  = 10.0*tol0;
tinc = (tend-tstart)/npts;

% We run through the calculation twice with two tolerance values
for i = 1:2

tol = tol*0.1;

tendnu = tstart + tinc;

% Call setup function
[iwsav, rwsav, ifail] = d02pq(tstart, tendnu, yinit, tol, thresh, method);

fprintf('\nCalculation with TOL = %8.1e\n\n', tol);
fprintf('    t         y1        y2        y3        y4\n');
fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tstart, yinit);

tsave(1) = tstart;
ysave(1, :) = yinit;
tnow = tstart;
j=1;
while tnow < tend
[tnow, ynow, ypnow, user, iwsav, rwsav, ifail] = ...
d02pf(@f, n, iwsav, rwsav);

if tnow >= tendnu
% Just print some of the results
if rem(j, 16) == 0
fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tnow, ynow);
end
j = j+1;
tsave(j) = tnow;
ysave(j, :) = ynow;
if tnow < tend
tendnu = tendnu + tinc;
[iwsav, rwsav, ifail] = d02pr(tendnu, iwsav, rwsav);
end
end
end

[fevals, stepcost, waste, stepsok, hnext, iwsav, ifail] = d02pt(iwsav, rwsav);
fprintf('Cost of the integration in evaluations of f is %d\n', fevals);

end

% Plot results
fig1 = figure;
hold on;
xlabel('Orbit - x');
ylabel('Orbit - y');
plot(ysave(:,1), ysave(:, 2), '-xr');
% Plot errors with a different scale
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
hold on;
axis([-0.25 0.05 -0.1 0.1]);
xlabel('x Deviation from True Ellipse');
ylabel('y Deviation from True Ellipse');
d = dev(ysave(:,1), ysave(:, 2));
plot(ax2, d.*cos(tsave), d.*sin(tsave),'-*b');
text(-0.05, 0, 'Deviation', 'Color', 'b');

function [yp, user] = f(t, n, y, user)
r = sqrt(y(1)^2+y(2)^2);
yp = [y(3); y(4); -y(1)/r^3; -y(2)/r^3];

function [d] = dev(x, y)
ecc = 0.7;
d =  abs((x+ecc).*(x+ecc)+y.*y/(ecc*ecc)-1);
```
```d02pr example results

Calculation with TOL =  1.0e-04

t         y1        y2        y3        y4
0.000     0.300     0.000     0.000     2.380
3.142    -1.700     0.000    -0.000    -0.420
6.283     0.300    -0.000     0.000     2.380
9.425    -1.700     0.000    -0.000    -0.420
12.566     0.300    -0.000     0.000     2.380
15.708    -1.700     0.000    -0.000    -0.420
18.850     0.300    -0.000     0.001     2.380
Cost of the integration in evaluations of f is 1443

Calculation with TOL =  1.0e-05

t         y1        y2        y3        y4
0.000     0.300     0.000     0.000     2.380
3.142    -1.700     0.000    -0.000    -0.420
6.283     0.300    -0.000     0.000     2.380
9.425    -1.700     0.000    -0.000    -0.420
12.566     0.300    -0.000     0.000     2.380
15.708    -1.700     0.000    -0.000    -0.420
18.850     0.300    -0.000     0.000     2.380
Cost of the integration in evaluations of f is 1509
``` 