matlab pde
TRANSCRIPT
CHEE 412Partial Differential Equations in
MATLABHadis Karimi
Queen’s UniversityMarch 2011
1
Introduction
• Parabolic partial differential equations are encountered in many chemical engineering applications
• MATLAB’s pdepe command can solve these• For partial dierential equations in two space
dimensions PDE Toolbox can solve four types of equations: Elliptic, Parabolic, Hyperbolic and Eigenvalue
2
Agenda• Solving a parabolic PDE in MATLAB using “pdepe” function• A mass transfer example• A heat transfer example• Solving a system of parabolic PDE’s in MATLAB using “pdepe”
function• A mass transfer example• Solving other types of PDE’s using PDE toolbox• A heat transfer example using PDE toolbox
3
PDE in One Space Dimension
• The form of Parabolic PDE’s in MATLAB
• Boundary conditions
• Initial conditions
)xu
,u,t,x(s))xu
,u,t,x(bx(x
xtu
)xu
,u,t,x(c mm
0)u,u,t,xl(b).t,xl(q)u,t,xl(p x 0)u,u,t,xr(b).t,xr(q)u,t,xr(p x
)x(f)x,0(u 4
m=0 for Cartesian, for cylindrical, 1 and for spherical 2
Example 1: A Mass Transfer System
X=
L 2
2
2
xC
DtC
5
Initial Conditions
X=
L 2
6
0
1
CC
Lx0
0C
LxL 21
0C
LxL 32
@ t=0
Boundary Conditions
X=
L 3
7
0xC
0x
0xC
Lx 3
xC
DxC
D
Lx
32
2
xC
DxC
D
Lx
21
1
Steps to Solve PDE’s in MATLAB
1- Define the system 2- Specify boundary conditions3- Specify initial conditions4- Write System m-file 5-Write Boundary Conditions m-file6- Write Initial condition m-file7-Write MATLAB script M-file that solves and
plots
8
1- Define the System
0m
9
2
2
xC
DtC
1)u,u,t,x(c x
x11x u.DxC
.D)u,u,t,x(b
0)u,u,t,x(s x
)xu
,u,t,x(s))xu
,u,t,x(bx(x
xtu
)xu
,u,t,x(c mm
2-Specify Boundary Conditions0
xC
,0x
10
0)u,t,0(p
1
121 D
DD)t,L(q
0)u,t,L(p 1
0)u,u,t,xl(b).t,xl(q)u,t,xl(p x
0)u,u,t,xr(b).t,xr(q)u,t,xr(p x
1D1
)t,0(q
0xC
DD
)DD(,Lx
orxC
DxC
D,Lx
11
121
211
xC
.D1
xC
.Db 1
3- Specify Initial Conditions
0
0
C)x,0(u
C)x,0(C
11
)x(f)x,0(u
4- Write System m-file
function [c,b,s] = system(x,t,u,DuDx)
c = 1;
b =D1*DuDx;
s = 0;
end
12
5-Write Boundary Conditions m-file
function [pl,ql,pr,qr] = bc1(xl,ul,xr,ur,t)
pl = 0;ql = 1/D1;pr = 0;qr = (D2-D1)/D1;end
13
6- Write Initial Conditions m-file
function value = initial1(x)
value = C0;
end
14
7-Write MATLAB script M-file that solves and plots
m = 0;%Define the solution meshx = linspace(0,1,20);t = linspace(0,2,10);%Solve the PDEu = pdepe(m,@system,@initial1,@bc1,x,t);%Plot solutionsurf(x,t,u);title('Surface plot of solution.');xlabel('Distance x');ylabel('Time t');
15
Results
16
Example 2: A Heat Transfer System
17
L
x
T=0 2
2
p xT
ktT
c
''qxT
k
0)0,L(T
0)0,x(T
q”
Defining System
18
0sxT
kb
1c
)xu
,u,t,x(s))xu
,u,t,x(bx(x
xtu
)xu
,u,t,x(c mm
2
2
p xT
ktT
c
Defining System
function [c,b,s]=pdecoef(x,t,u,DuDx)
global rho cp k
c=rho*cp;
b=k*DuDx;
s=0;
end
19
Initial Conditions
function u0=pdeic(x)
u0=0;
end
20
Boundary Conditions
21
''qxT
k
0)u,u,t,xl(b).t,xl(q)u,t,xl(p x 0)u,u,t,xr(b).t,xr(q)u,t,xr(p x
0)0,L(T xT
kb
Remember
x=Lp=T=urq=0
x=0p=q”q=10q
xT
k ''
or
Writing Boundary Condition m-file
function[pl,ql,pr,qr]=pdebc(xl, ul,xr,ur,t)
global q
pl=q;
ql=1;
pr=ur;
qr=0;
end22
Calling the Solver
tend=10;
m=0;
x=linspace(0,L,200);
t=linspace(0,tend,50);
sol=pdepe(m,@pdecoef,@pdeic,@pdebc,x,t);
23
Plotting
Temperature=sol(:,:,1);
figure
plot(t,Temperature(:,1))
24
System of PDE’s in MATLAB
)u,u,t,x(s))u,u,t,x(bx(x
xu)u,u,t,x(c
.
.
.
)u,u,t,x(s))u,u,t,x(bx(x
xu)u,u,t,x(c
)u,u,t,x(s))u,u,t,x(bx(x
xu)u,u,t,x(c
xnxnmm
ntxn
x2x2mm
t2x2
x1x1mm
t1x1
25
Boundary Conditions
26
0)u,u,t,xr(b).t,xr(q)u,t,xr(p
0)u,u,t,xl(b).t,xl(q)u,t,xl(p
.
.
.
0)u,u,t,xr(b).t,xr(q)u,t,xr(p
0)u,u,t,xl(b).t,xl(q)u,t,xl(p
xnnn
xnnn
x111
x111
Initial Conditions
27
)x(f)x,0(u
.
.
.
)x(f)x,0(u
)x(f)x,0(u
nn
22
11
MucosaMucosa
SalivaSaliva
RL RS
Example 3: Mass Transport in the Saliva Layer
28
Drug Transport Direction
Lozenge
Blood Stream
RM
gvg
2g
2
gg
1v1
21
2
11
ck)r
c
r2
r
c(D
t
c
ck)rc
r2
rc
(Dtc
MucosaMucosa
SalivaSaliva
RL
Boundary Conditions
29
Drug Transport Direction
Lozenge
Blood Stream
)c)(cc(Dk
r
C
)c)(cc(Dk
rC
LLL
LLL
RrggRrgg,solgg
dRr
g
Rr11Rrgg,solg1
dRr
1
MucosaMucosa
SalivaSaliva
Boundary Conditions
30
Drug Transport Direction
Lozenge
Blood Stream
SS
SS
RrGRrgg
Rr2Rr11
ccK
ccK
RS
MucosaMucosa
SalivaSaliva
Initial Conditions
31
Drug Transport Direction
Lozenge
Blood Stream
Before any drug is released (at time = 0), the drug and glucose concentrations in the saliva are equal to zero: C1 =Cg =0
0
0)x,0(u
System
r2g
r11
gg
11
uD
uD
r
cD
rc
Db
2m
1
1c
2v
1v
gv
1v
uk
uk
ck
cks
32
gvg
2g
2
gg
1v1
122
1v1
21
2
11
ck)r
c
r2
r
c(D
t
c
ck)rc
Dr(r
rck)rc
r2
rc
(Dtc
Boundary Conditions at RL
33
)cc)(cc(Dk
DrC
DLLL Rr11Rrgg,sol
g1
d1Rr
11
)u)(uc(Dk
D
)u)(uc(Dk
D
)u,t,R(p
LL
LL
Rrg1Rr2g,solgg
dg
Rr11Rr2g,solg1
d1
L
)cc)(cc(Dk
Dr
CD
LLL RrggRrgg,solgg
dgRr
gg
1
1)u,t,R(q L
Boundary Conditions at Rs
0
0)u,t,R(q s
34
SS
SS
RrGRrgg
Rr2Rr11
ccK
ccK
SS
SS
SS
SS
RrGRr2g
Rr2Rr11
RrGRrgg
Rr2Rr11
scuK
cuK
ccK
ccK)u,t,R(p
System
function [c,b,s] = eqn (x,t,u,DuDx)
c = [1; 1];b = [D1; Dg] .* DuDx;s = [-kv*u(1); -kv*u(2)]; end
35
Boundary Conditions
function [pl,ql,pr,qr] = bc2(xl,ul,xr,ur,t)
pl = [D1*kd/D1g*(csolg-ul(2))*(rou1 -ul(1))); Dg*kd/Dgg*(csolg-ul(2))*(roug-ul(2)))];
ql = [-1; -1];
pr = [K1*ur(1)-c2Rs; Kg*ur(2)-cGRs];
qr = [0; 0];
end
36
Initial Conditions
function value = initial2(x);
value = [0;0];
end
37
Solving and Plottingm = 2;x = linspace(0,1,10);t = linspace(0,1,10);sol = pdepe(m,@eqn,@initial2,@bc2,x,t);u1 = sol(:,:,1);u2 = sol(:,:,2);subplot(2,1,1)surf(x,t,u1);title('u1(x,t)');xlabel('Distance x');ylabel('Time t');subplot(2,1,2)surf(x,t,u2);title('u2(x,t)');xlabel('Distance x');ylabel('Time t');
38
Plotting
39
Single PDE in Two Space Dimensions
1. Elliptic
2. Parabolic
3. Hyperbolic
4. Eigenvalue
40
fau)uc.(
fau)uc.(du t
fau)uc.(du tt
duau)uc.(
Example 4
0T2
T=10 T=0Laplace’s Equation
No Heat
No Heat
41
0yT
xT
2
2
2
2
Step 1• Start the toolbox by typing in
>> pdetool at the Matlab prompt
42
Step 2Select Heat Transfer Application
43
Step 3The Draw Menu
44
Step 4Create Rectangle
45
Step 5The Boundary Menu
46
Step 6Display Options
47
Step 7Select Boundary 4
48
Step 8 Specify Boundary Condition
Steps 9 - 10 Select Boundary 1 and specify Steps 9 - 10 Select Boundary 1 and specify conditioncondition
49
Steps 11 - 14 Specify remaining boundary conditions
50
Step 15 The PDE Mode
51
Step 16 PDE Menu
52
Step 17 Specify PDE parameters
53
)v.(0T2
PDE Solution: T Calculated
54
Step 18 The Plot Menu
55
Step 19 Specify plotting parameters
56
Finally the Solution: Temperature Contours
57
Thank You!
Any questions?
58
References
• P. Howard, Partial Differential Equations in MATLAB 7.0, Spring 2005.
• Kim, K. S., & Simon, L. ,Transport Mechanisms in oral transmucosal drug delivery: Implications for pain management. Mathematical Biosciences , 93-100, 2011.
59