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_stiff_imp_bandjac (d02nh)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_ode_ivp_stiff_imp_bandjac (d02nh) is a forward communication function for integrating stiff systems of implicit ordinary differential equations coupled with algebraic equations when the Jacobian is a banded matrix.

Syntax

[t, tout, y, ydot, rwork, inform, ysav, wkjac, jacpvt, lderiv, ifail] = d02nh(t, tout, y, ydot, rwork, rtol, atol, itol, inform, resid, ysav, jac, wkjac, jacpvt, monitr, lderiv, itask, itrace, 'neq', neq, 'sdysav', sdysav)
[t, tout, y, ydot, rwork, inform, ysav, wkjac, jacpvt, lderiv, ifail] = nag_ode_ivp_stiff_imp_bandjac(t, tout, y, ydot, rwork, rtol, atol, itol, inform, resid, ysav, jac, wkjac, jacpvt, monitr, lderiv, itask, itrace, 'neq', neq, 'sdysav', sdysav)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: nwkjac and njcpvt were removed from the interface; neq was made optional

Description

nag_ode_ivp_stiff_imp_bandjac (d02nh) is a general purpose function for integrating the initial value problem for a stiff system of implicit ordinary differential equations coupled with algebraic equations, written in the form
At,yy=gt,y.  
It is designed specifically for the case where the resulting Jacobian is a banded matrix (see the description of jac).
Both interval and step oriented modes of operation are available and also modes designed to permit intermediate output within an interval oriented mode.
An outline of a typical calling program for nag_ode_ivp_stiff_imp_bandjac (d02nh) is given below. It calls the banded matrix linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt), the Backward Differentiation Formula (BDF) integrator setup function nag_ode_ivp_stiff_bdf (d02nv), and its diagnostic counterpart nag_ode_ivp_stiff_integ_diag (d02ny).
.
.
.
[...] = d02nv(...);
[...] = d02nt(...);
[..., ifail] = d02nh(...);
if (ifail ~= 1 and ifail < 14) 
  [...] = d02ny(...);
end
.
.
.
The linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt) and one of the integrator setup functions, nag_ode_ivp_stiff_dassl (d02mv), nag_ode_ivp_stiff_bdf (d02nv) or nag_ode_ivp_stiff_blend (d02nw), must be called prior to the call of nag_ode_ivp_stiff_imp_bandjac (d02nh). The integrator diagnostic function nag_ode_ivp_stiff_integ_diag (d02ny) may be called after the call to nag_ode_ivp_stiff_imp_bandjac (d02nh). There is also a function, nag_ode_ivp_stiff_contin (d02nz), designed to permit you to change step size on a continuation call to nag_ode_ivp_stiff_imp_bandjac (d02nh) without restarting the integration process.

References

See the D02M–N Sub-chapter Introduction.

Parameters

Compulsory Input Parameters

1:     t – double scalar
t, the value of the independent variable. The input value of t is used only on the first call as the initial point of the integration.
2:     tout – double scalar
The next value of t at which a computed solution is desired. For the initial t, the input value of tout is used to determine the direction of integration. Integration is permitted in either direction (see also itask).
Constraint: toutt.
3:     yneq – double array
The values of the dependent variables (solution). On the first call the first neq elements of y must contain the vector of initial values.
4:     ydotneq – double array
If lderiv1=true, ydot must contain approximations to the time derivatives y of the vector y.
If lderiv1=false, ydot need not be set on entry.
5:     rwork50+4×neq – double array
6:     rtol: – double array
The dimension of the array rtol must be at least 1 if itol=1 or 2, and at least neq otherwise
The relative local error tolerance.
Constraint: rtoli0.0 for all relevant i (see itol).
7:     atol: – double array
The dimension of the array atol must be at least 1 if itol=1 or 3, and at least neq otherwise
The absolute local error tolerance.
Constraint: atoli0.0 for all relevant i (see itol).
8:     itol int64int32nag_int scalar
A value to indicate the form of the local error test. itol indicates to nag_ode_ivp_stiff_imp_bandjac (d02nh) whether to interpret either or both of rtol or atol as a vector or a scalar. The error test to be satisfied is ei/wi<1.0, where wi is defined as follows:
itol rtol atol wi
1 scalar scalar rtol1×yi+atol1
2 scalar vector rtol1×yi+atoli
3 vector scalar rtoli×yi+atol1
4 vector vector rtoli×yi+atoli
ei is an estimate of the local error in yi, computed internally, and the choice of norm to be used is defined by a previous call to an integrator setup function.
Constraint: itol=1, 2, 3 or 4.
9:     inform23 int64int32nag_int array
10:   resid – function handle or string containing name of m-file
resid must evaluate the residual
r=gt,y-At,yy  
in one case and
r=-At,yy  
in another.
[r, ires] = resid(neq, t, y, ydot, ires)

