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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_withdraw_ivp_rk_reset_tend (d02pw)

## Purpose

nag_ode_ivp_rk_reset_tend (d02pw) resets the end point in an integration performed by nag_ode_ivp_rk_onestep (d02pd).
Note: this function is scheduled to be withdrawn, please see d02pw in Advice on Replacement Calls for Withdrawn/Superseded Routines..

## Syntax

[ifail] = d02pw(tendnu)
[ifail] = nag_ode_withdraw_ivp_rk_reset_tend(tendnu)

## Description

nag_ode_ivp_rk_reset_tend (d02pw) and its associated functions (nag_ode_ivp_rk_onestep (d02pd), nag_ode_ivp_rk_setup (d02pv), nag_ode_ivp_rk_interp (d02px), nag_ode_ivp_rk_diag (d02py) and nag_ode_ivp_rk_errass (d02pz)) 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_rk_reset_tend (d02pw) 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_rk_onestep (d02pd)) in the current direction of integration. It is much more efficient to use nag_ode_ivp_rk_reset_tend (d02pw) for this purpose than to use nag_ode_ivp_rk_setup (d02pv) 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_rk_setup (d02pv).

## 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_rk_setup (d02pv) and tnow is returned by the preceding call to nag_ode_ivp_rk_onestep (d02pd). tendnu must be distinguishable from tnow for the method and the machine precision being used.

None.

### Output Parameters

