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_rkts_range (d02pe)

Purpose

nag_ode_ivp_rkts_range (d02pe) solves an initial value problem for a first-order system of ordinary differential equations using Runge–Kutta methods.

Syntax

[tgot, ygot, ypgot, ymax, user, iwsav, rwsav, ifail] = d02pe(f, twant, ygot, ymax, iwsav, rwsav, 'n', n, 'user', user)
[tgot, ygot, ypgot, ymax, user, iwsav, rwsav, ifail] = nag_ode_ivp_rkts_range(f, twant, ygot, ymax, iwsav, rwsav, 'n', n, 'user', user)

Description

nag_ode_ivp_rkts_range (d02pe) and its associated functions (nag_ode_ivp_rkts_setup (d02pq), nag_ode_ivp_rkts_diag (d02pt) and nag_ode_ivp_rkts_errass (d02pu)) solve an 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_rkts_range (d02pe) is designed for the usual task, namely to compute an approximate solution at a sequence of points. You must first call nag_ode_ivp_rkts_setup (d02pq) to specify the problem and how it is to be solved. Thereafter you call nag_ode_ivp_rkts_range (d02pe) repeatedly with successive values of twant, the points at which you require the solution, in the range from tstart to tend (as specified in nag_ode_ivp_rkts_setup (d02pq)). In this manner nag_ode_ivp_rkts_range (d02pe) returns the point at which it has computed a solution tgot (usually twant), the solution there (ygot) and its derivative (ypgot). If nag_ode_ivp_rkts_range (d02pe) encounters some difficulty in taking a step toward twant, then it returns the point of difficulty (tgot) and the solution and derivative computed there (ygot and ypgot, respectively).
In the call to nag_ode_ivp_rkts_setup (d02pq) you can specify either the first step size for nag_ode_ivp_rkts_range (d02pe) to attempt or that it computes automatically an appropriate value. Thereafter nag_ode_ivp_rkts_range (d02pe) estimates an appropriate step size for its next step. This value and other details of the integration can be obtained after any call to nag_ode_ivp_rkts_range (d02pe) by a call to nag_ode_ivp_rkts_diag (d02pt). The local error is controlled at every step as specified in nag_ode_ivp_rkts_setup (d02pq). If you wish to assess the true error, you must set method to a positive value in the call to nag_ode_ivp_rkts_setup (d02pq). This assessment can be obtained after any call to nag_ode_ivp_rkts_range (d02pe) by a call to nag_ode_ivp_rkts_errass (d02pu).
For more complicated tasks, you are referred to functions nag_ode_ivp_rkts_onestep (d02pf), nag_ode_ivp_rkts_reset_tend (d02pr) and nag_ode_ivp_rkts_interp (d02ps), all of which are used by nag_ode_ivp_rkts_range (d02pe).

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:     f – function handle or string containing name of m-file
f must evaluate the functions fifi (that is the first derivatives yiyi) for given values of the arguments tt, yiyi.
[yp, user] = f(t, n, y, user)

Input Parameters

1:     t – double scalar
tt, the current value of the independent variable.
2:     n – int64int32nag_int scalar
nn, the number of ordinary differential equations in the system to be solved.
3:     y(n) – double array
The current values of the dependent variables, yiyi, for i = 1,2,,ni=1,2,,n.
4:     user – Any MATLAB object
f is called from nag_ode_ivp_rkts_range (d02pe) with the object supplied to nag_ode_ivp_rkts_range (d02pe).

Output Parameters

1:     yp(n) – double array
The values of fifi, for i = 1,2,,ni=1,2,,n.
2:     user – Any MATLAB object
2:     twant – double scalar
tt, the next value of the independent variable where a solution is desired.
Constraint: twant must be closer to tend than the previous value of tgot (or tstart on the first call to nag_ode_ivp_rkts_range (d02pe)); see nag_ode_ivp_rkts_setup (d02pq) for a description of tstart and tend. twant must not lie beyond tend in the direction of integration.
3:     ygot(n) – double array
n, the dimension of the array, must satisfy the constraint n1n1.
On the first call to nag_ode_ivp_rkts_range (d02pe), ygot need not be set. On all subsequent calls ygot must remain unchanged.
4:     ymax(n) – double array
n, the dimension of the array, must satisfy the constraint n1n1.
On the first call to nag_ode_ivp_rkts_range (d02pe), ymax need not be set. On all subsequent calls ymax must remain unchanged.
5:     iwsav(130130) – int64int32nag_int array
6:     rwsav(32 × n + 35032×n+350) – double array
These must be the same arrays supplied in a previous call to nag_ode_ivp_rkts_setup (d02pq). They must remain unchanged between calls.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the arrays ygot, ymax. (An error is raised if these dimensions are not equal.)
nn, the number of ordinary differential equations in the system to be solved.
Constraint: n1n1.
2:     user – Any MATLAB object
user is not used by nag_ode_ivp_rkts_range (d02pe), but is passed to f. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

