You are on page 1of 59

CHEE 412

Partial Differential Equations in


MATLAB
Hadis Karimi
Queens University
March 2011

1
Introduction
Parabolic partial differential equations are
encountered in many chemical engineering
applications
MATLABs 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 PDEs in MATLAB using pdepe
function
A mass transfer example
Solving other types of PDEs using PDE toolbox
A heat transfer example using PDE toolbox



3
PDE in One Space Dimension
The form of Parabolic PDEs in MATLAB


Boundary conditions


Initial conditions

)
x
u
, u , t , x ( s ))
x
u
, u , t , x ( b x (
x
x
t
u
)
x
u
, u , t , x ( c
m m
c
c
+
c
c
c
c
=
c
c
c
c

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
=
0

D
1

X
=
L
1

D
2

X
=
L
2

D
3

X
=
L
2

2
2
x
C
D
t
C
c
c
=
c
c
5
Initial Conditions
X
=
0

D
1

X
=
L
1

D
2

X
=
L
2

D
3

X
=
L
2

6
0
1
C C
L x 0
=
s s
0 C
L x L
2 1
=
s s
0 C
L x L
3 2
=
s s
@ t=0
Boundary Conditions
X
=
0

D
1

X
=
L
1

D
2

X
=
L
2

D
3

X
=
L
3

7
0
x
C
0 x
=
c
c
=
0
x
C
L x
3
=
c
c
=
x
C
D
x
C
D
L x
3 2
2
c
c
=
c
c

=
x
C
D
x
C
D
L x
2 1
1
c
c
=
c
c

=
Steps to Solve PDEs in MATLAB
1- Define the system
2- Specify boundary conditions
3- Specify initial conditions
4- Write System m-file
5-Write Boundary Conditions m-file
6- Write Initial condition m-file
7-Write MATLAB script M-file that solves and
plots
8
1- Define the System
0 m =
9
2
2
x
C
D
t
C
c
c
=
c
c
1 ) u , u , t , x ( c
x
=
x 1 1 x
u . D
x
C
. D ) u , u , t , x ( b =
c
c
=
0 ) u , u , t , x ( s
x
=
)
x
u
, u , t , x ( s ))
x
u
, u , t , x ( b x (
x
x
t
u
)
x
u
, u , t , x ( c
m m
c
c
+
c
c
c
c
=
c
c
c
c

2-Specify Boundary Conditions
0
x
C
, 0 x =
c
c
=
10
0 ) u , t , 0 ( p =
1
1 2
1
D
D D
) 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
= +
1
D
1
) t , 0 ( q =
0
x
C
D
D
) D D (
, L x
or
x
C
D
x
C
D , L x
1
1
1 2
1
2 1 1
=
c
c
=
c
c
=
c
c
=
x
C
. D
1
c
c
x
C
. D b
1
c
c
=
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 mesh
x = linspace(0,1,20);
t = linspace(0,2,10);
%Solve the PDE
u = pdepe(m,@system,@initial1,@bc1,x,t);
%Plot solution
surf(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
x
T
k
t
T
c
c
c
=
c
c

' '
q
x
T
k =
c
c

0 ) 0 , L ( T
0 ) 0 , x ( T
=
=
q
Defining System
18
0 s
x
T
k b
1 c
=
c
c
=
=
)
x
u
, u , t , x ( s ))
x
u
, u , t , x ( b x (
x
x
t
u
)
x
u
, u , t , x ( c
m m
c
c
+
c
c
c
c
=
c
c
c
c

2
2
p
x
T
k
t
T
c
c
c
=
c
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
' '
q
x
T
k =
c
c

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 =
x
T
k b
c
c
=
Remember
x=L
p=T=ur
q=0
x=0
p=q
q=1
0 q
x
T
k
' '
= +
c
c
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;
end


22
Calling the Solver
tend=10;
m=0;
x=linspace(0,L,200);
t=linspace(0,tend,50);
sol=pdepe(m,@pdecoef,@pdeic,@pde
bc,x,t);


23
Plotting
Temperature=sol(:,:,1);
figure
plot(t,Temperature(:,1))

24
System of PDEs in MATLAB
) u , u , t , x ( s )) u , u , t , x ( b x (
x
x u ) u , u , t , x ( c
.
.
.
) u , u , t , x ( s )) u , u , t , x ( b x (
x
x u ) u , u , t , x ( c
) u , u , t , x ( s )) u , u , t , x ( b x (
x
x u ) u , u , t , x ( c
x n x n
m m
nt x n
x 2 x 2
m m
t 2 x 2
x 1 x 1
m m
t 1 x 1
+
c
c
=
+
c
c
=
+
c
c
=

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
x n n n
x n n n
x 1 1 1
x 1 1 1
= +
= +
= +
= +
Initial Conditions







27
) x ( f ) x , 0 ( u
.
.
.
) x ( f ) x , 0 ( u
) x ( f ) x , 0 ( u
n n
2 2
1 1
=
=
=
Mucosa

Saliva

R
L

R
S






Example 3: Mass Transport in the
Saliva Layer
28
Drug
Transport
Direction
Lozenge

Blood Stream
R
M

g v
g
2
g
2
g
g
1 v
1
2
1
2
1
1
c k )
r
c
r
2
r
c
( D
t
c
c k )
r
c
r
2
r
c
( D
t
c

c
c
+
c
c
=
c
c

c
c
+
c
c
=
c
c
Mucosa

Saliva

R
L






Boundary Conditions
29
Drug
Transport
Direction
Lozenge

Blood Stream
) c )( c c (
D
k
r
C
) c )( c c (
D
k
r
C
L L L
L L L
R r g g R r g g , sol
gg
d
R r
g
R r 1 1 R r g g , sol
g 1
d
R r
1
= = =
= = =
=
c
c
=
c
c
Mucosa

Saliva






Boundary Conditions
30
Drug
Transport
Direction
Lozenge

Blood Stream
S S
S S
R r G R r g g
R r 2 R r 1 1
c c K
c c K
= =
= =
=
=
R
S

Mucosa

Saliva






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: C
1
=C
g
=0

(

=
0
0
) x , 0 ( u
System
(

=
(
(
(
(

c
c
c
c
=
r 2 g
r 1 1
g
g
1
1
u D
u D
r
c
D
r
c
D
b
2 m
1
1
c
=
(

=
(

=
(

=
2 v
1 v
g v
1 v
u k
u k
c k
c k
s
32
g v
g
2
g
2
g
g
1 v
1
1
2 2
1 v
1
2
1
2
1
1
c k )
r
c
r
2
r
c
( D
t
c
c k )
r
c
D r (
r
r c k )
r
c
r
2
r
c
( D
t
c

c
c
+
c
c
=
c
c

c
c
c
c
=
c
c
+
c
c
=
c
c

Boundary Conditions at R
L

33
) c c )( c c (
D
k
D
r
C
D
L L L
R r 1 1 R r g g , sol
g 1
d
1 R r
1
1 = = =
=
c
c
(
(
(
(



=
= =
= =
) u )( u c (
D
k
D
) u )( u c (
D
k
D
) u , t , R ( p
L L
L L
R r g 1 R r 2 g , sol
gg
d
g
R r 1 1 R r 2 g , sol
g 1
d
1
L
) c c )( c c (
D
k
D
r
C
D
L L L
R r g g R r g g , sol
gg
d
g R r
g
g = = =
=
c
c
(

=
1
1
) u , t , R ( q
L
Boundary Conditions at R
s

(

=
0
0
) u , t , R ( q
s
34
S S
S S
R r G R r g g
R r 2 R r 1 1
c c K
c c K
= =
= =
=
=
(
(

=
(
(

=
= =
= =
= =
= =
S S
S S
S S
S S
R r G R r 2 g
R r 2 R r 1 1
R r G R r g g
R r 2 R r 1 1
s
c u K
c u K
c c K
c c K
) 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 Plotting
m = 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
f au ) u c .( = + V V
f au ) u c .( du
t
= + V V
f au ) u c .( du
tt
= + V V
du au ) u c .( = + V V
Example 4
0 T
2
= V
T=10
T=0
Laplaces Equation
No Heat
No Heat
41
0
y
T
x
T
2
2
2
2
=
c
c
+
c
c
Step 1
Start the toolbox by typing in
>> pdetool
at the Matlab prompt
42
Step 2 Select Heat Transfer Application
43
Step 3 The Draw Menu
44
Step 4 Create Rectangle
45
Step 5 The Boundary Menu
46
Step 6 Display Options
47
Step 7 Select Boundary 4
48
Step 8 Specify Boundary Condition
Steps 9 - 10 Select Boundary 1 and specify
condition
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 c V ) v .(
0 T
2
= V
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

You might also like