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′ = f(t,y)  given  y(t0) = y0 $y′=f(t,y) given y(t0)=y0$
where y$y$ is the vector of n$n$ solution components and t$t$ is the independent variable.
nag_ode_ivp_rkts_reset_tend (d02pr) is used to reset the final value of the independent variable, tf${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:     tendnu – double scalar
The new value for tf${t}_{f}$.
Constraint: sign(tendnutnow) = sign(tendtstart)$\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:     iwsav(130$130$) – int64int32nag_int array
3:     rwsav(350$350$) – double array
Note: the communication array rwsav used by the other functions in the suite must be used here however, only the first 350$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.

None.

### Output Parameters

1:     iwsav(130$130$) – int64int32nag_int array
2:     rwsav(350$350$) – double array
Note: the communication array rwsav used by the other functions in the suite must be used here however, only the first 350$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:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\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 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.

Not applicable.

None.

## Example

```function nag_ode_ivp_rkts_reset_tend_example
% 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;
ynowsave = zeros(npts+1, n);
tnowsave = 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] = ...
nag_ode_ivp_rkts_setup(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);

tnowsave(1) = tstart;
ynowsave(1, :) = yinit;
tnow = tstart;
j=1;
while tnow < tend
[tnow, ynow, ypnow, user, iwsav, rwsav, ifail] = ...
nag_ode_ivp_rkts_onestep(@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;
tnowsave(j) = tnow;
ynowsave(j, :) = ynow;
if tnow < tend
tendnu = tendnu + tinc;
[iwsav, rwsav, ifail] = ...
nag_ode_ivp_rkts_reset_tend(tendnu, iwsav, rwsav);
end
end
end

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

end

% Plot results
fig = figure('Number', 'off');
hold on;
xlabel('Orbit - x');
ylabel('Orbit - y');
plot(ynowsave(:,1), ynowsave(:, 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');
plot(ax2, dev(ynowsave(:,1), ynowsave(:, 2)).*cos(tnowsave), dev(ynowsave(:,1), ynowsave(:, 2)).*sin(tnowsave),'-*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);
```
```

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

```
```function d02pr_example
% 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;
ynowsave = zeros(npts+1, n);
tnowsave = 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);

tnowsave(1) = tstart;
ynowsave(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;
tnowsave(j) = tnow;
ynowsave(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
fig = figure('Number', 'off');
hold on;
xlabel('Orbit - x');
ylabel('Orbit - y');
plot(ynowsave(:,1), ynowsave(:, 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');
plot(ax2, dev(ynowsave(:,1), ynowsave(:, 2)).*cos(tnowsave), dev(ynowsave(:,1), ynowsave(:, 2)).*sin(tnowsave),'-*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);
```
```

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

```

Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013