65
An Introductory Comparative Study of Partial Differential Equation Analysis in Matlab 1 and R 2 W. E. Schiesser Lehigh University Bethlehem, USA 3 Revised 20MAR13 1 Matlab is a product of the Mathworks, Inc. (http://www.mathworks.com) 2 R is an open source scientific programming system (http://www.R-project.org/) 3 Iacocca B312, Lehigh University, 111 Research Drive, Bethlehem, PA 18015 USA; [email protected]; 610-758-4264 1

Method of Lines.pdf

Embed Size (px)

Citation preview

Page 1: Method of Lines.pdf

An Introductory Comparative Studyof Partial Differential Equation Analysis

in Matlab1 and R2

W. E. SchiesserLehigh UniversityBethlehem, USA3

Revised20MAR13

1Matlab is a product of the Mathworks, Inc. (http://www.mathworks.com)2R is an open source scientific programming system (http://www.R-project.org/)3Iacocca B312, Lehigh University, 111 Research Drive, Bethlehem, PA 18015 USA;

[email protected]; 610-758-4264

1

Page 2: Method of Lines.pdf

Table of Contents

Introduction 3

1.1 Example No. 1 3

1.1.1 ODE routine in Matlab 4

1.1.2 ODE routine in R 7

1.1.3 Main program in Matlab 13

1.1.4 Main program in R 17

1.1.5 Comparison of Matlab and R output 22

1.2 Example No. 2 28

1.2.1 ODE routine in Matlab 30

1.2.2 ODE routine in R 32

1.2.3 Main program in Matlab 37

1.2.4 Main program in R 41

1.2.5 Comparison of Matlab and R output 47

1.3 Conclusions 53

Appendix 1 - Derivation of an analytical solution for example no. 1 54

1.1a Analytical solution by finite Fourier sine transform 54

1.1b Analytical solution by the Green’s function 58

References 61

2

Page 3: Method of Lines.pdf

Introduction

The intent of this introductory study of partial differential equation (PDE) analysis is toprovide a comparison of the coding in Matlab and R for a basic or generic PDE. For thispurpose, we have selected as the example PDE variants of the 1D diffusion equation.

(1.1) Example No. 1

The 1D diffusion equation is

∂u

∂t=

[D∂u

∂x

]∂x

(1.1)

where

Variables Interpretationparameters

u dependent variable

x boundary value independent variable

t initial value independent variable

D diffusivity (a parameter to be specified)

Table 1.1: Variables and parameters in eq. (1.1)

The objective is to compute the solution to eq. (1.1), u(x, t), in numerical form.Eq. (1.1) is first order in t and second order in x. It therefore requires one initial

condition (IC) and two boundary conditions (BCs).

u(x, t = 0) = 0 (1.2a)

u(x = 0, t) = 1;∂u(x = 1, t)

∂x= 0 (1.2b)(1.2c)

BC (1.2b) is termed Dirichlet since the dependent variable u is specified. BC (1.2c) istermed Neumann since the derivative ∂u/∂x is specified.D in eq. (1.1) can be a function of the independent variables x, t and/or the dependent

variable u (D = D(x, t, u)) so that eq. (1.1) can have a variable coefficient (D a functionof x, t) or can be nonlinear (D a function of u) or a combination of these cases.

Eqs. (1.1) and (1.2) constitute the complete specification of the example PDE. Wenow consider the coding of these equations in a “side-by-side“ format in Matlab and R.

The numerical solutions of eqs. (1.1) and (1.2) are computed using a basic technique,the method of lines (MOL), in which the spatial derivative terms (e.g., the RHS of eq.(1.1)) are replaced with algebraic approximations, in this case, finite differences (FDs)1.

3

Page 4: Method of Lines.pdf

The spatial variable (x) is thereby represented algebraically on a grid and only oneindependent variable (t) remains so that the PDE is replaced with an approximating setof ordinary differential equations (ODEs) in the remaining initial value variable (t).

We now consider a routine for the ODE system in Matlab and then in R for compar-ison2.

(1.1.1) ODE routine in Matlab

A MOL/ODE routine for eqs. (1.1) and (1.2) is in Listing 1.1a.

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

%

% BC at x = 0 (Dirichlet)

u(1)=1;

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

%

% BC at x = 1 (Neumann)

ux(nx)=0;

%

% Diffusivity

for i=1:nx

if(ncase==1)D(i)=1 ;end

if(ncase==2)D(i)=u(i) ;end

if(ncase==3)D(i)=exp(u(i));end

ux(i)=D(i)*ux(i);

end

%

% uxx

if (ndss== 2) uxx=dss002(xl,xu,nx,ux); % second order

elseif(ndss== 4) uxx=dss004(xl,xu,nx,ux); % fourth order

1Other approximations include finite elements, finite volumes, least squares, spectralelements.

2An additional comparison of Matlab and R is available from [1]; application of R todifferential equation numerical integration is discussed in [4].

4

Page 5: Method of Lines.pdf

elseif(ndss== 6) uxx=dss006(xl,xu,nx,ux); % sixth order

elseif(ndss== 8) uxx=dss008(xl,xu,nx,ux); % eighth order

elseif(ndss==10) uxx=dss010(xl,xu,nx,ux); % tenth order

end

%

% PDE

ut=uxx’;

ut(1)=0;

%

% Increment calls to pde_1

ncall=ncall+1;

Listing 1.1a: ODE Matlab routine for eqs. (1.1) and (1.2)

We can note the following details about Listing 1.1a.

• The function and a global area are defined.

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

The global area is used to access variables and parameters defined numerically inother routines, and in particular the main program that is discussed subsequently.

• BC (1.2b) is implemented at grid point 1 corresponding to x = 0.

%

% BC at x = 0 (Dirichlet)

u(1)=1;

• The derivative ∂u/∂x is computed by a call to one of five library differentiationroutines. These routines are available from a download (http://www.pdecomp.net,v. Compendium book).

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

5

Page 6: Method of Lines.pdf

For the present case, ndss = 4 is set in the main program (and made availablethrough the global area) so that fourth order FD approximations in dss004 areused in calculating the derivative ∂u/∂x which is placed in the vector ux. TheRHS input parameters of dss004, xl,xu,nx,u, are defined numerically in the mainprogram. For the first call to pde 1, u is set by IC (1.2a), and in subsequent callsto pde 1 by the ODE integrator, ode15s, called in the main program (note that u

is the second input argument of pde 1).

• BC (1.2c) is used to reset the derivative ∂u(x = xu = 1, t)/∂x from dss004 (at gridpoint nx).

%

% BC at x = 1 (Neumann)

ux(nx)=0;

• The diffusivity D in eq. (1.1) is defined for three cases, ncase=1,2,3. The value ofncase is coded in the main program.

%

% Diffusivity

for i=1:nx

if(ncase==1)D(i)=1 ;end

if(ncase==2)D(i)=u(i) ;end

if(ncase==3)D(i)=exp(u(i));end

ux(i)=D(i)*ux(i);

end

These cases correspond to

ncase Diffusivity (eq. (1.1))

1 D = constant

2 D = u

3 D = eu

Table 1.2: Diffusivity in eq. (1.1)

The coding demonstrates the ease of handling nonlinearities numerically.

• The second derivative in x (the eq. (1.1) RHS) is computed as the derivative of thefirst derivative, so-called stagewise differentiation.

6

Page 7: Method of Lines.pdf

%

% uxx

if (ndss== 2) uxx=dss002(xl,xu,nx,ux); % second order

elseif(ndss== 4) uxx=dss004(xl,xu,nx,ux); % fourth order

elseif(ndss== 6) uxx=dss006(xl,xu,nx,ux); % sixth order

elseif(ndss== 8) uxx=dss008(xl,xu,nx,ux); % eighth order

elseif(ndss==10) uxx=dss010(xl,xu,nx,ux); % tenth order

end

Since this second derivative in the vector uxx is then the RHS of eq. (1.1), it canbe used next to program eq. (1.1).

• The following code illustrates the ease of the MOL programming of eq. (1.1).

%

% PDE

ut=uxx’;

ut(1)=0;

Here we have used the Matlab vector utility (rather than subscripting). The trans-pose ’ ensures ut is returned as a column vector as required by the ODE inte-grator ode15s. Also, since BC (1.2b) specifies u(x = 0, t) = 1, the derivative∂u(x = 0, t)/∂t = 0 which is programmed to ensure the ODE integrator ode15s

does not move u(x = 0, t) away from its specified value. In other words, the ODEat x = 0 is defined by BC (1.2b) rather than by PDE (1.1).

• Finally, the number of calls to pde 1 is incremented (and returned to the mainprogram as a global variable).

%

% Increment calls to pde_1

ncall=ncall+1;

This completes the programming of eqs. (1.1), (1.2b) and (1.2c). The only remainingrequirement is the programming of IC (1.2a) which is in the main program discussedsubsequently.

We now consider the equivalent ODE routine programmed in R.

(1.1.2) ODE routine in R

A MOL/ODE routine in R for eqs. (1.1) and (1.2) is in Listing 1.1b.

pde_1=function(t,u,parms){

#

# BC at x = 0 (Dirichlet)

u[1]=1;

7

Page 8: Method of Lines.pdf

#

# ux

if (ndss== 2){ux=dss002(xl,xu,nx,u); # second order

}else if(ndss== 4){ux=dss004(xl,xu,nx,u); # fourth order

}else if(ndss== 6){ux=dss006(xl,xu,nx,u); # sixth order

}else if(ndss== 8){ux=dss008(xl,xu,nx,u); # eighth order

}else if(ndss==10){ux=dss010(xl,xu,nx,u); # tenth order

}

#

# BC at x = 1 (Neumann)

ux[nx]=0;

#

# Diffusivity

D=rep(0,nx);

for(i in 1:nx){

if(ncase==1){D[i]=1 ;}

if(ncase==2){D[i]=u[i] ;}

if(ncase==3){D[i]=exp(u[i]);}

ux[i]=D[i]*ux[i];

}

#

# uxx

if (ndss== 2){uxx=dss002(xl,xu,nx,ux); # second order

}else if(ndss== 4){uxx=dss004(xl,xu,nx,ux); # fourth order

}else if(ndss== 6){uxx=dss006(xl,xu,nx,ux); # sixth order

}else if(ndss== 8){uxx=dss008(xl,xu,nx,ux); # eighth order

}else if(ndss==10){uxx=dss010(xl,xu,nx,ux); # tenth order

}

#

# PDE

ut=rep(0,nx);

ut=uxx;

ut[1]=0;

#

# Increment calls to pde_1

ncall <<- ncall+1;

#

# Return derivative vector

return(list(c(ut)));

}

Listing 1.1b: ODE R routine for eqs. (1.1) and (1.2)

We can now make a comparsion of Listings 1.1a and 1.1b.

• Each routine starts with a function definition.

8

Page 9: Method of Lines.pdf

Matlab

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

R

pde_1=function(t,u,parms){

In the case of Matlab, a global area is defined. For R, variables and parametersdefined in the superior routine, in this case, the main program, are automaticallyavailable to the subordinate routine.

• The programming of BC (1.2b) is similar in both systems

Matlab

%

% BC at x = 0 (Dirichlet)

u(1)=1;

R

#

# BC at x = 0 (Dirichlet)

u[1]=1;

Note the use of % for a comment and ( ) for an array in Matlab; in R, these are #

and [ ].

• The calculation of the first derivative ∂u/∂x is similar in the two systems.

Matlab

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

9

Page 10: Method of Lines.pdf

R

#

# ux

if (ndss== 2){ux=dss002(xl,xu,nx,u); # second order

}else if(ndss== 4){ux=dss004(xl,xu,nx,u); # fourth order

}else if(ndss== 6){ux=dss006(xl,xu,nx,u); # sixth order

}else if(ndss== 8){ux=dss008(xl,xu,nx,u); # eighth order

}else if(ndss==10){ux=dss010(xl,xu,nx,u); # tenth order

}

The if - else if - else syntax is slightly different, including the use of end inMatlab and } in R.

• The programming of BC (1.2c) is similar.

Matlab

%

% BC at x = 1 (Neumann)

ux(nx)=0;

R

#

# BC at x = 1 (Neumann)

ux[nx]=0;

• The programming of the diffusivity for the three cases ncase=1,2,3 and the linear(ncase=1) or nonlinear (ncase=2,3) function in eq. (1.1), D∂u/∂x, is within a for.Note the difference in the syntax of the for, i.e., for i=1:nx for Matlab and for(i

in 1:nx) for R. Also, the for in Matlab is concluded with end while in R, it isconcluded with }.

Matlab

%

% Diffusivity

for i=1:nx

if(ncase==1)D(i)=1 ;end

if(ncase==2)D(i)=u(i) ;end

if(ncase==3)D(i)=exp(u(i));end

10

Page 11: Method of Lines.pdf

ux(i)=D(i)*ux(i);

end

R

#

# Diffusivity

D=rep(0,nx);

for(i in 1:nx){

if(ncase==1){D[i]=1 ;}

if(ncase==2){D[i]=u[i] ;}

if(ncase==3){D[i]=exp(u[i]);}

ux[i]=D[i]*ux[i];

}

Note that Matlab automatically (dynamically) allocates (defines) the array D while Rrequires that the array is declared (preallocated) before it is used, that is, D=rep(0,nx).rep is a R utility that in this case sets the nx elements of D to zero. These values ofD are then set to the required values for the calculation, but D must first be declared.

ux was declared by the call to a dss routine (in this case, dss004 since ndss=4 fromthe main program in Listing 1.2b to follow) so that the use of rep was not required.

• The programming of the second derivative (with the linear/nonlinear diffusivityfunction) is similar to the programming of the first derivative.

Matlab

%

% uxx

if (ndss== 2) uxx=dss002(xl,xu,nx,ux); % second order

elseif(ndss== 4) uxx=dss004(xl,xu,nx,ux); % fourth order

elseif(ndss== 6) uxx=dss006(xl,xu,nx,ux); % sixth order

elseif(ndss== 8) uxx=dss008(xl,xu,nx,ux); % eighth order

elseif(ndss==10) uxx=dss010(xl,xu,nx,ux); % tenth order

end

R

#

# uxx

if (ndss== 2){uxx=dss002(xl,xu,nx,ux); # second order

}else if(ndss== 4){uxx=dss004(xl,xu,nx,ux); # fourth order

11

Page 12: Method of Lines.pdf

}else if(ndss== 6){uxx=dss006(xl,xu,nx,ux); # sixth order

}else if(ndss== 8){uxx=dss008(xl,xu,nx,ux); # eighth order

}else if(ndss==10){uxx=dss010(xl,xu,nx,ux); # tenth order

}

The calculation of the second derivative uxx is by differentiation of the first derivativeux, that is, by stagewise differentiation.

• The programming of eq. (1.1) is similar in the two systems.

Matlab

%

% PDE

ut=uxx’;

ut(1)=0;

R

#

# PDE

ut=rep(0,nx);

ut=uxx;

ut[1]=0;

For Matlab, a transpose of u is required for the integrator ode15s. For R, the vectorof derivatives ut must first be declared (with a rep). In both cases, the vector utilityis used so the programming of ut can be accomplished with a single line of code(without subscripting). Also, in both cases, the derivative in t at x = 0 is set tozero for the Neumann BC (1.2b).

• The number of calls to the ODE routine is incremented. For Matlab, the new valueof ncall is returned to the main program through the global area. For R this valueis returned with <<-.

Matlab

%

% Increment calls to pde_1

ncall=ncall+1;

R

12

Page 13: Method of Lines.pdf

#

# Increment calls to pde_1

ncall <<- ncall+1;

• In Matlab, the computed derivative vector, ut, is returned through the LHS argu-ment of pde 1. In R, the derivative vector is returned as a list. Note also the useof the R vector utility c. That is, ut is declared a vector, then a list, and finallyis returned. This format for the return is required by the R ODE integrators inthe library deSolve [4] (including lsodes used in the R main program discussedsubsequently).

Matlab

ut returned as the LHS argument

R

#

# Return derivative vector

return(list(c(ut)));

}

The final } concludes pde 1 in R.