Input Parameters

1:     neq int64int32nag_int scalar
The number of equations being solved.
2:     t – double scalar
t, the current value of the independent variable.
3:     yneq – double array
The value of yi, for i=1,2,,neq.
4:     ydotneq – double array
The value of yi, for i=1,2,,neq, at t.
5:     ires int64int32nag_int scalar
The form of the residual that must be returned in array r.
ires=-1
The residual defined in equation (2) must be returned.
ires=1
The residual defined in equation (1) must be returned.

Output Parameters

1:     rneq – double array
ri must contain the ith component of r, for i=1,2,,neq, where
r=gt,y-At,yy (1)
or
r=-At,yy (2)
and where the definition of r is determined by the input value of ires.
2:     ires int64int32nag_int scalar
Should be unchanged unless one of the following actions is required of the integrator, in which case ires should be set accordingly.
ires=2
Indicates to the integrator that control should be passed back immediately to the calling (sub)program with the error indicator set to ifail=11.
ires=3
Indicates to the integrator that an error condition has occurred in the solution vector, its time derivative or in the value of t. The integrator will use a smaller time step to try to avoid this condition. If this is not possible, the integrator returns to the calling (sub)program with the error indicator set to ifail=7.
ires=4
Indicates to the integrator to stop its current operation and to enter monitr immediately with argument imon=-2.
11:   ysavldysavsdysav – double array
ldysav, the first dimension of the array, must satisfy the constraint ldysavneq.
An appropriate value for sdysav is described in the specifications of the integrator setup functions nag_ode_ivp_stiff_dassl (d02mv), nag_ode_ivp_stiff_bdf (d02nv) and nag_ode_ivp_stiff_blend (d02nw). This value must be the same as that supplied to the integrator setup function.
12:   jac – function handle or string containing name of m-file
jac must evaluate the Jacobian of the system. If this option is not required, the actual argument for jac must be the string nag_ode_ivp_stiff_imp_bandjac_dummy_jac (d02nhz). (nag_ode_ivp_stiff_imp_bandjac_dummy_jac (d02nhz) is included in the NAG Toolbox.) You must indicate to the integrator whether this option is to be used by setting the argument jceval appropriately in a call to the banded linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt).
First we must define the system of nonlinear equations which is solved internally by the integrator. The time derivative, y, generated internally, has the form
y = y-z / hd ,  
where h is the current step size and d is a argument that depends on the integration method in use. The vector y is the current solution and the vector z depends on information from previous time steps. This means that d dy ​ ​ = hd d dy ​ ​ . The system of nonlinear equations that is solved has the form
A t,y y - g t,y = 0  
but it is solved in the form
r t,y = 0 ,  
where r is the function defined by
r t,y = hd A t,y y-z / hd - g t,y .  
It is the Jacobian matrix r y  that you must supply in jac as follows:
ri yj = aij t,y + hd yj k=1 neq aik t,y yk - gi t,y .  
[p] = jac(neq, t, y, ydot, h, d, ml, mu, p)

Input Parameters

1:     neq int64int32nag_int scalar
The number of equations being solved.
2:     t – double scalar
t, the current value of the independent variable.
3:     yneq – double array
yi, for i=1,2,,neq, the current solution component.
4:     ydotneq – double array
The derivative of the solution at the current point t.
5:     h – double scalar
The current step size.
6:     d – double scalar
The argument d which depends on the integration method.
7:     ml int64int32nag_int scalar
8:     mu int64int32nag_int scalar
The number of subdiagonals and superdiagonals respectively in the band.
9:     pml+mu+1neq – double array
Is set to zero.

