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_pde_1d_parab_fd (d03pc)

Purpose

nag_pde_1d_parab_fd (d03pc) integrates a system of linear or nonlinear parabolic partial differential equations (PDEs) in one space variable. The spatial discretization is performed using finite differences, and the method of lines is employed to reduce the PDEs to a system of ordinary differential equations (ODEs). The resulting system is solved using a backward differentiation formula method.

Syntax

[ts, u, rsave, isave, ind, user, cwsav, lwsav, iwsav, rwsav, ifail] = d03pc(m, ts, tout, pdedef, bndary, u, x, acc, rsave, isave, itask, itrace, ind, cwsav, lwsav, iwsav, rwsav, 'npde', npde, 'npts', npts, 'user', user)
[ts, u, rsave, isave, ind, user, cwsav, lwsav, iwsav, rwsav, ifail] = nag_pde_1d_parab_fd(m, ts, tout, pdedef, bndary, u, x, acc, rsave, isave, itask, itrace, ind, cwsav, lwsav, iwsav, rwsav, 'npde', npde, 'npts', npts, 'user', user)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: lrsave, lisave have been removed from the interface
.

Description

nag_pde_1d_parab_fd (d03pc) integrates the system of parabolic equations:
npde
Pi,j(Uj)/(t) + Qi = xm()/(x)(xmRi),  i = 1,2,,npde,  axb,  tt0,
j = 1
j=1npdePi,j Uj t +Qi=x-m x (xmRi),  i=1,2,,npde,  axb,  tt0,
(1)
where Pi,jPi,j, QiQi and RiRi depend on xx, tt, UU, UxUx and the vector UU is the set of solution values
U (x,t) = [ U1 (x,t) ,, Unpde (x,t) ]T ,
U (x,t) = [ U 1 (x,t) ,, U npde (x,t) ] T ,
(2)
and the vector UxUx is its partial derivative with respect to xx. Note that Pi,jPi,j, QiQi and RiRi must not depend on (U)/(t) U t .
The integration in time is from t0t0 to touttout, over the space interval axbaxb, where a = x1a=x1 and b = xnptsb=xnpts are the leftmost and rightmost points of a user-defined mesh x1,x2,,xnptsx1,x2,,xnpts. The coordinate system in space is defined by the value of mm; m = 0m=0 for Cartesian coordinates, m = 1m=1 for cylindrical polar coordinates and m = 2m=2 for spherical polar coordinates. The mesh should be chosen in accordance with the expected behaviour of the solution.
The system is defined by the functions Pi,jPi,j, QiQi and RiRi which must be specified in pdedef.
The initial values of the functions U(x,t)U(x,t) must be given at t = t0t=t0. The functions RiRi, for i = 1,2,,npdei=1,2,,npde, which may be thought of as fluxes, are also used in the definition of the boundary conditions for each equation. The boundary conditions must have the form
βi(x,t)Ri(x,t,U,Ux) = γi(x,t,U,Ux),  i = 1,2,,npde,
βi(x,t)Ri(x,t,U,Ux)=γi(x,t,U,Ux),  i=1,2,,npde,
(3)
where x = ax=a or x = bx=b.
The boundary conditions must be specified in bndary.
The problem is subject to the following restrictions:
(i) t0 < toutt0<tout, so that integration is in the forward direction;
(ii) Pi,jPi,j, QiQi and the flux RiRi must not depend on any time derivatives;
(iii) the evaluation of the functions Pi,jPi,j, QiQi and RiRi is done at the mid-points of the mesh intervals by calling the pdedef for each mid-point in turn. Any discontinuities in these functions must therefore be at one or more of the mesh points x1,x2,,xnptsx1,x2,,xnpts;
(iv) at least one of the functions Pi,jPi,j must be nonzero so that there is a time derivative present in the problem; and
(v) if m > 0m>0 and x1 = 0.0x1=0.0, which is the left boundary point, then it must be ensured that the PDE solution is bounded at this point. This can be done by either specifying the solution at x = 0.0x=0.0 or by specifying a zero flux there, that is βi = 1.0βi=1.0 and γi = 0.0γi=0.0. See also Section [Further Comments].
The parabolic equations are approximated by a system of ODEs in time for the values of UiUi at mesh points. For simple problems in Cartesian coordinates, this system is obtained by replacing the space derivatives by the usual central, three-point finite difference formula. However, for polar and spherical problems, or problems with nonlinear coefficients, the space derivatives are replaced by a modified three-point formula which maintains second-order accuracy. In total there are npde × nptsnpde×npts ODEs in the time direction. This system is then integrated forwards in time using a backward differentiation formula method.

References

Berzins M (1990) Developments in the NAG Library software for parabolic equations Scientific Software Systems (eds J C Mason and M G Cox) 59–72 Chapman and Hall
Berzins M, Dew P M and Furzeland R M (1989) Developing software for time-dependent problems using the method of lines and differential-algebraic integrators Appl. Numer. Math. 5 375–397
Dew P M and Walsh J (1981) A set of library routines for solving parabolic equations in one space variable ACM Trans. Math. Software 7 295–314
Skeel R D and Berzins M (1990) A method for the spatial discretization of parabolic equations in one space variable SIAM J. Sci. Statist. Comput. 11(1) 1–32