This completes the programming of the ODE routine in R. The main programs thatcalled the ODE routines are considered next.

(1.1.3) Main program in Matlab

The main program in Matlab that calls the ODE routine pde 1 in Listing 1.1a is inListing 1.2a.

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

%

% Grid in x, initial condition

xl=0;xu=1;nx=26;

for ix=1:nx

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

13

Page 14: Method of Lines.pdf

u0(ix)=0;

end

%

% Independent variable for ODEs

t0=0.0;tf=1.5;nout=11;

tout=linspace(t0,tf,nout);

ncall=0;

%

% ODE integration

ncase=1;ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

%

% Display selected output

for it=1:nout

if(it==1);u(it,1)=0;end

if(it> 1);u(it,1)=1;end

fprintf(’\n t x u(x,t)\n’);

for ix=1:5:nx

fprintf(’%7.2f%8.2f%12.5f\n’,t(it),xg(ix),u(it,ix));

end

end

fprintf(’\n ncall = %4d\n’,ncall);

%

% Plot numerical solution

figure(1);

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t=0,0.15,...,1.5’);

Listing 1.2a: Main program that calls pde 1 of Listing 1.1a

We can note the following details about Listing 1.2a.

• Previous files are cleared and a global area is defined.

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

The global area provides for a sharing of variables and parameters between routines,in this case, between the main program of Listing 1.2a and the ODE routine ofListing 1.1a.

14

Page 15: Method of Lines.pdf

• A grid in x of 26 points is defined for 0 ≤ x ≤ 1. The grid point values are thenplaced in array xg and the IC vector of eq. (1.2a) is placed in array u0.

%

% Grid in x, initial condition

xl=0;xu=1;nx=26;

for ix=1:nx

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

u0(ix)=0;

end

Here we have used a for with subscripting, which was selected to facilitate a com-parison with the R code to follow. Other Matlab utilities could have been used. Forexample, xg could be defined numerically with

xg=[xl:(xu-xl)/(nx-1):xu]’;

in place of the for where a transpose (’) has been used to convert xg to a columnvector. Similarly, the IC vector could have been coded as

u0=zeros(nx,1);

which specifies a vector u0 of zeros with nx rows and 1 column.

• The vector of output values of t, tout, is defined by the Matlab utility linspace

for 0 ≤ t ≤ 1.5 with 11 values (including at t = 0).

%

% Independent variable for ODEs

t0=0.0;tf=1.5;nout=11;

tout=linspace(t0,tf,nout);

ncall=0;

In other words, tout has the values t = 0, 0.15, ..., 1. Finally, the counter for thecalls to pde 1 in Listing 1.1a is initialized.

• The 26 ODEs are integrated by a call to ode15s, a stiff integrator with a variety ofoptions. In this, only the default options are used.

%

% ODE integration

ncase=1;ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

15

Page 16: Method of Lines.pdf

Also, ncase is specified to select the diffusivity in pde 1 of Listing 1.1a. ndss =

4 selects the dss004 differentiator in Listing 1.1a. ncase and ndss are passed topde 1 as global parameters.

In the call to ode15s, note the use of pde 1 in Listing 1.1a, the vector of outputvalues of t, tout, and the IC vector u0. Generally, the order of these inputs mustbe maintained as used here when calling ode15s. The solution is returned as t, avector of values of t in tout, and u, a 2D array with dimensions u(11,26) for the11 values of t and the 26 corresponding ODE solutions.

• The numerical ODE solution is displayed with fprintf. First, the value of u(x =0, t) is set according to IC (1.2a) for t = 0 and according to BC (1.2b) for t > 0. Thereason this is required is because the Matlab integrators in general do not return adependent variable that is set algebraically rather than by the integration of an ODE.Thus, u(x = 0) is defined algebraically in pde 1 of Listing 1.1a for use with the MOL,but this value is not returned to the main program of Listing 1.2a and therefore mustbe defined explicitly in the main program. Also, the distinction between it=1 fort = 0 and it>1 for t > 0 is required because there is an inconsistency between IC(1.2a) and BC (1.2b). This amounts to a finite unit step or discontinuity which canbe handled computationally because eq. (1.1) is parabolic and therefore tends tosmooth discontinuities (generally, this would not happen with hyperbolic PDEs).

%

% Display selected output

for it=1:nout

if(it==1);u(it,1)=0;end

if(it> 1);u(it,1)=1;end

fprintf(’\n t x u(x,t)\n’);

for ix=1:5:nx

fprintf(’%7.2f%8.2f%12.5f\n’,t(it),xg(ix),u(it,ix));

end

end

fprintf(’\n ncall = %4d\n’,ncall);

Note that the for in ix displays only every fifth value of the solution (for ix=1:5:nx)to keep the displayed output to a modest size (that is, u(x, t) for x = 0, 0.20, ..., 10).Finally, the number of calls to pde 1 of Listing 1.1a is displayed as an indicationof the effort required to compute a solution. Note that in general Matlab defines acharacter string with a single quote (’) while this is done with a double quote (")in R.

• A plot of u(x, t) against x is produced with a single call to the Matlab utility plot

which matches the dimension of xg (26) with the second dimension of u (also 26)as defined by the ODE integrator, ode15s. In this way, t becomes a parameter inthe plotting with a series of nout=11 curves through the first dimension of u (11).

16

Page 17: Method of Lines.pdf

%

% Plot numerical solution

figure(1);

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t=0,0.15,...,1.5’);

Labeling of the horizontal and vertical axes, and a title for the plot are also added.

This completes the programming of eqs. (1.1), (1.2b) and (1.2c). The only remainingrequirement is the programming of IC (1.2a) which is in the main program discussedsubsequently.

(1.1.4) Main program in R

The main program in R is considered next with a comparison of equivalent statementsfrom Listing 1.2a.

#

# Access ODE integrator

library("deSolve");