Output Parameters

1:     pml+mu+1neq – double array
Elements of the Jacobian matrix r y stored as specified by the following pseudocode
 for i = 1:neq
 j1 = max(i-ml,1);
 j2 = min(i+mu,neq);
 for j = j1:j2
 k = min(ml+1-i,0)+j;
 p(k,i) = δr/δy(i,j);
 end
 end 
See also the function document for nag_lapack_dgbtrf (f07bd).
Only nonzero elements of this array need be set, since it is preset to zero before the call to jac.
13:   wkjacnwkjac – double array
nwkjac, the dimension of the array, must satisfy the constraint nwkjac2mL+mU+1×neq, where mL and mU are the number of subdiagonals and superdiagonals respectively in the band, defined by a call to nag_ode_ivp_stiff_bandjac_setup (d02nt).
This value must be the same as that supplied to the linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt).
Constraint: nwkjac2mL+mU+1×neq, where mL and mU are the number of subdiagonals and superdiagonals respectively in the band, defined by a call to nag_ode_ivp_stiff_bandjac_setup (d02nt).
14:   jacpvtnjcpvt int64int32nag_int array
njcpvt, the dimension of the array, must satisfy the constraint njcpvtneq.
The size of the array jacpvt. This value must be the same as that supplied to the linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt).
Constraint: njcpvtneq.
15:   monitr – function handle or string containing name of m-file
monitr performs tasks requested by you. If this option is not required, then the actual argument for monitr must be the string nag_ode_ivp_stiff_exp_fulljac_dummy_monit (d02nby). (nag_ode_ivp_stiff_exp_fulljac_dummy_monit (d02nby) is included in the NAG Toolbox.)
[hnext, y, imon, inln, hmin, hmax] = monitr(neq, ldysav, t, hlast, hnext, y, ydot, ysav, r, acor, imon, hmin, hmax, nqu)

Input Parameters

1:     neq int64int32nag_int scalar
The number of equations being solved.
2:     ldysav int64int32nag_int scalar
An upper bound on the number of equations to be solved.
3:     t – double scalar
The current value of the independent variable.
4:     hlast – double scalar
The last step size successfully used by the integrator.
5:     hnext – double scalar
The step size that the integrator proposes to take on the next step.
6:     yneq – double array
y, the values of the dependent variables evaluated at t.
7:     ydotneq – double array
The time derivatives y of the vector y.
8:     ysavldysavsdysav – double array
Workspace to enable you to carry out interpolation using either of the functions nag_ode_ivp_stiff_nat_interp (d02xj) or nag_ode_ivp_stiff_c1_interp (d02xk).
9:     rneq – double array
If imon=0 and inln=3, then the first neq elements contain the residual vector At,yy-gt,y.
10:   acorneq2 – double array
With imon=1, acori1 contains the weight used for the ith equation when the norm is evaluated, and acori2 contains the estimated local error for the ith equation. The scaled local error at the end of a timestep may be obtained by calling the double function nag_ode_ivp_stiff_errest (d02za) as follows:
 [errloc, ifail] = d02za(acor(1:neq,2), acor(1:neq,1));
 % Check ifail before proceeding 
11:   imon int64int32nag_int scalar
A flag indicating under what circumstances monitr was called:
imon=-2
Entry from the integrator after ires=4 (set in resid) caused an early termination (this facility could be used to locate discontinuities).
imon=-1
The current step failed repeatedly.
imon=0
Entry after a call to the internal nonlinear equation solver (see inln).
imon=1
The current step was successful.
12:   hmin – double scalar
The minimum step size to be taken on the next step.
13:   hmax – double scalar
The maximum step size to be taken on the next step.
14:   nqu int64int32nag_int scalar
The order of the integrator used on the last step. This is supplied to enable you to carry out interpolation using either of the functions nag_ode_ivp_stiff_nat_interp (d02xj) or nag_ode_ivp_stiff_c1_interp (d02xk).