1:     $\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, an invalid input value for tendnu was detected or an invalid call to nag_ode_ivp_rk_reset_tend (d02pw) was made, for example without a previous call to the integration function nag_ode_ivp_rk_onestep (d02pd). You cannot continue integrating the problem.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\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 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_rk_reset_tend (d02pw) 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. The value of $\pi$ is obtained by using nag_math_pi (x01aa).
Note that the length of ${\mathbf{tol}}=\text{1.0e−4}$ and work is large enough for any valid combination of input arguments to nag_ode_ivp_rk_setup (d02pv).
```function d02pw_example

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

% Initialize variables and arrays.
neq = int64(4);
lenwrk = int64(32*neq);
method = int64(3);
ecc = 0.7;

tstart = 0.0;
ystart = [1.0-ecc; 0.0; 0.0; sqrt((1.0+ecc)/(1.0-ecc))];
tend = 6.0*pi;
thres = [1e-10; 1e-10; 1e-10; 1e-10];
task = 'Complex Task';  % tell d02pv to use d02pd.
errass = false;
hstart = 0.0;

% We run through the calculation twice: once to output the results at
% a collection of points, and again to accumulate a series of results for
% plotting.
nstep = [6; 96];

% Prepare to accumulate results.  The first and last points should be the
% same - i.e. so the plot shows a closed trajectory.
xarray = zeros(nstep(2)+1, 1);
yarray = zeros(nstep(2)+1, 4);

for icalc = 1:2
tinc  = tend/nstep(icalc);

% For each calculation, use two tolerances;
% plot results corresponding to the second one.
tol = [1.0e-4; 1.0e-5];
for itol = 1:2;

istep = 1;
twant = tinc;

% d02pv is a setup routine to be called prior to d02pd.
[work, ifail] = d02pv( ...
tstart, ystart, twant, tol(itol), thres, method, ...
task, errass, lenwrk, 'neq', neq, 'hstart', hstart);

if icalc == 1
% Output initial results.
fprintf('Calculation with tol = %1.1e\n\n',tol(itol));
fprintf('   t         y1        y2        y3        y4\n');
fprintf(' %6.3f', tstart);
for ieq = 1:neq
fprintf('  %8.4f', ystart(ieq));
end
fprintf('\n');
else
% Store current results.
xarray(1) = tstart;
for ieq = 1:neq
yarray(1, ieq) = ystart(ieq);
end
end

tnow = 0.0;
while tnow < tend
if icalc == 1
% For the first calculation, keep integrating till the
% (current value for the) endpoint is reached.
while tnow < twant
[tnow, ynow, ypnow, work, ifail] = ...
d02pd( ...
@f, neq, work);
end
% Output current result.
fprintf(' %6.3f', tnow);
for ieq = 1:neq
fprintf('  %8.4f', ynow(ieq));
end
fprintf('\n');
else
% For the second calculation, just take a single step.
[tnow, ynow, ypnow, work, ifail] = ...
d02pd( ...
@f, neq, work);
% Store current result.
xarray(istep+1) = tnow;
for ieq = 1:neq
yarray(istep+1, ieq) = ynow(ieq);
end
end

% Update the endpoint, and call d02pw to reset it.
istep = istep + 1;
twant = istep*tinc;
[ifail] = d02pw(twant);
end

if icalc == 1
% d02py is a diagnostic routine.
[totfcn, stpcst, waste, stpsok, hnext, ifail] = ...
d02py;
fprintf(['\nCost of the integration in evaluations of ', ...
'F is %1.0f\n\n\n'], totfcn);
end
end
end

% Use the first two components of the solution, and calculate the deviation
% from a true ellipse.
x = yarray(:,1);
y = yarray(:,2);
xdev = zeros(nstep(2)+1, 1);
ydev = zeros(nstep(2)+1, 1);
for i = 1:nstep(2)+1
fac = abs((x(i) + ecc)*(x(i) + ecc) + y(i)*y(i)/(ecc*ecc) - 1.0);
xdev(i) = fac*cos(xarray(i));
ydev(i) = fac*sin(xarray(i));
end
% Plot results.
fig1 = figure;
display_plot(x, y, xdev, ydev);

function [yp] = f(t, y, yp)
% Evaluate derivative vector.

yp = zeros(4, 1);
r = sqrt((y(1)^2 + y(2)^2));
yp(1) =  y(3);
yp(2) =  y(4);
yp(3) = -y(1)/r^3;
yp(4) = -y(2)/r^3;

function display_plot(x1, y1, x2, y2)
% Plot the results.  First, get the list of default colours (we have to
% specify the plot colours by hand).
axes('XLim', [-2 0.5], 'YLim', [-0.8 0.9]);
cols = get(gca, 'ColorOrder');
% Plot the first curve.
hline1 = line(x1, y1, 'Color', cols(1,:));
ax1 = gca;
set(ax1, 'XColor', cols(1,:), 'YColor', cols(1,:));
% Label these axes.
xlabel('Orbit - x');
ylabel('Orbit - y');
% Set up the second set of axes and plot the second curve.
ax2 = axes('Position', get(ax1,'Position'), ...
'XAxisLocation', 'top', 'YAxisLocation', 'right', ...
'Color','none', 'XColor',cols(2,:), 'YColor', cols(2,:), ...
'XLim', [-0.25 0.06], 'YLim', [-0.1 0.1]);
hline2 = line(x2, y2, 'Color', cols(2,:), 'Parent', ax2);
% Label these axes.
h = title('RK7-8 orbital solution');
set(h,'Position',[-0.15,0.08]);
% Add a legend, specifying the lines explicitly.
legend([hline1, hline2],'Orbit','Deviation','Location','Best');
% Set some features of the two lines.
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'LineStyle', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'LineStyle', '--');
```
```d02pw example results

Calculation with tol = 1.0e-04

t         y1        y2        y3        y4
0.000    0.3000    0.0000    0.0000    2.3805
3.142   -1.7000    0.0000   -0.0000   -0.4201
6.283    0.3000   -0.0000    0.0001    2.3805
9.425   -1.7000    0.0000   -0.0000   -0.4201
12.566    0.3000   -0.0003    0.0016    2.3805
15.708   -1.7001    0.0001   -0.0001   -0.4201
18.850    0.3000   -0.0010    0.0045    2.3805

Cost of the integration in evaluations of F is 571

Calculation with tol = 1.0e-05

t         y1        y2        y3        y4
0.000    0.3000    0.0000    0.0000    2.3805
3.142   -1.7000   -0.0000    0.0000   -0.4201
6.283    0.3000    0.0000   -0.0000    2.3805
9.425   -1.7000    0.0000   -0.0000   -0.4201
12.566    0.3000   -0.0001    0.0004    2.3805
15.708   -1.7000    0.0000   -0.0000   -0.4201
18.850    0.3000   -0.0003    0.0012    2.3805

Cost of the integration in evaluations of F is 748

``` PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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