Parameters

Compulsory Input Parameters

1:     m – int64int32nag_int scalar
The coordinate system used:
m = 0m=0
Indicates Cartesian coordinates.
m = 1m=1
Indicates cylindrical polar coordinates.
m = 2m=2
Indicates spherical polar coordinates.
Constraint: m = 0m=0, 11 or 22.
2:     ts – double scalar
The initial value of the independent variable tt.
Constraint: ts < toutts<tout.
3:     tout – double scalar
The final value of tt to which the integration is to be carried out.
4:     pdedef – function handle or string containing name of m-file
pdedef must compute the functions Pi,jPi,j, QiQi and RiRi which define the system of PDEs. pdedef is called approximately midway between each pair of mesh points in turn by nag_pde_1d_parab_fd (d03pc).
[p, q, r, ires, user] = pdedef(npde, t, x, u, ux, ires, user)

Input Parameters

1:     npde – int64int32nag_int scalar
The number of PDEs in the system.
2:     t – double scalar
The current value of the independent variable tt.
3:     x – double scalar
The current value of the space variable xx.
4:     u(npde) – double array
u(i)ui contains the value of the component Ui(x,t)Ui(x,t), for i = 1,2,,npdei=1,2,,npde.
5:     ux(npde) – double array
ux(i)uxi contains the value of the component (Ui(x,t))/(x) Ui(x,t) x , for i = 1,2,,npdei=1,2,,npde.
6:     ires – int64int32nag_int scalar
Set to 1​ or ​1-1​ or ​1.
7:     user – Any MATLAB object
pdedef is called from nag_pde_1d_parab_fd (d03pc) with the object supplied to nag_pde_1d_parab_fd (d03pc).

Output Parameters

1:     p(npde,npde) – double array
p(i,j)pij must be set to the value of Pi,j(x,t,U,Ux)Pi,j(x,t,U,Ux), for i = 1,2,,npdei=1,2,,npde and j = 1,2,,npdej=1,2,,npde.
2:     q(npde) – double array
q(i)qi must be set to the value of Qi(x,t,U,Ux)Qi(x,t,U,Ux), for i = 1,2,,npdei=1,2,,npde.
3:     r(npde) – double array
r(i)ri must be set to the value of Ri(x,t,U,Ux)Ri(x,t,U,Ux), for i = 1,2,,npdei=1,2,,npde.
4:     ires – int64int32nag_int scalar
Should usually remain unchanged. However, you may set ires to force the integration function to take certain actions as described below:
ires = 2ires=2
Indicates to the integrator that control should be passed back immediately to the calling (sub)routine with the error indicator set to ifail = 6ifail=6.
ires = 3ires=3
Indicates to the integrator that the current time step should be abandoned and a smaller time step used instead. You may wish to set ires = 3ires=3 when a physically meaningless input or output value has been generated. If you consecutively set ires = 3ires=3, then nag_pde_1d_parab_fd (d03pc) returns to the calling function with the error indicator set to ifail = 4ifail=4.
5:     user – Any MATLAB object
5:     bndary – function handle or string containing name of m-file
bndary must compute the functions βiβi and γiγi which define the boundary conditions as in equation (3).
[beta, gamma, ires, user] = bndary(npde, t, u, ux, ibnd, ires, user)

Input Parameters

1:     npde – int64int32nag_int scalar
The number of PDEs in the system.
2:     t – double scalar
The current value of the independent variable tt.
3:     u(npde) – double array
u(i)ui contains the value of the component Ui(x,t)Ui(x,t) at the boundary specified by ibnd, for i = 1,2,,npdei=1,2,,npde.
4:     ux(npde) – double array
ux(i)uxi contains the value of the component (Ui(x,t))/(x) Ui(x,t) x  at the boundary specified by ibnd, for i = 1,2,,npdei=1,2,,npde.
5:     ibnd – int64int32nag_int scalar
Determines the position of the boundary conditions.
ibnd = 0ibnd=0
bndary must set up the coefficients of the left-hand boundary, x = ax=a.
ibnd0ibnd0
Indicates that bndary must set up the coefficients of the right-hand boundary, x = bx=b.
6:     ires – int64int32nag_int scalar
Set to 1​ or ​1-1​ or ​1.
7:     user – Any MATLAB object
bndary is called from nag_pde_1d_parab_fd (d03pc) with the object supplied to nag_pde_1d_parab_fd (d03pc).

Output Parameters