Input Parameters Omitted from the MATLAB Interface

iuser ruser

Output Parameters

1:     tgot – double scalar
tt, the value of the independent variable at which a solution has been computed. On successful exit with ifail = 0ifail=0, tgot will equal twant. On exit with ifail > 1ifail>1, a solution has still been computed at the value of tgot but in general tgot will not equal twant.
2:     ygot(n) – double array
An approximation to the true solution at the value of tgot. At each step of the integration to tgot, the local error has been controlled as specified in nag_ode_ivp_rkts_setup (d02pq). The local error has still been controlled even when tgottwanttgottwant, that is after a return with ifail > 1ifail>1.
3:     ypgot(n) – double array
An approximation to the first derivative of the true solution at tgot.
4:     ymax(n) – double array
ymax(i)ymaxi contains the largest value of |yi||yi| computed at any step in the integration so far.
5:     user – Any MATLAB object
6:     iwsav(130130) – int64int32nag_int array
7:     rwsav(32 × n + 35032×n+350) – double array
Information about the integration for use on subsequent calls to nag_ode_ivp_rkts_range (d02pe) or other associated functions.
8:     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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

  ifail = 1ifail=1
On entry, a previous call to the setup function has not been made or the communication arrays have become corrupted.
On entry, n = _n=_, but the value passed to the setup function was .
On entry, the communication arrays have become corrupted, or a catastrophic error has already been detected elsewhere. You cannot continue integrating the problem.
tend (setup) had already been reached in a previous call.
To start a new problem, you will need to call the setup function.
twant does not lie in the direction of integration.
twant is too close to the last value of tgot (tstart on setup).
twant lies beyond tend (setup) in the direction of integration, but is very close to tend.
twant lies beyond tend (setup) in the direction of integration.
You cannot call this function after it has returned an error.
You must call the setup function to start another problem.
You cannot call this function when you have specified, in the setup function, that the step integrator will be used.
W ifail = 2ifail=2
This function is being used inefficiently because the step size has been reduced drastically many times to obtain answers at many points. Using the order 44 and 55 pair method at setup is more appropriate here. You can continue integrating this problem.
W ifail = 3ifail=3
Approximately __ function evaluations have been used to compute the solution since the integration started or since this message was last printed. However, you can continue integrating the problem.
W ifail = 4ifail=4
Approximately __ function evaluations have been used to compute the solution since the integration started or since this message was last printed. Your problem has been diagnosed as stiff. If the situation persists, it will cost roughly __ times as much to reach tend (setup) as it has cost to reach the current time. You should probably call functions intended for stiff problems. However, you can continue integrating the problem.
W ifail = 5ifail=5
In order to satisfy your error requirements the solver has to use a step size of __ at the current time, __. This step size is too small for the machine precision, and is smaller than __.
W ifail = 6ifail=6
The global error assessment algorithm failed at start of integration.
The integration is being terminated.
The global error assessment may not be reliable for times beyond __.
The integration is being terminated.

Accuracy

The accuracy of integration is determined by the parameters tol and thresh in a prior call to nag_ode_ivp_rkts_setup (d02pq) (see the function document for nag_ode_ivp_rkts_setup (d02pq) for further details and advice). Note that only the local error at each step is controlled by these parameters. The error estimates obtained are not strict bounds but are usually reliable over one step. Over a number of steps the overall error may accumulate in various ways, depending on the properties of the differential system.

Further Comments