#

# Access functions

setwd("c:/R/pde_intro");

source("pde_1.R");

source("dss004.R");

#

# Grid in x, initial condition

xl=0;xu=1;nx=26;

xg=rep(0,nx);u0=rep(0,nx);

for(ix in 1:nx){

xg[ix]=(xu-xl)*(ix-1)/(nx-1);

u0[ix]=0;

}

#

# Independent variable for ODEs

t0=0;tf=1.5;nout=11;

tout=seq(from=0,to=tf,by=(tf-t0)/(nout-1));

ncall=0;

#

# ODE integration

ncase=1;ndss=4;

out=lsodes(y=u0,times=tout,func=pde_1,parms=NULL)

#

# Display selected output

17

Page 18: Method of Lines.pdf

for(it in 1:nout){

if(it==1){out[it,2]=0;}

if(it> 1){out[it,2]=1;}

cat(sprintf("\n t x u(x,t)\n"));

for(ix in 1:nx){

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)

*(ix-21)*(ix-26)==0){

cat(sprintf("%7.2f%8.2f%12.5f\n",

out[it,1],xg[ix],out[it,ix+1]));

}

}

}

cat(sprintf("\n ncall = %4d\n",ncall));

#

# Plot numerical solution

par(mfrow=c(1,1));

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.15,...,1.5",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.15,...,1.5;",lty=1,lwd=2);

Listing 1.2b: Main program that calls pde 1 of Listing 1.1b

We can note the following details of Listing 1.2b with a comparison of the equivalentMatlab statements in Listing 1.2a.

• Some preliminaries are first programmed.

Matlab

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

R

#

# Access ODE integrator

library("deSolve");

#

# Access functions

18

Page 19: Method of Lines.pdf

setwd("c:/R/pde_intro");

source("pde_1.R");

source("dss004.R");

The Matlab statements have already been considered (in Section (1.1.3)). The Rstatements access the ODE library deSolve followed by the ODE routine pde 1 ofListing 1.1b and the library differentiator dss004. The setwd utility (set workingdirectory) specifies the location of these two files (note the use of the forward slash,/, rather than the usual backslash \).

• The grid in x has 26 points for 0 ≤ x ≤ 1.

Matlab

%

% Grid in x, initial condition

xl=0;xu=1;nx=26;

for ix=1:nx

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

u0(ix)=0;

end

R

#

# Grid in x, initial condition

xl=0;xu=1;nx=26;

xg=rep(0,nx);u0=rep(0,nx);

for(ix in 1:nx){

xg[ix]=(xu-xl)*(ix-1)/(nx-1);

u0[ix]=0;

}

For the R code, the grid in x xg and the IC array u0 are first declared (preal-located) with a rep. Alternative R coding for the grid can be considered, e.g.,xg=seq(from=xl,to=xu,by=(xu-xl)*(ix-1)/(nx-1)) based on the R utility seq.u0 is already an nx-vector of zeros from u0=rep(0,nx) and therefore does not requirethe programming in the for.

• The output points in t are specified.

Matlab

%

19

Page 20: Method of Lines.pdf

% Independent variable for ODEs

t0=0.0;tf=1.5;nout=11;

tout=linspace(t0,tf,nout);

ncall=0;

R

#

# Independent variable for ODEs

t0=0;tf=1.5;nout=11;

tout=seq(from=0,to=tf,by=(tf-t0)/(nout-1));

ncall=0;

Note the use of seq for R in place of linspace for Matlab.

• The ODE integration in R is by lsodes ([4]).

Matlab

%

% ODE integration

ncase=1;ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

R

#

# ODE integration

ncase=1;ndss=4;

out=lsodes(y=u0,times=tout,func=pde_1,parms=NULL)

The @ in the first RHS argument of ode15s indicates this argument is a function(pde 1 of Listing 1.1a) rather than a parameter or variable. The solution arrayfrom lsodes, out ([4]), contains both the 11 values of t and the 26 ODE solutions.Therefore, the dimensions of out are out(11,27). The second dimension in out is27 rather than 26 to include t as explained next. Note the use of the IC vector,u0, the vector of output values of t, tout, and the ODE routine, pde 1 in Listing1.1b. The argument parms (to pass parameters to the ODE routine) is unused.y,times,func,parms are reserved names for lsodes.

An alternative for the ODE integration is

ode(y=u0,times=tout,func=pde_1,parms=NULL)

20

Page 21: Method of Lines.pdf

The R utility ode uses the integrator lsoda as the default. lsoda switches betweena nonstiff and stiff integrator depending on an eigenvalue analysis (the “a” standsfor automatic).

lsoda and lsodes have a variety of options as explained in the R documentation.Here we have used the defaults for lsodes.

• The numerical solution is displayed in a similar way for Matlab and R.

Matlab

%

% Display selected output

for it=1:nout

if(it==1);u(it,1)=0;end

if(it> 1);u(it,1)=1;end

fprintf(’\n t x u(x,t)\n’);

for ix=1:5:nx

fprintf(’%6.2f%8.3f%10.5f\n’,t(it),xg(ix),u(it,ix));

end

end

fprintf(’\n ncall = %4d\n’,ncall);

R

#

# Display selected output

for(it in 1:nout){

if(it==1){out[it,2]=0;}

if(it> 1){out[it,2]=1;}

cat(sprintf("\n t x u(x,t)\n"));

for(ix in 1:nx){

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)

*(ix-21)*(ix-26)==0){

cat(sprintf("%7.2f%8.2f%12.5f\n",

out[it,1],xg[ix],out[it,ix+1]));

}

}

}

cat(sprintf("\n ncall = %4d\n",ncall));

Note the use of out[it,2] for u(x = 0, t) in the R coding since out[it,1] referencest. Thus, out[it,27] references u(x = 1, t). Also, the output is for every fifth valueof x, that is, x = 0, 0.2, ..., 1 through the use of the if in ix. A comparsion of the

21

Page 22: Method of Lines.pdf

Matlab and R numerical output is given subsequently. The final value of the numberof calls to pde 1 in Listing 1.1b is displayed.

• The plotting in R is by the matplot utility. A 1× 1 array of plots, that is, a singleplot, is specified with par(mfrow=c(1,1)).

Matlab

%

% Plot numerical solution

figure(1);

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t=0,0.15,...,1.5’);

R

#

# Plot numerical solution

par(mfrow=c(1,1));

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.15,...,1.5",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.15,...,1.5;",lty=1,lwd=2);

The labeling of the plots in Matlab and R is similar. matplot has additional param-eters for specifying the line type and the horizontal axis limits. The vertical axis isscaled as a default (ylim is also available).

This concludes the R programming for eqs. (1.1) and (1.2). The output from Listings 1.1aand 1.2a for Matlab, and Listings 1.1b and 1.2b for R, is now discussed and compared.

(1.1.5) Comparison of Matlab and R output

Abbreviated output from Listings 1.1a and 1.2a is given in Table 1.3.

t x u(x,t)

0.00 0.00 0.00000

0.00 0.20 0.00000

0.00 0.40 0.00000

0.00 0.60 0.00000

0.00 0.80 0.00000

0.00 1.00 0.00000

.

.

22

Page 23: Method of Lines.pdf

(output for t = 0.15

to 0.30 removed)

.

.

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.87040

0.45 0.40 0.75347

0.45 0.60 0.66065

0.45 0.80 0.60105

0.45 1.00 0.58052

.

.

(output for t = 0.60

to 1.35 removed)

.

.

t x u(x,t)

1.50 0.00 1.00000

1.50 0.20 0.99033

1.50 0.40 0.98160

1.50 0.60 0.97467

1.50 0.80 0.97022

1.50 1.00 0.96869

ncall = 154

Table 1.3: Output from Listings, 1.1a 1.2a for ncase=1 (D = 1)

We can note the following details about this output.

• IC (1.2a) (t = 0) is correct. This is perhaps an obvious detail, but checking the ICis always a good idea since if it is incorrect, the remainder of the numerical outputwill also be incorrect.

• BC (1.2b) (x = 0, t > 0) is correct. This is also an important detail since it is thisunit value that drives the entire system of 26 away from the zero initial value.

• The solution approaches u(x, t → ∞) = 1 which is the correct exact solution (seeeqs. (1.3)). Note in particular that this constant, unit value satisfies eqs. (1.1) and(1.2).

• The computational effort is quite modest with ncall = 154.

Abbreviated output from Listings 1.1b and 1.2b is given in Table 1.4

23

Page 24: Method of Lines.pdf

t x u(x,t)

0.00 0.00 0.00000

0.00 0.20 0.00000

0.00 0.40 0.00000

0.00 0.60 0.00000

0.00 0.80 0.00000

0.00 1.00 0.00000

.

.

(output for t = 0.15

to 0.30 removed)

.

.

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.87036

0.45 0.40 0.75342

0.45 0.60 0.66064

0.45 0.80 0.60107

0.45 1.00 0.58055

.

.

(output for t = 0.60

to 1.35 removed)

.

.

t x u(x,t)

1.50 0.00 1.00000

1.50 0.20 0.99028

1.50 0.40 0.98152

1.50 0.60 0.97456

1.50 0.80 0.97009

1.50 1.00 0.96855

ncall = 230

Table 1.4: Output from Listings 1.1b 1.2b for ncase=1 (D = 1)

Again, the details for Table 1.3 apply to Table 1.4. The computational effort is modestwith ncall = 230.

The two solutions in Tables 1.3, 1.4 are compared in Table 1.5 at t = 0.45.

Matlab (Table 1.3)

t x u(x,t)

24

Page 25: Method of Lines.pdf

0.45 0.00 1.00000

0.45 0.20 0.87040

0.45 0.40 0.75347

0.45 0.60 0.66065

0.45 0.80 0.60105

0.45 1.00 0.58052

R (Table 1.4)

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.87036

0.45 0.40 0.75342

0.45 0.60 0.66064

0.45 0.80 0.60107

0.45 1.00 0.58055

Table 1.5: Comparison of the numerical solutions at t = 0.45

The two solutions agree to four figures where they are changing relatively rapidly (att = 0.45). The graphical output is in Figs. 1.1 (Matlab) and 1.2 (R) (placed at the end).

The two plots have essentially the same features. The differences are due to differentplotting utilities, plot for Matlab and matplot for R.

The following analytical solution to eqs. (1.1) and (1.2) is derived in Appendix 1,

u(x, t) = 1− 2∞∑i=1

1

λie−(λ

2i /L

2)t sin(λix/L) (1.3a)

with the eigenvaluesλi = (i− 1/2)π, i = 1, 2, ... (1.3b)

The approach to the steady state (equilibrium) solution u(x, t → ∞) = 1 (from eq.(1.3a)) is clear in Figs. 1.1 and 1.2. A comparison of the analytical and numericalsolutions follows.

R (Table 1.4)

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.87036

0.45 0.40 0.75342

0.45 0.60 0.66064

0.45 0.80 0.60107

0.45 1.00 0.58055

25

Page 26: Method of Lines.pdf