1:     beta(npde) – double array
beta(i)betai must be set to the value of βi(x,t)βi(x,t) at the boundary specified by ibnd, for i = 1,2,,npdei=1,2,,npde.
2:     gamma(npde) – double array
gamma(i)gammai must be set to the value of γi(x,t,U,Ux)γi(x,t,U,Ux) at the boundary specified by ibnd, for i = 1,2,,npdei=1,2,,npde.
3:     ires – int64int32nag_int scalar
Should usually remain unchanged. However, you may set ires to force the integration function to take certain actions as described below:
ires = 2ires=2
Indicates to the integrator that control should be passed back immediately to the calling (sub)routine with the error indicator set to ifail = 6ifail=6.
ires = 3ires=3
Indicates to the integrator that the current time step should be abandoned and a smaller time step used instead. You may wish to set ires = 3ires=3 when a physically meaningless input or output value has been generated. If you consecutively set ires = 3ires=3, then nag_pde_1d_parab_fd (d03pc) returns to the calling function with the error indicator set to ifail = 4ifail=4.
4:     user – Any MATLAB object
6:     u(npde,npts) – double array
npde, the first dimension of the array, must satisfy the constraint npde1npde1.
The initial values of U(x,t)U(x,t) at t = tst=ts and the mesh points x(j)xj, for j = 1,2,,nptsj=1,2,,npts.
7:     x(npts) – double array
npts, the dimension of the array, must satisfy the constraint npts3npts3.
The mesh points in the spatial direction. x(1)x1 must specify the left-hand boundary, aa, and x(npts)xnpts must specify the right-hand boundary, bb.
Constraint: x(1) < x(2) < < x(npts)x1<x2<<xnpts.
8:     acc – double scalar
A positive quantity for controlling the local error estimate in the time integration. If E(i,j)E(i,j) is the estimated error for UiUi at the jjth mesh point, the error test is:
|E(i,j)| = acc × (1.0 + |u(i,j)|).
|E(i,j)|=acc×(1.0+|uij|).
Constraint: acc > 0.0acc>0.0.
9:     rsave(lrsave) – double array
lrsave, the dimension of the array, must satisfy the constraint lrsave(6 × npde + 10) × npde × npts + (3 × npde + 21) × npde +  7 × npts + 54lrsave(6×npde+10)×npde×npts+(3×npde+21)×npde+7×npts+54.
If ind = 0ind=0, rsave need not be set on entry.
If ind = 1ind=1, rsave must be unchanged from the previous call to the function because it contains required information about the iteration.
10:   isave(lisave) – int64int32nag_int array
lisave, the dimension of the array, must satisfy the constraint lisavenpde × npts + 24lisavenpde×npts+24.
If ind = 0ind=0, isave need not be set on entry.
If ind = 1ind=1, isave must be unchanged from the previous call to the function because it contains required information about the iteration. In particular:
isave(1)isave1
Contains the number of steps taken in time.
isave(2)isave2
Contains the number of residual evaluations of the resulting ODE system used. One such evaluation involves computing the PDE functions at all the mesh points, as well as one evaluation of the functions in the boundary conditions.
isave(3)isave3
Contains the number of Jacobian evaluations performed by the time integrator.
isave(4)isave4
Contains the order of the last backward differentiation formula method used.
isave(5)isave5
Contains the number of Newton iterations performed by the time integrator. Each iteration involves an ODE residual evaluation followed by a back-substitution using the LULU decomposition of the Jacobian matrix.
11:   itask – int64int32nag_int scalar
Specifies the task to be performed by the ODE integrator.
itask = 1itask=1
Normal computation of output values u at t = toutt=tout.
itask = 2itask=2
One step and return.
itask = 3itask=3
Stop at first internal integration point at or beyond t = toutt=tout.
Constraint: itask = 1itask=1, 22 or 33.
12:   itrace – int64int32nag_int scalar
The level of trace information required from nag_pde_1d_parab_fd (d03pc) and the underlying ODE solver. itrace may take the value 1-1, 00, 11, 22 or 33.
itrace = 1itrace=-1
No output is generated.
itrace = 0itrace=0
Only warning messages from the PDE solver are printed on the current error message unit (see nag_file_set_unit_error (x04aa)).
itrace > 0itrace>0
Output from the underlying ODE solver is printed on the current advisory message unit (see nag_file_set_unit_advisory (x04ab)). This output contains details of Jacobian entries, the nonlinear iteration and the time integration during the computation of the ODE system.
If itrace < 1itrace<-1, then 1-1 is assumed and similarly if itrace > 3itrace>3, then 33 is assumed.
The advisory messages are given in greater detail as itrace increases. You are advised to set itrace = 0itrace=0, unless you are experienced with sub-chapter D02M–N.
13:   ind – int64int32nag_int scalar
Indicates whether this is a continuation call or a new integration.
ind = 0ind=0
Starts or restarts the integration in time.
ind = 1ind=1
Continues the integration after an earlier exit from the function. In this case, only the parameters tout and ifail should be reset between calls to nag_pde_1d_parab_fd (d03pc).
Constraint: ind = 0ind=0 or 11.
14:   cwsav(1010) – cell array of strings
If ind = 0ind=0, cwsav need not be set on entry.
If ind = 1ind=1, cwsav must be unchanged from the previous call to the function.
15:   lwsav(100100) – logical array
If ind = 0ind=0, lwsav need not be set on entry.
If ind = 1ind=1, lwsav must be unchanged from the previous call to the function.
16:   iwsav(505505) – int64int32nag_int array
If ind = 0ind=0, iwsav need not be set on entry.
If ind = 1ind=1, iwsav must be unchanged from the previous call to the function.
17:   rwsav(11001100) – double array
If ind = 0ind=0, rwsav need not be set on entry.
If ind = 1ind=1, rwsav must be unchanged from the previous call to the function.

