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_stiff_dassl (d02mv)

## Purpose

nag_ode_ivp_stiff_dassl (d02mv) is an integration method specific setup function which must be called prior to linear algebra setup functions and integrators from the SPRINT suite of functions, if the DASSL implementation of Backward Differentiation Formulae (BDF) is to be used. Note that this method is also available, independent from the SPRINT suite, using nag_ode_dae_dassl_gen (d02ne)

## Syntax

[con, rwork, ifail] = d02mv(neqmax, sdysav, maxord, con, tcrit, hmin, hmax, h0, maxstp, mxhnil, norm_p, rwork)
[con, rwork, ifail] = nag_ode_ivp_stiff_dassl(neqmax, sdysav, maxord, con, tcrit, hmin, hmax, h0, maxstp, mxhnil, norm_p, rwork)

## Description

An integrator setup function must be called before the call to any linear algebra setup function or integrator from the SPRINT suite of functions in this sub-chapter. This setup function, nag_ode_ivp_stiff_dassl (d02mv), makes the choice of the DASSL integrator and permits you to define options appropriate to this choice. Alternative choices of integrator from this suite are the BDF method and the BLEND method which can be chosen by initial calls to nag_ode_ivp_stiff_bdf (d02nv) or nag_ode_ivp_stiff_blend (d02nw) respectively.

## References

See the D02M–N sub-chapter Introduction.

## Parameters

### Compulsory Input Parameters

1:     neqmax – int64int32nag_int scalar
A bound on the maximum number of differential equations to be solved.
Constraint: neqmax1${\mathbf{neqmax}}\ge 1$.
2:     sdysav – int64int32nag_int scalar
The second dimension of the array ysav that will be supplied to the integrator, as declared in the (sub)program from which the integrator is called (e.g., see nag_ode_ivp_stiff_exp_fulljac (d02nb)).
Constraint: sdysavmaxord + 3${\mathbf{sdysav}}\ge {\mathbf{maxord}}+3$.
3:     maxord – int64int32nag_int scalar
The maximum order to be used for the BDF method. If maxord = 0${\mathbf{maxord}}=0$ or maxord > 5${\mathbf{maxord}}>5$ then maxord = 5${\mathbf{maxord}}=5$ is assumed.
Constraint: maxord0${\mathbf{maxord}}\ge 0$.
4:     con(3$3$) – double array
Values to be used to control step size choice during integration. If any con(i) = 0.0${\mathbf{con}}\left(i\right)=0.0$ on entry, it is replaced by its default value described below. In most cases this is the recommended setting.
con(1)${\mathbf{con}}\left(1\right)$, con(2)${\mathbf{con}}\left(2\right)$, and con(3)${\mathbf{con}}\left(3\right)$ are factors used to bound step size changes. If the current step size h$h$ fails, then the modulus of the next step size is bounded by con(1) × |h|${\mathbf{con}}\left(1\right)×|h|$. The default value of con(1)${\mathbf{con}}\left(1\right)$ is 2.0$2.0$. Note that the new step size may be used with a method of different order to the failed step. If the initial step size is h$h$, then the modulus of the step size on the second step is bounded by con(3) × |h|${\mathbf{con}}\left(3\right)×|h|$. At any other stage in the integration, if the current step size is h$h$, then the modulus of the next step size is bounded by con(2) × |h|${\mathbf{con}}\left(2\right)×|h|$. The default values are 10.0$10.0$ for con(2)${\mathbf{con}}\left(2\right)$ and 1000.0$1000.0$ for con(3)${\mathbf{con}}\left(3\right)$.
Constraints:
These constraints must be satisfied after any zero values have been replaced by default values.
• 0.0 < con(1) < con(2) < con(3)$0.0<{\mathbf{con}}\left(1\right)<{\mathbf{con}}\left(2\right)<{\mathbf{con}}\left(3\right)$;
• con(2) > 1.0${\mathbf{con}}\left(2\right)>1.0$;
• con(3) > 1.0${\mathbf{con}}\left(3\right)>1.0$.
5:     tcrit – double scalar
A point beyond which integration must not be attempted. The use of tcrit is described under the parameter itask in the specification for the integrator (e.g., see nag_ode_ivp_stiff_exp_fulljac (d02nb)). A value, 0.0$0.0$ say, must be specified even if itask subsequently specifies that tcrit will not be used.
6:     hmin – double scalar
The minimum absolute step size to be allowed. Set hmin = 0.0${\mathbf{hmin}}=0.0$ if this option is not required.
7:     hmax – double scalar
The maximum absolute step size to be allowed. Set hmax = 0.0${\mathbf{hmax}}=0.0$ if this option is not required.
8:     h0 – double scalar
The step size to be attempted on the first step. Set h0 = 0.0${\mathbf{h0}}=0.0$ if the initial step size is calculated internally.
9:     maxstp – int64int32nag_int scalar
The maximum number of steps to be attempted during one call to the integrator after which it will return with ${\mathbf{ifail}}={\mathbf{2}}$ (e.g., see nag_ode_ivp_stiff_exp_fulljac (d02nb)). Set maxstp = 0${\mathbf{maxstp}}=0$ if no limit is to be imposed.
10:   mxhnil – int64int32nag_int scalar
The maximum number of warnings printed (if itrace0${\mathbf{itrace}}\ge 0$, e.g., see nag_ode_ivp_stiff_exp_fulljac (d02nb)) per problem when t + h = t$t+h=t$ on a step (h = ​ current step size$h=\text{​ current step size}$). If mxhnil0${\mathbf{mxhnil}}\le 0$, a default value of 10$10$ is assumed.
11:   norm_p – string (length ≥ 1)
Indicates the type of norm to be used.
norm = 'M'${\mathbf{norm}}=\text{'M'}$
Maximum norm.
norm = 'A'${\mathbf{norm}}=\text{'A'}$
Averaged L2 norm.
norm = 'D'${\mathbf{norm}}=\text{'D'}$
Is the same as 'A'.
If vnorm$\mathit{vnorm}$ denotes the norm of the vector v$v$ of length n$n$, then for the averaged L2 norm
 vnormB = sqrt(1/n ∑ i = 1n ((vi)/(wi))2), $vnormB=1n∑i=1n (viwi) 2,$