Analytical solution

(eqs. (1.3))

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.87036

0.45 0.40 0.75342

0.45 0.60 0.66064

0.45 0.80 0.60107

0.45 1.00 0.58055

Table 1.6: Comparison of the numerical and analytical solutions

The agreement in Table 1.6 is to five figures, which is perhaps unexpected with only 26points in x (26 ODEs). An R routine for the calculation of the analytical solution valuesin Table 1.6 is listed in the Appendix.

For ncase=2,3 in Listings 1.1 and 1.2 (for D = u,D = eu as programmed in List-ings 1.1), analytical soltions are not readily available for comparison with the numericalsolutions. These cases illustrate the ease of computing the solution to a nonlinear PDEwhen an analytical solution may not be available.

Abbreviated numerical output from Listing 1.2b for ncase=2,3 is given in Tables 1.7,1.8.

t x u(x,t)

0.00 0.00 0.00000

0.00 0.20 0.00000

0.00 0.40 0.00000

0.00 0.60 0.00000

0.00 0.80 0.00000

0.00 1.00 0.00000

.

.

(output for t = 0.15

to 0.30 removed)

.

.

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.85877

0.45 0.40 0.69904

0.45 0.60 0.51985

0.45 0.80 0.32228

0.45 1.00 0.17349

.

26

Page 27: Method of Lines.pdf

.

(output for t = 0.60

to 1.35 removed)

.

.

t x u(x,t)

1.50 0.00 1.00000

1.50 0.20 0.97933

1.50 0.40 0.96021

1.50 0.60 0.94467

1.50 0.80 0.93450

1.50 1.00 0.93096

ncall = 584

Table 1.7: Output from Listings 1.1b, 1.2b for ncase=2 (D = u)

We note in particular that u(x, t) has not approached the equilibrium solution u(x, t→∞) = 1 at t = 1.50 as closely for ncase=2 as for ncase=1 in Table 1.4. This is as expectedsince for ncase=2 and D = u, 0 ≤ u ≤ 1 which gives 0 ≤ D ≤ 1, while for ncase=1,D = 1. Thus, for ncase=2, the diffusivity is lower and therefore the rate of diffusion islower. This results in a slower response to u(x, t) = 1. This slower response is evidentfrom comparing Figs. 1.2 and 1.3.

Also, the number of calls to pde 1 increased from 230 for ncase=1 to 584 for ncase=2.This increase can be attributed to the greater computational effort for the integrationof the 26 nonlinear ODEs (ncase=2) than is required for the linear ODEs of case=1. Inparticular, the Newton solver in lsodes requires more iterations for convergence in thenonlinear case.

t x u(x,t)

0.00 0.00 0.00000

0.00 0.20 0.00000

0.00 0.40 0.00000

0.00 0.60 0.00000

0.00 0.80 0.00000

0.00 1.00 0.00000

.

.

(output for t = 0.15

to 0.30 removed)

.

.

t x u(x,t)

0.45 0.00 1.00000

0.45 0.20 0.96808

27

Page 28: Method of Lines.pdf

0.45 0.40 0.93819

0.45 0.60 0.91359

0.45 0.80 0.89734

0.45 1.00 0.89165

.

.

(output for t = 0.60

to 1.35 removed)

.

.

t x u(x,t)

1.50 0.00 1.00000

1.50 0.20 0.99997

1.50 0.40 0.99994

1.50 0.60 0.99992

1.50 0.80 0.99990

1.50 1.00 0.99990

ncall = 423

Table 1.8: Output from Listings 1.1b, 1.2b for ncase=3 (D = eu)

We note in particular that u(x, t) has approached the equilibrium solution u(x, t→∞) =1 at t = 1.50 more closely for ncase=3 than for ncase=1 in Table 1.4. This is as expectedsince for ncase=3 and D = eu, 0 ≤ u ≤ 1 which gives 1 ≤ D ≤ e, while for ncase=1,D = 1. Thus, for ncase=3, the diffusivity is higher and therefore the rate of diffusion ishigher. This results in a faster response to u(x, t) = 1. This faster response is evidentfrom comparing Figs. 1.2 and 1.4.

Also, the number of calls to pde 1 increased from 230 for ncase=1 to 423 for ncase=3.Again, this increase can be attributed to the greater computational effort for the integra-tion of the 26 nonlinear ODEs (ncase=3) than is required for the linear ODEs of case=1.In particular, the Newton solver in lsodes requires more iterations for convergence inthe nonlinear case.

(1.2) Example No. 2

The PDE considered as the second example is the following nonlinear form of the diffusionequation.

∂u

∂t=∂u

∂x

∂2u

∂x2(1.5a)

The dependent variable, u(x, t), and the independent variables, x and t, are explained inTable 1.1.

Eq. (1.5) is first order in t and second order in x so it requires one IC and two BCs.

28

Page 29: Method of Lines.pdf

u(x, t = 0) = f(x) (1.6a)

∂u(xl, t)

∂x=∂u(xu, t)

∂x= 0 (1.6b,c)

Eqs (1.6b,c) require some additional discussion.

• BCs (1.6b,c) are Neumann since the derivative ∂u/∂x is specified at the boundaries,x = xl, xu. However, this derivative is also a nonlinear coefficient in eq. (1.5a) sothat at the boundaries,

∂u(x = xl, t)

∂t=∂u(x = xu, t)

∂t= 0 (1.6d,e)

• Eqs. (1.6d,e) in effect specify Dirichlet BCs since they can be integrated to give

u(x = xl, t) = cl; u(x = xu, t) = cu; (1.6f,g)

where cl, cu are specified constants. In other words, the dependent variable u(x, t)is specified at the boundaries corresponding to Dirichlet BCs.

• This conclusion raises the question of how the BCs in the MOL analysis should beimplemented (coded). There are two possibilities:

– The BCs can be implemented without using the PDE. For example, this isusually done with Dirichlet BCs (just the dependent variable at the boundariesis used).

– The PDE can be used at the boundaries with the BCs included in the approxi-mation of the PDE. For example, this is usually done with Neumann and thirdtype BCs. We now consider this second approach for BCs (1.6f,g) with f(x) ineq. (1.6a) taken as a unit step.

• Since BCs (1.6f,g) are used, BCs (1.6b,c) are not actually observed in the solution.This apparent contradiction results from the nonlinear coefficient of eq. (1.5a). Todemonstrate this, a solution is also computed for the linear diffusion equation

∂u/∂t = ∂2u/∂x2 (1.5b)

for which BCs (1.6b,c) are clearly observed.

The Matlab programming of eqs. (1.5) and (1.6) is in Listing 1.3a that follows.

29

Page 30: Method of Lines.pdf

(1.2.1) ODE routine in Matlab

The following programming of eqs. (1.5) and (1.6) is analogous to Listing 1.1a.

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

%

% BCs (Neumann)

ux(1) =0;

ux(nx)=0;

nl=2;nu=2;

%

% uxx

if (ndss== 2) uxx=dss042(xl,xu,nx,u,ux,nl,nu); % second order

elseif(ndss== 4) uxx=dss044(xl,xu,nx,u,ux,nl,nu); % fourth order

elseif(ndss== 6) uxx=dss046(xl,xu,nx,u,ux,nl,nu); % sixth order

elseif(ndss== 8) uxx=dss048(xl,xu,nx,u,ux,nl,nu); % eighth order

elseif(ndss==10) uxx=dss050(xl,xu,nx,u,ux,nl,nu); % tenth order

end

%

% PDE

for(i=1:nx)

if(ncase==1)ut(i)=ux(i)^2*uxx(i);end

if(ncase==2)ut(i)=uxx(i);end

end

ut=ut’;

%

% Increment calls to pde_1

ncall=ncall+1;

Listing 1.3a: ODE Matlab routine for eqs. (1.5) and (1.6)

We can note the following details about Listing 1.3a.

• The function and a global area are defined.

30

Page 31: Method of Lines.pdf

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

• The first derivative in eq. (1.5a), ∂u/∂x, is computed by a call to one of five firstderivative routines, dss002 to dss010, as selected by ndss set in the main programto follow. For the purpose of the subsequent calculations, ndss=4. Also, nx=21 andxl=0,xu=1 are defined in the main program. Note that these parameters are passedas global to pde 1.

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

• BCs (1.6b,c) are implemented.

%

% BCs (Neumann)

ux(1) =0;

ux(nx)=0;

nl=2;nu=2;

nl=nu=2 specifies Neumann BCs to the second derivative routines (nl=nu=1 specifyDirichlet BCs).

• The second derivative in eqs. (1.5), ∂2u/∂x2, is computed by a call to one of fivesecond derivative routines, dss042 to dss050 (again ndss=4 is used).

%

% uxx

if (ndss== 2) uxx=dss042(xl,xu,nx,u,ux,nl,nu); % second order

elseif(ndss== 4) uxx=dss044(xl,xu,nx,u,ux,nl,nu); % fourth order

elseif(ndss== 6) uxx=dss046(xl,xu,nx,u,ux,nl,nu); % sixth order

elseif(ndss== 8) uxx=dss048(xl,xu,nx,u,ux,nl,nu); % eighth order

elseif(ndss==10) uxx=dss050(xl,xu,nx,u,ux,nl,nu); % tenth order

end

• The PDE is programmed to give a derivative vector, ut, corresponding to the LHSof eqs. (1.5) according to ncase set in the main program. For ncase=1, eq. (1.5a)is used. For ncase=2, eq. (1.5b) is used.

31

Page 32: Method of Lines.pdf

%

% PDE

for(i=1:nx)

if(ncase==1)ut(i)=ux(i)^2*uxx(i);end

if(ncase==2)ut(i)=uxx(i);end

end

ut=ut’

The close resemblance of this coding to eqs. (1.5) is clear. A transpose then producesa column derivative vector as required by the ODE integrator ode15s.

• Finally, the counter for the calls to pde 1 is incremented (and returned to the mainprogram as part of the output).

%

% Increment calls to pde_1

ncall=ncall+1;

We next consider the corresponding routine in R for the nx=21 ODEs.

(1.2.2) ODE routine in R

The ODE routine in R for eqs. (1.5) is in Listing 1.3b.

pde_1=function(t,u,parms){

#

# ux

if (ndss== 2){ux=dss002(xl,xu,nx,u); # second order

}else if(ndss== 4){ux=dss004(xl,xu,nx,u); # fourth order

}else if(ndss== 6){ux=dss006(xl,xu,nx,u); # sixth order

}else if(ndss== 8){ux=dss008(xl,xu,nx,u); # eighth order

}else if(ndss==10){ux=dss010(xl,xu,nx,u); # tenth order

}

#

# BCs (Neumann)

ux[1] =0;

ux[nx]=0;

nl=2;nu=2;

#

# uxx

if (ndss== 2){uxx=dss042(xl,xu,nx,u,ux,nl,nu); # second order

}else if(ndss== 4){uxx=dss044(xl,xu,nx,u,ux,nl,nu); # fourth order

}else if(ndss== 6){uxx=dss046(xl,xu,nx,u,ux,nl,nu); # sixth order

}else if(ndss== 8){uxx=dss048(xl,xu,nx,u,ux,nl,nu); # eighth order

}else if(ndss==10){uxx=dss050(xl,xu,nx,u,ux,nl,nu); # tenth order

32

Page 33: Method of Lines.pdf

}

#

# PDE

ut=rep(0,nx);

for(i in 1:nx){

if(ncase==1){ut[i]=ux[i]^2*uxx[i];}

if(ncase==2){ut[i]=uxx[i];}

}

#

# Increment calls to pde_1

ncall <<- ncall+1;

#

# Return derivative vector

return(list(c(ut)));

}