Optional Input Parameters

1:     npde – int64int32nag_int scalar
Default: The first dimension of the array u.
The number of PDEs in the system to be solved.
Constraint: npde1npde1.
2:     npts – int64int32nag_int scalar
Default: The dimension of the array x and the second dimension of the array u. (An error is raised if these dimensions are not equal.)
The number of mesh points in the interval [a,b][a,b].
Constraint: npts3npts3.
3:     user – Any MATLAB object
user is not used by nag_pde_1d_parab_fd (d03pc), but is passed to pdedef and bndary. 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

lrsave lisave iuser ruser

Output Parameters

1:     ts – double scalar
The value of tt corresponding to the solution values in u. Normally ts = toutts=tout.
2:     u(npde,npts) – double array
u(i,j)uij will contain the computed solution at t = tst=ts.
3:     rsave(lrsave) – double array
If ind = 1ind=1, rsave must be unchanged from the previous call to the function because it contains required information about the iteration.
4:     isave(lisave) – int64int32nag_int array
If ind = 1ind=1, isave must be unchanged from the previous call to the function because it contains required information about the iteration. In particular:
isave(1)isave1
Contains the number of steps taken in time.
isave(2)isave2
Contains the number of residual evaluations of the resulting ODE system used. One such evaluation involves computing the PDE functions at all the mesh points, as well as one evaluation of the functions in the boundary conditions.
isave(3)isave3
Contains the number of Jacobian evaluations performed by the time integrator.
isave(4)isave4
Contains the order of the last backward differentiation formula method used.
isave(5)isave5
Contains the number of Newton iterations performed by the time integrator. Each iteration involves an ODE residual evaluation followed by a back-substitution using the LULU decomposition of the Jacobian matrix.
5:     ind – int64int32nag_int scalar
ind = 1ind=1.
6:     user – Any MATLAB object
7:     cwsav(1010) – cell array of strings
If ind = 1ind=1, cwsav must be unchanged from the previous call to the function.
8:     lwsav(100100) – logical array
If ind = 1ind=1, lwsav must be unchanged from the previous call to the function.
9:     iwsav(505505) – int64int32nag_int array
If ind = 1ind=1, iwsav must be unchanged from the previous call to the function.
10:   rwsav(11001100) – double array
If ind = 1ind=1, rwsav must be unchanged from the previous call to the function.
11:   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,touttstoutts,
ortouttstout-ts is too small,
oritask1itask1, 22 or 33,
orm0m0, 11 or 22,
orm > 0m>0 and x(1) < 0.0x1<0.0,
orthe mesh points ​x(i) the mesh points ​xi  are not ordered,
ornpts < 3npts<3,
ornpde < 1npde<1,
oracc0.0acc0.0,
orind0ind0 or 11,
orlrsave is too small,
orlisave is too small.
W ifail = 2ifail=2
The underlying ODE solver cannot make any further progress across the integration range from the current point t = tst=ts with the supplied value of acc. The components of u contain the computed values at the current point t = tst=ts.
W ifail = 3ifail=3
In the underlying ODE solver, there were repeated errors or corrector convergence test failures on an attempted step, before completing the requested task. The problem may have a singularity or acc is too small for the integration to continue. Integration was successful as far as t = tst=ts.
  ifail = 4ifail=4
In setting up the ODE system, the internal initialization function was unable to initialize the derivative of the ODE system. This could be due to the fact that ires was repeatedly set to 33 in at least pdedef or bndary, when the residual in the underlying ODE solver was being evaluated.
  ifail = 5ifail=5
In solving the ODE system, a singular Jacobian has been encountered. You should check your problem formulation.
W ifail = 6ifail=6
When evaluating the residual in solving the ODE system, ires was set to 22 in at least pdedef or bndary. Integration was successful as far as t = tst=ts.
  ifail = 7ifail=7
The value of acc is so small that the function is unable to start the integration in time.
  ifail = 8ifail=8
In one of pdedef or bndary, ires was set to an invalid value.
  ifail = 9ifail=9 (nag_ode_ivp_stiff_imp_revcom (d02nn))
A serious error has occurred in an internal call to the specified function. Check the problem specification and all parameters and array dimensions. Setting itrace = 1itrace=1 may provide more information. If the problem persists, contact NAG.
W ifail = 10ifail=10
The required task has been completed, but it is estimated that a small change in acc is unlikely to produce any change in the computed solution. (Only applies when you are not operating in one step mode, that is when itask2itask2.)
  ifail = 11ifail=11
An error occurred during Jacobian formulation of the ODE system (a more detailed error description may be directed to the current error message unit).
  ifail = 12ifail=12
Not applicable.
  ifail = 13ifail=13
Not applicable.
  ifail = 14ifail=14
The flux function RiRi was detected as depending on time derivatives, which is not permissible.

Accuracy