Output Parameters

1:     hnext – double scalar
The next step size to be used. If this is different from the input value, then imon must be set to 4.
2:     yneq – double array
These values must not be changed unless imon is set to 2.
3:     imon int64int32nag_int scalar
May be reset to determine subsequent action in nag_ode_ivp_stiff_imp_bandjac (d02nh).
imon=-2
Integration is to be halted. A return will be made from the integrator to the calling (sub)program with ifail=12.
imon=-1
Allow the integrator to continue with its own internal strategy. The integrator will try up to three restarts unless imon-1 on exit.
imon=0
Return to the internal nonlinear equation solver, where the action taken is determined by the value of inln (see inln).
imon=1
Normal exit to the integrator to continue integration.
imon=2
Restart the integration at the current time point. The integrator will restart from order 1 when this option is used. The solution y, provided by monitr, will be used for the initial conditions.
imon=3
Try to continue with the same step size and order as was to be used before the call to monitr. hmin and hmax may be altered if desired.
imon=4
Continue the integration but using a new value of hnext and possibly new values of hmin and hmax.
4:     inln int64int32nag_int scalar
The action to be taken by the internal nonlinear equation solver when monitr is exited with imon=0. By setting inln=3 and returning to the integrator, the residual vector is evaluated and placed in the array r, and then monitr is called again. At present this is the only option available: inln must not be set to any other value.
5:     hmin – double scalar
The minimum step size to be used. If this is different from the input value, then imon must be set to 3 or 4.
6:     hmax – double scalar
The maximum step size to be used. If this is different from the input value, then imon must be set to 3 or 4. If hmax is set to zero, no limit is assumed.
16:   lderiv2 – logical array
lderiv1 must be set to true if you have supplied both an initial y and an initial y. lderiv1 must be set to false if only the initial y has been supplied.
lderiv2 must be set to true if the integrator is to use a modified Newton method to evaluate the initial y and y. Note that y and y, if supplied, are used as initial estimates. This method involves taking a small step at the start of the integration, and if itask=6 on entry, t and tout will be set to the result of taking this small step. lderiv2 must be set to false if the integrator is to use functional iteration to evaluate the initial y and y, and if this fails a modified Newton method will then be attempted. lderiv2=true is recommended if there are implicit equations or the initial y and y are zero.
17:   itask int64int32nag_int scalar
The task to be performed by the integrator.
itask=1
Normal computation of output values of yt at t=tout (by overshooting and interpolating).
itask=2
Take one step only and return.
itask=3
Stop at the first internal integration point at or beyond t=tout and return.
itask=4
Normal computation of output values of yt at t=tout but without overshooting t=tcrit. tcrit must be specified as an option in one of the integrator setup functions before the first call to the integrator, or specified in the optional input function before a continuation call. tcrit may be equal to or beyond tout, but not before it, in the direction of integration.
itask=5
Take one step only and return, without passing tcrit. tcrit must be specified as under itask=4.
itask=6
The integrator will solve for the initial values of y and y only and then return to the calling (sub)program without doing the integration. This option can be used to check the initial values of y and y. Functional iteration or a ‘small’ backward Euler method used in conjunction with a damped Newton iteration is used to calculate these values (see lderiv). Note that if a backward Euler step is used then the value of t will have been advanced a short distance from the initial point.
Note:  if nag_ode_ivp_stiff_imp_bandjac (d02nh) is recalled with a different value of itask (and tout altered), then the initialization procedure is repeated, possibly leading to different initial conditions.
Constraint: 1itask6.
18:   itrace int64int32nag_int scalar
The level of output that is printed by the integrator. itrace may take the value -1, 0, 1, 2 or 3.
itrace<-1
-1 is assumed and similarly if itrace>3, then 3 is assumed.
itrace=-1
No output is generated.
itrace=0
Only warning messages are printed on the current error message unit (see nag_file_set_unit_error (x04aa)).
itrace>0
Warning messages are printed as above, and on the current advisory message unit (see nag_file_set_unit_advisory (x04ab)) output is generated which details Jacobian entries, the nonlinear iteration and the time integration. The advisory messages are given in greater detail the larger the value of itrace.