Listing 1.3b: ODE R routine for eqs. (1.5) and (1.6)

We can now make a comparsion of Listings 1.3a and 1.3b.

• Each routine starts with a function definition.

Matlab

function ut=pde_1(t,u)

%

% Parameters

global xl xu nx ncall ndss ncase

R

pde_1=function(t,u,parms){

In the case of Matlab, a global area is defined. For R, variables and parametersdefined in the superior routine, in this case, the main program, are automaticallyavailable to the subordinate routine.

• The calculation of the first derivative ∂u/∂x is similar in the two systems.

Matlab

%

% ux

if (ndss== 2) ux=dss002(xl,xu,nx,u); % second order

elseif(ndss== 4) ux=dss004(xl,xu,nx,u); % fourth order

33

Page 34: Method of Lines.pdf

elseif(ndss== 6) ux=dss006(xl,xu,nx,u); % sixth order

elseif(ndss== 8) ux=dss008(xl,xu,nx,u); % eighth order

elseif(ndss==10) ux=dss010(xl,xu,nx,u); % tenth order

end

R

#

# ux

if (ndss== 2){ux=dss002(xl,xu,nx,u); # second order

}else if(ndss== 4){ux=dss004(xl,xu,nx,u); # fourth order

}else if(ndss== 6){ux=dss006(xl,xu,nx,u); # sixth order

}else if(ndss== 8){ux=dss008(xl,xu,nx,u); # eighth order

}else if(ndss==10){ux=dss010(xl,xu,nx,u); # tenth order

}

The if - else if - else syntax is slightly different, including the use of end inMatlab and } in R.

• The programming of BCs (1.6b,c) is similar.

Matlab

%

% BCs (Neumann)

ux(1) =0;

ux(nx)=0;

nl=2;nu=2;

R

#

# BCs (Neumann)

ux[1] =0;

ux[nx]=0;

nl=2;nu=2;

• The programming of the second derivative in eqs. (1.5) is similar to the programmingof the first derivative.

Matlab

%

34

Page 35: Method of Lines.pdf

% uxx

if (ndss== 2) uxx=dss042(xl,xu,nx,u,ux,nl,nu); % second order

elseif(ndss== 4) uxx=dss044(xl,xu,nx,u,ux,nl,nu); % fourth order

elseif(ndss== 6) uxx=dss046(xl,xu,nx,u,ux,nl,nu); % sixth order

elseif(ndss== 8) uxx=dss048(xl,xu,nx,u,ux,nl,nu); % eighth order

elseif(ndss==10) uxx=dss050(xl,xu,nx,u,ux,nl,nu); % tenth order

end

R

%

% uxx

if (ndss== 2) uxx=dss042(xl,xu,nx,u,ux,nl,nu); % second order

elseif(ndss== 4) uxx=dss044(xl,xu,nx,u,ux,nl,nu); % fourth order

elseif(ndss== 6) uxx=dss046(xl,xu,nx,u,ux,nl,nu); % sixth order

elseif(ndss== 8) uxx=dss048(xl,xu,nx,u,ux,nl,nu); % eighth order

elseif(ndss==10) uxx=dss050(xl,xu,nx,u,ux,nl,nu); % tenth order

end

The calculation of the second derivative uxx is by one of five second derivativeroutines, dss042 to dss050.

• The programming of eqs. (1.5) is similar in the two systems.

Matlab

%

% PDE

for(i=1:nx)

if(ncase==1)ut(i)=ux(i)^2*uxx(i);end

if(ncase==2)ut(i)=uxx(i);end

end

ut=ut’;

R

#

# PDE

ut=rep(0,nx);

for(i in 1:nx){

if(ncase==1){ut[i]=ux[i]^2*uxx[i];}

if(ncase==2){ut[i]=uxx[i];}

}

35

Page 36: Method of Lines.pdf

The result is the derivative vector ut (the LHS of eqs. (1.5)). For Matlab, thisvector is dynamically allocated as it is formed. For R, this vector is first declared(preallocated) with rep.

• The number of calls to the ODE routine is incremented. For Matlab, the new valueof ncall is returned to the main program through the global area. For R this valueis returned with <<-.

Matlab

%

% Increment calls to pde_1

ncall=ncall+1;

R

#

# Increment calls to pde_1

ncall <<- ncall+1;

• In Matlab, the computed derivative vector, ut, is returned through the LHS argu-ment of pde 1. In R, the derivative vector is returned as a list. Note also the useof the R vector utility c. That is, ut is declared a vector, then a list, and finally isreturned. This format for the return is required by the R ODE integrators in thelibrary deSolve [4] (including lsodes used in the R main program discussed next).

Matlab

ut returned as the LHS argument

R

#

# Return derivative vector

return(list(c(ut)));

}

The final } concludes pde 1 in R.

This completes the programming of the ODE routine in R. The main programs thatcalled the ODE routines are considered next.

36

Page 37: Method of Lines.pdf

(1.2.3) Main program in Matlab

The main program in Matlab that calls the ODE routine pde 1 in Listing 1.3a is inListing 1.4a.

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

%

% Select case

%

% ncase = 1 - nonlinear

%

% ncase = 2 - linear

%

ncase=1;

%

% Grid in x, initial condition

xl=0;xu=1;nx=21;

for(ix=1:nx)

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

if(ix==11)u0(ix)=0.5;end

if(ix< 11)u0(ix)=0; end

if(ix> 11)u0(ix)=1; end

end

%

% Independent variable for ODEs

t0=0;nout=11;

if(ncase==1)tf=0.1;end

if(ncase==2)tf=0.5;end

tout=linspace(t0,tf,nout);

ncall=0;

%

% ODE integration

ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

%

% Display selected output

for(it=1:nout)

fprintf(’\n t x u(x,t)\n’);

for(ix=1:nx)

37

Page 38: Method of Lines.pdf

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)...

*(ix-21)==0)

fprintf(’%7.2f%8.2f%12.5f\n’,...

t(it),xg(ix),u(it,ix));

end

end

end

fprintf(’\n ncall = %4d\n’,ncall);

%

% Plot numerical solution

if(ncase==1)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.01,...,0.1’);

end

if(ncase==2)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.05,...,0.5’);

end

Listing 1.4a: Main program that calls pde 1 of Listing 1.3a

We can note the following details about Listing 1.4a.

• Previous files are cleared and a global area is defined.

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

The global area provides for a sharing of variables and parameters between routines,in this case, between the main program of Listing 1.4a and the ODE routine ofListing 1.3a.

• The selection of eq. (1.5a) and (1.5b) is according to ncase.

%

% Select case

%

% ncase = 1 - nonlinear

%

38

Page 39: Method of Lines.pdf

% ncase = 2 - linear

%

ncase=1;

• A grid in x of 21 points is defined for 0 ≤ x ≤ 1 with a uniform interval

(xu-xl)/(nx-1)=(1-0)/(21-1)=0.05

The grid point values are then placed in array xg and the IC vector of eq. (1.6a) isplaced in array u0.

%

% Grid in x, initial condition

xl=0;xu=1;nx=21;

for(ix=1:nx)

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

if(ix==11)u0(ix)=0.5;end

if(ix< 11)u0(ix)=0; end

if(ix> 11)u0(ix)=1; end

end

We have used a unit step for f(x) in eq. (1.6a). Since this is a finite discontinuity,we can only approximate it numerically, in this case as a unit ramp with the valuesf(x = 0.45) = 0, f(x = 0.5) = 0.5, f(x = 0.55) = 1 (that will be clear in thegraphical output to follow).

• The vector of output values of t, tout, is defined by the Matlab utility linspace.Since the nonlinear PDE, eq. (1.5a), responds more rapidly in t than the linearPDE, eq. (1.5b), it has a shorter t scale, 0 ≤ t ≤ 0.1, while eq. (1.5b) has the tscale, 0 ≤ t ≤ 0.5. This difference in t scales was determined by trial and error (andobserving the plotted solutions).

for 0 ≤ t ≤ 1 with 11 values (including at t = 0).

%

% Independent variable for ODEs

t0=0;nout=11;

if(ncase==1)tf=0.1;end

if(ncase==2)tf=0.5;end

tout=linspace(t0,tf,nout);

ncall=0;

Finally, the counter for the calls to pde 1 in Listing 1.1a is initialized.

• The 21 ODEs are integrated by a call to ode15s, a stiff integrator with a variety ofoptions. In this, only the default options are used.

39

Page 40: Method of Lines.pdf

%

% ODE integration

ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

ndss = 4 selects dss004,dss044 in Listing 1.3a. ndss is passed to pde 1 as a globalparameter.

In the call to ode15s, note the use of pde 1 in Listing 1.3a, the vector of outputvalues of t, tout, and the IC vector u0. Generally, the order of these inputs mustbe maintained as used here when calling ode15s. The solution is returned as t, avector of values of t in tout, and u, a 2D array with dimensions u(11,21) for the11 values of t and the 21 corresponding ODE solutions.

• The numerical ODE solution is displayed with fprintf as a function of t (with thefor in it) and x (with the for in ix). To keep the volume of output to a manageablelevel, the solution is displayed at x = 0, 0.25, 0.5, 0.75, 1 (ix=1,6,11,16,21). Ratherthan using the if, this selected output could also be produced with for ix=1:5:nx

but the if was used for a more direct comparison with the R programming in Listing1.4b. Also, the if has the advantage that any values of x can be selected for theoutput (through the choice of the values of ix).

%

% Display selected output

for(it=1:nout)

fprintf(’\n t x u(x,t)\n’);

for(ix=1:nx)

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)...

*(ix-21)==0)

fprintf(’%7.2f%8.2f%12.5f\n’,...

t(it),xg(ix),u(it,ix));

end

end

end

fprintf(’\n ncall = %4d\n’,ncall);

Finally, the number of calls to pde 1 of Listing 1.3a is displayed as an indicationof the effort required to compute a solution. Note that in general Matlab defines acharacter string with a single quote (’) while this is done with a double quote (")in R.

• A plot of u(x, t) against x is produced with a single call to the Matlab utility plot

which matches the dimension of xg (21) with the second dimension of u (also 21)as defined by the ODE integrator, ode15s. In this way, t becomes a parameter inthe plotting with a series of nout=11 curves through the first dimension of u (11).

40

Page 41: Method of Lines.pdf

%

% Plot numerical solution

if(ncase==1)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.01,...,0.1’);