nag_pde_1d_parab_fd (d03pc) controls the accuracy of the integration in the time direction but not the accuracy of the approximation in space. The spatial accuracy depends on both the number of mesh points and on their distribution in space. In the time integration only the local error over a single step is controlled and so the accuracy over a number of steps cannot be guaranteed. You should therefore test the effect of varying the accuracy parameter, acc.

Further Comments

nag_pde_1d_parab_fd (d03pc) is designed to solve parabolic systems (possibly including some elliptic equations) with second-order derivatives in space. The parameter specification allows you to include equations with only first-order derivatives in the space direction but there is no guarantee that the method of integration will be satisfactory for such systems. The position and nature of the boundary conditions in particular are critical in defining a stable problem. It may be advisable in such cases to reduce the whole system to first-order and to use the Keller box scheme function nag_pde_1d_parab_keller (d03pe).
The time taken depends on the complexity of the parabolic system and on the accuracy requested.

Example

We use the example given in Dew and Walsh (1981) which consists of an elliptic-parabolic pair of PDEs. The problem was originally derived from a single third-order in space PDE. The elliptic equation is
1/r()/(r) (r2(U1)/(r)) = 4α (U2 + r(U2)/(r))
1r r (r2 U1 r )=4α (U2+r U2 r )
and the parabolic equation is
(1r2) (U2)/(t) = 1/r()/(r) (r((U2)/(r)U2U1))
(1-r2) U2 t =1r r (r ( U2 r -U2U1) )
where (r,t)[0,1] × [0,1](r,t)[0,1]×[0,1]. The boundary conditions are given by
U1 = (U2)/(r) = 0  at ​r = 0,
U1= U2 r =0  at ​r=0,
and
()/(r)(rU1) = 0   and   U2 = 0   at ​ r = 1.
r (rU1)= 0   and   U2= 0   at ​ r=1.
The first of these boundary conditions implies that the flux term in the second PDE, ((U2)/(r)U2U1) ( U2 r - U2 U1 ) , is zero at r = 0r=0.
The initial conditions at t = 0t=0 are given by
U1 = 2αr  and  U2 = 1.0,   ​r[0,1].
U1=2αr  and  U2=1.0,   ​r[0,1].
The value α = 1α=1 was used in the problem definition. A mesh of 2020 points was used with a circular mesh spacing to cluster the points towards the right-hand side of the spatial interval, r = 1r=1.
function  nag_pde_1d_parab_fd_example
%  Solution of an elliptic-parabolic pair of PDEs.
global alpha;

% Set values for problem parameters.
npde = 2;

% Number of points on calculation mesh, and on interpolated mesh.
npts = 20;
intpts = 6;

itype = 1;
neqn = npde*npts;
lisave = neqn + 24;
nwk = (10 + 6*npde)*neqn;
lrsave = nwk + (21 + 3*npde)*npde + 7*npts + 54;

% Define some arrays.
rsave = zeros(lrsave, 1);
u = zeros(npde, npts);
uinterp = zeros(npde, intpts, itype);
x = zeros(npts, 1);
isave = zeros(lisave, 1, 'int64');
cwsav = {''; ''; ''; ''; ''; ''; ''; ''; ''; ''};
lwsav = false(100, 1);
iwsav = zeros(505, 1, 'int64');
rwsav = zeros(1100, 1);

% Set up the points on the interpolation grid.
xinterp = [0.0 0.4 0.6 0.8 0.9 1.0];

acc = 1.0e-3;
alpha = 1.0;

itrace = 0;
itask = 1;
m = 1; % Use cylindrical polar coordinates.

% We run through the calculation twice; once to output the interpolated
% results, and once to store the results for plotting.
niter = [5, 28];

% Prepare to store plotting results.
tsav = zeros(niter(2), 1);
usav = zeros(2, niter(2), npts);
isav = 0;