Optional Input Parameters

1:     neq int64int32nag_int scalar
Default: the dimension of the arrays y, ydot and the first dimension of the array ysav. (An error is raised if these dimensions are not equal.)
The number of differential equations to be solved.
Constraint: neq1.
2:     sdysav int64int32nag_int scalar
Default: the second dimension of the array ysav.
An appropriate value for sdysav is described in the specifications of the integrator setup functions nag_ode_ivp_stiff_dassl (d02mv), nag_ode_ivp_stiff_bdf (d02nv) and nag_ode_ivp_stiff_blend (d02nw). This value must be the same as that supplied to the integrator setup function.

Output Parameters

1:     t – double scalar
The value at which the computed solution y is returned (usually at tout).
2:     tout – double scalar
Normally unchanged. However, when itask=6, then tout contains the value of t at which initial values have been computed without performing any integration. See descriptions of itask and lderiv.
3:     yneq – double array
The computed solution vector, evaluated at t (usually t=tout).
4:     ydotneq – double array
The time derivatives y of the vector y at the last integration point.
5:     rwork50+4×neq – double array
6:     inform23 int64int32nag_int array
7:     ysavldysavsdysav – double array
Communication array, used to store information between calls to nag_ode_ivp_stiff_imp_bandjac (d02nh).
8:     wkjacnwkjac – double array
nwkjac=2mL+mU+1×neq, where mL and mU are the number of subdiagonals and superdiagonals respectively in the band, defined by a call to nag_ode_ivp_stiff_bandjac_setup (d02nt).
Communication array, used to store information between calls to nag_ode_ivp_stiff_imp_bandjac (d02nh).
9:     jacpvtnjcpvt int64int32nag_int array
njcpvt=neq.
Communication array, used to store information between calls to nag_ode_ivp_stiff_imp_bandjac (d02nh).
10:   lderiv2 – logical array
lderiv1 is normally unchanged. However if itask=6 and internal initialization was successful then lderiv1=true.
lderiv2=true, if implicit equations were detected. Otherwise lderiv2=false.
11:   ifail int64int32nag_int scalar
ifail=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=1
An illegal input was detected on entry, or after an internal call to monitr. If itrace>-1, then the form of the error will be detailed on the current error message unit (see nag_file_set_unit_error (x04aa)).
   ifail=2
The maximum number of steps specified has been taken (see the description of optional inputs in the integrator setup functions and the optional input continuation function, nag_ode_ivp_stiff_contin (d02nz)).
W  ifail=3
With the given values of rtol and atol no further progress can be made across the integration range from the current point t. The components y1,y2,,yneq contain the computed values of the solution at the current point t.
W  ifail=4
There were repeated error test failures on an attempted step, before completing the requested task, but the integration was successful as far as t. The problem may have a singularity, or the local error requirements may be inappropriate.
W  ifail=5
There were repeated convergence test failures on an attempted step, before completing the requested task, but the integration was successful as far as t. This may be caused by an inaccurate Jacobian matrix or one which is incorrectly computed.
W  ifail=6
Some error weight wi became zero during the integration (see the description of itol). Pure relative error control (atoli=0.0) was requested on a variable (the ith) which has now vanished. The integration was successful as far as t.
   ifail=7
resid set its error flag (ires=3) continually despite repeated attempts by the integrator to avoid this.
   ifail=8
lderiv1=false on entry but the internal initialization function was unable to initialize y (more detailed information may be directed to the current error message unit, see nag_file_set_unit_error (x04aa)).
   ifail=9
A singular Jacobian r y  has been encountered. You should check the problem formulation and Jacobian calculation.
   ifail=10