end

if(ncase==2)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.05,...,0.5’);

end

Labeling of the horizontal and vertical axes, and a title for the plot are also added.The difference in the t interval for ncase=1,2 is included in the title.

This completes the programming of eqs. (1.5) and (1.6). The only remaining re-quirement is the programming of IC (1.6a) which is in the main program discussedsubsequently.

(1.2.4) Main program in R

The main program in R is considered next with a comparison of equivalent statementsfrom Listing 1.4a.

#

# Access ODE integrator

library("deSolve");

#

# Access functions

setwd("c:/R/pde_intro");

source("pde_1.R");

source("dss004.R");

source("dss044.R");

#

# Select case

#

# ncase = 1 - nonlinear

#

# ncase = 2 - linear

#

ncase=1;

#

# Grid in x, initial condition

xl=0;xu=1;nx=21;

41

Page 42: Method of Lines.pdf

xg=rep(0,nx);u0=rep(0,nx);

for(ix in 1:nx){

xg[ix]=(xu-xl)*(ix-1)/(nx-1);

if(ix==11){u0[ix]=0.5;}

if(ix< 11){u0[ix]=0;}

if(ix> 11){u0[ix]=1;}

}

#

# Independent variable for ODEs

t0=0;nout=11;

if(ncase==1){tf=0.1;}

if(ncase==2){tf=0.5;}

tout=seq(from=0,to=tf,by=(tf-t0)/(nout-1));

ncall=0;

#

# ODE integration

ndss=4;

out=lsodes(y=u0,times=tout,func=pde_1,parms=NULL)

#

# Display selected output

for(it in 1:nout){

cat(sprintf("\n t x u(x,t)\n"));

for(ix in 1:nx){

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)

*(ix-21)==0){

cat(sprintf("%7.2f%8.2f%12.5f\n",

out[it,1],xg[ix],out[it,ix+1]));

}

}

}

cat(sprintf("\n ncall = %4d\n",ncall));

#

# Plot numerical solution

par(mfrow=c(1,1));

if(ncase==1){

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.01,...,0.1",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.01,...,0.1;",lty=1,lwd=2);

}

if(ncase==2){

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.05,...,0.5",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.05,...,0.5;",lty=1,lwd=2);

}

42

Page 43: Method of Lines.pdf

Listing 1.4b: Main program that calls pde 1 of Listing 1.3b

We can note the following details of Listing 1.4b with a comparison of the equivalentMatlab statements in Listing 1.4a.

• Some preliminaries are first programmed.

Matlab

%

% Clear previous files

clear all

clc

%

% Parameters shared with the ODE routine

global xl xu nx ncall ndss ncase

R

#

# Access ODE integrator

library("deSolve");

#

# Access functions

setwd("c:/R/pde_intro");

source("pde_1.R");

source("dss004.R");

source("dss044.R");

The Matlab statements have already been considered (in Section (1.2.3)). The Rstatements access the ODE library deSolve followed by the ODE routine pde 1

of Listing 1.1b and the library differentiators dss004,dss044. The setwd utility(set working directory) specifies the location of these two files (note the use of theforward slash, /, rather than the usual backslash \).

• The selection of ncase=1,2 is the same in Matlab and R.

• The grid in x has 21 points for 0 ≤ x ≤ 1. The IC is an approximation to a unitstep as discussed previously in Section 1.2.3.

Matlab

%

% Grid in x, initial condition

xl=0;xu=1;nx=21;

43

Page 44: Method of Lines.pdf

for(ix=1:nx)

xg(ix)=(xu-xl)*(ix-1)/(nx-1);

if(ix==11)u0(ix)=0.5;end

if(ix< 11)u0(ix)=0; end

if(ix> 11)u0(ix)=1; end

end

R

#

# Grid in x, initial condition

xl=0;xu=1;nx=21;

xg=rep(0,nx);u0=rep(0,nx);

for(ix in 1:nx){

xg[ix]=(xu-xl)*(ix-1)/(nx-1);

if(ix==11){u0[ix]=0.5;}

if(ix< 11){u0[ix]=0;}

if(ix> 11){u0[ix]=1;}

}

For the R code, the grid in x xg and the IC array u0 are first declared (preallocated)with a rep. Alternative R coding for the grid can be considered, e.g.,

xg=seq(from=xl,to=xu,by=(xu-xl)*(ix-1)/(nx-1))

based on the R utility seq.

• The output points in t are specified.

Matlab

%

% Independent variable for ODEs

t0=0;nout=11;

if(ncase==1)tf=0.1;end

if(ncase==2)tf=0.5;end

tout=linspace(t0,tf,nout);

ncall=0;

R

#

# Independent variable for ODEs

44

Page 45: Method of Lines.pdf

t0=0;nout=11;

if(ncase==1){tf=0.1;}

if(ncase==2){tf=0.5;}

tout=seq(from=0,to=tf,by=(tf-t0)/(nout-1));

ncall=0;

Note the use of seq for R in place of linspace for Matlab.

• The ODE integration in R is by lsodes ([4]).

Matlab

%

% ODE integration

ndss=4;

[t,u]=ode15s(@pde_1,tout,u0);

R

#

# ODE integration

ndss=4;

out=lsodes(y=u0,times=tout,func=pde_1,parms=NULL)

The @ in the first RHS argument of ode15s indicates this argument is a function(pde 1 of Listing 1.3a) rather than a parameter or variable. The solution arrayfrom lsodes, out ([4]), contains both the 11 values of t and the 21 ODE solutions.Therefore, the dimensions of out are out(11,22). The second dimension in out is22 rather than 21 to include t as explained next. Note the use of the IC vector,u0, the vector of output values of t, tout, and the ODE routine, pde 1 in Listing1.3b. The argument parms (to pass parameters to the ODE routine) is unused.y,times,func,parms are reserved names for lsodes.

An alternative for the ODE integration is

ode(y=u0,times=tout,func=pde_1,parms=NULL)

The R utility ode uses the integrator lsoda as the default. lsoda switches betweena nonstiff and stiff integrator depending on an eigenvalue analysis (the “a” standsfor automatic).

lsoda and lsodes have a variety of options as explained in the R documentation.Here we have used the defaults for lsodes.

• The numerical solution is displayed in a similar way for Matlab and R.

45

Page 46: Method of Lines.pdf

Matlab

%

% Display selected output

for(it=1:nout)

fprintf(’\n t x u(x,t)\n’);

for(ix=1:nx)

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)...

*(ix-21)==0)

fprintf(’%7.2f%8.2f%12.5f\n’,...

t(it),xg(ix),u(it,ix));

end

end

end

fprintf(’\n ncall = %4d\n’,ncall);

R

#

# Display selected output

for(it in 1:nout){

cat(sprintf("\n t x u(x,t)\n"));

for(ix in 1:nx){

if((ix-1)*(ix- 6)*(ix-11)*(ix-16)

*(ix-21)==0){

cat(sprintf("%7.2f%8.2f%12.5f\n",

out[it,1],xg[ix],out[it,ix+1]));

}

}

}

cat(sprintf("\n ncall = %4d\n",ncall));

Note the offset of 1 in the second subscript of out, i.e., ix+1, in the R coding sinceout[it,1] references t. Thus, out[it,22] references u(x = 1, t). Also, the outputis for every fourth value of x, that is, x = 0, 0.25, ..., 1 through the use of the if

in ix. A comparsion of the Matlab and R numerical output is given subsequently.The final value of the number of calls to pde 1 in Listing 1.3b is displayed.

• The plotting in R is by the matplot utility. A 1× 1 array of plots, that is, a singleplot, is specified with par(mfrow=c(1,1)).

(start)

Matlab

46

Page 47: Method of Lines.pdf

%

% Plot numerical solution

if(ncase==1)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.01,...,0.1’);

end

if(ncase==2)

plot(xg,u);

xlabel(’x’);ylabel(’u(x,t)’);

title(’u(x,t) vs x, t = 0,0.05,...,0.5’);

end

R

#

# Plot numerical solution

par(mfrow=c(1,1));

if(ncase==1){

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.01,...,0.1",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.01,...,0.1;",lty=1,lwd=2);

}

if(ncase==2){

matplot(x=xg,y=t(out[,-1]),type="l",xlab="x",

ylab="u(x,t) vs x, t=0,0.05,...,0.5",xlim=c(xl,xu),

main="u(x,t) vs x, t=0,0.05,...,0.5;",lty=1,lwd=2);

}

The labeling of the plots in Matlab and R is similar. matplot has additional param-eters for specifying the line type and the horizontal axis limits. The vertical axis isscaled as a default (ylim is also available).

This concludes the R programming for eqs. (1.5) and (1.6). The output from Listings 1.3aand 1.4a for Matlab, and Listings 1.3b and 1.4b for R, is now discussed and compared.

(1.2.5) Comparison of Matlab and R output

Abbreviated output from Listings 1.3a and 1.4a is given in Table 1.9.

t x u(x,t)

0.00 0.00 0.00000

0.00 0.25 0.00000

0.00 0.50 0.50000

47

Page 48: Method of Lines.pdf

0.00 0.75 1.00000

0.00 1.00 1.00000

t x u(x,t)

0.01 0.00 0.00000

0.01 0.25 0.09146

0.01 0.50 0.49995

0.01 0.75 0.90848

0.01 1.00 1.00000

. .

. .

. .

Output from t = 0.02 to 0.08

removed

. .

. .

. .

t x u(x,t)

0.09 0.00 0.00000

0.09 0.25 0.24177

0.09 0.50 0.49998

0.09 0.75 0.75819

0.09 1.00 1.00000

t x u(x,t)

0.10 0.00 0.00000

0.10 0.25 0.24444

0.10 0.50 0.49998

0.10 0.75 0.75553

0.10 1.00 1.00000

ncall = 440

Table 1.9: Output from Listings, 1.3a 1.4a for ncase=1 (nonlinear PDE)

We can note the following details about this output.

• IC (1.6a) (t = 0) is correct (as programmed in Listing 1.4a). Checking the IC isalways a good idea since if it is incorrect, the remainder of the numerical outputwill also be incorrect.

• The solution approaches

u(x, t→∞) = x (1.7a)

48

Page 49: Method of Lines.pdf

as reflected in the output for t = 0.1. This steady state solution clearly satisfieseq. (1.5a). It does not satisfy Neumann BCs (1.16b,c) directly, but rather, satisfiesDirichlet BCs (1.6f,g) (with cl = 0, cu = 1) as discussed previously. This final linearsolution is also reflected in Figs. 1.5 and 1.7.

• The computational effort is modest with ncall = 440.

Abbreviated output from Listings 1.3b and 1.4b is given in Table 1.10

t x u(x,t)

0.00 0.00 0.00000

0.00 0.25 0.00000

0.00 0.50 0.50000

0.00 0.75 1.00000

0.00 1.00 1.00000

t x u(x,t)

0.01 0.00 0.00000

0.01 0.25 0.09148

