Matlab Pde

59
CHEE 412 Partial Differential Equations in MATLAB Hadis Karimi Queen’s University March 2011 1

Transcript of Matlab Pde

Page 1: Matlab Pde

CHEE 412Partial Differential Equations in

MATLABHadis Karimi

Queen’s UniversityMarch 2011

1

Page 2: Matlab Pde

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

Page 3: Matlab Pde

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

Page 4: Matlab Pde

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

Page 5: Matlab Pde

Example 1: A Mass Transfer System

X=

L 2

2

2

xC

DtC

5

Page 6: Matlab Pde

Initial Conditions

X=

L 2

6

0

1

CC

Lx0

0C

LxL 21

0C

LxL 32

@ t=0

Page 7: Matlab Pde

Boundary Conditions

X=

L 3

7

0xC

0x

0xC

Lx 3

xC

DxC

D

Lx

32

2

xC

DxC

D

Lx

21

1

Page 8: Matlab Pde

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

Page 9: Matlab Pde

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

Page 10: Matlab Pde

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

Page 11: Matlab Pde

3- Specify Initial Conditions

0

0

C)x,0(u

C)x,0(C

11

)x(f)x,0(u

Page 12: Matlab Pde

4- Write System m-file

function [c,b,s] = system(x,t,u,DuDx)

c = 1;

b =D1*DuDx;

s = 0;

end

12

Page 13: Matlab Pde

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

Page 14: Matlab Pde

6- Write Initial Conditions m-file

function value = initial1(x)

value = C0;

end

14

Page 15: Matlab Pde

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

Page 16: Matlab Pde

Results

16

Page 17: Matlab Pde

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”

Page 18: Matlab Pde

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

Page 19: Matlab Pde

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

Page 20: Matlab Pde

Initial Conditions

function u0=pdeic(x)

u0=0;

end

20

Page 21: Matlab Pde

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

Page 22: Matlab Pde

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

Page 23: Matlab Pde

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

Page 24: Matlab Pde

Plotting

Temperature=sol(:,:,1);

figure

plot(t,Temperature(:,1))

24

Page 25: Matlab Pde

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

Page 26: Matlab Pde

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

Page 27: Matlab Pde

Initial Conditions

27

)x(f)x,0(u

.

.

.

)x(f)x,0(u

)x(f)x,0(u

nn

22

11

Page 28: Matlab Pde

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

Page 29: Matlab Pde

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

Page 30: Matlab Pde

MucosaMucosa

SalivaSaliva

Boundary Conditions

30

Drug Transport Direction

Lozenge

Blood Stream

SS

SS

RrGRrgg

Rr2Rr11

ccK

ccK

RS

Page 31: Matlab Pde

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

Page 32: Matlab Pde

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

Page 33: Matlab Pde

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

Page 34: Matlab Pde

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

Page 35: Matlab Pde

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

Page 36: Matlab Pde

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

Page 37: Matlab Pde

Initial Conditions

function value = initial2(x);

value = [0;0];

end

37

Page 38: Matlab Pde

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

Page 39: Matlab Pde

Plotting

39

Page 40: Matlab Pde

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.(

Page 41: Matlab Pde

Example 4

0T2

T=10 T=0Laplace’s Equation

No Heat

No Heat

41

0yT

xT

2

2

2

2

Page 42: Matlab Pde

Step 1• Start the toolbox by typing in

>> pdetool at the Matlab prompt

42

Page 43: Matlab Pde

Step 2Select Heat Transfer Application

43

Page 44: Matlab Pde

Step 3The Draw Menu

44

Page 45: Matlab Pde

Step 4Create Rectangle

45

Page 46: Matlab Pde

Step 5The Boundary Menu

46

Page 47: Matlab Pde

Step 6Display Options

47

Page 48: Matlab Pde

Step 7Select Boundary 4

48

Page 49: Matlab Pde

Step 8 Specify Boundary Condition

Steps 9 - 10 Select Boundary 1 and specify Steps 9 - 10 Select Boundary 1 and specify conditioncondition

49

Page 50: Matlab Pde

Steps 11 - 14 Specify remaining boundary conditions

50

Page 51: Matlab Pde

Step 15 The PDE Mode

51

Page 52: Matlab Pde

Step 16 PDE Menu

52

Page 53: Matlab Pde

Step 17 Specify PDE parameters

53

)v.(0T2

Page 54: Matlab Pde

PDE Solution: T Calculated

54

Page 55: Matlab Pde

Step 18 The Plot Menu

55

Page 56: Matlab Pde

Step 19 Specify plotting parameters

56

Page 57: Matlab Pde

Finally the Solution: Temperature Contours

57

Page 58: Matlab Pde

Thank You!

Any questions?

58

Page 59: Matlab Pde

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