fprintf('nag_pde_1d_parab_fd example program results\n\n');
for icalc = 1:2

  % Set spatial mesh points.
  piby2 = 0.5*pi;
  hx = piby2/(npts-1.0);
  x(1) = 0.0;
  x(npts) = 1.0;
  for i = 2:npts-1
    x(i) = sin(hx*(i-1.0));
  end

  % Set initial conditions.
  ts = 0.0;
  tout = 0.1e-4;

  % Set the initial values.
  [u] = uinit(x, npts);

  % Start the integration in time.
  ind = 0;

  % Counter for saved results.
  isav = 0;

  % Loop over endpoints for the integration.  We've set itask = 1, which
  % gives normal computation of output values at t = tout.
  for iter = 1:niter(icalc)

    %Set the endpoint.
    if icalc == 1
      tout = 10.0*tout;
    else
      if iter < 10
        tout = 2.0*tout;
      else
        if iter == 10
          tout = 0.01;
        else
          if iter < 20
            tout = tout + 0.01;
          else
            tout = tout + 0.1;
          end
        end
      end
    end

    % The first time this is called, ind is 0, which (re)starts the
    % integration in time.  On exit, ind is set to 1; using this value
    % on a subsequent call continues the integration.  This means that
    % only tout and ifail should be reset between calls (i.e. the
    % output variables must have the same names as the input
    % variables - cf original MB22 example).
    [ts, u, rsave, isave, ind, user, cwsav, lwsav, iwsav, rwsav, ...
      ifail] = nag_pde_1d_parab_fd(int64(m), ts, tout, @pdedef, ...
      @bndary, u, x, acc, rsave, isave, int64(itask), ...
      int64(itrace), int64(ind), cwsav, lwsav, iwsav, rwsav);
    if ifail ~= 0
      % Parameters out of range, or convergence problems.
      % Print message and exit.
      fprintf('Warning: nag_pde_1d_parab_fd returned with ifail = %1d \n\n',ifail);
      return;
    end

    if icalc == 1
      % Output interpolation points first time through.
      if iter == 1
        fprintf([' accuracy requirement = %12.5e\n', ...
          ' parameter alpha =  %12.3e\n'], acc, alpha);
        fprintf(' t / x     ');
        for i = 1:intpts
          fprintf('%8.4f', xinterp(i));
        end
        fprintf('\n\n');
      end

      % Call nag_pde_1d_parab_fd_interp to do interpolation of results onto
      % coarser grid.
      [uinterp, ifail] = nag_pde_1d_parab_fd_interp(int64(m), u, x, xinterp, ...
                                                    int64(itype));
      if ifail ~= 0
        % Parameters out of range, or convergence problems.
        % Print message and exit.
        fprintf(['Warning: nag_pde_1d_parab_fd_interp returned with ifail = ', ...
          '%1d \n\n'], ifail);
        return;
      end

      % Output interpolated results for this time step.
      fprintf('%7.4f  u(1)', tout);
      for i = 1:intpts
        fprintf('%8.4f', uinterp(1,i,1));
      end
      fprintf('\n');
      fprintf('     u(2)');
      for i = 1:intpts
        fprintf('%8.4f', uinterp(2,i,1));
      end
      fprintf('\n\n');
    else
      % Save this timestep, and this set of results.
      isav = isav+1;
      tsav(isav) = ts;
      for ipt = 1:npts
        for isol = 1:2
          usav(isol,isav,ipt) = u(isol,ipt);
        end
      end
    end
  end

  if icalc == 1
    % Output some statistics.
    fprintf([' Number of integration steps in time = %6d\n', ...
      ' Number of function evaluations = %6d\n', ...
      ' Number of Jacobian evaluations = %6d\n', ...
      ' Number of iterations = %6d\n'], isave(1), isave(2), ...
      isave(3), isave(5));
  else
    % Plot results.
    fig1 = figure('Number', 'off');
    plot_results(x, tsav, squeeze(usav(1,:,:)), 'U1');
    fig2 = figure('Number', 'off');
    plot_results(x, tsav, squeeze(usav(2,:,:)), 'U2');
  end
end

function [p, q, r, ires, user] = pdedef(npde, t, x, u, ux, ires, user)
  % Evaluate Pij, Qi and Ri which define the system of PDEs.
  global alpha;

  p = zeros(npde,npde);
  q = zeros(npde,1);
  r = zeros(npde,1);
  q(1) = 4.0*alpha*(u(2) + x*ux(2));
  q(2) = 0.0;
  r(1) = x*ux(1);
  r(2) = ux(2) - u(1)*u(2);
  p(1,1) = 0.0;
  p(1,2) = 0.0;
  p(2,1) = 0.0;
  p(2,2) = 1.0 - x*x;

function [beta, gamma, ires, user] = bndary(npde, t, u, ux, ibnd, ires, user)
  % Evaluate beta and gamma to define the boundary conditions.

  if (ibnd == 0)
  beta(1) = 0.0;
  beta(2) = 1.0;
  gamma(1) = u(1);
  gamma(2) = -u(1)*u(2);
  else
  beta(1) = 1.0;
  beta(2) = 0.0;
  gamma(1) = -u(1);
  gamma(2) = u(2);
  end

function [u] = uinit(x, npts)
  % Set initial values for solution.
  global alpha;

  for i = 1:npts
    u(1,i) = 2.0*alpha*x(i);
    u(2,i) = 1.0;
  end

function plot_results(x, t, u, ident)
  % 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 array as a mesh.
  mesh(x, t, u);
  set(gca, 'YScale', 'log');
  set(gca, 'YTick', [0.00001 0.0001 0.001 0.01 0.1 1]);
  set(gca, 'YMinorGrid', 'off');
  set(gca, 'YMinorTick', 'off');

  % Label the axes, and set the title.
  xlabel('x', labFmt{:});
  ylabel('t', labFmt{:});
  zlabel([ident,'(x,t)'], labFmt{:});
  title({['Solution ',ident,' of elliptic-parabolic pair'], ...
      'using method of lines and BDF'}, titleFmt{:});
  title(['Solution ',ident,' of elliptic-parabolic pair'], titleFmt{:});

  % Set the axes limits tight to the x and y range.
  axis([x(1) x(end) t(1) t(end)]);

  % Set the view to something nice (determined empirically).
  view(-125, 30);
 