An error occurred during Jacobian formulation or back-substitution (a more detailed error description may be directed to the current error message unit, see nag_file_set_unit_error (x04aa)).
W  ifail=11
resid signalled the integrator to halt the integration and return (ires=2). Integration was successful as far as t.
W  ifail=12
monitr set imon=-2 and so forced a return but the integration was successful as far as t.
W  ifail=13
The requested task has been completed, but it is estimated that a small change in rtol and atol is unlikely to produce any change in the computed solution. (Only applies when you are not operating in one step mode, that is when itask2 or 5.)
   ifail=14
The values of rtol and atol are so small that nag_ode_ivp_stiff_imp_bandjac (d02nh) is unable to start the integration.
   ifail=15
The linear algebra setup function nag_ode_ivp_stiff_bandjac_setup (d02nt) was not called before the call to nag_ode_ivp_stiff_imp_bandjac (d02nh).
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The accuracy of the numerical solution may be controlled by a careful choice of the arguments rtol and atol, and to a much lesser extent by the choice of norm. You are advised to use scalar error control unless the components of the solution are expected to be poorly scaled. For the type of decaying solution typical of many stiff problems, relative error control with a small absolute error threshold will be most appropriate (that is, you are advised to choose itol=1 with atol1 small but positive).

Further Comments

The cost of computing a solution depends critically on the size of the differential system and to a lesser extent on the degree of stiffness of the problem. For nag_ode_ivp_stiff_imp_bandjac (d02nh) the cost is proportional to neq× ml+mu+1 2, though for problems which are only mildly nonlinear the cost may be dominated by factors proportional to neq×ml+mu+1 except for very large problems.
In general, you are advised to choose the BDF option (setup function nag_ode_ivp_stiff_bdf (d02nv)) but if efficiency is of great importance and especially if it is suspected that y A-1g  has complex eigenvalues near the imaginary axis for some part of the integration, you should try the BLEND option (setup function nag_ode_ivp_stiff_blend (d02nw)).

Example

This example solves the well-known stiff Robertson problem written as an implicit differential system and in implicit form
r1 = a + b + c r2 = 0.04a - 1.0E4bc - 3.0E7b2 - b r3 = 3.0E7b2 - c  
exploiting the fact that we can show that a+b+c = 0  for all time. Integration is over the range 0,10.0 with initial conditions a=1.0 and b=c=0.0 using scalar relative error control and vector absolute error control (itol=2). We integrate using a BDF method (setup function nag_ode_ivp_stiff_bdf (d02nv)) and a modified Newton method. The Jacobian is calculated numerically and we employ a default monitor, string nag_ode_ivp_stiff_exp_fulljac_dummy_monit (d02nby). We perform a normal integration (itask=1) to obtain the value at tout=10.0 by integrating past tout and interpolating. We also illustrate the use of itask=6 to calculate initial values of y and y and then return without integrating further.
function d02nh_example


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

neq    = int64(3);
maxord = int64(5);
sdysav = maxord+1;
petzld = false;
tcrit  = 0;
hmin   = 1.0e-10;
hmax   = 10;
h0     = 0;
maxstp = int64(200);
mxhnil = int64(5);
const = zeros(6, 1);
rwork = zeros(50+4*neq, 1);

% Setup for d02nh.
[const, rwork, ifail] = d02nv( ...
                               neq, sdysav, maxord, 'Newton', petzld, ...
                               const, tcrit, hmin, hmax, h0, maxstp, ...
                               mxhnil, 'Average-L2', rwork);
ml     = int64(1);
mu     = int64(2);
nwkjac = int64(neq*(2*ml+mu+1));

% Numerical Jacobian
[rwork, ifail] = d02nt( ...
			neq, neq, 'Numerical', ml, mu, nwkjac, neq, rwork);

% Initialize variables and arrays for integration
t    = 0;
tout = 10;
y    = [1; 0; 0];
ydot = zeros(neq, 1);
rtol = [0.0001];
atol = [1e-06; 1e-07; 1e-06];
itol = int64(2);
inform(1:23) = int64(0);
ysave  = zeros(neq, sdysav);
wkjac  = zeros(nwkjac, 1);
jacpvt(1:neq) = int64(0);
lderiv = [false; true];
itask  = int64(6);
itrace = int64(0);