while for the maximum norm
 vnorm = max |(vi)/(wi)|. 1 ≤ i ≤ n
$vnorm=max1≤i≤n |viwi| .$
If you wish to weight the maximum norm or the L2 norm, then rtol and atol should be scaled appropriately on input to the integrator (see under itol in the specification of the integrator for the formulation of the weight vector wi${w}_{i}$ from rtol and atol, e.g., see nag_ode_ivp_stiff_exp_fulljac (d02nb)).
Only the first character to the actual parameter norm_p is passed to nag_ode_ivp_stiff_dassl (d02mv); hence it is permissible for the actual argument to be more descriptive, e.g., ‘Maximum’, ‘Average L2’ or ‘Default’ in a call to nag_ode_ivp_stiff_dassl (d02mv).
Constraint: norm = 'M'${\mathbf{norm}}=\text{'M'}$, 'A'$\text{'A'}$ or 'D'$\text{'D'}$.
12:   rwork(50 + 4 × neqmax$50+4×{\mathbf{neqmax}}$) – double array
This must be the same workspace array as the array rwork supplied to the integrator. It is used to pass information from the setup function to the integrator and therefore the contents of this array must not be changed before calling the integrator.

None.

None.

### Output Parameters

1:     con(3$3$) – double array
The values actually to be used by the integration function.
2:     rwork(50 + 4 × neqmax$50+4×{\mathbf{neqmax}}$) – double array
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, neqmax < 1${\mathbf{neqmax}}<1$, or sdysav < maxord + 3${\mathbf{sdysav}}<{\mathbf{maxord}}+3$, or maxord < 0${\mathbf{maxord}}<0$, or maxord > 5${\mathbf{maxord}}>5$, or invalid value for element of the array con, or norm ≠ 'M'${\mathbf{norm}}\ne \text{'M'}$, 'A'$\text{'A'}$ or 'D'$\text{'D'}$.

Not applicable.

None.

## Example

This example solves the plane pendulum problem defined by the equations:
 x′ = u y′ = v u′ = − λx v′ = − λy − 1 x2 + y2 = 1