nag_pde_1d_parab_fd example program results

 accuracy requirement =  1.00000e-03
 parameter alpha =     1.000e+00
 t / x       0.0000  0.4000  0.6000  0.8000  0.9000  1.0000

 0.0001  u(1)  0.0000  0.8008  1.1988  1.5990  1.7958  1.8485
     u(2)  0.9997  0.9995  0.9994  0.9988  0.9663 -0.0000

 0.0010  u(1)  0.0000  0.7982  1.1940  1.5841  1.7179  1.6734
     u(2)  0.9969  0.9952  0.9937  0.9484  0.6385 -0.0000

 0.0100  u(1)  0.0000  0.7676  1.1239  1.3547  1.3635  1.2830
     u(2)  0.9627  0.9495  0.8754  0.5537  0.2908 -0.0000

 0.1000  u(1)  0.0000  0.3908  0.5007  0.5297  0.5120  0.4744
     u(2)  0.5468  0.4299  0.2995  0.1479  0.0724 -0.0000

 1.0000  u(1)  0.0000  0.0007  0.0008  0.0008  0.0008  0.0007
     u(2)  0.0010  0.0007  0.0005  0.0002  0.0001 -0.0000

 Number of integration steps in time =     78
 Number of function evaluations =    378
 Number of Jacobian evaluations =     25
 Number of iterations =    190

function  d03pc_example
%  Solution of an elliptic-parabolic pair of PDEs.
global alpha;

% Set values for problem parameters.
npde = 2;

% Number of points on calculation mesh, and on interpolated mesh.
npts = 20;
intpts = 6;

itype = 1;
neqn = npde*npts;
lisave = neqn + 24;
nwk = (10 + 6*npde)*neqn;
lrsave = nwk + (21 + 3*npde)*npde + 7*npts + 54;

% Define some arrays.
rsave = zeros(lrsave, 1);
u = zeros(npde, npts);
uinterp = zeros(npde, intpts, itype);
x = zeros(npts, 1);
isave = zeros(lisave, 1, 'int64');
cwsav = {''; ''; ''; ''; ''; ''; ''; ''; ''; ''};
lwsav = false(100, 1);
iwsav = zeros(505, 1, 'int64');
rwsav = zeros(1100, 1);

% Set up the points on the interpolation grid.
xinterp = [0.0 0.4 0.6 0.8 0.9 1.0];

acc = 1.0e-3;
alpha = 1.0;

itrace = 0;
itask = 1;
m = 1; % Use cylindrical polar coordinates.

% We run through the calculation twice; once to output the interpolated
% results, and once to store the results for plotting.
niter = [5, 28];

% Prepare to store plotting results.
tsav = zeros(niter(2), 1);
usav = zeros(2, niter(2), npts);
isav = 0;

fprintf('d03pc example program results\n\n');
for icalc = 1:2

  % Set spatial mesh points.
  piby2 = 0.5*pi;
  hx = piby2/(npts-1.0);
  x(1) = 0.0;
  x(npts) = 1.0;
  for i = 2:npts-1
    x(i) = sin(hx*(i-1.0));
  end

  % Set initial conditions.
  ts = 0.0;
  tout = 0.1e-4;

  % Set the initial values.
  [u] = uinit(x, npts);

  % Start the integration in time.
  ind = 0;

  % Counter for saved results.
  isav = 0;

  % Loop over endpoints for the integration.  We've set itask = 1, which
  % gives normal computation of output values at t = tout.
  for iter = 1:niter(icalc)

    %Set the endpoint.
    if icalc == 1
      tout = 10.0*tout;
    else
      if iter < 10
        tout = 2.0*tout;
      else
        if iter == 10
          tout = 0.01;
        else
          if iter < 20
            tout = tout + 0.01;
          else
            tout = tout + 0.1;
          end
        end
      end
    end

    % The first time this is called, ind is 0, which (re)starts the
    % integration in time.  On exit, ind is set to 1; using this value
    % on a subsequent call continues the integration.  This means that
    % only tout and ifail should be reset between calls (i.e. the
    % output variables must have the same names as the input
    % variables - cf original MB22 example).
    [ts, u, rsave, isave, ind, user, cwsav, lwsav, iwsav, rwsav, ...
      ifail] = d03pc(int64(m), ts, tout, @pdedef, ...
      @bndary, u, x, acc, rsave, isave, int64(itask), ...
      int64(itrace), int64(ind), cwsav, lwsav, iwsav, rwsav);
    if ifail ~= 0
      % Parameters out of range, or convergence problems.
      % Print message and exit.
      fprintf('Warning: d03pc returned with ifail = %1d \n\n',ifail);
      return;
    end

    if icalc == 1
      % Output interpolation points first time through.
      if iter == 1
        fprintf([' accuracy requirement = %12.5e\n', ...
          ' parameter alpha =  %12.3e\n'], acc, alpha);
        fprintf(' t / x     ');
        for i = 1:intpts
          fprintf('%8.4f', xinterp(i));
        end
        fprintf('\n\n');
      end

      % Call d03pz to do interpolation of results onto coarser grid.
      [uinterp, ifail] = d03pz(int64(m), u, x, xinterp, int64(itype));
      if ifail ~= 0
        % Parameters out of range, or convergence problems.
        % Print message and exit.
        fprintf(['Warning: d03pz returned with ifail = ', ...
          '%1d \n\n'], ifail);
        return;
      end

      % Output interpolated results for this time step.
      fprintf('%7.4f  u(1)', tout);
      for i = 1:intpts
        fprintf('%8.4f', uinterp(1,i,1));
      end
      fprintf('\n');
      fprintf('     u(2)');
      for i = 1:intpts
        fprintf('%8.4f', uinterp(2,i,1));
      end
      fprintf('\n\n');
    else
      % Save this timestep, and this set of results.
      isav = isav+1;
      tsav(isav) = ts;
      for ipt = 1:npts
        for isol = 1:2
          usav(isol,isav,ipt) = u(isol,ipt);
        end
      end
    end
  end

  if icalc == 1
    % Output some statistics.
    fprintf([' Number of integration steps in time = %6d\n', ...
      ' Number of function evaluations = %6d\n', ...
      ' Number of Jacobian evaluations = %6d\n', ...
      ' Number of iterations = %6d\n'], isave(1), isave(2), ...
      isave(3), isave(5));
  else
    % Plot results.
    fig1 = figure('Number', 'off');
    plot_results(x, tsav, squeeze(usav(1,:,:)), 'U1');
    fig2 = figure('Number', 'off');
    plot_results(x, tsav, squeeze(usav(2,:,:)), 'U2');
  end
