PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_ode_withdraw_ivp_rk_onestep (d02pd)
Purpose
nag_ode_ivp_rk_onestep (d02pd) is a one-step function for solving an initial value problem for a first-order system of ordinary differential equations using Runge–Kutta methods.
Note: this function is scheduled to be withdrawn, please see
d02pd in
Advice on Replacement Calls for Withdrawn/Superseded Routines..
Syntax
Description
nag_ode_ivp_rk_onestep (d02pd) and its associated functions
(
nag_ode_ivp_rk_setup (d02pv),
nag_ode_ivp_rk_reset_tend (d02pw),
nag_ode_ivp_rk_interp (d02px),
nag_ode_ivp_rk_diag (d02py) and
nag_ode_ivp_rk_errass (d02pz))
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
where
is the vector of
solution components and
is the independent variable.
nag_ode_ivp_rk_onestep (d02pd) is designed to be used in complicated tasks when solving systems of ordinary differential equations. You must first call
nag_ode_ivp_rk_setup (d02pv) to specify the problem and how it is to be solved. Thereafter you (repeatedly) call
nag_ode_ivp_rk_onestep (d02pd) to take one integration step at a time from
tstart in the direction of
tend (as specified in
nag_ode_ivp_rk_setup (d02pv)). In this manner
nag_ode_ivp_rk_onestep (d02pd) returns an approximation to the solution
ynow and its derivative
ypnow at successive points
tnow. If
nag_ode_ivp_rk_onestep (d02pd) encounters some difficulty in taking a step, the integration is not advanced and the function returns with the same values of
tnow,
ynow and
ypnow as returned on the previous successful step.
nag_ode_ivp_rk_onestep (d02pd) tries to advance the integration as far as possible subject to passing the test on the local error and not going past
tend.
In the call to
nag_ode_ivp_rk_setup (d02pv) you can specify either the first step size for
nag_ode_ivp_rk_onestep (d02pd) to attempt or that it compute automatically an appropriate value. Thereafter
nag_ode_ivp_rk_onestep (d02pd) 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_rk_onestep (d02pd) by a call to
nag_ode_ivp_rk_diag (d02py). The local error is controlled at every step as specified in
nag_ode_ivp_rk_setup (d02pv). If you wish to assess the true error, you must set
in the call to
nag_ode_ivp_rk_setup (d02pv). This assessment can be obtained after any call to
nag_ode_ivp_rk_onestep (d02pd) by a call to
nag_ode_ivp_rk_errass (d02pz).
If you want answers at specific points there are two ways to proceed:
(i) |
The more efficient way is to step past the point where a solution is desired, and then call nag_ode_ivp_rk_interp (d02px) to get an answer there. Within the span of the current step, you can get all the answers you want at very little cost by repeated calls to nag_ode_ivp_rk_interp (d02px). This is very valuable when you want to find where something happens, e.g., where a particular solution component vanishes. You cannot proceed in this way with
.
|
(ii) |
The other way to get an answer at a specific point is to set tend to this value and integrate to tend. nag_ode_ivp_rk_onestep (d02pd) will not step past tend, so when a step would carry it past, it will reduce the step size so as to produce an answer at tend exactly. After getting an answer there (), you can reset tend to the next point where you want an answer, and repeat. tend could be reset by a call to nag_ode_ivp_rk_setup (d02pv), but you should not do this. You should use nag_ode_ivp_rk_reset_tend (d02pw) instead because it is both easier to use and much more efficient. This way of getting answers at specific points can be used with any of the available methods, but it is the only way with .
It can be inefficient. Should this be the case, the code will bring the matter to your attention. |
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:
– function handle or string containing name of m-file
-
f must evaluate the functions
(that is the first derivatives
) for given values of the arguments
,
.
[yp] = f(t, y)
Input Parameters
- 1:
– double scalar
-
, the current value of the independent variable.
- 2:
– double array
-
The current values of the dependent variables,
, for .
Output Parameters
- 1:
– double array
-
The values of
, for .
- 2:
– int64int32nag_int scalar
-
, the number of ordinary differential equations in the system to be solved by the integration function.
Constraint:
.
- 3:
– double array
-
The dimension of the array
work
must be at least
(see
nag_ode_ivp_rk_setup (d02pv))
This
must be the same array as supplied to
nag_ode_ivp_rk_setup (d02pv). It
must remain unchanged between calls.
Optional Input Parameters
None.
Output Parameters
- 1:
– double scalar
-
, the value of the independent variable at which a solution has been computed.
- 2:
– double array
-
The dimension of the array
ynow will be
An approximation to the solution at
tnow. The local error of the step to
tnow was no greater than permitted by the specified tolerances (see
nag_ode_ivp_rk_setup (d02pv)).
- 3:
– double array
-
The dimension of the array
ypnow will be
An approximation to the derivative of the solution at
tnow.
- 4:
– double array
-
The dimension of the array
work will be
(see
nag_ode_ivp_rk_setup (d02pv))
Information about the integration for use on subsequent calls to nag_ode_ivp_rk_onestep (d02pd) or other associated functions.
- 5:
– int64int32nag_int scalar
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.
-
-
On entry, an invalid call to
nag_ode_ivp_rk_onestep (d02pd) was made, for example without a previous call to the setup function
nag_ode_ivp_rk_setup (d02pv). You cannot continue integrating the problem.
- W
-
nag_ode_ivp_rk_onestep (d02pd) is being used inefficiently because the step size has been reduced drastically many times to obtain answers at many points
tend. If you really need the solution at this many points, you should use
nag_ode_ivp_rk_interp (d02px) to obtain the answers inexpensively. If you need to change from
to do this, restart the integration from
tnow,
ynow by a call to
nag_ode_ivp_rk_setup (d02pv). If you wish to continue as before, call
nag_ode_ivp_rk_onestep (d02pd) again. The monitor of this kind of inefficiency will be reset automatically so that the integration can proceed.
- W
-
A considerable amount of work has been expended in the (primary) integration. This is measured by counting the number of calls to
f. At least
calls have been made since the last time this counter was reset. Calls to
f in a secondary integration for global error assessment (when
in the call to
nag_ode_ivp_rk_setup (d02pv)) are not counted in this total. The integration was interrupted. If you wish to continue on towards
tend, just call
nag_ode_ivp_rk_onestep (d02pd) again. The counter measuring work will be reset to zero automatically.
- W
-
It appears that this problem is stiff. The methods implemented in
nag_ode_ivp_rk_onestep (d02pd) can solve such problems, but they are inefficient. You should change to another code based on methods appropriate for stiff problems. The integration was interrupted. If you want to continue on towards
tend, just call
nag_ode_ivp_rk_onestep (d02pd) again. The stiffness monitor will be reset automatically.
- W
-
It does not appear possible to achieve the accuracy specified by
tol and
thres in the call to
nag_ode_ivp_rk_setup (d02pv) with the precision available on the computer being used and with this value of
method. You cannot continue integrating this problem. A larger value for
method, if possible, will permit greater accuracy with this precision. To increase
method and/or continue with larger values of
tol and/or
thres, restart the integration from
tnow,
ynow by a call to
nag_ode_ivp_rk_setup (d02pv).
- W
-
(This error exit can only occur if
in the call to
nag_ode_ivp_rk_setup (d02pv).) The global error assessment may not be reliable beyond the current integration point
tnow. This may occur because either too little or too much accuracy has been requested or because
is not smooth enough for values of
just beyond
tnow and current values of the solution
. The integration cannot be continued. This return does not mean that you cannot integrate past
tnow, rather that you cannot do it with
. However, it may also indicate problems with the primary integration.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
The accuracy of integration is determined by the arguments
tol and
thres in a prior call to
nag_ode_ivp_rk_setup (d02pv). Note that only the local error at each step is controlled by these arguments. 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_rk_onestep (d02pd) returns with
and the accuracy specified by
tol and
thres 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
ynow should be monitored 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_rk_onestep (d02pd) (except when
) by a call to
nag_ode_ivp_rk_diag (d02py). If
in the call to
nag_ode_ivp_rk_setup (d02pv), global error assessment is available after any return from
nag_ode_ivp_rk_onestep (d02pd) (except when
) by a call to
nag_ode_ivp_rk_errass (d02pz).
After a failure with
or
the diagnostic
functions
nag_ode_ivp_rk_diag (d02py) and
nag_ode_ivp_rk_errass (d02pz)
may be called only once.
If nag_ode_ivp_rk_onestep (d02pd) returns with then it is advisable to change to another code more suited to the solution of stiff problems. nag_ode_ivp_rk_onestep (d02pd) will not return with if the problem is actually stiff but it is estimated that integration can be completed using less function evaluations than already computed.
Example
This example solves the equation
reposed as
over the range
with initial conditions
and
. We use relative error control with threshold values of
for each solution component and print the solution at each integration step across the range. We use a medium order Runge–Kutta method
(
)
with tolerances
and
in turn so that we may compare the solutions. The value of
is obtained by using
nag_math_pi (x01aa).
Note that the length of
work is large enough for any valid combination of input arguments to
nag_ode_ivp_rk_setup (d02pv).
Open in the MATLAB editor:
d02pd_example
function d02pd_example
fprintf('d02pd example results\n\n');
method = int64(2);
tstart = 0;
tend = 2*pi;
n = int64(2);
errass = false;
lenwrk = int64(32*n);
yinit = [0;1];
hstart = 0;
thresh = [1e-08; 1e-08];
ynow = zeros(20, n);
tnow = zeros(20, 1);
err1 = zeros(20, 2);
err2 = zeros(20, 2);
tol = 1.0e-3;
for i = 1:2
tol = tol*0.1;
[work, ifail] = d02pv(tstart, yinit, tend, tol, thresh, method, ...
'Complex', errass, lenwrk);
tnow(1) = tstart;
ynow(1,:) = yinit;
j=1;
while tnow(j) < tend
j=j+1;
[tnow(j), ynow(j,:), ypnow, work, ifail] = d02pd(@f, n, work);
err1(j, i) = ynow(j, 1)-sin(tnow(j));
err2(j, i) = ynow(j, 2)-cos(tnow(j));
end
fprintf('\nCalculation with TOL = %8.1e:\n\n', tol);
[fevals, stepcost, waste, stepsok, hnext, ifail] = d02py;
fprintf(' Number of evaluations of f = %d\n', fevals);
if i == 1
tnow1 = tnow;
end
npts(i) = j;
end
fig1 = figure;
title({['First-order ODEs solution by single stepping'],...
['Medium-order Runge-Kutta Method, Two Tolerances']});
hold on;
axis([0 10 -1.2 1.2]);
xlabel('t');
ylabel('Solution (y, y'')');
plot(tnow(1:npts(2)), ynow(1:npts(2), 1), '-xr');
text(ceil(tnow(npts(2))), ynow(npts(2), 1), 'y', 'Color', 'r');
plot(tnow(1:npts(2)), ynow(1:npts(2), 2), '-xg');
text(ceil(tnow(npts(2))), ynow(npts(2), 2), 'y''', 'Color', 'g');
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-9 1e-4]);
ylabel('abs(Error)');
plot(ax2, tnow1(1:npts(1)), abs(err1(1:npts(1), 1)), '-*b');
text(ceil(tnow1(npts(1))), err1(npts(1), 1) - 1e-5, ...
'y-error (tol=0.001)', 'Color', 'b');
plot(ax2, tnow, abs(err1(:, 2)), '-sm');
text(ceil(tnow(npts(2))), err1(npts(2), 2), 'y-error (tol=0.0001)', ...
'Color', 'm');
hold off;
function [yp] = f(t, y)
yp = zeros(2, 1);
yp(1) = y(2);
yp(2) = -y(1);
d02pd example results
Calculation with TOL = 1.0e-04:
Number of evaluations of f = 78
Calculation with TOL = 1.0e-05:
Number of evaluations of f = 118
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015