0.01 0.50 0.50000

0.01 0.75 0.90852

0.01 1.00 1.00000

. .

. .

. .

Output from t = 0.02 to 0.08

removed

. .

. .

. .

t x u(x,t)

0.09 0.00 0.00000

0.09 0.25 0.24180

0.09 0.50 0.50000

0.09 0.75 0.75820

0.09 1.00 1.00000

t x u(x,t)

0.10 0.00 0.00000

0.10 0.25 0.24447

0.10 0.50 0.50000

0.10 0.75 0.75553

0.10 1.00 1.00000

ncall = 350

49

Page 50: Method of Lines.pdf

Table 1.10: Output from Listings 1.3b 1.4b for ncase=1 (nonlinear PDE)

Again, the details for Table 1.9 apply to Table 1.10. The computational effort is modestwith ncall = 350.

The two solutions in Tables 1.9, 1.10 are compared in Table 1.11 at t = 0.1.

Matlab (Table 1.9)

t x u(x,t)

0.10 0.00 0.00000

0.10 0.25 0.24444

0.10 0.50 0.49998

0.10 0.75 0.75553

0.10 1.00 1.00000

R (Table 1.10)

t x u(x,t)

0.10 0.00 0.00000

0.10 0.25 0.24447

0.10 0.50 0.50000

0.10 0.75 0.75553

0.10 1.00 1.00000

Table 1.11: Comparison of the numerical solutions for ncase=1 at t = 0.1

The two solutions agree to four figures. The graphical output is in Figs. 1.5 (Matlab)and 1.7 (R). The approach to the solution of eq. (1.7a) is clear (and also confirmed bythe t = 0.1 output in Tables 1.9 and 1.10).

For ncase=2, the linear PDE, eq. (1.5b), is integrated by Matlab (Listings 1.3a, 1.4a)and R (Listings 1.3b, 1.4b), to give the following numerical output.

t x u(x,t)

0.00 0.00 0.00000

0.00 0.25 0.00000

0.00 0.50 0.50000

0.00 0.75 1.00000

0.00 1.00 1.00000

t x u(x,t)

0.05 0.00 0.11463

0.05 0.25 0.22397

0.05 0.50 0.50000

0.05 0.75 0.77603

0.05 1.00 0.88537

50

Page 51: Method of Lines.pdf

t x u(x,t)

0.10 0.00 0.26324

0.10 0.25 0.33252

0.10 0.50 0.50000

0.10 0.75 0.66748

0.10 1.00 0.73676

. .

. .

. .

Output from t = 0.15 to 0.45

removed

. .

. .

. .

t x u(x,t)

0.50 0.00 0.49535

0.50 0.25 0.49671

0.50 0.50 0.49999

0.50 0.75 0.50327

0.50 1.00 0.50463

ncall = 124

Table 1.12: Output from Listings 1.3a 1.4a for ncase=2 (linear PDE)

The corresponding R output from Listings 13.b and 1.4b follows

t x u(x,t)

0.00 0.00 0.00000

0.00 0.25 0.00000

0.00 0.50 0.50000

0.00 0.75 1.00000

0.00 1.00 1.00000

t x u(x,t)

0.05 0.00 0.11459

0.05 0.25 0.22400

0.05 0.50 0.50000

0.05 0.75 0.77600

0.05 1.00 0.88541

t x u(x,t)

0.10 0.00 0.26324

0.10 0.25 0.33254

0.10 0.50 0.50000

51

Page 52: Method of Lines.pdf

0.10 0.75 0.66746

0.10 1.00 0.73676

. .

. .

. .

Output from t = 0.15 to 0.45

removed

. .

. .

. .

t x u(x,t)

0.50 0.00 0.49543

0.50 0.25 0.49677

0.50 0.50 0.50000

0.50 0.75 0.50323

0.50 1.00 0.50457

ncall = 188

Table 1.13: Output from Listings 1.3b 1.4b for ncase=2 (linear PDE)

The computational effort is quite modest with ncall = 124,188. The two solutions arecompared in Table 1.14.

Matlab (Table 1.12)

t x u(x,t)

0.10 0.00 0.26324

0.10 0.25 0.33252

0.10 0.50 0.50000

0.10 0.75 0.66748

0.10 1.00 0.73676

R (Table 1.13)

t x u(x,t)

0.10 0.00 0.26324

0.10 0.25 0.33254

0.10 0.50 0.50000

0.10 0.75 0.66746

0.10 1.00 0.73676

Table 1.14: Comparison of the numerical solutions for ncase=2 at t = 0.1

52

Page 53: Method of Lines.pdf

Table 1.14 indicates the solutions agree to four figures at t = 0.1 where they are changingrapidly. The graphical output for ncase=1 is given in Fig. 1.6 (Matlab) and Fig. 1.8(R). The approach to the steady state solution

u(x, t→∞) = 0.5 (1.7b)

is clear (and also confirmed by the t = 0.5 output in Tables 1.12 and 1.13).

(1.3) Conclusions

The preceding discussion of the PDE numerical methods for eqs. (1.1) and (1.2) (exampleno. 1) and for eqs. (1.5) and (1.6) (example no.), and the associated Matlab and R coding,indicated the following points.

• The prgramming of examples nos. 1 and 2, for the linear and nonlinear cases, isstraighforward. Also the programming in Matlab and R has a similar format.

• The graphical output from Matlab and R is similar in the essential details. Differ-ences are due to the plotting routines plot and matplot.

• The numerical solutions have good accuracy as determined, for example, by com-paring the Matlab and R solutions, and for example no. 1, by comparison of thenumerical and analytical solutions.

• The ODE integrators, ode15s and lsodes, efficiently calculated the ODE numericalsolutions.

The procedures used for the numerical integration of the PDEs in R can be readilyextended to nonlinear systems of PDEs in 1D, 2D and 3D for a variety of BCs. For thisreason, the development of PDE applications in R is expected to continue.

53

Page 54: Method of Lines.pdf

Appendix 1

Derivation of an analytical solution for example no. 1

We consider the derivation of an analytical solution for eqs. (1.1) and (1.2) by twomethods: (1) finite Fourier sine transform and (2) Green’s function.

(1.1a) Analytical solution by finite Fourier sine transform

We consider the analytical solution of eqs. (1.1) and (1.2) by a finite Fourier sine trans-form (FST), which is equivalent to the method of separation of variables. The FSTtransform pair (forward and inverse transforms) is ([3], pp 405-415)

Fx {f(x)} =

∫ L

0

f(x) sin(λix)dx = f(λi) (1.4a)

F−1x

{f(λi)

}=

2

L

∞∑i=1

f(λi) sin(λix) = f(x) (1.4b)

where an overline (overbar) is used to denote a transformed function. The FST is termed“finite” because the interval in x, 0 ≤ x ≤ L, is finite. The consequence of this finiteinterval is that the inversion (by eq. (1.4b)) involves an infinite series (rather than anintegral). This makes the inversion particularly easy, that is, it just requires substitutionof f(λi) in an infinite series (rather than evaluation of an integral).

A table of FST pairs can be developed by application of the forward transform, eq.(1.4a), to a series of functions of x. FST pairs of particular relevance in the solution ofeq. (1.1) and (1.2) follow.

no. f(x) f(λi)

1 0 0

2 11

λi

3d2f(x)

dx2(−1)i+1df(x = L)

dx+λif(x = 0)− λ2i f(λi)

4∂u(x, t)

∂t

du(λi, t)

dt

Table 1.9: Table of FST pairs (λi = (i− 1/2)π/L)

For example, pair (1) follows as

Fx {0} =

∫ L

0

(0) sin(λix)dx = 0

54

Page 55: Method of Lines.pdf

Similarly, for pair (2)

Fx {1} =

∫ L

0

(1) sin(λix)dx = − 1

λicos(λix)|L0 =

1

λi[1− cos(λiL)] =

1

λi

Pair (3) follows from

Fx

{d2f(x)

dx2

}=

∫ L

0

{d2f(x)

dx2

}sin(λix)dx

followed by integration by parts twice ([3], p 408).Pair (4) follows from

Fx

{∂u(x, t)

∂t

}=

∫ L

0

∂u(x, t)

∂tsin(λix)dx =

∂t

[∫ L

0

u(x, t) sin(λix)dx

]=df(λi)

dt

To apply the FST to eqs. (1.1) and (1.2), we multiply eq. (1.1) by sin(λi) and integratefrom x = 0 to x = L.∫ L

0

∂u(x, t)

∂tsin(λix)dx =

∫ L

0

∂2u(x, t)

∂x2sin(λix)dx (1.4c)

Application of the FST’s in Table 1.9 to eq. (1.4c) gives the ODE

du(λi, t)

dt= λi − λ2iu(λi, t) (1.4d)

where we have applied BCs (1.2b) and (1.2c) (this illustrates an advantage of the FST,that is, it naturally includes BCs). From pair (3), f(x = 0) → u(x = 0, t) = 1, df(x =L)/dx→ ∂u(x = L, t)/∂x = 0.

Eq. (1.4d) requires an IC which is the FST of eq. (1.2a).

u(λi, t = 0) = 0 (1.4e)

Eq. (1.4e) follows from pair (1) in Table 1.9.Since 0 ≤ t ≤ ∞ in eq. (1.4d), we can apply a Laplace transform, defined as

Lt {f(t)} =

∫ ∞0

f(t)e−stdt = f(s) (1.4f)

A table of Laplace transform pairs can be developed by application of the forwardtransform, eq. (1.4f), to a series of functions of t. Laplace transform pairs of particularrelevance in the solution of eq. (1.4d) follow.

55

Page 56: Method of Lines.pdf

no. f(t) f(s)

1 0 0

2 11

s

3 e−at1

s+ a

4 f(s)g(s)∫ t0f(τ)g(t− τ)dτ

5df(t)

dtsf(s)− f(t = 0)

Table 1.10: Table of Laplace transform pairs

Pair (2) follows from eq. (1.4f).

Lt {1} =

∫ ∞0

(1)e−stdt = −1

se−st|∞0 =

1

s

Pair (3) follows from eq. (1.4f)

Lt{e−at

}=

∫ ∞0

e−ate−stdt =

∫ ∞0

e−(s+a)tdt = − 1

s+ ae−(s+a)t|∞0 =

1

s+ a

Pair (4) involves the convolution integral∫ t0f(τ)g(t− τ)dτ and is readily available from

tabulated Laplace transform pairs.Pair(5) follows from eq. (1.4f) and integration by parts

Lt

{df(t)

dt

}=

∫ ∞0

df(t)

dte−stdt = f(t)e−st|∞0 − (−s)

∫ ∞0

f(t)e−stdt = −f(t = 0) + sf(s)

For f(t)→ u(λi, t), eq. (1.4f) gives

Lt {u(λi, t)} =

∫ ∞0

u(λi, t)e−stdt = u(λi, s) (1.4g)

Then eq. (1.4d) transforms to

su(λi, s) =λisλ2iu(λi, s)