end

function [p, q, r, ires, user] = pdedef(npde, t, x, u, ux, ires, user)
  % Evaluate Pij, Qi and Ri which define the system of PDEs.
  global alpha;

  p = zeros(npde,npde);
  q = zeros(npde,1);
  r = zeros(npde,1);
  q(1) = 4.0*alpha*(u(2) + x*ux(2));
  q(2) = 0.0;
  r(1) = x*ux(1);
  r(2) = ux(2) - u(1)*u(2);
  p(1,1) = 0.0;
  p(1,2) = 0.0;
  p(2,1) = 0.0;
  p(2,2) = 1.0 - x*x;

function [beta, gamma, ires, user] = bndary(npde, t, u, ux, ibnd, ires, user)
  % Evaluate beta and gamma to define the boundary conditions.

  if (ibnd == 0)
  beta(1) = 0.0;
  beta(2) = 1.0;
  gamma(1) = u(1);
  gamma(2) = -u(1)*u(2);
  else
  beta(1) = 1.0;
  beta(2) = 0.0;
  gamma(1) = -u(1);
  gamma(2) = u(2);
  end

function [u] = uinit(x, npts)
  % Set initial values for solution.
  global alpha;

  for i = 1:npts
    u(1,i) = 2.0*alpha*x(i);
    u(2,i) = 1.0;
  end

function plot_results(x, t, u, ident)
  % 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 array as a mesh.
  mesh(x, t, u);
  set(gca, 'YScale', 'log');
  set(gca, 'YTick', [0.00001 0.0001 0.001 0.01 0.1 1]);
  set(gca, 'YMinorGrid', 'off');
  set(gca, 'YMinorTick', 'off');

  % Label the axes, and set the title.
  xlabel('x', labFmt{:});
  ylabel('t', labFmt{:});
  zlabel([ident,'(x,t)'], labFmt{:});
  title({['Solution ',ident,' of elliptic-parabolic pair'], ...
      'using method of lines and BDF'}, titleFmt{:});
  title(['Solution ',ident,' of elliptic-parabolic pair'], titleFmt{:});

  % Set the axes limits tight to the x and y range.
  axis([x(1) x(end) t(1) t(end)]);

  % Set the view to something nice (determined empirically).
  view(-125, 30);
 
d03pc example program results

 accuracy requirement =  1.00000e-03
 parameter alpha =     1.000e+00
 t / x       0.0000  0.4000  0.6000  0.8000  0.9000  1.0000

 0.0001  u(1)  0.0000  0.8008  1.1988  1.5990  1.7958  1.8485
     u(2)  0.9997  0.9995  0.9994  0.9988  0.9663 -0.0000

 0.0010  u(1)  0.0000  0.7982  1.1940  1.5841  1.7179  1.6734
     u(2)  0.9969  0.9952  0.9937  0.9484  0.6385 -0.0000

 0.0100  u(1)  0.0000  0.7676  1.1239  1.3547  1.3635  1.2830
     u(2)  0.9627  0.9495  0.8754  0.5537  0.2908 -0.0000

 0.1000  u(1)  0.0000  0.3908  0.5007  0.5297  0.5120  0.4744
     u(2)  0.5468  0.4299  0.2995  0.1479  0.0724 -0.0000

 1.0000  u(1)  0.0000  0.0007  0.0008  0.0008  0.0008  0.0007
     u(2)  0.0010  0.0007  0.0005  0.0002  0.0001 -0.0000

 Number of integration steps in time =     78
 Number of function evaluations =    378
 Number of Jacobian evaluations =     25
 Number of iterations =    190


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