If nag_ode_ivp_rkts_range (d02pe) returns with ifail = 5ifail=5 and the accuracy specified by tol and thresh is really required then you should consider whether there is a more fundamental difficulty. For example, the solution may contain a singularity. In such a region the solution components will usually be large in magnitude. Successive output values of ygot and ymax should be monitored (or nag_ode_ivp_rkts_onestep (d02pf) should be used since this takes one integration step at a time) with the aim of trapping the solution before the singularity. In any case numerical integration cannot be continued through a singularity, and analytical treatment may be necessary.
Performance statistics are available after any return from nag_ode_ivp_rkts_range (d02pe) by a call to nag_ode_ivp_rkts_diag (d02pt). If method > 0method>0 in the call to nag_ode_ivp_rkts_setup (d02pq), global error assessment is available after a return from nag_ode_ivp_rkts_range (d02pe) with ifail = 0ifail=0, 22, 33, 44, 55 or 66 by a call to nag_ode_ivp_rkts_errass (d02pu).
After a failure with ifail = 5ifail=5 or 66 each of the diagnostic functions nag_ode_ivp_rkts_diag (d02pt) and nag_ode_ivp_rkts_errass (d02pu) may be called only once.
If nag_ode_ivp_rkts_range (d02pe) returns with ifail = 4ifail=4 then it is advisable to change to another code more suited to the solution of stiff problems. nag_ode_ivp_rkts_range (d02pe) will not return with ifail = 4ifail=4 if the problem is actually stiff but it is estimated that integration can be completed using less function evaluations than already computed.

Example

function nag_ode_ivp_rkts_range_example
% Set initial conditions and input
method = int64(1);
tstart = 0;
tend = 2*pi;
yinit = [0;1];
hstart = 0;
thresh = [1e-08; 1e-08];
npts = 40;
tol0 =  1.0E-3;
ygot = zeros(npts+1, 2);
tgot = zeros(npts+1, 1);
err1 = zeros(npts+1, 2);
err2 = zeros(npts+1, 2);
ymax = zeros(1, 2);

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





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

  tol = tol*0.1;

  % Call setup function
  [iwsav, rwsav, ifail] = ...
    nag_ode_ivp_rkts_setup(tstart, tend, yinit, tol, thresh, method);

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

  tgot(1) = tstart;
  ygot(1,:) = yinit;
  twant = tstart;
  for j=1:npts
    twant = twant + tinc;
    [tgot(j+1), ygot(j+1,:), ypgot, ymax, user, iwsav, rwsav, ifail] = ...
      nag_ode_ivp_rkts_range(@f, twant, ygot(j, :), ymax, iwsav, rwsav);

    err1(j+1, i) =  ygot(j+1, 1)-sin(tgot(j+1));
    err2(j+1, i) =  ygot(j+1, 2)-cos(tgot(j+1));

    if rem(j, 5) == 0
      fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tgot(j+1), ygot(j+1, :), err1(j+1, i), err2(j+1));
    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');
title('First-order ODEs using Runge-Kutta Low-order Method using Two Tolerances');
hold on;
axis([0 10 -1.2 1.2]);
xlabel('t');
ylabel('Solution (y, y'')');
plot(tgot, ygot(:, 1), '-xr');
text(ceil(tgot(npts+1)), ygot(npts+1, 1)-0.2, 'y', 'Color', 'r');
plot(tgot, ygot(:, 2), '-xg');
text(ceil(tgot(npts+1)), ygot(npts+1, 2), 'y''', 'Color', 'g');
% Plot errors with a different (log) scale
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','bottom',...
           'YAxisLocation','right',...
           'YScale', 'log', ...
           'Color','none',...
           'XColor','k','YColor','k');
hold on;
axis([0 10 1e-7 0.01]);
ylabel('abs(Error)');
plot(ax2, tgot, abs(err1(:, 1)), '-*b');
text(ceil(tgot(npts+1)), err1(npts+1, 1), 'y-error (tol=0.001)', 'Color', 'b');
plot(ax2, tgot, abs(err1(:, 2)), '-sm');
text(ceil(tgot(npts+1)), err1(npts+1, 2), 'y-error (tol=0.0001)', 'Color', 'm');



function [yp, user] = f(t, n, y, user)
  yp = [y(2); -y(1)];
 

Calculation with TOL =  1.0e-03

    t         y1        y2       err1     err2
  0.000     0.000     1.000     0.000     0.000
  0.785     0.707     0.707    -0.000    -0.000
  1.571     0.999    -0.000    -0.001    -0.000
  2.356     0.706    -0.706    -0.001     0.001
  3.142    -0.000    -0.998    -0.000     0.002
  3.927    -0.706    -0.705     0.001     0.002
  4.712    -0.998     0.001     0.002     0.001
  5.498    -0.705     0.706     0.002    -0.002
  6.283     0.001     0.997     0.001    -0.003
Cost of the integration in evaluations of f is 421

