hide long namesshow long names
hide short namesshow short names
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_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_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 = f(t,y)  given  y(t0) = y0
y=f(t,y)  given  y(t0)=y0
where yy is the vector of nn solution components and tt is the independent variable.
nag_ode_ivp_rk_reset_tend (d02pw) is used to reset the final value of the independent variable, tftf, 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:     tendnu – double scalar
The new value for tftf.
Constraint: sign(tendnutnow) = sign(tendtstart)sign(tendnu-tnow)=sign(tend-tstart), 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.

Optional Input Parameters

None.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=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.

Accuracy

Not applicable.

Further Comments

None.

Example

This example integrates a two body problem. The equations for the coordinates (x(t),y(t))(x(t),y(t)) of one body as functions of time tt in a suitable frame of reference are
x = x/(r3)
x=-xr3
y = y/(r3),   r = sqrt(x2 + y2).
y=-yr3,   r=x2+y2.
The initial conditions
x(0) = 1ε, x(0) = 0
y(0) = 0, y(0) = sqrt((1 + ε)/(1ε))
x(0)=1-ε, x(0)=0 y(0)=0, y(0)= 1+ε 1-ε
lead to elliptic motion with 0 < ε < 10<ε<1. ε = 0.7ε=0.7 is selected and reposed as
y1 = y3
y2 = y4
y3 = (y1)/(r3)
y4 = (y2)/(r3)
y1=y3 y2=y4 y3=- y1r3 y4=- y2r3
over the range [0,6π][0,6π]. Relative error control is used with threshold values of 1.0e−101.0e−10 for each solution component and compute the solution at intervals of length ππ 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 (method = 3method=3) is also used with tolerances tol = 1.0e−4tol=1.0e−4 and tol = 1.0e−5tol=1.0e−5 in turn so that the solutions may be compared. The value of ππ is obtained by using nag_math_pi (x01aa).
Note that the length of tol = 1.0e−4tol=1.0e−4 and work is large enough for any valid combination of input arguments to nag_ode_ivp_rk_setup (d02pv).
function nag_ode_ivp_rk_reset_tend_example
% Initialize variables and arrays.
neq = 4;
lenwrk = 32*neq;
method = 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 nag_ode_ivp_rk_setup to use nag_ode_ivp_rk_onestep.
errass = false;
hstart = 0.0;

fprintf('nag_ode_ivp_rk_reset_tend example program results \n\n');

% 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, we use two tolerances (and we'll plot the
    % results corresponding to the second one).
    tol = [1.0e-4; 1.0e-5];
    for itol = 1:2;

        istep = 1;
        twant = tinc;

        % nag_ode_ivp_rk_setup is a setup routine to be called prior to nag_ode_ivp_rk_onestep.
        [work, ifail] = nag_ode_ivp_rk_setup(tstart, ystart, twant, tol(itol), thres, ...
            int64(method), task, errass, int64(lenwrk), 'neq', ...
            int64(neq), 'hstart', hstart);
        if ifail ~= 0
            % Unsuccessful call.  Print message and exit.
            error('Warning: nag_ode_ivp_rk_setup returned with ifail = %1d ',ifail);
        end

        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] = nag_ode_ivp_rk_onestep(@f, ...
                        int64(neq), work);
                    if ifail ~= 0
                        % Unsuccessful call.  Print message and exit.
                        error(...
                            'Warning: nag_ode_ivp_rk_onestep returned with ifail = %1d ',...
                            ifail);
                    end
                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] = nag_ode_ivp_rk_onestep(@f, ...
                    int64(neq), work);
                if ifail ~= 0
                    % Unsuccessful call.  Print message and exit.
                    error(...
                        'Warning: nag_ode_ivp_rk_onestep returned with ifail = %1d ',...
                        ifail);
                end
                % Store current result.
                xarray(istep+1) = tnow;
                for ieq = 1:neq
                    yarray(istep+1, ieq) = ynow(ieq);
                end
            end

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

        if icalc == 1
            % nag_ode_ivp_rk_diag is a diagnostic routine.
            [totfcn, stpcst, waste, stpsok, hnext, ifail] = nag_ode_ivp_rk_diag;
            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.
fig = figure('Number', 'off');
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)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot the results.  First, get the list of default colours (we have to
% specify the plot colours by hand).
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', labFmt{:});
ylabel('Orbit - y', labFmt{:});
% NB We don't give a title to this plot, because it would collide with the
% the xlabel for the second set of axes (see below).
% 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], 'FontSize', 13);
hline2 = line(x2, y2, 'Color', cols(2,:), 'Parent', ax2);
% Label these axes.
xlabel('x Deviation from True Ellipse', labFmt{:});
ylabel('y Deviation from True Ellipse', labFmt{:});
% 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', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
 
nag_ode_ivp_rk_reset_tend example program 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



function d02pw_example
% Initialize variables and arrays.
neq = 4;
lenwrk = 32*neq;
method = 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;

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

% 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, we use two tolerances (and we'll plot the
    % 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, ...
            int64(method), task, errass, int64(lenwrk), 'neq', ...
            int64(neq), 'hstart', hstart);
        if ifail ~= 0
            % Unsuccessful call.  Print message and exit.
            error('Warning: d02pv returned with ifail = %1d ',ifail);
        end

        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, ...
                        int64(neq), work);
                    if ifail ~= 0
                        % Unsuccessful call.  Print message and exit.
                        error(...
                            'Warning: d02pd returned with ifail = %1d ',...
                            ifail);
                    end
                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, ...
                    int64(neq), work);
                if ifail ~= 0
                    % Unsuccessful call.  Print message and exit.
                    error(...
                        'Warning: d02pd returned with ifail = %1d ',...
                        ifail);
                end
                % 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.
fig = figure('Number', 'off');
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)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot the results.  First, get the list of default colours (we have to
% specify the plot colours by hand).
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', labFmt{:});
ylabel('Orbit - y', labFmt{:});
% NB We don't give a title to this plot, because it would collide with the
% the xlabel for the second set of axes (see below).
% 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], 'FontSize', 13);
hline2 = line(x2, y2, 'Color', cols(2,:), 'Parent', ax2);
% Label these axes.
xlabel('x Deviation from True Ellipse', labFmt{:});
ylabel('y Deviation from True Ellipse', labFmt{:});
% 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', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
 
d02pw example program 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–2013