You are on page 1of 9

Process Modelling & Simulation 314

TUTORIAL9:SOLVINGPDES2SOLUTIONS

3. UsingpdepetosolveaparticularPDE
Asafirststep,comparethePDEequations(4)(7)withMatlabsrequiredequationformsforpdepe,
equations(1)(3).Whatareappropriatevaluesorequationsthefollowingpdepeinputs?
m=0
c=1
f=dy/dx
s=0
u0=0
LefthandBC:

RighthandBC:

[note:whenm=0,bothxmandxm=1]

pl=0
ql=1
pr=A*(1yr) [note:yrmeansthevalueofyontherighthandboundary]
qr=1

Thescriptfilepdesim.mshouldlooklikethis:
% PDESIM: Solve a PDE using Matlab's PDE solver, pdepe
% Set simulation parameters
t = 0:0.2:5;
x = 0:0.2:1;
A = 2;
tol = 1e-3;

% Solve the PDE


y = solvePDE(x,t,A,tol);

% Display the results in 2-d and 3-d plots


figure(1)
% Graph two 2-d plots in figure 1
subplot(1,2,1)
plot(t,y)
title('Fig 1(a): y(x,t) for different x'); xlabel('t'); ylabel('y')
subplot(1,2,2)
plot(x,y')
title('Fig 1(b): y(x,t) for different t'); xlabel('x'); ylabel('y')
figure(2)
% Generate a 3-d surface plot for figure 2
[xplot,tplot] = meshgrid(x,t);
surf(xplot,tplot,y);
title('Fig 2'); xlabel('x'); ylabel('t'); zlabel('y')

Page1

ThefunctionfilesolvePDE.mshouldlooklikethis:
function sol = solvePDE(xmesh,tspan,A,tol)
% SOLVEPDE: Solve for y(x,t) given by the PDE
%
%
dy/dt = d2y/dx2
%
%
BC1: dy/dx = 0
at x=0
%
BC2: dy/dx = A(1-y) at x=1
%
IC:
y = 0 at t=0
%
% for spatial points xmesh, at time points tspan, for parameter A and
% relative solution tolerance tol.
m = 0;
opts = odeset('Reltol',tol);
% Solve the PDE using pdepe
sol = pdepe(m,@pdefun,@icfun,@bcfun,xmesh,tspan,opts);
%
%
%
%

In general, the solution is returned in the 3-d array sol, where


sol(i,j,k) is the numerical solution at time i and node j for the kth
differential variable. Since there is only one differential variable
in this problem, the array returned is just 2-d: sol(i,j)

% Nested functions that embody the PDE equation(s), initial and boundary
% conditions
% (Note that parameter values passed through as arguments in the main
% function solvePDE are automatically available in the nested functions
% below.)
%-------------------------------------------------------------------------% PDE function
function [cfun,ffun,sfun] = pdefun(x,t,y,dydx)
cfun = 1;
ffun = dydx;
sfun = 0;
end
%-------------------------------------------------------------------------% Initial condition function
function y0 = icfun(x)
y0 = 0;
end
%-------------------------------------------------------------------------% Boundary condition function
function [pl,ql,pr,qr] = bcfun(xl,yl,xr,yr,t)
pl = 0;
ql = 1;
pr = -A*(1-yr);
qr = 1;
end
%-------------------------------------------------------------------------end

Page2

Theplotsshouldlooklikethis:

Fig 1(b): y(x,t) for different t


1

0.9

0.9

0.8

0.8

0.7

0.7

0.6

0.6

0.5

0.5

Fig 1(a): y(x,t) for different x


1

0.4

0.4

0.3

0.3

0.2

0.2

0.1

0.1

0.5
x

Fig 2

1
0.8

0.6
0.4
0.2
0
6
1

0.8
0.6

2
t

0.4
0

0.2
0

Page3

Doesthenumericalsolutionappeartosatisfytheinitialconditions,thetwoboundaryconditions,and
thesteadystatesolution?Explainbrieflyhowyouverifiedthisfromaninspectionofthegraphs.
IC:Fig1(a)andFig2clearlyshowthaty=0att=0forallx,sotheinitialconditionisok.

BC1:ThelefthandBCsaysthegradientdy/dxshouldbezeroatx=0.FromFig1(b),thegradient
seemstobenonzeroatthestart,butgetsclosertozeroasthesolutionprogresses,butthisis
difficulttojudgebecauseoftherelativelycoarsenodespacing(x=0.2).

BC2:TherighthandBCsaysthegradientshouldrangefromdy/dx=Awheny(1,t)=0downto
dy/dx=0wheny(1,t)=1.ThisishardtojudgefromquantitativelyfromFig1(b)withoutactually
calculatingthegradient,butitdoesappeartobeconsistentOntherighthandboundary,the
slopeislowwhenyisnearone,andtheslopeincreaseswhenyisclosertozero.

SS:Fig1(a)andFig(2)bothshowthaty1forallxastincreases,soitseemstogetthesteady
statesolutionright.

Reducethespatialnode(x)spacingfrom0.2to0.05.Rerunthesimulation andcommentagainon
theboundaryconditionatx=0:
Witharoundfourtimesasmanynodesasbefore,thegradientdy/dxatx=0seemstobecloserto
zeroasrequiredbyeqn(5),evenatshorttimes.WithmorenodesIthinkitwouldgetcloserstill.

Thesolutionwiththenodalspacingreducedtox=0.05
canbeseenopposite:

Fig 1(b): y(x,t) for different t


1
0.9
0.8
0.7

0.6
0.5
0.4
0.3
0.2
0.1
0

Page4

0.5
x

4. Usingpdepetosolvethecatalystpelletmodel

Thescriptfilepdesimcat.mshouldlooklikethis:

% PDESIMCAT: Solve a PDE using Matlab's PDE solver, pdepe


close all
% Set simulation parameters
t = 0:0.05:1;
% Change this for the sensitivity study
r = 0:0.1:1;
% Change this for the sensitivity study
alpha = 1;
beta = 1;
tol = 1e-3;
% Change this for the sensitivity study

% Solve the PDE


c = solvePDEcat(r,t,alpha,beta,tol);

% Display the results in 2-d and 3-d plots


figure(1)
% Graph two 2-d plots in figure 1
subplot(1,2,1)
plot(t,c)
title('Fig 1(a): c(r,t) for different r'); xlabel('t'); ylabel('c')
subplot(1,2,2)
plot(r,c')
title('Fig 1(b): c(r,t) for different t'); xlabel('r'); ylabel('c')
css = csteady(r,alpha,beta);
% Analytical steady state solution
hold on
plot(r,css,'ko')
figure(2)
% Generate a 3-d surface plot for figure 2
[rplot,tplot] = meshgrid(r,t);
surf(rplot,tplot,c);
title('Fig 2'); xlabel('r'); ylabel('t'); zlabel('c')

Page5

ThefunctionfilesolvePDEcat.mshouldlooklikethis:

function sol = solvePDEcat(rmesh,tspan,alpha,beta,tol)


% SOLVEPDECAT: Solve the spherical catalyst model with first order reaction
% and diffusion by using pdepe.
m = 2;
opts = odeset('RelTol',tol);
% Solve the PDE using pdepe
sol = pdepe(m,@pdefun,@icfun,@bcfun,rmesh,tspan,opts);
%
%
%
%

In general, the solution is returned in the 3-d array sol, where


sol(i,j,k) is the numerical solution at time i and node j for the kth
differential variable. Since there is only one differential variable
in this problem, the array returned is just 2-d: sol(i,j)

% Nested functions that embody the PDE equation(s), initial and boundary
% conditions
% (Note that parameter values passed through as arguments in the main
% function solvePDE are automatically available in the nested functions
% below.)
%-------------------------------------------------------------------------% PDE function
function [cfun,ffun,sfun] = pdefun(r,t,c,dcdr)
cfun = 1;
ffun = alpha*dcdr;
sfun = -beta*c;
end
%-------------------------------------------------------------------------% Initial condition function
function c0 = icfun(r)
c0 = 0;
end
%-------------------------------------------------------------------------% Boundary condition function
function [pl,ql,pr,qr] = bcfun(rl,cl,rr,cr,t)
pl = 0;
ql = 1/alpha;
pr = cr-1;
qr = 0;
end
%-------------------------------------------------------------------------end

Page6

Theplotsshouldlooklikethis:

Fig 1(b): c(r,t) for different t


1

0.9

0.9

0.8

0.8

0.7

0.7

0.6

0.6

0.5

0.5

Fig 1(a): c(r,t) for different r


1

0.4

0.4

0.3

0.3

0.2

0.2

0.1

0.1

0.5
t

0.5
r

Fig 2

1
0.8

0.6
0.4
0.2
0
1
1
0.8

0.5

0.6
0.4
t

0.2
0

Page7

5. Checkingthesolutionatsteadystate

After the extra statements that calculate the steady state errors and draw the graph have been
addedtopdesimcat.m,thefollowinggraphshouldbeproduced:

-3

Fig 3: Steady state solution error

x 10

0.9
0.8
0.7

c err

0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

0.2

0.3

0.4

0.5
r

0.6

0.7

0.8

0.9

Alongwiththeoutput:

Steady state solution errors


Maximum absolute error: 8.72e-004
Average absolute error: 2.96e-004

Page8

Briefsensitivitystudy
Performseveralsensitivityrunsassetoutinthetablebelow,recordtheresultsandaddacomment:

Relative
Number cerrmax
cerravg
Comment
tf
tolerance ofnodes
1
1e3
11
8.72e004 2.96e004 Base case. The errors are already very
small. But, this really doesnt check
anythingaboutthetransientsolution.
2

1e3

11

8.46e004 2.80e004 Doublingtfreducesthesteadystateerrora


little, suggesting that t=1 is not quite
steadystate.

1e3

11

8.46e004 2.80e004 Increasingtfbyanother50%hasnoeffect,


sothesolutionisessentiallysteadyatt=2.

1e6

11

8.46e004 2.80e004 DecreasingthePDEsolvertolerancehasno


furthereffectonthesteadystateerror.

1e6

21

2.25e004 5.32e005 Doublingthenumberofnodesreducesthe


average steady state error by a factor of
about5.

1e6

41

5.79e005 1.01e005 Asabove.Clearlythenumberofnodeshas


astrongeffectonthesteadystateerror.

6. Challengeproblem(optional)

Ver1.0(26/4/2012)

Page9

You might also like