% Solve for the initial values y and dy/dx as check
[t, tout, y, ydot, rwork, inform, ysave, wkjac, jacpvt, lderiv, ifail] = ...
d02nh( ...
       t, tout, y, ydot, rwork, rtol, atol, itol, inform, @resid, ysave, ...
       'd02nhz', wkjac, jacpvt, 'd02nby', lderiv, itask, itrace);

fprintf ('Initial y   : %8.4f %8.4f %8.4f\n',y);
fprintf ('Initial ydot: %8.4f %8.4f %8.4f\n',ydot);

% Use these initial estimates and integrate to tout 
% (overshoot and interpolate)
itask = int64(1);
tout = 10;
[t, tout, y, ydot, rwork, inform, ysave, wkjac, jacpvt, lderiv, ifail] = ...
d02nh( ...
       t, tout, y, ydot, rwork, rtol, atol, itol, inform, @resid, ysave, ...
       'd02nhz', wkjac, jacpvt, 'd02nby', lderiv, itask, itrace);

fprintf ('\n    x          y\n');
fprintf ('%8.3f %13.5f %13.5f %13.5f\n',tout,y);

[hu, h, tcur, tolsf, nst, nre, nje, nqu, nq, niter, imxer, algequ, ifail] = ...
  d02ny( ...
	 neq, neq, rwork, inform);

fprintf('\nDiagnostic information\n integration status:\n');
fprintf('   last and next step sizes = %8.5f, %8.5f\n',hu, h);
fprintf('   integration stopped at x = %8.5f\n',tcur);
fprintf(' algorithm statistics:\n');
fprintf('   number of time-steps and Newton iterations  = %5d %5d\n', ...
        nst,niter);
fprintf('   number of residual and jacobian evaluations = %5d %5d\n', ...
        nre,nje);
fprintf('   order of method last used and next to use   = %5d %5d\n', ...
        nqu,nq);
fprintf('   component with largest error                = %5d\n',imxer);



function [f, ires] = fcn(neq, t, y, ires)
  % Evaluate derivative vector.
  f = zeros(3,1);
  f(1) = -0.04*y(1) + 10000*y(2)*y(3);
  f(2) =  0.04*y(1) - 10000*y(2)*y(3) - 30000000*y(2)*y(2);
  f(3) =  30000000*y(2)*y(2);

function p = jac(neq, t, y, h, d, p)
  % Evaluate the Jacobian.
  p = zeros(neq, neq);
  hxd = h*d;
  p(1,1) = 1 - hxd*(-0.04);
  p(1,2) = -hxd*(10000*y(3));
  p(1,3) = -hxd*(10000*y(2));
  p(2,1) = -hxd*(0.04);
  p(2,2) = 1 - hxd*(-10000*y(3)-60000000*y(2));
  p(2,3) = -hxd*(-10000*y(2));
  p(3,2) = -hxd*(60000000*y(2));
  p(3,3) = 1 - hxd*(0);

function [r, ires] = resid(neq, t, y, ydot, ires)

  r = zeros(neq,1);
  r(1) = -ydot(1) - ydot(2) - ydot(3);
  r(2) = -ydot(2);
  r(3) = -ydot(3);
  if (ires == 1) ;
     r(1) = 0.0d0 + r(1);
     r(2) = 0.04d0*y(1) - 1.0d4*y(2)*y(3) - 3.0d7*y(2)*y(2) + r(2);
     r(3) = 3.0d7*y(2)*y(2) + r(3);
  end ;
d02nh example results

Initial y   :   1.0000   0.0000   0.0000
Initial ydot:  -0.0400   0.0400   0.0000
 Warning: Equation(=i1) and possibly other equations are
 implicit and in calculating the initial values the
 equations will be treated as implicit.
 In above message i1 =         1

    x          y
  10.000       0.84135       0.00002       0.15863

Diagnostic information
 integration status:
   last and next step sizes =  0.94084,  0.94084
   integration stopped at x = 10.25785
 algorithm statistics:
   number of time-steps and Newton iterations  =    48    66
   number of residual and jacobian evaluations =   119    15
   order of method last used and next to use   =     4     4
   component with largest error                =     3

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