$x′ = u y′ = v u′ = -λx v′ = -λy-1 x2+y2 = 1$
The additional algebraic constraint xu + yv = 0$xu+yv=0$ can be derived, and after appropriate substitution and manipulation to avoid a singular Jacobian solves the equations:
 y1 ′ = y3 − y6y1 y2 ′ = y4 − y6y2 y3 ′ = − y5y1 y4 ′ = − y5y2 − 1 0 = y1y3 + y2y4 0 = y12 + y22 − 1
$y1′ = y3-y6y1 y2′ = y4-y6y2 y3′ = -y5y1 y4′ = -y5y2-1 0 = y1y3+y2y4 0 = y12+y22-1$
with given initial conditions and derivatives.
```function nag_ode_ivp_stiff_dassl_example
% Initialize variables and arrays.
neq = 6;
neqmax = neq;
nrw = 50+4*neqmax;
ninf = 23;
nwkjac = neqmax*(neqmax + 1);
maxord = 5;
sdysav = maxord+3;
maxstp = 5000;
mxhnil = 5;
ldysav = neq;

h0 = 0.0;
hmax = 0.0;
hmin = 1.0e-10;

const = zeros(3, 1);
rwork = zeros(nrw, 1);
wkjac = zeros(nwkjac, 1);
ysave = zeros(ldysav, sdysav);
inform = zeros(ninf, 1);

t = 0.0;
tout = pi;
itol = 1;
rtol = [1e-03];
atol = [1e-06];
tcrit = tout;
lderiv = [true; true];
y = [1; 0; 0; 0; 0; 0];
ydot = [
y(3) - y(6)*y(1);
y(4) - y(6)*y(2);
-y(5)*y(1);
-y(5)*y(2) - 1.0;
-3.0*y(4);
0.0 ];
nstep = 18;

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

% nag_ode_ivp_stiff_dassl is an integration method-specific setup routine to be called prior
% to linear algebra setup and integrator routines if the DASSL
% implementation of BDF is to be used.
[const, rwork, ifail] = nag_ode_ivp_stiff_dassl(int64(neqmax), int64(sdysav), ...
int64(maxord), const, tcrit, hmin, hmax, h0, int64(maxstp), ...
int64(mxhnil), 'Average-L2', rwork);
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('nag_ode_ivp_stiff_dassl returned with ifail = %1d ',ifail);
end

% Output header and initial results.
fprintf(['\nPendulum problem with relative tolerance = %7.1e\n', ...
'                  and absolute tolerance = %7.1e\n\n'], ...
rtol(1), atol(1));
fprintf('   t        y1      y2      y3      y4      y5      y6\n');
fprintf(' %6.4f  %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n', t, y);

% nag_ode_ivp_stiff_fulljac_setup is a setup routine (specifically for full matrix linear algebra)
% to be called prior to a routine from sub-chapter d02n-m (e.g. nag_ode_ivp_stiff_imp_fulljac,
% as here).
[rwork, ifail] = nag_ode_ivp_stiff_fulljac_setup(int64(neq), int64(neqmax), 'Analytic', ...
int64(nwkjac), rwork);

% Prepare to store results for plotting, then loop over values for the
% independent variable.
tkeep = zeros(nstep+1,1);
ykeep = zeros(nstep+1, neq);
tkeep(1) = t;
ykeep(1, :) = y';
for istep = 1:nstep
tout = istep/nstep*pi;
itrace = 0; % Ask for warning messages only.
[t, tout, y, ydot, rwork, inform, ysave, wkjac, lderiv, ifail] = ...
nag_ode_ivp_stiff_imp_fulljac(t, tout, y, ydot, rwork, rtol, atol, int64(itol), ...
int64(inform), @resid, ysave, @jac, wkjac, ...
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('nag_ode_ivp_stiff_imp_fulljac returned with ifail = %1d ',ifail);
end
% Store current results for plotting.
tkeep(istep+1) = t;
ykeep(istep+1,:) = y';
end

% Output final results.
fprintf(' %6.4f  %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n', t, y);
% Plot results.
fig = figure('Number', 'off');
display_plot(ykeep(:,1), ykeep(:,2));

function pdj = jac(neq, t, y, ydot, h, d, pdj)
% Evaluate the Jacobian.

pdj = zeros(neq, neq);
hxd = h*d;
pdj(1,1) =  (1.0 + hxd*y(6));
pdj(1,3) = -hxd;
pdj(1,6) =  hxd*y(1);
pdj(2,2) =  (1.0 + hxd*y(6));
pdj(2,4) = -hxd;
pdj(2,6) =  hxd*y(2);
pdj(3,1) =  hxd*y(5);
pdj(3,3) =  1.0;
pdj(3,5) =  hxd*y(1);
pdj(4,2) =  hxd*y(5);
pdj(4,4) =  1.0;
pdj(4,5) =  hxd*y(2);
pdj(5,1) = -hxd*y(3);
pdj(5,2) = -hxd*y(4);
pdj(5,3) = -hxd*y(1);
pdj(5,4) = -hxd*y(2);
pdj(6,1) = -2.0*hxd*y(1);
pdj(6,2) = -2.0*hxd*y(2);
function [hnext, y, imon, inln, hmin, hmax] = monitr(neq, neqmax, ...
t, hlast, hnext, y, ydot, ysave, r, acor, imon, hmin, hmax, nqu)

inln = int64(0);
function [r, ires] = resid(neq, t, y, ydot, ires)
% Evaluate the residue.

r = zeros(neq,1);
if ires == -1
r(1) = -ydot(1);
r(2) = -ydot(2);
r(3) = -ydot(3);
r(4) = -ydot(4);
r(5) = 0.0;
r(6) = 0.0;
else
r(1) =  y(3) - y(6)*y(1) - ydot(1);
r(2) =  y(4) - y(6)*y(2) - ydot(2);
r(3) = -y(5)*y(1) - ydot(3);
r(4) = -y(5)*y(2) - 1.0 - ydot(4);
r(5) =  y(1)*y(3) + y(2)*y(4);
r(6) =  y(1)^2 + y(2)^2 - 1.0;
end
function display_plot(x, y)
% 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 results.
plot(x, y, '-+');
title({'DASSL Implementation of BDF Method for Stiff ODE',...
'Plane Pendulum Problem'}, titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Pendulum Displacement', labFmt{:});
```
```
nag_ode_ivp_stiff_dassl example program results

Pendulum problem with relative tolerance = 1.0e-03
and absolute tolerance = 1.0e-06

t        y1      y2      y3      y4      y5      y6
0.0000   1.0000  0.0000  0.0000  0.0000  0.0000  0.0000
3.1416  -0.9872 -0.1597 -0.0902  0.5579  0.4790 -0.0000

```
```function d02mv_example
% Initialize variables and arrays.
neq = 6;
neqmax = neq;
nrw = 50+4*neqmax;
ninf = 23;
nwkjac = neqmax*(neqmax + 1);
maxord = 5;
sdysav = maxord+3;
maxstp = 5000;
mxhnil = 5;
ldysav = neq;

h0 = 0.0;
hmax = 0.0;
hmin = 1.0e-10;

const = zeros(3, 1);
rwork = zeros(nrw, 1);
wkjac = zeros(nwkjac, 1);
ysave = zeros(ldysav, sdysav);
inform = zeros(ninf, 1);

t = 0.0;
tout = pi;
itol = 1;
rtol = [1e-03];
atol = [1e-06];
tcrit = tout;
lderiv = [true; true];
y = [1; 0; 0; 0; 0; 0];
ydot = [
y(3) - y(6)*y(1);
y(4) - y(6)*y(2);
-y(5)*y(1);
-y(5)*y(2) - 1.0;
-3.0*y(4);
0.0 ];
nstep = 18;

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

% d02mv is an integration method-specific setup routine to be called prior
% to linear algebra setup and integrator routines if the DASSL
% implementation of BDF is to be used.
[const, rwork, ifail] = d02mv(int64(neqmax), int64(sdysav), ...
int64(maxord), const, tcrit, hmin, hmax, h0, int64(maxstp), ...
int64(mxhnil), 'Average-L2', rwork);
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('d02mv returned with ifail = %1d ',ifail);
end

% Output header and initial results.
fprintf(['\nPendulum problem with relative tolerance = %7.1e\n', ...
'                  and absolute tolerance = %7.1e\n\n'], ...
rtol(1), atol(1));
fprintf('   t        y1      y2      y3      y4      y5      y6\n');
fprintf(' %6.4f  %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n', t, y);

% d02ns is a setup routine (specifically for full matrix linear algebra)
% to be called prior to a routine from sub-chapter d02n-m (e.g. d02ng,
% as here).
[rwork, ifail] = d02ns(int64(neq), int64(neqmax), 'Analytic', ...
int64(nwkjac), rwork);

% Prepare to store results for plotting, then loop over values for the
% independent variable.
tkeep = zeros(nstep+1,1);
ykeep = zeros(nstep+1, neq);
tkeep(1) = t;
ykeep(1, :) = y';
for istep = 1:nstep
tout = istep/nstep*pi;
itrace = 0; % Ask for warning messages only.
[t, tout, y, ydot, rwork, inform, ysave, wkjac, lderiv, ifail] = ...
d02ng(t, tout, y, ydot, rwork, rtol, atol, int64(itol), ...
int64(inform), @resid, ysave, @jac, wkjac, ...
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('d02ng returned with ifail = %1d ',ifail);
end
% Store current results for plotting.
tkeep(istep+1) = t;
ykeep(istep+1,:) = y';
end

% Output final results.
fprintf(' %6.4f  %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n', t, y);
% Plot results.
fig = figure('Number', 'off');
display_plot(ykeep(:,1), ykeep(:,2));

function pdj = jac(neq, t, y, ydot, h, d, pdj)
% Evaluate the Jacobian.

pdj = zeros(neq, neq);
hxd = h*d;
pdj(1,1) =  (1.0 + hxd*y(6));
pdj(1,3) = -hxd;
pdj(1,6) =  hxd*y(1);
pdj(2,2) =  (1.0 + hxd*y(6));
pdj(2,4) = -hxd;
pdj(2,6) =  hxd*y(2);
pdj(3,1) =  hxd*y(5);
pdj(3,3) =  1.0;
pdj(3,5) =  hxd*y(1);
pdj(4,2) =  hxd*y(5);
pdj(4,4) =  1.0;
pdj(4,5) =  hxd*y(2);
pdj(5,1) = -hxd*y(3);
pdj(5,2) = -hxd*y(4);
pdj(5,3) = -hxd*y(1);
pdj(5,4) = -hxd*y(2);
pdj(6,1) = -2.0*hxd*y(1);
pdj(6,2) = -2.0*hxd*y(2);
function [hnext, y, imon, inln, hmin, hmax] = monitr(neq, neqmax, ...
t, hlast, hnext, y, ydot, ysave, r, acor, imon, hmin, hmax, nqu)

inln = int64(0);
function [r, ires] = resid(neq, t, y, ydot, ires)
% Evaluate the residue.

r = zeros(neq,1);
if ires == -1
r(1) = -ydot(1);
r(2) = -ydot(2);
r(3) = -ydot(3);
r(4) = -ydot(4);
r(5) = 0.0;
r(6) = 0.0;
else
r(1) =  y(3) - y(6)*y(1) - ydot(1);
r(2) =  y(4) - y(6)*y(2) - ydot(2);
r(3) = -y(5)*y(1) - ydot(3);
r(4) = -y(5)*y(2) - 1.0 - ydot(4);
r(5) =  y(1)*y(3) + y(2)*y(4);
r(6) =  y(1)^2 + y(2)^2 - 1.0;
end
function display_plot(x, y)
% 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 results.
plot(x, y, '-+');
title({'DASSL Implementation of BDF Method for Stiff ODE',...
'Plane Pendulum Problem'}, titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Pendulum Displacement', labFmt{:});
```
```
d02mv example program results

Pendulum problem with relative tolerance = 1.0e-03
and absolute tolerance = 1.0e-06

t        y1      y2      y3      y4      y5      y6
0.0000   1.0000  0.0000  0.0000  0.0000  0.0000  0.0000
3.1416  -0.9872 -0.1597 -0.0902  0.5579  0.4790 -0.0000

```

Chapter Contents
Chapter Introduction
NAG Toolbox

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