which follows from the application of pairs (2), (3) and (5) in Table 1.10. Note thatthat eq. (1.1) after two successive integral transforms in x and t (denoted with a doubleoverline) is reduced to an algebraic equation.

56

Page 57: Method of Lines.pdf

Solution for u(λi, s) gives

u(λi, s) =λi

s(s+ λ2i )(1.4h)

Application of transform pair (4) of Table 1.10 to eq. (1.4h) gives

u(λi, t) = L−1t{u(λi, s)

}= λi

∫ t

0

e−λ2i τdτ

=λi−λ2i

e−λ2i τ |t0

=1

λi(1− e−λ2i t) (1.4i)

Eq. (1.4i) can be verified by substitution in eq. (1.4d).

Eq. (1.4d) Eq. (1.4i)

du(λi, t)

dtλie−λ2i t

−λi −λi

λ2iu(λi, t) λi(1− e−λ2i t)

0 0

Table 1.11: Verification of eq. (1.4i) as the solution to eq. (1.4d)

Eq. (1.4i) also satisfies IC (1.4e).Then the inverse FST of eq. (1.4b) gives the solution to eq. (1.1)

u(x, t) = F−1x

{1

λi(1− e−λ2i t)

}

=2

L

∞∑i=1

1

λi(1− e−λ2i t) sin(λix)

= 1− 2

L

∞∑i=1

1

λie−λ

2i t sin(λix) (1.4j)

where we have made use of2

L

∞∑i=1

1

λisin(λix) = 1

that follows from eq. (1.4b) and transform pair (2) of Table 1.9.

57

Page 58: Method of Lines.pdf

Eq. (1.4j) can be confirmed as the solution of eqs. (1.1) and (1.2).

Eq. (1.1) Eq. (1.4j)

∂u

∂tλie−λ2i t

−∂2u

∂x2−λie−λ

2i t

0 0

Table 1.12: Verification of eq. (1.4i) as the solution to eq. (1.4d)

IC (1.2a) is verified as

u(x, t = 0) = 1− 2

L

∞∑i=1

1

λie−λ

2i (0) sin(λix) = 1− 1 = 0

BC (1.2b) is verified as

u(x = 0, t) = 1− 2

L

∞∑i=1

1

λie−λ

2i t sin(λi(0)) = 1− 0 = 1

BC (1.2c) is verified as

∂u(x, t = 0)

∂x= − 2

L

∞∑i=1

e−λ2i t cos(λiL) = 0

This completes the solution of eqs. (1.1) and (1.2) by successive integral transforms.The solution, eq. (1.4j), can also be derived through the use of a Green’s function.

(1.1b) Analytical solution by the Green’s function

An analytical solution to eqs. (1.1) and (1.2) is available through the use of a Green’sfunction ([2], pp 48-49).

u(x, t) = D

∫ t

0

∂ξG(x, ξ, t− τ)|ξ=0dτ (1.5a)

where G(x, ξ, t) is the Green’s function

G(x, ξ, t) =2

L

∞∑n=0

sin

[π(2n+ 1)x

2L

]sin

[π(2n+ 1)ξ

2L

]exp

[−π

2(2n+ 1)2t

4L2

](1.5b)

Substitution of eq. (1.5b) in eq. (1.5a) gives

58

Page 59: Method of Lines.pdf

u(x, t) =2

L

∞∑n=0

sin

[π(2n+ 1)x

2L

] [π(2n+ 1)

2L

]cos

[π(2n+ 1)ξ

2L

]ξ=0

·

∫ t

0

exp

[−π

2(2n+ 1)2(t− τ)

4L2

]dτ

=2

L

∞∑n=0

[π(2n+ 1)

2L

]sin

[π(2n+ 1)x

2L

] ∫ t

0

exp

[−π

2(2n+ 1)2(t− τ)

4L2

]dτ

exp

[−π

2(2n+ 1)2t

4L2

]can be factored out of the integral and the integration in τ com-

pleted.

u(x, t) =2

L

∞∑n=0

[π(2n+ 1)

2L

]sin

[π(2n+ 1)x

2L

]exp

[−π

2(2n+ 1)2

4L2t

[4L2

π2(2n+ 1)2

]exp

[π2(2n+ 1)2

4L2τ

]t0

=2

L

∞∑n=0

[2L

π(2n+ 1)

]sin

[π(2n+ 1)x

2L

]exp

[−π

2(2n+ 1)2

4L2t

]{exp

[π2(2n+ 1)2

4L2t

]− 1

}

= 2∞∑n=0

[1

π(n+ 1/2)

]sin

[π(n+ 1/2)x

L

]{1− exp

[−π

2(n+ 1/2)2

L2t

]}With a change in the summation index n,

u(x, t) = 2∞∑n=1

[1

π(n− 1/2)

]sin

[π(n− 1/2)x

L

]{1− exp

[−π

2(n− 1/2)2

L2t

]}With the application of pair (2) in Table 1.9,

u(x, t) = 1− 2∞∑n=1

[1

π(n− 1/2)

]sin

[π(n− 1/2)x

L

]exp

[−π

2(n− 1/2)2

L2t

]With λi = (n− 1/2)π,

u(x, t) = 1− 2∞∑n=1

(1

λi

)exp

(−λ

2i

L2t

)sin

(λix

L

)(1.5c)

Eqs. (1.4i) and (1.5c) are the same.The numerical evaluation of the analytical solution, eqs. (1.4i) and (1.5c) (and eq.

(1.3)), is accomplished by the following R routine.

59

Page 60: Method of Lines.pdf

#

# Parameters

L=1;

#

# t

t=0.45;

# t=5;

#

# x

x=seq(from=0,to=1,by=0.2);

#

# Heading

cat(sprintf("\n t x u(x,t)\n"));

#

# Step through x

for(ix in 1:6){

#

# Analytical solution u(x,t)

anal=0;

#

# Step through eigenvalues, terms in solution

for(i in 1:5){

#

# Eigenvalue

lam=(i-1/2)*pi;

#

# Next term in series solution

anal=anal+(2/lam)*exp(-(lam^2/L^2)*t)*sin(lam*x[ix]/L);

}

#

# Display analytical solution at x,t

cat(sprintf("%7.2f%8.2f%12.5f\n",t,x[ix],1-anal));

#

# Next value of x

}

Listing 1.3: R routine for the numerical evaluation of the analytical solution

The details of Listing 1.3 are essentially self-explanatory by a comparison with eqs. (1.3),(1.4i), (1.5c). Note in particular the statement

anal=anal+(2/lam)*exp(-(lam^2/L^2)*t)*sin(lam*x[ix]/L)

for the series in the RHS of eqs. (1.3), (1.4i) and (1.5c). For t > 0 (e.g., t=0.45),the series converges rapidly and therefore only 5 terms are required to obtain accuratenumerical values of the analytical solution.

60

Page 61: Method of Lines.pdf

The eigenvalue λi = (i− 1/2)π is programmed as lam=(i-1/2)*pi where the in-linevalue of π in R has been used. The output from Listing 1.3 is given in Table 1.6.

This derivation of the analytical solution to eqs. (1.1) and (1.2) illustrates the relativecomplexity of the analytical approach to PDE analysis. Also, for the two cases D =u,D = eu, an analytical approach may not be possible. But analytical solutions play animportant role as a stringent test of numerical methods, e.g., Table. 1.6, and thereforeideally the two approaches should be used in parallel.

References

[1] Hiebeler, D., Matlab/R Referencehttp://www.math.umaine.edu/∼hiebeler/comp/matlabR.html

[2] Polyanin, A. D. (2002), Handbook of Linear Partial Differential Equations for Engi-neers and Scientists, Chapman and Hall/CRC, Boca Raton, FL

[3] Schiesser, W.E (1994), Computational Mathematics in Engineering and Applied Sci-ence: ODEs, DAEs, and PDEs, CRC Press, Boca Raton, FL

[4] Soetaert, K., J. Cash and F. Mazzia (2012), Solving Differential Equations in R,Springer-Verlag, Heidelberg, Germany

61

Page 62: Method of Lines.pdf

Index

Aanalytical solution 54

comparison with numerical solution 25,44derivation by Fourier transform 54derivation by Green’s function 58numerical evaluation 60verification 57-58

Bboundary condition 3,29

Dirichlet 29inconsistency with initial condition 16

boundary value variable 3Neumann 29,31,34programming 29

Cconvolution integral 56

Ddependent variable 3differentiation routine, see dss004

diffusion equation 3linear 29nonlinear 28

diffusivity 3linear form 3-4,6,8,11

numerical solution 22-24nonlinear form 3-4,6,8,11

numerical solution 26Dirichlet boundary condition 3-4,9,29dss004 4-7,11,30-31,34,43dss044 30-31,34,43discontinuity 16

Eeigenvalue 25

Ffinite sin transform 54

see also Fourier transformFourier transform 54

62

Page 63: Method of Lines.pdf

inverse 54PDE solution 54-58table 54

GGreen’s function 58

PDE solution 58-59

Iindependent variable 3,28infinite series solution 25initial condition 3,29

insistency with boundary condition 16initial value variable 3integral transform 31

repeated 31

LLaplace transform 56

convolution 56ODE solution 56-57table 56

lsoda 13,20lsodes 13,17

Mmain program in Matlab 13,37main program in R 17,41Matlab

comparison with R 9-11,13,18-21output 22-25

for 6,10,13,16format 14fprintf 16,21

single quote ’ 40function 5global 5,9,13-14,18

if 7,10,16linspace 14-15,37main program 13,37,41ode15s 7,14-15plot 16-17transpose 5,7,12,15,32vector facility 5,7,12

63

Page 64: Method of Lines.pdf

vector definition 15zeros 15

method of lines 4ODE routine in Matlab 4,30ODE routine in R 7,32spatial grid 13-15,17temporal grid 15,17

MOL, see method of lines

NNeumann boundary condition 3-4,6,9,29,31,34nonlinear PDE solution 26-28numerical PDE solution 25,47

comparison with analytical solution 25,47

OODE

analytical integrationLaplace transform 56verification 57

numerical integrationode15s 7,15lsoda 13,20lsodes 13,17

routine in Matlab 5routine in R 7

ordinary differential equation, see ODE

Pparabolic PDE 16,28partial differential equation, see PDEPDE 3

analytical solution 54verification 57

linear numerical solution 22-24nonlinear numerical solution 26-28

RR (programming)

<<- 13array declaration 11cat 18,21comparison with Matlab 9-11,13,18-21

output 23-25

64

Page 65: Method of Lines.pdf

deSolve 13,17for 8,11function 7list 8,13,36return 8,13,36

global variable 13<<- 13,33

if 8,11library 17list 8,13lsoda 13,20lsodes 13,17matplot 18,22ode 20lsoda 20

out 17,20-21par(mrow) 18,22rep 8,11-12,17return 8,13setwd 17-18seq 17,19,44source 17-18sprintf 18,21

quote " 40vector facility 8,12

SStagewise differentiation 6

UUnit ramp 39Unit step 39

65