Calculation with TOL =  1.0e-04

    t         y1        y2       err1     err2
  0.000     0.000     1.000     0.000     0.000
  0.785     0.707     0.707    -0.000    -0.000
  1.571     1.000    -0.000    -0.000    -0.000
  2.356     0.707    -0.707    -0.000     0.001
  3.142    -0.000    -1.000    -0.000     0.002
  3.927    -0.707    -0.707     0.000     0.002
  4.712    -1.000     0.000     0.000     0.001
  5.498    -0.707     0.707     0.000    -0.002
  6.283     0.000     1.000     0.000    -0.003
Cost of the integration in evaluations of f is 871

function d02pe_example
% Set initial conditions and input
method = int64(1);
tstart = 0;
tend = 2*pi;
yinit = [0;1];
hstart = 0;
thresh = [1e-08; 1e-08];
npts = 40;
tol0 =  1.0E-3;
ygot = zeros(npts+1, 2);
tgot = zeros(npts+1, 1);
err1 = zeros(npts+1, 2);
err2 = zeros(npts+1, 2);
ymax = zeros(1, 2);

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





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

  tol = tol*0.1;

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

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

  tgot(1) = tstart;
  ygot(1,:) = yinit;
  twant = tstart;
  for j=1:npts
    twant = twant + tinc;
    [tgot(j+1), ygot(j+1,:), ypgot, ymax, user, iwsav, rwsav, ifail] = ...
      d02pe(@f, twant, ygot(j, :), ymax, iwsav, rwsav);

    err1(j+1, i) =  ygot(j+1, 1)-sin(tgot(j+1));
    err2(j+1, i) =  ygot(j+1, 2)-cos(tgot(j+1));

    if rem(j, 5) == 0
      fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tgot(j+1), ygot(j+1, :), err1(j+1, i), err2(j+1));
    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');
title('First-order ODEs using Runge-Kutta Low-order Method using Two Tolerances');
hold on;
axis([0 10 -1.2 1.2]);
xlabel('t');
ylabel('Solution (y, y'')');
plot(tgot, ygot(:, 1), '-xr');
text(ceil(tgot(npts+1)), ygot(npts+1, 1)-0.2, 'y', 'Color', 'r');
plot(tgot, ygot(:, 2), '-xg');
text(ceil(tgot(npts+1)), ygot(npts+1, 2), 'y''', 'Color', 'g');
% Plot errors with a different (log) scale
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','bottom',...
           'YAxisLocation','right',...
           'YScale', 'log', ...
           'Color','none',...
           'XColor','k','YColor','k');
hold on;
axis([0 10 1e-7 0.01]);
ylabel('abs(Error)');
plot(ax2, tgot, abs(err1(:, 1)), '-*b');
text(ceil(tgot(npts+1)), err1(npts+1, 1), 'y-error (tol=0.001)', 'Color', 'b');
plot(ax2, tgot, abs(err1(:, 2)), '-sm');
text(ceil(tgot(npts+1)), err1(npts+1, 2), 'y-error (tol=0.0001)', 'Color', 'm');



function [yp, user] = f(t, n, y, user)
  yp = [y(2); -y(1)];
 

Calculation with TOL =  1.0e-03

    t         y1        y2       err1     err2
  0.000     0.000     1.000     0.000     0.000
  0.785     0.707     0.707    -0.000    -0.000
  1.571     0.999    -0.000    -0.001    -0.000
  2.356     0.706    -0.706    -0.001     0.001
  3.142    -0.000    -0.998    -0.000     0.002
  3.927    -0.706    -0.705     0.001     0.002
  4.712    -0.998     0.001     0.002     0.001
  5.498    -0.705     0.706     0.002    -0.002
  6.283     0.001     0.997     0.001    -0.003
Cost of the integration in evaluations of f is 421

Calculation with TOL =  1.0e-04

    t         y1        y2       err1     err2
  0.000     0.000     1.000     0.000     0.000
  0.785     0.707     0.707    -0.000    -0.000
  1.571     1.000    -0.000    -0.000    -0.000
  2.356     0.707    -0.707    -0.000     0.001
  3.142    -0.000    -1.000    -0.000     0.002
  3.927    -0.707    -0.707     0.000     0.002
  4.712    -1.000     0.000     0.000     0.001
  5.498    -0.707     0.707     0.000    -0.002
  6.283     0.000     1.000     0.000    -0.003
Cost of the integration in evaluations of f is 871


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