0% found this document useful (0 votes)
76 views157 pages

Numerical & Statistical Anylysis For Cheme's Part3

Numerical & statistical analysis for chemE's: 3

Uploaded by

Fug az
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views157 pages

Numerical & Statistical Anylysis For Cheme's Part3

Numerical & statistical analysis for chemE's: 3

Uploaded by

Fug az
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 157

CBE 301

Application of Numerical
and Statistical Techniques
in Chemical and Biomolecular Engineering

Lecture Notes

Part 3


c Manolis Doxastakis
Chemical and Biomolecular Engineering
University of Tennessee, Knoxville, 2021
How To Use These Notes:
• As the cover page suggests, this text is a set of Lecture Notes, NOT a textbook!
Suggested titles of textbooks will be provided by the instructor and you can find
additional resources online and in the library.
• A large number of sources were used, in addition to textbooks as well as notes written
by Prof. M. Nikolaou (Univ. of Houston).
• Certain topics covered in detail in textbooks are presented herein rather telegraphically
while others are elaborated on, particularly when they refer to material not often
covered in textbooks.
• In many places, throughout the notes some space has been intentionally left blank, for
the student to understand a certain topic by being forced to fill in the missing material.
That is frequently done during lecture time.
• In other places assignments are given for homework not to hand in (ÒP Hwnthi).
• Additional practice problems from past exams will be provided
• The examples have been carefully selected to correspond to a variety of problems of
interest to the evolving nature of chemical and biomolecular engineering. While the
emphasis is on numerical methods, the physical picture is also important.
• There are several basic software tools used throughout: MATLAB, Mathematica,
and Excel. The student should be familiar with computational tools along with the
mathematical and programming principles of computation. Each tool does certain
tasks particularly well and may be adequate for others; therefore you are most efficient
when you learn how to use multiple tools.
• The code included with some examples is intentionally kept simple, to illustrate con-
cepts. Professional code is a lot more complicated, although the numerical recipe
involved is usually not very different. The emphasis herein is to critically demonstrate
applications of the discussed mathematical methods rather than learning the technical
use of the software
• The nature of the material requires active participation of the student. Therefore,
study and perform the numerical examples on your own and expand problems by
altering parameters and methods.

Notation:
◦ Uppercase, boldface: Matrices. e.g. M
◦ Lowercase, boldface: vectors. e.g. v
◦ Lowercase, italics: scalars. e.g. f
Contents
1 INTRODUCTION: FIRST-PRINCIPLES MATHEMATICAL MODELING 1
1.1 What is a mathematical model? 1
1.2 Where do models come from? 1
1.3 Developing first-principles models 1
2 ANALYTICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS (ODE) 9
2.1 Why analytical solutions? 9
2.2 First-order linear ordinary differential equations (ODEs) 9
2.3 Second-order linear ordinary differential equations (ODE) 12
2.3.1 Case 1: λ1 6= λ2 real, then Exponential solution . . . . . . . . . . . . 14
2.3.2 Case 2: λ1 , λ2 complex, then Oscillatory solution . . . . . . . . . . . 15
2.3.3 Case 3: λ1 = λ2 = λ, real, then Polynomial × Exponential solution . 18
2.4 Second-order ODE, Two-point boundary value problems 20
2.5 Simple cases of second-order ODE (important in transport phenomena) 21
2.6 Simultaneous multiple first-order linear ODE: matrix analysis 23
2.7 Higher-order ODE 27
2.8 Nonlinear ordinary differential equations 28
2.9 Software for analytical solution of ODE 29
3 NUMERICAL INTEGRATION 30
3.1 Why numerical integration? 30
3.2 How to do numerical integration? 30
3.3 General integration approach 31
3.4 The trapezoidal rule 32
3.5 Multiple application of the trapezoidal rule 32
3.6 Simpson’s 1/3 rule 33
3.7 Multiple application of Simpson’s 1/3 rule 34
3.8 Simpson’s 3/8 rule 35
4 NUMERICAL DIFFERENTIATION 36
4.1 Why numerical differentiation? 36
4.2 How to do numerical differentiation? 36
4.3 Basic approaches to numerical differentiation 37
4.4 Basic formulas for first-order derivatives 38
4.5 Approximation of higher-order derivatives 40
4.6 Additional formulas 41
5 NUMERICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS (ODE) 42
5.1 Why numerical solution of ODE? 42
5.2 What is numerical solution of ODE? 42
5.3 How are ODE solved numerically? 42
5.4 The finite-difference method for numerical solution of ODE 43
5.5 Numerical solution of initial-value ODE problems: General idea 47
5.6 Euler’s method 47
5.7 How accurate is Euler’s method? 48
5.8 How to improve the accuracy of Euler’s method? 50
5.9 Extensions of Euler’s method (OPTIONAL) 51
5.10 Introduction to Runge-Kutta methods 52
5.11 Derivation of the second-order (n=2) Runge-Kutta method (OPTIONAL) 52
5.12 The fourth-order (n=4) Runge-Kutta method 54
5.13 Multi-step methods 56
5.14 Software for numerical solution of ODE 57
5.15 Step size adaptation 61
6 NUMERICAL SOLUTION OF SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS 62
7 STABILITY, INSTABILITY, AND CHAOS 70
7.1 ODE stability, instability, and chaos 70
7.2 Linear ODE stability 80
7.3 Stability, instability, and chaos of numerical schemes for ODE integration 80
7.4 ODE stiffness 83
7.5 Software for numerical solution of stiff ODE 84
8 HIGHER-ORDER DIFFERENTIAL EQUATIONS & BOUNDARY-VALUE PROBLEMS 85
8.1 The shooting method for boundary-value problems (BVP) 85
8.2 Finite-difference methods for boundary-value problems 89
9 PARTIAL DIFFERENTIAL EQUATIONS (PDE) 92
9.1 Classification of PDE 92
9.2 Developing PDEs 94
10 NUMERICAL TECHNIQUES FOR PDE 97
10.1 Finite-difference (FD) solution of elliptic PDE 98
10.2 Solution of large systems of equations in elliptic PDE 105
10.3 Explicit finite-difference (FD) methods for numerical solution of parabolic PDE 106
10.4 Numerical stability of explicit methods 112
10.5 Implicit finite-difference (FD) methods for numerical solution of parabolic PDE 113
10.6 Numerical stability of implicit methods 116
10.7 The Crank-Nicolson implicit finite difference (FD) method 117
10.8 Numerical stability of Crank-Nicolson implicit methods 122
10.9 Understanding transport dynamics using numerical integration of PDE (OPTIONAL) 123
10.10 Parabolic PDE in 2-D 125
A Mathematica code for Example 55 126
B Mathematica code for Example 56 127
C Mathematica code for Example 57 128
D Mathematica code for Example 59 129
E Mathematica code for Example 60 134
F Mathematica code for Example 66 136
G MATLAB code for Example 67 137
H MATLAB code for Example 69 139
I MATLAB code for Example ?? 140
J Mathematica code for Example 74 142
K MATLAB code for Example 74 144
L MATLAB code for Example 75 146
M MATLAB code for Example 79 149
N MATLAB code for Example 81 151
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

1 INTRODUCTION: FIRST-PRINCIPLES MATHEMATICAL MODELING


1.1 What is a mathematical model?

Definition 1: Mathematical model
A mathematical model is a recipe that employs the mathematical language to do calculations
that can answer specific questions about a system (e.g. characterization of the system’s
behavior, prediction of the system’s evolution, identification of optimal treatment of the
system).
“All models are wrong, but some are useful.” (George Box)

1.2 Where do models come from?


• Experience (mental models in human minds)
• Standard operation data
• First principles (conservation laws, constitutive equations)
• Fitting of experimental data (continuous or discrete-time)

1.3 Developing first-principles models


Need: 1) System Variables 2) System Equations
• Conservation law: Balance for conserved quantity S (i.e. mass, energy, momentum):
   
" # " # " # amount of S amount of S
Accumulation inflow outflow  generated in 
 
consumed in
 
of S in system of S of S system system
= − + −
time | time {z time } | time {z time }
Transport rate equations Kinetic rate expressions
Equilibria relationships

-Lumped parameter Systems: Ordinary differential equations (ODE)


Dependent variable does not change with space (i.e. well-mixed vessel)
-Distributed parameter systems: Partial differential equations (PDE)
Dependent variable changes with spatial location
• Constitutive equations: Specify what each term in conservation equations may look like
(e.g., ideal gas law, reaction kinetics, heat transfer equations).

Parameter values may still have to be evaluated experimentally.



Ref: G. Stephanopoulos, Chemical Process Control, Prentice - Hall, 1984

-1-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 1 Mathematical models for a continuous-flow tank heater

(Lumped-parameter System)
Assumptions: ...............

• Steady State energy balance:


IN OU T
z }| { z }| {
0 = F Cp (Ti − Tref ) + Q − F Cp (T − Tref )
⇒ Q = F Cp (T − Ti )
| {z }
algebraic equation

• Unsteady State energy balance (F = Fi ):

dH
= F Cp (Ti − Tref ) + Q − F Cp (T − Tref )
dt
H=ρCp V T dT 1
=⇒ = [F Cp (Ti − T ) + Q] (1)
ρCp V =Const. dt ρCp V
| {z }
differential equation

(ÒP Hwnthi: What if ρCp V 6= Const. ?)

• Unsteady State mass and energy balances:


Mass balance:∗
dVρ ρ=const dV 1
= Fi − F =⇒ = (Fi (t) − F (t)) (2)
dt dt ρ
Energy Balance:

d(VρCp (T − Tref ))
= Fi Cp (Ti − Tref ) − F Cp (T − Tref ) + Q (3)
dt

Tref should not appear in the final model: Eq. 3 ⇒

dV d(T − Tref )
ρCp (T − Tref ) + VρCp =Fi Cp (Ti − Tref ) − F Cp (T − Tref ) + Q
dt dt

Usign Eq. 2 ⇒

1 dT
ρCp (T − Tref ) (Fi − F ) + VρCp =Fi Cp (Ti − Tref ) − F Cp (T − Tref ) + Q ⇒
 ρ
 dt


F = mass flow rate

-2-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

dT
Cp T Fi − 
CpX
X T
X−X
X CpX
F TX FXi + 
refX ref F + VρCp
CpT  
dt
= Fi Cp Ti − X
FiX
CXp TXX−
ref FX
X T +
Cp

X
X FCp
Tref + Q


dT Fi (t) Q(t)
⇒ = (Ti (t) − T (t)) + (4)
dt V (t)ρ V (t)ρCp

Eqs. 2 and 4: Process model (dynamic)



• Constitutive equations: QU At (Tc − T ), F = a V (Torricelli’s law)

Example 2 Mathematical model for a jacket-cooled continuous-flow stirred tank reactor (CSTR)

Exothermic reaction A→B.


Assume uniform physical properties.
(r: rate of reaction per unit volume)

• Conservation equations:

d(ρV )
Total mass balance: = ρi Fi − ρF (5)
dt

dnA d(CA V )
Mass balance on A: = = CAi Fi − CA F − rV (6)
dt dt

dnB d(CB V )
Mass balance on B: = = −CB F + rV (7)
dt dt

Total energy balance: dH


= ρi F H i −ρF H −Q (8)
dt |{z} |{z}
CAi H̃A (T ) CA H̃A (T )
+ρCp (Ti −T ) +CB H̃B T

-3-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

dH ∂H dT ∂H dnA ∂H dnB
H = H(T, nA , nB ) ⇒ = + + (9)
dt ∂T dt
|{z} ∂nA | dt
{z } ∂nB | dt
{z }
| {z } | {z }
ρV Cp H̃A (T ) CAi Fi −CA F −rV H̃B (T ) −CB F +rV
partial partial
molar molar
enthalpy enthalpy

After manipulations (how?):

dV
= Fi − F (ρ = ρi ) (10)
dt

dCA Fi
= (CAi − CA ) − r (11)
dt V

dT Fi Q
= (Ti − T ) + Jr − (12)
dt V ρCp V

H̃A −H̃B
where J = ρCp
(heat of reaction).

• Constitutive equations:

r = k0 e−E/RT CA (First-order Arrhenius kinetics) (13)


Q = U At (T − Tc ) (Heat transfer) (14)

Example 3 Mathematical model for a jacket-cooled continuous-flow stirred tank reactor (CSTR)
with multiple reactions∗

Consider a continuous stirred tank reactor (CSTR) system with two inlet streams (1 and
2) and one outlet stream. Stream 1 is a mixture of A and B with composition cA1 , cB1 ,
(moles/volume) and has volumetric flow rate F1 and temperature T1 . Stream 2 is pure R.
The reactions taking place are:
Reaction 1: A + R → P1
Reaction 2: B + 2R → P2
Both reactions are endothermic and follow mass action kinetics. Heat is supplied to the

Problem II.8 in Stephanopoulos, Chemical Process Control, Prentice Hall, 1984

-4-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

reaction mixture by steam which flows through a coil, immersed in the reactor’s content,
with a heat transfer area At . Develop a dynamic model for this CSTR.

• Total mass balance:

d(ρV ) dV dρ
= ρ1 F1 + ρ2 F2 − ρF ⇒ ρ +V = ρ1 F1 + ρ2 F2 − ρF ⇒
dt dt dt
dV
= F1 + F2 − F (15)
dt

• Mass balance on A:

dnA d(cA V ) dV dcA


= = cA1 F1 − cA F − rA V ⇒ cA +V = cA1 F1 − cA F − rA V ⇒
dt dt dt dt
dcA
cA (F1 + F2 − @@) + V
F = cA1 F1 − H H − rA V ⇒
cAH
F
dt
dcA F1 F2
= (cA1 − cA ) − cA − rA (16)
dt V V

• Mass balance on B:

dnB d(cB V ) dV dcB


= = cB1 F1 − cB F − rB V ⇒ cB +V = cB1 F1 − cB F − rB V ⇒
dt dt dt dt
dcB
cB (F1 + F2 − @@) + V
F = cB1 F1 − H H − rB V ⇒
cBH
F
dt
dcB F1 F2
= (cB1 − cB ) − cB − rB (17)
dt V V

• Mass balance on R:

dnR d(cR V )
= = cR2 F2 − cR F − (rA + 2rB )V ⇒
dt dt
dV dcR
cR +V = cR2 F2 − cR F − (rA + 2rB )V ⇒
dt dt
dcR
cR (F1 + F2 − @@) + V
F = cR2 F2 − H H − (rA + 2rB )V ⇒
cRH
F
dt
dcR F2 F1
= (cR2 − cR ) − cR − (rA + 2rB ) (18)
dt V V

-5-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Mass balance on P1 :

dnP1 d(cP1 V ) dV dcP1


= = rP1 V − cP1 F ⇒ cP1 +V = rA V − cP1 F ⇒
dt dt dt dt
dcP1
cP1 (F1 + F2 − @ @) + V
F = rA V − H
cPH
1FH⇒
dt
dcP1 F1 + F2
= rA − cP1 (19)
dt V

• Mass balance on P2 :

dnP2 d(cP2 V ) dV dcP2


= = rP2 V − cP2 F ⇒ cP2 +V = rB V − cP2 F ⇒
dt dt dt dt
dcP2
cP2 (F1 + F2 − @F@) + V = rB V − HcPHH⇒
2F
dt
dcP2 F1 + F2
= r B − cP 2 (20)
dt V

• Energy balance:

dH
= ρ1 F1 H1 (T1 ) + ρ2 F2 H2 (T2 ) − ρF H(T ) + Q
dt

Now,

H = H(T, nA , nB , nR , nP1 , nP2 ) ⇒


dH ∂H dT ∂H dnA ∂H dnB ∂H dnR ∂H dnP1 ∂H dnP2
= + + + + +
dT ∂T dt ∂nA dt ∂nB dt ∂nR dt ∂nP1 dt ∂nP2 dt
dT
= ρCp V + H̄A (T )(cA1 F1 − cA F − rA V ) + H̄B (T )(cB1 F1 − cB F − rB V )
dt
+H̄R (T )(cR2 F2 − cR F − (rA + 2rB )V )
+H̄P1 (T )(rA V − cP1 F ) + H̄P2 (T )(rB V − cP2 F )
dT    
= ρCp V + H̄P1 (T ) − H̄A (T ) − H̄R (T ) rA V + H̄P2 (T ) − H̄B (T ) − 2H̄R (T ) rB V
dt | {z } | {z }
∆H1 ∆H2
 
+ H̄A (T )cA1 + H̄B (T )cB1 F1 + H̄R (T )cR2 F2
| {z } | {z }
ρ1 H1 (T ) ρ2 H2 (T )
 
− H̄A (T )cA + H̄B (T )cB + H̄R (T )cR + H̄P1 (T )cP1 + H̄P2 cP2 F
| {z }
ρH(T )

-6-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Therefore,

dT
ρCp V + ∆H1 rA V + ∆H2 rB V = ρ1 F1 H1 (T1 ) + ρ2 F2 H2 (T2 ) − ρFXH(T )+Q
X X
XX
dt
− ρ1 H1 (T )F1 − ρ2 H2 (T )F2 + ρH(T X⇒
)F
XXX
X
dT
ρCp V = ρ1 F1 (H1 (T ) − H1 (T )) + ρ2 F2 (H2 (T ) − H2 (T )) + Q − ∆H1 rA V − ∆H2 rB V
dt
dT F1 F2 Q ∆H1 rA + ∆H2 rB
⇒ = (T1 − T ) + (T2 − T ) + − (21)
dt Cp V Cp V ρCp V ρCp

Possible constitutive equations:


E1
 
rA = k1,0 exp − cA cR (mass-action law and Arrhenius kinetics)
RT
E2
 
rB = k2,0 exp − cB c2R (mass-action law and Arrhenius kinetics)
RT
Q = U At (Tc − T ) (heat transfer)

-7-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 4 Mathematical model for a tubular heat exchanger

(Distributed-parameter system)
Steam
Balance volume

Liquid

Condensate

Q=heat/time-Area
A=cross section of inner tube
ν=average velocity, uniform over cross-sectional area
Assume thin walls of zero resistance to heat transfer.
Energy Balance:

δm U (Ts −T )
z }| {∂T z}|{
cp ρAδz = ρcp νAT |z + Q (2πRδz) − ρcp νAT |z+δz
| {z ∂t} | {z } | {z }
Rate of enthalphy IN Rate of enthalpy OUT
Enthalpy
accumulation
rate

δz → 0 ⇒
∂T ∂T 2
ρcp = −ρcp ν + U (Ts − T ) (22)
∂t ∂z R

-8-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2 ANALYTICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS


(ODE)
2.1 Why analytical solutions?
-Computation
-Understand qualitative behavior of equation

2.2 First-order linear ordinary differential equations (ODEs)


dx
= α(t)x(t) + f (t) , initial condition x(t0 ) given
dt | {z }
forcing function
or input

• Case 1: f (t) = 0 (Unforced system)

dx
= α(t)x(t), initial condition x(t0 ) given (23)
dt


dx Z
⇒ = α(t)dt ⇒ ln |x| = α(t)dt +c



x


| {z } ⇒
b(t) 


t = t0 ⇒ ln |x(t0 )| = b(t0 ) + c

Z t
x(t)>0 x(t)
=⇒ ln x(t) − ln x(t0 ) = b(t) − b(t0 ) ⇒ ln = α(τ )dτ ⇒
x(t0 ) t0

Rt
α(τ )dτ
x(t) = x(t0 )e t0
(24)

Example 5 Unforced linear time-varying system


dx
= sin t x(t) x(2) = 1
dt
Rt
sin τ dτ
x(t) = 1 · e 2 = e− cos t+cos 2 , t≥2

-9-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 6 Unforced linear time-invariant system


dx
= −3x(t) x(0) = 2
dt
Rt
(−3)dτ
x(t) = 2 · e 0 = 2e−3t

Example 7 General first-order unforced linear time-invariant system

dx
α (t)x(t),
= |{z} initial condition x(t0 ) given (25)
dt const


x(t) = x(t0 )eα(t−t0 ) (26)

• Case 2: f (t) 6= 0 (Forced system)

dx
= α(t)x(t) + f (t) (27)
dt

dx

⇒ k(t) = k(t)α(t)x(t) + k(t)f (t) 

dx dk
dt

⇒ k(t) = − x(t) + k(t)f (t) ⇒
dk  dt dt
Determine k(t) such that − k(t)α(t) = 

dt

dx dk d
k(t) + x(t) = k(t)f (t) ⇒ (k(t)x(t)) = k(t)f (t) ⇒
dt dt dt
Z 
⇒ k(t)f (t) = k(τ )f (τ )dτ +c 



| {z
g(t)
}




t = t0 ⇒ k(t0 )x(t0 ) = g(t0 ) + c 
Z t
k(t)x(t) − k(t0 )x(t0 ) = g(t) − g(t0 ) = k(τ )f (τ )dτ ⇒
t0
k(t0 ) Z t
k(τ )

⇒ x(t) = x(t0 ) + f (τ )dτ 

k(t) t0 k(t)



dk
Rt
−α(s)ds


−k(t)α(t) = ⇒ k(t) = k(t0 )e t0


dt
Rt Z t Rt
α(τ )dτ α(s)ds
x(t) = e 0 x(t0 ) + e τ f (τ )dτ (28)
| {z } t0
Effect of initial conditions | {z }
Effect of forcing function

-10-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 8 General first-order forced linear time-invariant system

Eq. 28 for α = constant ⇒


Z t
α(t−t0 )
x(t) = e x(t0 ) + eα(t−τ ) f (τ )dτ (29)
| {z } t0
Effect of initial conditions | {z }
Effect of forcing function

Example 9 Forced first-order linear time-invariant system


dx
= −3x(t) + 1 x(0) = 0 (30)
dt

Z t Z t t
1 3τ 1

−3t −3(t−τ ) −3t −3t
x(t) = e ·0+ e · 1 · dτ = 0 + e 3τ
e dτ = e e = e−3t (e3t − 1)
0 0 3 0 3
1
= (1 − e−3t )
3

Sketch response:

Summary: Single linear ODE ⇒ exponential solution

-11-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.3 Second-order linear ordinary differential equations (ODE)


d2 x dx dx
2
+ α + bx(t) = f (t), α, b : constant, x(t0 ), (t0 ) : given
dt dt dt
d2 x
(What if the coefficient of dt2
is not 1?)

- Will show that x(t) is “exponential” or “periodic”:

Define auxiliary variables:

dy1

y1 (t)=x(t)
ˆ 
 = y2 (t)
dt


⇒ (why?)
dx  dy2
y2 (t)=
ˆ (t)


 = −αy2 (t) − by1 (t) + f (t)
dt dt

dy1       
 dt  0 1 y (t)
 1 +
0 y1 (t0 ) 
= given (why?)
  

 dy 
2 −b −a y2 (t) f (t) y2 (t0 ) 
| dt
| {z } | {z } | {z }
{z } A y(t) u(t)
dy
dt

Apply standard diagonalization procedure:



λ −1

|λI − A| = ⇒
b λ + α

Characteristic equation:
λ2 + αλ + b = 0 ⇒ (31)

Eigenvalues: √
−α ± α2 − 4b
λ1,2 = (32)
2
• Assume λ1 6= λ2 (i.e. α2 6= 4b)

Eigenvectors:
                 
λ
 1
−1  ν1  0 ν1 1 λ
 2
−1  w1  0 w1 1
= ⇒   =  , = ⇒ = 
b λ1 + α ν2 0 ν2 λ1 b λ2 + α w2 0 w2 λ2

-12-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Modal matrix:    
1 1 1 λ
 2
−1
P=
ˆ ⇒ P−1 =
λ1 λ2 λ2 − λ1 −λ1 1

Define z as:

y(t) = Pz(t) 

dz


⇒P = APz(t) + u(t) ⇒
dy  dt
= Ay(t) + u(t) 


dt

dz −1 −1
dt | {zAP} z(t) + P u(t) = Λz(t) + v(t) ⇒
= P
h i
λ 0 1
Λ=
0 λ2

dz1
 
= λ1 z1 (t) + ν1 (t) ⇒ z1 (t) = z1 (t0 )eλ1 (t−t0 ) + 0t eλ1 (t−τ ) ν1 (τ )dτ
 R 
 
dt Why?

 

 dz  Hint: Eq. 29
2
= λ2 z2 (t) + ν2 (t) ⇒ z2 (t) = z2 (t0 )eλ2 (t−t0 ) + 0t eλ2 (t−τ ) ν2 (τ )dτ

 R 

 
dt

Need initial conditions for z:


      
z (t )
 1 0 =
1 λ
 2
−1 y1 (t0 ) 1 λ y (t ) − y2 (t0 ) 
 2 1 0
=
z2 (t0 ) λ2 − λ1 −λ1 1 y2 (t0 ) λ2 − λ1 −λ1 y1 (t0 ) + y2 (t0 )


1 
(λ2 y1 (t0 ) − y2 (t0 )) eλ1 (t−t0 ) + tt0 eλ1 (t−τ ) ν1 (τ )dτ
R

 z1 (t) = 

λ2 − λ1

 

... ⇒
1
(−λ1 y1 (t0 ) + y2 (t0 )) eλ2 (t−t0 ) + tt0 eλ2 (t−τ ) ν2 (τ )dτ
 R 
 z2 (t) =

 


λ2 − λ1

      
y (t)
 1 =
1 1  z1 (t)  z1 (t) + z2 (t) 
=
y2 (t) λ1 λ2 z2 (t) λ1 z1 (t) + λ2 z2 (t)
| {z }
P

-13-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Therefore,
 
!
1 dx Z t
 
λ1 (t−t0 ) λ1 t
e−λ1 τ f (τ )dτ 
 
x(t) = y1 (t) =  λ2 x(t0 ) − (t0 ) e −e
λ2 − λ1 
 dt |
t0
{z }

| {z }
Effect of initial conditions Effect of forcing term

  (33)
!
1 dx Z t
 
−λ1 x(t0 ) + (t0 ) eλ2 (t−t0 ) +eλ2 t e−λ2 τ f (τ )dτ 
 
+ 
λ2 − λ1 
 dt |
t0
{z }

| {z }
Effect of initial conditions Effect of forcing term

Note: eλ1 t =mode 1, eλ2 t =mode 2.

2.3.1 Case 1: λ1 6= λ2 real, then Exponential solution

Two “modes”: eλ1 t , eλ2 t (Assumption λ1 6= λ2 )

Example 10 2nd -order forced linear ODE with exponential solution




 x(0) = 1
d2 x dx


2
+ 5 + 4x(t) = 1
dt dt  dx
(0) = −1



dt
Eq. 31 ⇒

λ2 + 5λ + 4 = 0 ⇒ λ1,2 = −1, −4
 

1 Z t
 
−t −t
eτ · 1 · dτ 
 
⇒ x(t) = (−4 − (−1))e − e

−4 − (−1)  |0 {z }

eτ |t0 =(et −1)

 

1 Z t
 
−4t −4t
e4τ · 1 · dτ 
 
+ (−(−1) + (−1))e
 +e
−4 − (−1)  |0 {z }

1 4τ t
4
e |0 = 41 (e4 t−1)

1 1
 
= −3e−t − (1 − e−t ) + 0 (1 − e−4t )
−3 4

-14-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2 1 1
= e−t + e−4t + (Verify!)
3 12 4

Sketch solution:

Mathematica:
DSolve[{x00 [t] + 5x0 [t] + 4x[t] == 1, x[0] == 1, x0 [0] == −1}, x[t], t]
nn oo
1 −4t
x[t] → 12 e (1 + 8e3t + 3e4t )
or
DSolve[{x00 [t] + 5x0 [t] + 4x[t] == 1, x[0] == 1, x0 [0] == −1}, x, t]
nn h ioo
1 −4t
x → Function {t}, 12 e (1 + 8e3t + 3e4t )

2.3.2 Case 2: λ1 , λ2 complex, then Oscillatory solution

λ1 , λ2 complex ⇒ λ1 , λ2 complex conjugate of each other, i.e.





 λ1 = |{z}
α +j β

 |{z}

 Re(λ1 ) Im(λ1 )
λ2 = λ̄1 ⇒
α −j β
λ2 = |{z}





 |{z}
Re(λ1 )

Im(λ1 )


−α2 +4b
where α = − α2 , β = 2
from Eq. 31. Eq. 33 ⇒
 
 
 ! 
 dx Z t 
λ̄1 x(t0 ) − (t0 ) eλ1 (t−t0 ) − eλ1 (t−τ ) f (τ )dτ +
 
 
1  dt t0

x(t) =
 
 ! Z t 
λ̄1 − λ1  dx 
| {z } 
+ −λ1 x(t0 ) + (t0 ) eλ̄1 (t−t0 ) + eλ̄1 (t−τ ) f (τ )dτ 
dt
 
Difference  t0 
of complex  }
conjugate | {z }| {z 
Difference of complex conjugate numbers! Difference of complex
numbers!
conjugate numbers!

-15-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

" ! ! #
1
Z t
dx
= 2A Im λ̄1 x(t0 ) − (t0 ) eλ1 (t−t0 ) + 2A Im e λ̄1 (t−τ )
f (τ )dτ (34)
2A Im(λ̄1 ) dt t0

Compute Explicitly:
Im(λ̄1 ) = Im(α − jβ) = −β (35)

Therefore
" ! #
dx
Im λ̄1 x(t0 ) − (t0 ) eλ1 (t−t0 ) =
dt
"( ) #
dx α(t−t0 )
= Im (α − jβ)x(t0 ) − (t0 ) e {cos[β(t − t0 )] + j sin[β(t − t0 )]}
dt
" #
α(t−t0 ) dx
=e αx(t0 ) sin[β(t − t0 )] − βx(t0 ) cos[β(t − t0 )] − (t0 ) sin[β(t − t0 )] (36)
dt

and
Z t  Z t 
λ̄1 (t−τ ) α(t−τ )
Im e f (τ )dτ = Im e {cos[β(t − τ )] − j sin[β(t − τ )]}f (τ )dτ
t0 t0
Z t
=− eα(t−τ ) sin[β(t − τ )]f (τ )dτ (37)
t0

Eqs. 34, 35, 36, 37 ⇒

dx
 
1 α(t−t0 )  αx(t0 ) sin[β(t − t0 )] − βx(t0 ) cos[β(t − t0 )] − (t0 ) sin[β(t − t0 )] 
x(t) = e dt
−β
 
− tt0 eα(t−τ ) sin[β(t − τ )]f (τ )dτ
R

(38)
Conclusion:
Exponential factor: eαt , α = Re(λ1 )
Periodic factor frequency: β = Im(λ1 )
Boundedness of solution (stability of system) depends only on α

α < 0 ⇔ system stability

-16-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 11 2nd -order linear ODE with oscillatory solution


 
 x(0) = −1 
d2 x dx

 

2
+ 4 + 5x(t) = 0 dx
dt dt (0) = 1 





dt

2 −4 ± 16 − 20
Eq. 31 ⇒ λ + 4λ + 5 = 0 ⇒ λ1,2 = = −2 ± j ⇒ α = −2, β = 1
2
Eq. 38 ⇒
1 −2t
x(t) = e [(−2)(−1) sin t − (1)(−1) cos t − (−1) sin t − 0] = e−2t (− sin t − cos t)
−1

Check:
x(0) = e(−2)(0) (−0 − 1) = −1

dx
(0) = −2e−2t (− sin t − cos t)|0 + e−2t (− cos t + sin t)|0 = −2 ∗ 1 ∗ (0 − 1) + 1 ∗ (−1 + 0) = 1
dt

dx
(t) = (−2)e−2t (− sin t − cos t) + e−2t (− cos t + sin t) = e−2t (3 sin t + cos t)
dt

dx2
(t) = (−2)e−2t (3 sin t + cos t) + e−2t (3 cos t − sin t) = e−2t (−7 sin t + cos t)
dt2

dx2 dx
2
(t) + 4 + 5x(t) = e−2t ((−7 + 4 ∗ 3 + 5 ∗ (−1)) sin t + (1 + 4 ∗ 1 + 5 ∗ (−1)) cos t) = 0
dt dt

Sketch solution:

Mathematica:
DSolve[{x00 [t] + 4x0 [t] + 5x[t] == 0, x[0] == 1, x0 [0] == −1}, x[t], t]
{{x[t] → e−2t (Cos[t] + Sin[t])}}
or
DSolve[{x00 [t] + 4x0 [t] + 5x[t] == 0, x[0] == 1, x0 [0] == −1}, x, t]
{{x → Function [{t}, e−2t (Cos[t] + Sin[t])]}}

-17-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.3.3 Case 3: λ1 = λ2 = λ, real, then Polynomial × Exponential solution


 α 
λ = −

 2 

α2 − 4b = 0

Use eigenvector, generalized eigenvector (Jordan form):


Eigenvector v : (λI − A)v = 0 ⇒
        
λ −1  ν1  0 ν1 1
 = ⇒ = 
b λ + α ν2 0 ν2 λ

Generalized eigenvector w : (λI − A)w = v ⇒


        
λ −1  w1   1  w1 1 
 = ⇒ =
b λ + α w2 λ w2 λ−1

Modal matrix:    
1 1  −λ + 1 +1
P= ⇒ P−1 = 
λ λ−1 +λ −1
Define z as

y(t) = Pz(t) 
dz


⇒P = APz(t) + u(t) ⇒
dy  dt
= Ay(t) + u(t) 

dt
dz −1 −1
J z(t) + v(t) ⇒
| {zAP} z(t) + P u(t) = |{z}
= P
dt | {z }
Jordan
 
λ −1
 
J= form f (t)
0 λ −f (t)

 Z t
dz1
= λz1 (t) − z2 (t) + f (t) ⇒ z1 (t) = z1 (t0 )e λ(t−t0 )
+ eλ(t−τ ) (−z2 (τ ) + f (τ )) dτ



dt

t0

Z t
 dz2
= λz2 (t) − f (t) ⇒ z2 (t) = z2 (t0 )eλ(t−t0 ) − eλ(t−τ ) (−z2 (τ ) + f (τ )) dτ



dt

t0

(Why? Hint: Eq. 29)

The rest is similar to Case 1, i.e. compute z1 (t0 ), z2 (t0 ); substitute.

-18-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 12 2nd -order linear ODE with polynomial×exponential solution


 
2
 x(0) = −1 
dx dx

 

+ 4 + 4x(t) = 0 dx
dt2 dt 

 (0) = 1 


dt

Eq. 31 ⇒ λ2 + 4λ + 4 = 0 ⇒ λ1 = λ2 = −2
   
1 1 3 1
P= ⇒ P−1 = 
−2 −3 −2 −1

After computing Jordan form, initial conditions for z:


      
z (0)  3
 1
1  −1 −2
= = ⇒
z2 (0) −2 −1 1 1
| {z } | {z }
P−1 x(0)
Z t
⇒ z2 (t) = (1)e−2t − e−2(t−τ ) · 0 · dτ = e−2t
0
Z t Z t
−2t −2(t−τ ) −2τ −2t −2t
⇒ z1 (t) = −2e + e · (−e + 0) · dτ = −2e +e (−1)dτ =
0 0

= −2e−2t − te−2t = (−2 − t)e−2t


x(t) = Pz(t) ⇒ x(t) = z1 (t) + z2 (t) = (−1 − t)e−2t

Check:
x(0) = −1

dx
= −e−2t + (−1 − t)(−2)e−2t |0 = 1
dt 0
d2 x h −2t 0
i h
−2t −2t
i
= (1 + 2t)e = 2e + (1 + 2t)(−2)e = (4t)e−2t
dt2
d2 x dx
2
+ 4 + 4x(t) = (4t + 4(1 + 2t) + 4(−1 − t))e−2t = 0
dt dt

Mathematica:
DSolve[{x00 [t] + 4x0 [t] + 4x[t] == 0, x[0] == −1, x0 [0] == 1}, x[t], t]
{{x[t] → −e−2t (1 + t)}}
or
DSolve[{x00 [t] + 4x0 [t] + 4x[t] == 0, x[0] == −1, x0 [0] == 1}, x, t]
{{x → Function [{t}, −e−2t (1 + t)]}}

-19-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.4 Second-order ODE, Two-point boundary value problems



d2 x dx x(t0 ) 
2
+ α + bx(t) = f (t) given
dt dt x(t1 ) 

dx
Need (t0 ) to find solution via Eq. 33!
dt

Idea:
dx dx
1. Express solution x(t) in terms of x(t0 ), (t0 ), i.e. x(t) = A(t)x(t0 ) + B(t) (t0 )
dt dt
dx dx
2. t = t1 : Solve for (t0 ) : x(t1 ) = A(t1 )x(t0 ) + B(t1 ) (t0 )
dt dt
dx x(t1 ) − A(t1 )x(t0 )
⇒ (t0 ) =
dt B(t1 )
dx x(t1 ) − A(t1 )x(t0 )
3. Substitute (t0 ) in step 1 to get x(t) = A(t)x(t0 ) + B(t)
dt B(t1 )

Boundary conditions may be more complicated e.g. they may involve derivatives.

Example 13 Solution of two-point boundary-value problem


 
d2 x dx  x(0) = 1 
+ 5 + 4x(t) = 0
dt2 dt  x(1) = 0.2648 

Eq. 31 ⇒ λ2 + 5λ + 4 = 0 ⇒ λ1,2 = −1, −4 ⇒


" ! ! #
1 dx dx
x(t) = −4 − (0) e−t + −(−1) + (0) e−4t ⇒
−4 − (−1) dt dt
" #
t=1 1 −4 1 dx 1 1 dx
⇒ 0.2468 = − (0) + 4 + 4 (0) ⇒
−3 e e dt e e dt
dx (−3)(0.2468) + 4e − 1
e4
(0) = = −2.0391 ⇒
dt − 1e + e14
1 h i
x(t) = (−4 − (−2.0391))e−t + (1 − 2.0391)e−4t = 0.6536e−t + 0.3464e−4t
−3

Mathematica:
DSolve[{x00 [t] + 5x0 [t] + 4x[t] == 0, x[0] == 1, x[1] == 0.2468}, x[t], t]
{{x[t] → e−4t (0.346373 + 0.653627e3t )}}

-20-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.5 Simple cases of second-order ODE (important in transport phenomena)


d2 y
= f (x) (39)
dx2
R R
dy R
⇒ = f (x)dx + C1 ⇒ [ f (x)dx] dx + C1 x + C2 .
R R
dx
Compute C1 , C2 from initial conditions or boundary conditions.

Example 14 Solution of Eq. 39


 
2
dy  y(0) = 1 
2
= e−x
dx  y(1) = 0 

dy Z −x
⇒ = e dx + C1 = −e−x + C1 ⇒
dx

−x −x
⇒ y(t) = −e dx + C1 x + C2 = e
R
+ C1 x + C2 







x = 0 ⇒ 1 + C1 · 0 + C2 = 1 ⇒ C2 = 0 ⇒ y(x) = e−x − 0.3679x,





x = 1 ⇒ e−1 + C1 = 0 ⇒ C1 = −0.3679

0≤x≤1

(ÒP Hwnthi∗ :Apply eigenvalue/eigen vector diagonalization procedure. Hint: Jordan


form.)

d2 y dy
2
+α = f (x) (40)
dx dx
dy dw
Let w(t)=
ˆ . Then + αw(x) = f (x). Apply 1st -order ODE methods.
dx dx

d2 y
+ αy(x) = f (x) (41)
dx2

λ2 + α = 0 ⇒ λ = ± −α
Case 1: −α > 0 ⇒ Exponential solution
Case 2: −α < 0 ⇒ Oscillatory solution


Homework not to hand in

-21-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 15 Heat conduction as a two-point boundary-value problem


d2 T
= U (T (x) − Ta ), T (0) = T0 , T (L) = TL
dx2

Eq. 31 ⇒

λ2 − U = 0 ⇒ λ1,2 = ± U (42)

Eq. 33 ⇒
....................................

Mathematica: DSolve[{T 00 [x] == U (T [x] − Ta), T [0] == T0, T [L] == TL}, T [x], x];
Simplify[%]
√ √ √ √ √ √ √
e− Ux
(e2L U (T0−Ta)−e U x Ta+e U (2L+x) Ta+e2 U x (−T0+Ta)+eL U (Ta−TL)+e U (L+2x) (−Ta+TL)
)
{{T [x] → −1+e2L U
√ }}

Example 16 Two-point boundary-value problem for heat or mass transfer


 
2 y(0) = 1
dy  
= −4y(x)  
dx2  y π4 = 1 

λ2 + 4 = 0 ⇒ λ = ±2j, α = 0, β = 2 (frequency)
" #
1 0 dy 1 dy
y(x) = e 0 − 2 · 1 · cos 2x − (0) sin 2x = cos 2x + (0) sin 2x
−2 dx 2 dx

π π 1 dy π dy
x= ⇒ 1 = cos + (0) sin ⇒ (0) = 2 ⇒ y(x) = cos 2x + sin 2x (Check!)
4 2 2 dx 2 dx

Mathematica: DSolve[{y 00 [x] + 4y[x] == 0, y[0] == 1, y[Pi/4] == 1}, y[x], x]


{{y[x] → Cos[2x] + Sin[2x]}}

-22-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.6 Simultaneous multiple first-order linear ODE: matrix analysis


      
dx1
 dt
  a11 · · · a1n   x1 (t)   f1 (t) 
 . 
 .  =  .. .. 
 .   . 
.   ..  +  ..  ,
    
 .   . x(0) given (43)
      
dxn
dt
a n1 · · · a nn x n (t) f n (t)
| {z } | {z }| {z } | {z }
dx A x(t) f (t)
dt

Assume distinct eigenvalues {λ1 , . . . , λn }, implying linearly independent eigenvectors


{v1 , . . . , vn }. Let P=
ˆ [v1 |v2 | . . . |vn ] and

x(t) = Pz(t) (44)

Substitute into Eq. 43 to get

dz dz −1
P = APz(t) + f (t) ⇒ =P AP} z(t) + P−1 f (t) ⇒
dt dt | {z | {z }
Λ v(t)
      
dz1
λ1 0 ···
0 z1 (t) ν1 (t)
 dt 
.. .. ..   ..   .. 
    
  
 .  0 . .  .   . 
   
= +
   

.. 
 ..

..   ..   .. 
 
. 0
 

 . 

 .
 
 .   . 
  
dzn
dt
0 · · · 0 λn zn (t) νn (t)
| {z } | {z } | {z } | {z }
dz Λ z(t) v(t)
dt

or
Z t
dzi
= λi zi (t) + νi (t) ⇒ zi (t) = zi (0)eλi t + eλi (t−τ ) νi (τ )dτ , i = 1, . . . , n
dt 0

by Eq. 29. Find zi (0) through Eq. 44 as x(0) = Pz(0).

Note: The final solution can be summarized as


Z t
A(t−t0 )
x(t) = e x(t0 ) + eA(t−τ ) f (τ )dτ (45)
t0

-23-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

where
 
eλ1 (t−t0 )  
.. λ1
 
 
A(t−t0 )
 . 
 −1  ..


e = P P , Λ=
ˆ . 
..
  
.
   
 
  λn
eλn (t−t0 )
 
eλ1 (t−τ )
..
 
 
 . 
 −1
eA(t−τ ) = P P , ˆ [v1 |v2 | . . . |vn ] ,
P=
..

.
 
 
 
eλn (t−τ )

Also note:
  
wT

λ (t−t )
| |  e 1 0   — —1 — 
Λ(t−t0 ) −1

 ..  .. 
Pe P x(t0 ) = v1 | · · · |vn   x(t0 )



 . 
 . 
   — — — 
| | eλn (t−t0 )
wnT

c1 cn
z }| { z }| {

= eλ1 (t−t0 ) v1 w1T x(t0 ) + . . . + eλn (t−t0 ) vn wnT x(t0 ) (46)

| {z } | {z }
Mode 1 Mode n

Conclusion: Solution is sum of n modes, each being

constant · eλi t vi (47)

ÒP Hwnthi: When will the solution remain bounded at all times t ≥ 0?

-24-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 17 Solution of simultaneous multiple first-order linear ODE


      
dx1
 dt  
−2 −2 0  x1 (t)  u1 (t) 
 dx2 
 dt  = 0

0 1  x2 (t) + 
  
u 2 (t)


      
dx3
dt
0 −3 −4 x3 (t) u1 (t) + u2 (t)
| {z } | {z }| {z } | {z }
dx A x(t) f (t)
dt

Solve in term of inputs u1 (t) = t, u2 (t) = 1 and initial conditions x(0) = [10 5 2]T .
   

−2 1 −2 
0 3/2 1/2
−1
λ1,2,3 = −1, −2, −3, P =  1 0 −1 ⇒ P = 1 4 2
   

   
−1 0 3 0 1/2 1/2
      
dz1
 dt 
−1 0 0  z1 (t) 1/2 2  
x=Pz  dz2 
 u (t)
 1 
=⇒  dt   0 −2 0  z2 (t) +  3
= 6
  
       u2 (t)
dz3
dt
0 0 −3 z3 (t) 1/2 1
  

17/2 

 
−1
x1 (t) = −14e−t + 127 −2t 58 −3t

z(0) = P x(0) =  34  e − e + 61 t − 47
 
 
4 9 36
 
 

  
 

7/2

 

 
⇒ x2 (t) = 7e−t − 29 −3t
9
e + 13 t + 11
9
z1 (t) = e z1 (0) + 12 t + 32 (1 − e−t )
−t 
 


 

 
e−2t z2 (0) + 23 t + 49 (1 − e−2t ) x3 (t) = −7e−t + 29 −3t 2
 
z2 (t) = e −

 

3 3



e−3t z3 (0) + 61 t + 18
5
(1 − e−3t )

z3 (t) =

Mathematica:
DSolve[{x10 [t] == −2x1[t] − 2x2[t] + t,
x20 [t] == x3[t] + 1,
x30 [t] == −3x2[t] − 4x3[t] + 1 + t,
x1[0] == 10,
x2[0] == 5,
x3[0] == 2},
{x1[t], x2[t], x3[t]}, t];
Simplify[%];
ExpandAll[%];
TableForm[Transpose[%]]
−3t −2t
x1[t] → − 4736
− 58e9 + 127e4 − 14e−t + t
6
−3t
x2[t] → 119
− 29e9 + 7e−t + 3t
−3t
x3[t] → − 32 + 29e3 − 7e−t

-25-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Theorem 1: Stability of multiple linear ODE


dx
= Ax(t) + f (t) is stable if and only if all eigenvalues of A are in the left half of the
dt
complex plane.

Definition 2: ODE stiffness (qualitative property)


dx
= Ax(t) + f (t) is stiff if the eigenvalues of A differ “a lot”, e.g. λi  λj < 0.
dt

Then eλi t goes to zero “a lot” faster than eλj t .

Example 18 Solution of simultaneous stiff ODE


    
dx1
 dt 
−10 1  x1 (t) x1 (0) = 1
dx2
=
dt
0 −0.1 x2 (t) x2 (0) = 2

“forcing”
dx1 ↓
x2 (t) = x2 (0)e−0.1t = 2e−0.1t ⇒ = −10x1 (t) + x2 (t) ⇒
dt
Z t t
−10t −10(t−τ ) −0.1τ −10t −10t 2 +9.9t

x1 (t) = e x1 (0) + e 2e dτ = e +e e
0 9.9
0
2 2 2
   
= e−10t + e−10t e+9.9t − 1 = 1 − e−10t + e−0.1t (Check!)
9.9 9.9 9.9

0.8
modes of x1(t)

0.6

-10t
0.4 e
-0.1t
e
0.2

0
0 0.2 0.4 0.6 0.8 1
t

-26-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.7 Higher-order ODE


dn x dn−1 x dn x
! ( )
dx dx
=f , . . . , , x(t) , Boundary conditions for x, , . . . , n (48)
dtn dtn−1 dt dt dt

Convert to system of first-order ODE:


Define  

 y 1 (t) = x(t) 
 dy1
=y2 (t)

 
 dt
dx
y (t) = (t) dy2
 
=y3 (t)
 

 2 dt



 .
 
 dt
..

..
⇒ .
 

 .
..

 dyn−1
=yn (t)

 

dt

 

 
 n−1
 dyn
d x =f (yn (t), yn−1 (t), . . . , y2 (t), y1 (t))
 

yn (t) = dtn−1 (t)  dt
n n
o
and boundary conditions for x, dx
dt
, . . . , ddtnx become B.Cs for {y1 , y2 , . . . , yn−1 } (How?)

Example 19 The Falkner-Skan Equation (viscous fluid flow)







x00 + xx00 + 1
c−1
(1 − x02 ) = 0

x(0) = 0


(c = 2 for plane flow and c = 3 for axisymmetric flow)




x0 (0) = 0
x0 (∞) = 1


 
y1 = x 







y10 = y2
y 2 = x0 
⇒ y20 = y3 y1 (0) = y2 (0) = 0, y2 (∞) = 1
y3 = x00 y30 = −y1 y3 − 1
 
(1 − y22 )

 

c−1

Example 20 Velocity profile for viscous flow in porous ducts







f 0000 + Re(f 0 f 00 − f 000 ) = 0
f 0 (1) = 0







f (1) = 1 where Re is the Reynolds number
f 0 (−1) = 0







f (−1) = 1


 
y1 =f 







y10 = y2 y2 (1) = 0
= f0 y20
 
y2 = y3 y1 (1) = 1

 

⇒
y3 = f 00 






y30 = y4 y2 (−1) = 0
= f 000 y40
 
y4 = −Re(y2 y3 − y4 ) y1 (−1) = 1

 

-27-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

2.8 Nonlinear ordinary differential equations



y(x) ∈ R

y 0 (x) = f (x, y(x))


ODE: x∈R


y(x0 ) = y0 , given
f :R×R→R

Theorem 2: Existence and uniqueness of ODE solution is locally guaranteed if f, ∂f /∂y


are locally continuous
 
f :R×R→R  α < x < β, γ < y < δ where 
∂f
continuous for ⇒
∂y
:R×R→R  α < x0 < β, γ < y0 < δ 

 
 y 0 (x) = f (x, y(x))
⇒∃h>0: has a unique solution in (x0 − h, x0 + h) ⊂ (α, β)
y(x0 ) = y0 

Proof : Omitted.
Graphical interpretation: Fill in graph.

Example 21 Nonlinear ODE with multiple solutions


y 0 (x) = u(x)y(x)m
0 < m < 1, u(x) continuous
y(0) = y0

 Z x  1
1−m
Solution: y(x) = y01−m + (1 − m) u(s)ds
0

m
Solution is unique for y0 6= 0. This was expected by Theorem 2, since f (x, y(x))=u(x)y(x)
ˆ
and ∂f
∂y
(x, y(x))=u(x)my(x)
ˆ m−1
are continuous at (0, y0 ).

-28-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

 Z x  1
1−m
However two solutions exist for y0 = 0: y(x) = (1 − m) u(s)ds or y(x) = 0
0

∂f
because continuity for ∂y
is not satisfied at (0, 0):

∂f 1 ∂f 1
= u(x)my(x)m−1 = u(x)m 1−m
⇒ = u(0)m 1−m = ∞
∂y y(x) ∂y (x,y(x))=(0,0)
0

Example 22 Nonlinear ODE whose solution explodes in finite time


y 0 (x) = u(x)y(x)p
p > 1, u(x) continuous
y(0) = y0

y0
Solution: y(x) = h i 1

1 − (p − 1)y0p−1
Rx p−1
0 u(s)ds
Z x1
1
Notice that y(x1 ) = ∞ for x1 < ∞ such that u(s)ds =
0 (p − 1)y0p−1
(Why?)

2.9 Software for analytical solution of ODE


-Mathematica command: DSolve

-29-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

3 NUMERICAL INTEGRATION
Course Learning Objectives
Use the trapezoidal and Simpson’s rules to numerically integrate a function

3.1 Why numerical integration?


Rb
a f (x)dx not known in explicit form for most f (x)
Rb R 1 −x2
a f (x)dx not expressed as a finite number of fundamental functions e.g. 0 e dx

Even if there exists a closed-form expression, it may be unwieldy,


!
Z
dx 1 52/3 + 51/3 x + x2 1 −1 2x + 5
1/3
e.g. = ln + tan +C
5 − x3 52/3 6 5(51/3 − x)2 52/3 31/2 51/3 31/2

Example 23 Calculation of residual entropy


Z P
R dP
S = (Z − 1)
0 P
Data for saturated isobutene vapor at 360 K (Smith & Van Ness: ChE Thermodynamics)
P (bar) 0.1 0.5 2 4 6 8 10 12 14 15.41
Z 0.99737 0.98907 0.96483 0.93635 0.90734 0.87586 0.84077 0.80103 0.75506 0.71727

How to compute integral?

3.2 How to do numerical integration?


Integration

Trapezoidal, Romberg, Gauss Quadrature


Simpson’s rules (will not be covered)

Numerically Analytically
Integration: Easy Difficult
Differentiation: Difficult Easy

-30-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 24 Numerical differentiation vs numerical integration


Z b
df
=? IL ≤ f (x)dx ≤ IU
dx x=x1 a

3.3 General integration approach


Integrate interpolating polynomial (easy) rather than integrating original function (difficult):
Rb 
I≡ a f (x)dx 





 

f (x0 ) = y0 






 

f (x1 ) = y1 
 

  Z b Z b
known points

..

.

 ⇒ f (x)dx ≈ p(x)dx


 

 a a

f (xn ) = ym
 





f (x) = p(x) +e(x)




| {z } 


interpolating



polynomial

Resulting formulas: Trapezoidal rule, Simpson’s rules.

-31-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

3.4 The trapezoidal rule

f (b) − f (a)
f (x) ≈ p(x) = a0 + a1 x = f (a) + (x − a) ⇒
b−a

Theorem 3: Trapezoidal rule


Z b Z b
f (a) + f (b)
I=
ˆ f (x)dx ≈ p(x)dx = (b − a) (49)
a a 2
1 00
Truncation error E = Itrue − Iapprox ≈ − 12 f (ξ)(b − a)3 , a≤ξ≤b

3.5 Multiple application of the trapezoidal rule

Theorem 4: Multiple application of trapezoidal rule


Z b Z b Pn−1
f (x0 ) + 2 i=1f (xi ) + f (xn )
I=
ˆ f (x)dx ≈ p(x)dx = (b − a) (50)
a a 2n

Pn
(b − a)3 i=1 f 00 (ξi ) (b − a)3 ¯00
E=− ˆ−
= f , xi−1 ≤ ξi ≤ xi
12n2 n 12n2

-32-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

R3
Example 25 Calculate numerically the integral 0 (1 − e−2x )dx
−2x
The analytical integral can be calculated as x + e 2 |30 = 2.50124. If we evaluate the function
with a bin size equal to one, h = 1:

x 0 1 2 3
f (x) 0.0000 0.8647 0.9817 0.9975

0 + 2 ∗ (0.8647 + 0.9817) + 0.9975


I = (3 − 0) = 2.34
2∗3

h n E I
1 3 0.1561 2.3451
0.5 6 0.0409 2.4604
How would results change with h? 0.25 12 0.0103 2.4909
0.125 24 0.0026 2.4986
0.0625 48 0.0006 2.5006
0.03125 96 0.0002 2.5011

3.6 Simpson’s 1/3 rule

f (x) ≈ p(x)
= a0 + a1 x + a2 x 2
(x − x1 )(x − x2 ) (x − x0 )(x − x2 ) (x − x0 )(x − x1 )
= f (x0 ) + f (x1 ) + f (x2 ) ⇒
(x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 ) (x2 − x0 )(x2 − x1 )
| {z }
Lagrange polynomial

-33-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Theorem 5: Simpson’s 1/3 Rule


Z b Z b
h
I=
ˆ f (x)dx ≈ p(x)dx ≈ [f (x0 ) + 4f (x1 ) + f (x2 )] (51)
a a 3
1 (4)
E=− f (ξ)h5 , a≤ξ≤b
90

3.7 Multiple application of Simpson’s 1/3 rule


Z x2 Z x4 Z xn
I= ... + ...... + ⇒
x0 x2 xn−2

Theorem 6: Multiple application of Simpson’s 1/3 rule


Pn−1 Pn−2
f (x0 ) + 4 i=1,3,5,... f (xi ) + 2 j=2,4,6... f (xj ) + f (xn )
I ≈ (b − a) (52)
3n
Pn
(b − a)5 i=1 f (4) (ξi )
E=− , xi−1 ≤ ξi ≤ xi
180n4 n
Need to have even number of segments!

Example 26 Revisit Example 25

For h = 0.5, n = 6:

x 0.0000 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000


f (x) 0.0000 0.6321 0.8647 0.9502 0.9817 0.9933 0.9975

0 + 4 ∗ (0.6321 + 0.9502 + 0.9933) + 2 ∗ (0.8647 + 0.9817) + 0.9975


I = (3 − 0) ≈ 2.5
3∗6

Compare to trapezoidal for same h !

-34-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

3.8 Simpson’s 3/8 rule

f (x) ≈ p(x) = a0 + a1 x + a2 x2 + a3 x3 ⇒

Theorem 7: Simpson’s 3/8 rule


Z b Z b
3h
I=
ˆ f (x)dx ≈ p(x)dx ≈ [f (x0 ) + 3f (x1 ) + 3f (x2 ) + f (x3 )] (53)
a a 8
3 5 (4)
E=− h f (ξ), a≤ξ≤b
80

Example 27 Revisit Example 25

For h = 1, n = 3:

x 0 1 2 3
f (x) 0.0000 0.8647 0.9817 0.9975

3
I= [0 + 3 ∗ (0.8647 + 0.9817) + 0.9975] = 2.451
8

Multiple application using Simpson’s 1/3 rule:


Remark: Simpson’s 1/3: even segments n

Simpson’s 3/8: odd segments n

Can combine both rules! Therefore if n = 9 we can use Simpson’s 1/3 for first 6 segments
and Simpson’s 3/8 for last 3 !

-35-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

4 NUMERICAL DIFFERENTIATION
Course Learning Objectives
Perform numerical differentiation

4.1 Why numerical differentiation?


• Solve differential equations numerically by converting them to algebraic equations

• Approximate derivatives when analytical computation is expensive (e.g. Jacobian in


Newton-Raphson)

Example 28 Differential equations for temperature in CSTR


dT
Solve = AT + B + CeD/T (t) . T (0) given
dt

Analytical solution: Determine function T (t) = . . . , for t ≥ 0 (Is it feasible?)


Numerical solution: Determine values T (t1 ), T (t2 ), . . . , T (tn ) “numerically”
• Approximate derivatives to avoid symbolic computation of derivatives in derivative-
based methods (e.g. Newton-Raphson)

Example 29 Develop the secant method from Newton-Raphson


ÒP Hwnthi

4.2 How to do numerical differentiation?


Basic idea:

f (x0 + h) − f (x0 )
f 0 (x0 )=
ˆ lim
h→0 h
f (x) − f (x0 )
= lim
x→x0 x − x0
∆f
= lim
∆x→0 ∆x

Approximate f 0 (x) as
f (x0 + h) − f (x0 )
f 0 (x0 ) ≈ , h “small”
h
Caution: h may be “too” small, so that in computer’s memory f (x0 ) = f (x0 + h) from which
f 0 (x0 ) = 0!

-36-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 30 Numerical calculation of derivative

f (x + h) − f (x) f (x + h) − f (x)
f (x) = x2 , f 0 (x) = lim ≈ , h small
h→0 h h
Let x = 1, use arithmetic with three significant digits

h f (1+h)−f (1) ∼
= f 0 (1)
h
1.21−1
0.1 0.1
= 2.1
1.00−1
0.001 0.001
=0
True value: f 0 (1) = 2

Spreadsheet calculation
h df /dx|x=1
1 3
0.01 2.01
0.0001 2.0001
0.000001 2.000001
1E-08 2
1E-10 2
1E-12 2.000178
1E-14 1.998401
1E-16 0

4.3 Basic approaches to numerical differentiation

Numerical Differentiation

Use Small h Richardson extrapolation


Taylor Series (Iterative)
(Will not be covered)

-37-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

4.4 Basic formulas for first-order derivatives

f (x0 + h) − f (x0 )
• Forward difference: f 0 (x0 ) ≈
h

f 00 (ξ) 2
Proof: f (x0 + h) = f (x0 ) + f 0 (x0 )h + h ⇒
2!
f (x0 + h) − f (x0 ) f 00 (ξ)
⇒ f 0 (x0 ) = − h , x0 ≤ ξ ≤ x0 + h
| h
{z } | 2{z }
First-order approximation Truncation error
Forward difference O(h)
Forward finite divided difference

Geometric interpretation:

Finite−difference
approximation

f (x0 ) − f (x0 − h)
• Backward difference: f 0 (x0 ) ≈
h

f 00 (ξ) 2
Proof: f (x0 − h) = f (x0 ) − f 0 (x0 )h + h ⇒
2!

0 f (x0 ) − f (x0 − h) f 00 (ξ)


⇒ f (x0 ) = + h, x0 − h ≤ ξ ≤ x0
h 2
Geometric interpretation:

-38-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

f (x0 + h) − f (x0 − h)
• Centered difference: f 0 (x0 ) ≈
2h

f 00 (x0 ) 2 f 000 (ξ1 ) 3



0 
f (x0 + h) = f (x0 ) + f (x0 )h + h + h 

2! 3!


Proof: ⇒
f 00 (x0 ) 2 f 000 (ξ2 ) 3 
f (x0 − h) = f (x0 ) − f 0 (x0 )h +

h − h



2! 3!
⇒ f (x0 + h) − f (x0 − h) = 2f 0 (x0 )h + O(h3 ) ⇒

f (x0 + h) − f (x0 − h)
⇒ f 0 (x0 ) = + O(h2 )
2h
Geometric interpretation:

-39-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

4.5 Approximation of higher-order derivatives

f (x0 + 2h) − 2f (x0 + h) + f (x0 )


• Forward difference: f 00 (x0 ) ≈
h2

Proof:
f 00 (x0 ) 2
2f (x0 + h) = 2f (x0 ) + 2f 0 (x0 )h + 2 h + 2O(h3 ) (54)
2!
f 00 (x0 )
f (x0 + 2h) = f (x0 ) + f 0 (x0 )2h + (2h)2 + 2O((2h)3 ) (55)
2!

Equation 55 − Equation 54 ⇒

f (x0 + 2h) − 2f (x0 + h) = −f (x0 ) + f 00 (x0 )h2 + O(h3 ) ⇒

f (x0 + 2h) − 2f (x0 + h) + f (x0 )


f 00 (x0 ) = + O(h)
h{z2 | {z }
truncation
| }
second forward finite divided difference error

Geometric interpretation:

f (x0 +h+h)−f (x0 +h) f (x0 +h)−f (x0 )


f 0 (x0 + h) − f 0 (x0 ) −
Note: f 00 (x0 ) ≈ w h h
h h
f (x0 + 2h) − 2f (x0 + h) + f (x0 )
=
h2
f (x0 ) − 2f (x0 − h) + f (x0 − 2h)
• Backward difference: f 00 (x0 ) = + O(h)
h2

f (x0 + h) − 2f (x0 ) + f (x0 − h)


• Centered difference: f 00 (x0 ) = 2
+ O(h2 )
h

-40-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

4.6 Additional formulas


• Forward difference with O(h2 ) truncation error

−f (x0 + 2h) + 4f (x0 + h) − 3f (x0 )


f 0 (x0 ) = + O(h2 )
2h

f (x0 + h) − f (x0 ) f 00 (x0 )



0
− h + O(h2 )

f (x0 ) = 

h 2


Proof: ⇒
f (x0 + 2h) − 2f (x0 + h) + f (x0 ) 
f 00 (x0 ) =

+ O(h)



h2
−f (x0 + 2h) + 4f (x0 + h) − 3f (x0 )
⇒ f 0 (x0 ) = + O(h2 )
2h
Geometric interpretation:

Note: A large number of finite-difference formulas exist. Check textbook for comprehensive
list.

Example 31 Numerical approximation of derivatives

f (x) = −0.1x4 − 0.15x3 − 0.5x2 − 0.25x + 1.2


f 0 (0.5) = −0.9125 (True value)

Approximation, h = 0.25 true error%


f 0 (0.5) w -1.155 Forward O(h) -26.5
-0.714 Backward O(h) 21.7
-0.934 Centered O(h2 ) -2.4
-0.859 Forward O(h2 ) 5.82
-0.878 Backward O(h2 ) 3.77
-0.9125 Centered O(h4 ) 0

-41-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5 NUMERICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS


(ODE)
Course Learning Objectives
• Solve ordinary differential equations with applications in chemical reaction kinetics and
flow control, using Eulers and Runge-Kutta methods

5.1 Why numerical solution of ODE?


It may not be feasible (or easy) to find the solution of a differential equation analytically.

Example 32 Analytical solution of ODE describing liquid level in tank

dh q
ODE (From mass balance): A = Fi (t)−K h(t)
dt
(56)
Analytical solution:
Determine function h(t) for t ≥ 0, given h(0) and Fi (t)

5.2 What is numerical solution of ODE?


Calculate values of ODE solution at selected points of domain over which ODE is defined.

Example 33 Define numerical solution of ODE in Example 32

Define numerical solution of Eq. 56:

Consider points {t0 , t1 , t2 , . . .} and calculate values h1 (t), h(t2 ), . . . h(tn ), given
h(t0 ) = h(0) and Fi (t)

5.3 How are ODE solved numerically?


Convert ODE to set of algebraic equations using

- Finite-difference approximation of derivatives, or

- The finite-element approach (not covered in these notes).

-42-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.4 The finite-difference method for numerical solution of ODE


[ Derivatives ← Finite divided differences ] ⇒I [ ODE ← Algebraic equations]

Several variants, depending on finite-difference formulas used.

Example 34 Solution of ODE in Example 32 using Finite Differences

Equation 56: Approximate derivative at time t = tk by forward finite divided difference:

dh q
A = Fi (t) − K h(t) ⇒
dt

dh q
A (tk ) = Fi (tk ) − K h(tk ) ⇒
dt

h(tk+1 ) − h(tk ) q
A ≈ Fi (tk ) − K (h(tk )
tk+1 − tk

Notation:

hk =h(t
ˆ k) (57)

ˆ k+1 − tk
δt=t (58)

δt
 q 
hk+1 = hk + Fi,k − K hk , h0 = h(0), given (59)
A

Calculate {h1 , h2 , . . .} recursively:

-43-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

-44-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

- What if centered or backward finite divided differences are used?

Example 35 Example 34 revisited using centered finite differences

Equation 56: Approximate derivative at time t = tk by centered finite divided difference:

dh q
A = Fi (t) − K h(t) ⇒
dt

dh q
A (tk ) = Fi (tk ) − K h(tk ) ⇒
dt

h(tk+1 ) − h(tk−1 ) q
A ≈ Fi (tk ) − K (h(tk )
tk+1 − tk−1

| {z }
| {z }
hk+1 −hk−1
Fi,k −K hk
2δt

Notation:

2δt
 q 
hk+1 = hk−1 + Fi,k − K hk , h0 = h(0), given (60)
A

Calculate h1 from h0 using Eq. 59, then calculate {h2 , h3 , . . .} recursively from Eq. 60.

Would you expect more accurate solution for Example 35 than for Example 34?

-45-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

-
Anything suspicious in the above figure?

-46-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.5 Numerical solution of initial-value ODE problems: General idea


dy
Solve = f (x, y(x)), y(0) given
dx

Numerical solution

yi+1 = F (yi , yi−1 , . . . , xi , xi−1 , . . . , ; h) , y0 = y(0), given (61)

where
ˆ i+1 − xi
h=x (62)
• What is F ?

– Single-step methods: yi+1 = F (yi , xi , h) (e.g., Euler, Runge-Kutta)


– Multi-step methods: yi+1 = F (yi , yi−1 , . . . , xi , xi−1 , . . . ; h) (e.g. Adams-Bashforth-
Moulton)

• How small should h be?

5.6 Euler’s method


dy
Solve = f (x, y(x)), y(0) given
dx
dy dy y(xi+1 ) − y(xi )
= f (x, y(x)) ⇒ (xi ) = f (xi , y(xi )) ⇒ ≈ f (xi , y(xi )) ⇒
dx dx xi+1 − xi

Euler’s method: yi+1 = yi + f (xi , yi )h , y0 = y(0), given (63)

where
ˆ i+1 − xi
h=x (64)

-47-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 36 Solution of ODE using Euler’s method


Example 34 is Euler’s method!

Example 37 Solution of ODE using Euler’s method


dy
= 3x2 , y(0) = 1
dx
Exact solution: dy = 3x2 dx ⇒ y = x3 + 1
Euler’s method: yi+1 = yi + 3x2i h

i xi yi yexact
0 0 1 1
1 0.1 1.000 1.001
h = 0.1 ⇒ 2 0.2 1.003 1.008
3 0.3 1.015 1.027
4 0.4 1.042 1.064
5 0.5 1.090 1.125

dy
(ÒP Hwnthi: Solve: dx = −y, y(0) = 1, using forward, backward, and centered differ-
entiation formulas i.e. dx ≈ yi+1h−yi , dx
dy dy
≈ yi −yhi−1 , and dx
dy −yi−1
≈ yi+12h . Compare accuracies
for different values of h.)

5.7 How accurate is Euler’s method?


• What is meant by “accurate”?

• Sources of inaccuracy:

1. Round-off error (True for all methods)

Example 38 Round-off error

a = 2.54 a = 2.54 4
b = 3.14 but b = 3.14 3
a + b = 5.68 a + b = 5.68 7 ≈ 5.69
- Round-off error inherent in floating-point arithmetic.
- Not a serious concern for simple problems.

-48-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

*−→ Local Error


2. Truncation error
−→ Global Error (Local+Propagated)

What is “truncation error”?


Taylor series ⇒

y 00 (xi ) y (n) (xi )


y(xi+1 ) =y(xi ) + y 0 (xi )(xi+1 − xi ) + (xi+1 − xi )2 + . . . + (xi+1 − xi )n
2! n!
y (n+1) (ξ)
+ (xi+1 − xi )n+1 , ξ ∈ [xi , xi+1 ]
(n + 1)!
| {z }
Truncation error ⇔ O(hn+1 )

or

(n)
y 00 y y (n+1) (ξ) n+1
yi+1 = yi + yi0 h + i h2 + . . . + i hn + h , ξ ∈ [xi , xi+1 ] (65)
2! n! (n + 1)!
| {z }
Truncation error ⇔ O(hn+1 )

dy
All derivatives of y at xi can be computed in terms of f , to solve dx
= f (x, y(x))
given y(0).

Apply Eq. 65 to Euler’s method:

y (2) (ξ) 2
yi+1 = yi + yi0 h + h ⇒ yi+1 = yi + f (xi , yi )h +O(h2 ) ⇒
| 2!{z }
| {z }
Euler’s formula
Truncation error ⇔ O(h2 )

If the solution is a linear equation then error-free! Hence, Euler’s is a first-order method !

Theorem 8: Local and global error in Euler’s method

Elocal = O(h2 )
Eglobal = O(h)

(Which is bigger, global or local error?)

-49-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.8 How to improve the accuracy of Euler’s method?


• Use smaller h

• Use more terms in Taylor series:

yi00 2 y (n) n
Taylor series method: yi+1 = yi + yi0 h + h + ... + h + Rn+1 (66)
2! n! | {z }
O(hn+1 )

where

yi0 = f (yi , xi )
" # " # " #
d ∂f dy ∂f ∂f ∂f
yi00 0
= f (yi , xi )=
ˆ f (y(x), x) = + = f (y(x), x) +
dx (yi ,xi )
∂y dx ∂x (yi ,xi )
∂y ∂x (yi ,xi )

yi000 = f 00 (yi , xi ) = . . .

Example 39 Improving accuracy of Euler’s method

dy
Solve: = 3x2 , y(0) = 1 (Exact solution: y = (x3 + 1))
dx
Euler’s method: yi+1 = yi + 3x2i h

Taylor series: yi+1 = yi + 3x2i h + 3xi h2


|{z} |{z}
fi f0
i
2

i xi yi, Euler , h = 0.1 yi, Euler , h = 0.05 yi, Taylor yi, exact
0 0 1 1 1 1
1 0.05 - 1 1 1.000125
2 0.1 1 1.000375 1.00075 1.001
3 0.15 - 1.001875 1.003 1.003375
4 0.2 1.003 1.00525 1.0075 1.008

-50-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.9 Extensions of Euler’s method (OPTIONAL)


What is considered an improvement?
• Heun’s modification (Predictor-Corrector method):
0
Predictor: yi+1 = yi + f (xi , yi )h (67)
0
f (xi , yi ) + f (xi+1 , yi+1 )
Corrector: yi+1 = yi + h (68)
2

Graphical interpretation:
Fill in the graph.

Theorem 9: Local and global error in Heun’s method


Elocal = O(h3 ), Eglobal = O(h2 )

• Improved-polygon method:
h
yi+ 1 = yi + f (xi , yi ) (69)
2 2
yi+1 = yi + f (xi+ 1 , yi+ 1 )h (70)
2 2

Theorem 10: Local and global error in improved-polygon method

Elocal = O(h3 ), Eglobal = O(h2 )

Graphical interpretation:
Fill in the graph.

-51-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.10 Introduction to Runge-Kutta methods


yi+1 = yi + ϕ(xi , yi , h)h
ϕ = a1 k1 + a2 k2 + . . . + an kn

where k1 = f (xi , yi )
k2 = f (xi + p1 h, yi + q11 k1 h)
k3 = f (xi + p2 h, yi + q21 k1 h + q22 k2 h)
..
.
kn = f (xi + pn−1 h, yi + qn−1,1 k1 h + qn−1,2 k2 h + . . . + qn−1,n−1 kn−1 h)

Order n and coefficients p, q are selected to make truncation error of Runge-Kutta equivalent
to Taylor series method, without explicitly calculating derivatives.
Runge-Kutta order, n Runge-Kutta local error Runge-Kutta global error
2 O(h3 ) O(h2 )
3 O(h4 ) O(h3 )
4 O(h5 ) O(h4 )
ÒP Hwnthi: n = 1 ⇒ Runge-Kutta=?

5.11 Derivation of the second-order (n=2) Runge-Kutta method (OPTIONAL)


yi+1 = yi + (a1 k1 + a2 k2 )h (71)

where k1 = f (xi , yi )

∂f ∂f
k2 = f (xi + p1 h, yi + q11 k1 h) ≈ f (xi , yi ) + p1 h + q11 k1 h + O(h2 )
∂x xi ∂y xi

Therefore, Eq. 71 ⇒

2 ∂f ∂f
yi+1 = yi + a1 f (xi , yi )h + a2 f (xi , yi )h + a2 p1 h + a2 q11 h2 f (xi , yi ) + O(h3 ) (72)
∂x xi ∂y xi

Need values of a1 , a2 , p1 , q11 to make Eq. 72 like Taylor series:

0 h2
yi+1 = yi + f (xi , yi )h + f (xi , yi ) + O(h3 ) (73)
| {z } 2
( ∂f
∂x
+ ∂f dy
∂y dx )(x ,y )
i i

-52-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Comparing Eqs. 72 and 73 ⇒ 


a1 + a2 = 1 



a2 p1 = 1/2 

a2 q11 = 1/2

Three equations, four unknowns!

• Option 1:
a2 = 1/2 ⇒ a1 = 1/2, p1 = q11 = 1 ⇒

1 1
 
yi+1 = yi + k1 + k2 h
2 2
k1 = f (xi , yi ), k2 = f (xi + h, yi + h)

(Heun’s predictor-corrector method, Eqs. 67, 68)

• Option 2:
a2 = 1 ⇒ a1 = 0, p1 = q11 = 1/2 ⇒

yi+1 = yi + k2 h
1 1
 
k2 = f xi + h, yi + hk1 , k1 = f (xi , yi )
2 2

(Improved-polygon method, Eqs. 69, 70)

• Option 3 (Provides minimum bound on truncation error):


a2 = 2/3 ⇒ a1 = 1/3, p1 = q11 = 3/4 ⇒

1 2
 
yi+1 = yi + k1 + k2 h
3 3
3 3
 
k2 = f xi + h, yi + hk1 , k1 = f (xi , yi )
4 4

(Ralston’s method)

-53-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.12 The fourth-order (n=4) Runge-Kutta method


A de facto standard!

1
 
Runge-Kutta method: yi+1 = yi + (k1 + 2k2 + 2k3 + k4 ) h (74)
6

where k1 = f (xi , yi )
 
k2 = f xi + 12 h, yi + 12 h k1
 
k3 = f xi + 12 h, yi + 12 h k2
 
k4 = f xi + h, yi + h k3

(Note that if f (x, y) = f (x) then RK becomes Simpson’s 1/3 rule)

Theorem 11: Local and global error of the fourth-order Runge-Kutta method

Elocal = O(h5 ), Eglobal = O(h4 )

Example 40 Comparison of Euler, Taylor-series & Runge-Kutta methods

dy
Solve: = −y(x) + cos x, y(0) = 3
dx

Exact (analytical) solution from Mathematica:


Mathematica:
DSolve[{y 0 [x] == −y[x] + Cos[x], y[0] == 3}, y[x], x];
nn oo
y[x] → 21 (5e−x + Cos[x] + Sin[x])

Euler’s method: yi+1 = yi + h(−yi + cos xi )

h2
Taylor series: yi+1 = yi + h(−yi + cos xi ) + (yi − cos xi − sin xi ) (How?)
2
4th -order Runge-Kutta: (ÒP Hwnthi)

-54-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Solution on spreadsheet:

-55-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

5.13 Multi-step methods


• What are multi-step methods?∗
See Section 5.5. Graphically:
Single−step ODE integration method Multi−step ODE integration method

• Why multi-step methods?


Better Accuracy!

• How do multi-step methods work?

dy
Solve = f (x, y)
dx
yi+1 − yi−1
y 0 (xi ) ≈ ⇒ y i + 1 = y i − 1 + f (xi , y i )2h + O(h3 ) (75)
2h | {z } | {z } |{z}
↑ ↑ ↑
current point previous
point before point
previous

yi+1 − yi
Compare with Euler’s mehod: y 0 (xi ) ≈ ⇒
h

yi + 1 = y i + f (xi , y i )h + O(h2 ) (76)


| {z } |{z} |{z}
↑ ↑ ↑
current previous previous
point point point

Even though Eq. 75 appears to be more accurate than Eq. 76, it may be unstable, i.e. it may
produce errors that grow from step to step without bound! (See Example 35) To stabilize
it, use Eq. 75 in predictor-corrector scheme.


Actually, the correct term that should be used is “multi-point”, not “multi-step”.

-56-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 41 Adams-Bashforth formulas (Explicit)


4th order Adams-Bashforth:

h
yi+1 = yi + [55fi − 59fi−1 + 37fi−2 − 9fi−3 ] (77)
24

5th order Adams-Bashforth:

h
yi+1 = yi + [1901fi − 2774fi−1 + 2616fi−2 − 1274fi−3 + 251fi−4 ] (78)
720

where fi =f
ˆ (xi , y(xi )).

To use Eqs. 77 or 78, use some other method for the first 3 or 4 integration steps, respectively.

Example 42 Adams-Moulton formulas (Implicit)


4th order Adams-Moulton:

h
yi+1 = yi + [9fi+1 + 19fi − 5fi−1 + fi−2 ] (79)
24

5th order Adams-Moulton:

h
yi+1 = yi + [251fi+1 + 646fi − 264fi−1 + 106fi−2 − 19fi−3 ] (80)
720

where fi =f
ˆ (xi , y(xi )). Note the Eqs. 79 and 80 are implicit formulas, i.e. yi+1 appears both
on the left and right-hand sides (Where?). Some predictor-corrector iterations are needed
to compute yi+1 .

5.14 Software for numerical solution of ODE


MATLAB: >> help ode45
>> help ode23
>> help ode113
(ÒP Hwnthi: What methods does MATLAB use?)

Mathematica: NDSolve
(ÒP Hwnthi: What methods does Mathematica use?)

-57-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 43 Numerical solution of ODE in Example 40 using MATLAB

Create m-file RightHandSideODE.m first:

function F = RightHandSideODE(x,y)
F = - y + cos(x);

Run Matlab code (in command line or m-file) next:

>> ode45(@RightHandSideODE,[0 20],[3]);

2.5

1.5
y(x)

0.5

−0.5

−1
0 2 4 6 8 10 12 14 16 18 20
x

-58-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 44 Numerical solution of ODE in Example 40 using Mathematica

temp = NDSolve[{y 0 [x] == −y[x] + Cos[x], y[0] == 3}, y[x], {x, 0, 20}]
Plot[y[x]/.temp[[1]], {x, 0, 20}]
{{y[x] → InterpolatingFunction[{{0., 20.}}, <>][x]}}

3
2.5
2
1.5
1
0.5

5 10 15 20
-0.5

Example 45 Numerical solution of ODE in Example 32 using MATLAB


Create m-file tankODE.m first:

MATLAB
function f = tankODE (t , h )
%
% define variables A and K as global ,
% to assign numerical values to them once
% outside this function
%
global A K
if t < 5
Fi = 4;
else
Fi = 0;
end
f = ( Fi - K * sqrt ( h ))/ A ;

Run Matlab line of code next:

>> global A K;A=1;K=2;ode45(@tankODE,[0 8],[2]); xlabel(’t’); ylabel(’h(t)’)

-59-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

3.5

2.5

h(t)
1.5

0.5

−0.5
0 1 2 3 4 5 6 7 8
t

-Compare to Example 34 - Solution of ODE in Example 32 using Finite Differences.


-Why are the dots in the above graph not spaced equally? (Hint: See Section 5.15))

Example 46 Numerical solution of ODE in Example 32 using Mathematica


A = 1;
K = 2;
Fi[t ]:=If[t < 5, 4, 0]
temp = NDSolve[{h0 [t] == (Fi[t] − K ∗ Sqrt[h[t]])/A, h[0] == 2}, h[t], {t, 0, 8}]
Plot[h[t]/.temp[[1]], {t, 0, 8}, PlotRange->All];
NDSolve::mxst : Maximum number of 10000 steps reached at the point t == 6.983225252574072`. More . . .

{{h[t] → InterpolatingFunction[{{0., 6.98323}}, <>][t]}}

1 2 3 4 5 6 7

You can switch method:

A = 1;
K = 2;
Fi[t ]:=If[t < 5, 4, 0]
temp = NDSolve[{h0 [t] == (Fi[t] − K ∗ Sqrt[h[t]])/A, h[0] == 2}, h[t], {t, 0, 8}, Method → "BDF"]
Plot[h[t]/.temp[[1]], {t, 0, 8}, PlotRange->All];

-60-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

NDSolve::ndcf : Repeated convergence test failure at t == 6.947545890585031`; unable to continue. More . . .

{{h[t] → InterpolatingFunction[{{0., 6.94755}}, <>][t]}}

2 4 6 8

5.15 Step size adaptation


• Why adapt the size of each step when integrating ODE numerically?

Example 47 Adaptation of ODE integration step size

Observe Example 43 and Example 45. Why are some points spaced less densely than others?

Example 48 Adaptive Runge-Kutta method

Several variants, e.g. simple adaptive doubling/halving of integration step or Runge-Kutta-


Fehlberg.

-61-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

6 NUMERICAL SOLUTION OF SYSTEMS OF ORDINARY DIFFEREN-


TIAL EQUATIONS
Course Learning Objectives
• Solve ordinary differential equations with applications in chemical reaction kinetics and
flow control, using Eulers and Runge-Kutta methods

dy1

= f1 (x, y1 (x), y2 (x), . . . , yn (x))



dx






dy2 

= f2 (x, y1 (x), y2 (x), . . . , yn (x))

dy


dx ⇔ = f (x, y(x)) (81)
 dx
.. 


.





dyn 

= fn (x, y1 (x), y2 (x), . . . , yn (x))



dx
   
y1 f1
   
y  f 
 2  2
y=
ˆ  , f=
ˆ   , f : <n+1 → <n (82)
 ..   .. 
 .   . 
   
yn fn

Use the same formulas as for single equations (e.g. Euler, Runge-Kutta), after replacing the
scalar unknowns with vectors.

Example 49 Develop Euler’s method for multiple ODE

Euler’s method for a single ODE:

Eq. 63: yk+1 = yk + hf (xk , yk ) , y0 = y(0), given (83)

Euler’s method for a multiple ODE:


 
Vectorize 63: y(k+1) = y(k) + hf xk , y(k) , y(0) = y(0), given (84)

-62-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Explicitly:
 
(k+1) (k) (k) (0)
y1 = y1 + hf1 x(k) , y1 , . . . , yn(k) y1 = y1 (0)
.. .
. , .. (85)
 
(k)
yn(k+1) = yn(k) + hfn x(k) , y1 , . . . , yn(k) yn(0) = yn (0)

Extremely easy to program!

Example 50 Numerical solution of multiple ODE using Euler’s method


Solve
dy1


 = −0.5y1 (x), y1 (0) = 4
dx



 dy2

 = 4 − 0.3y2 (x) − 0.1y1 (x), y2 (0) = 6
dx
Eq. 85 ⇒
(i+1) (i) (i) (0)
y1 = y1 − h0.5y1 , y1 = 4
 
(i+1) (i) (i) (i) (0)
y2 = y2 − h 4 − 0.3y2 − 0.1y1 , y2 = 6

ÒP Hwnthi: Program above formulas in spreadsheet for h = 0.5.

x y1 y2
0 4 6
.. ..
0.5 . .
.. ..
1.0 . .
.. ..
1.5 . .
2.0 1.265625 9.094875

ÒP Hwnthi: What is the exact (analytical) solution of the above system of ODE?

Example 51 Numerical solution of multiple ODE using Euler’s method∗


The conversion of glucose to gluconic acid is a simple oxidation of the aldehyde group of the
sugar to a carbonyl group. This transformation can be achieved by a microorganism in a
fermentation process. The enzyme glucose oxidase, present in the microorganism, converts
glucose to glucononlactone. In turn, the gluconolactone hydrolyzes to form the gluconic acid.
The overall mechanism of the fermentation process which performs the transformation can
be described as follows:

Constantinides, A., Numerical Methods for Chemical Engineers, Prentice-Hall

-63-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Cell growth: Glucose + cells → cells


Glucose oxidase
Glucose oxidation: Glucose + O2 −−−−−−−−→ Gluconolactone + H2 O2

Gluconolactone hydrolysis: Gluconolactone + H2 O → gluconic acid


Catalase
Peroxide decomposition: H2 O2 −−−−→ H2 O+1/2O2

A mathematical model of the fermentation of the bacterium Pseudomonas ovalis, which


produces gluconic acid can be summarized as follows:
!
dy1 y1 (t)
Rate of cell growth: = b1 y1 (t) 1 − (86)
dt b2
dy2 b3 y1 (t)y4 (t)
Rate of gluconolactone formation: = − 0.9082b5 y2 (t) (87)
dt b4 + y4 (t)
dy3
Rate of gluconic acid formation: = b5 y2 (t) (88)
dt
!
dy4 b3 y1 (t)y4 (t)
Rate of glucose consumption: = −1.011 (89)
dt b4 + y4 (t)

where
y1 (t) = cell concentration at time t
y2 (t) = gluconolactone concentration at time t
y3 (t) = gluconic acid concentration at time t
y4 (t) = glucose concentration at time t
b1 , . . . , b 5 = parameters of the system, which are functions of temperature and pH. At
the operating conditions of temperature 30◦ C and pH 6.6, the values of the
five parameters b1 , . . . , b5 were determined from experimental data to be
b1 = 0.949, b2 = 3.439, b3 = 18.72, b4 = 37.51, b5 = 1.169.

The initial conditions are:

y1 (0) = 0.5 U.O.D./ml


y2 (0) = 0.0 mg/ml
(90)
y3 (0) = 0.0 mg/ml
y4 (0) = 50.0 mg/ml

-64-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Solve Eqs. 86-89 with initial conditions Eq. 90 using Euler’s method, Eq. 85, on spreadsheet:

-65-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 52 Develop fourth-order Runge-Kutta for multiple ODE

Runge-Kutta method for n ODE (Eqs. 81, 82):

1
 
(i+1) (i)
y =y + (k1 + 2k2 + 2k3 + k4 ) h (91)
6

where  
k1 = f xi , y(i)
 
k2 = f xi + 12 h, y(i) + 21 h k1
 
k3 = f xi + 12 h, y(i) + 21 h k2
 
k4 = f xi + h, y(i) + h k3

Explicitly:

(i+1) (i)
yj = yj + 61 (k1j + 2k2j + 2k3j + k4j )h , j = 1, . . . , n, i = 0, 1, . . . (92)

where  
(i) (i)
k1j = fj x(i) , y1 , y2 , . . . , yn(i)
 
(i) (i)
k2j = fj x(i) + h2 , y1 + h k211 , y2 + h k212 , . . . , yn(i) + h k1n
2
 
(i) (i)
k3j = fj x(i) + h2 , y1 + h k221 , y2 + h k222 , . . . , yn(i) + h k2n
2
 
(i) (i)
k4j = fj x(i) + h, y1 + hk31 , y2 + hk32 , . . . , yn(i) + hk3n

-66-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 53 Numerical solution of multiple ODE on MATLAB

Solve 51 using the Runge-Kutta method on Matlab. Create m-file BioSystemODE.m first:

MATLAB
function fVector = BioSystemODE ( time , yVector )
%
% define variables b1 , b2 , b3 , b4 , b5 as global ,
% to assign numerical values to them once
% outside this function
%
global b1 b2 b3 b4 b5
%
% compute right - hand side of vector equations dy / dt = f (t , y ( t ))
%
fVector = [ b1 * yVector (1)*(1 - yVector (1)/ b2 );
b3 * yVector (1)* yVector (4)/( b4 + yVector (4)) - 0.9082* b5 * yVector (2);
b5 * yVector (2);
-1.011* b3 * yVector (1)* yVector (4)/( b4 + yVector (4))]

Run Matlab code (preferably in m-file, e.g. BioSystemODEMain.m) next:

MATLAB
%
% assign numerical values to global variables b1 , b2 , b3 , b4 , b5 ,
% to be able to use such values in all sub - programs
%
global b1 b2 b3 b4 b5
b1 = 0.949;
b2 = 3.439;
b3 = 18.72;
b4 = 37.51;
b5 = 1.169;
% initial conditions
yVector0 = [0.5 0 0 50];
% set time inverval for numerical solution
timeInterval = [0 10];
%
% call ODE solver and label plot
%
[T , Y ] = ode45 ( @BioSystemODE , timeInterval , yVector0 );
plot (T , Y (: ,1) , ’o - ’ ,T , Y (: ,2) , ’s - ’ ,T , Y (: ,3) , ’* - ’ ,T , Y (: ,4) , ’v - ’)
xlabel ( ’t ’ );
ylabel ( ’ y_1 ( t ) , ␣ y_2 ( t ) , ␣ y_3 ( t ) , ␣ y_4 ( t ) ’ );
legend ( ’y_1 , ␣ cell ␣ conc . ␣ ( UOD / ml ) ’ ,...
’y_2 , ␣ gluconolactone ␣ conc . ␣ ( mg / ml ) ’ ,...
’y_3 , ␣ gluconic ␣ acid ␣ conc . ␣ ( mg / ml ) ’ ,...
’y_4 , ␣ glucose ␣ conc . ␣ ( mg / ml ) ’)

-67-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

60
y1, cell conc. (UOD/ml)
y , gluconolactone conc. (mg/ml)
2
y3, gluconic acid conc. (mg/ml)
50 y4, glucose conc. (mg/ml)

40
y1(t), y2(t), y3(t), y4(t)

30

20

10

0
0 1 2 3 4 5 6 7 8 9 10
t

-68-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 54 Numerical solution of multiple ODE on Mathematica

Example 51 solved using the Runge-Kutta method on Mathematica.

Mathematica
b1 = 0.949; b2 = 3.439; b3 = 18.72; b4 = 37.51; b5 = 1.169;
temp = NDSolve [{ y1 ’[ t ] == b1 * y1 [ t ]*(1 - y1 [ t ]/ b2 ) ,
y2 ’[ t ] == b3 * y1 [ t ]* y4 [ t ]/( b4 + y4 [ t ]) - 0.9082* b5 * y2 [ t ] ,
y3 ’[ t ] == b5 * y2 [ t ] , y4 ’[ t ] == -1.011* b3 * y1 [ t ]* y4 [ t ]/( b4 + y4 [ t ]) ,
y1 [0] == 0.5 , y2 [0] == 0 , y3 [0] == 0 , y4 [0] == 50} , { y1 [ t ] , y2 [ t ] ,
y3 [ t ] , y4 [ t ]} , {t , 0 , 10}]
Plot [{ y1 [ t ] /. temp [[1]] , y2 [ t ] /. temp [[1]] , y3 [ t ] /. temp [[1]] ,
y4 [ t ] /. temp [[1]]} , {t , 0 , 10} ,
PlotStyle -> { Dashing [{0.0 , 0.0}] , Dashing [{0.01 , 0.01}] ,
Dashing [{0.02 , 0.02}] , Dashing [{0.04 , 0.04}]} ,
AxesLabel -> { " t " , None } ,
PlotLegends -> { " y1 ( t ) " , " y2 ( t ) " , " y3 ( t ) " , " y4 ( t ) " }]

50

40
y1(t)
30 y2(t)
y3(t)
20
y4(t)
10

t
2 4 6 8 10

-69-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

7 STABILITY, INSTABILITY, AND CHAOS


Course Learning Objectives
• Determine the stability and stiffness of differential equations

7.1 ODE stability, instability, and chaos


• Extremely important concepts!
• Can be defined in many ways, all interrelated to each other. Also see Definition 5.

Definition 3: Loose definition of stability of a set of ODE


n o
The set of n ODEs dy dt
= f (t, y(t)), y(0) = given is said to be stable if a small change on
the initial condition y(0) results in a change in the solution y(t) that remains small.
Example 55 ODE stability w.r.t. initial conditions around a steady state
Consider the tank in Example 32:

ODE (From mass balance and constitutive


equation):
q
dh Fi (t) − K h(t)
= (93)
dt A

ODE system values: A = 1 m2 , K = 2 m5/2/min, Fi (t) = 2.8 m3/min. ODE solution for initial
conditions {1.5, 1.7, 1.9, 2.1, 2.3, 2.5} (Mathematica code in Apprendix A):
h(t)

2.4

2.2

2.0

1.8

1.6
t
2 4 6 8

-70-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Remark: It can be shown that Eq. 93 is stable over the set H ={h|0ˆ ≤ h}, i.e. for any two
initial conditions ha (0) in H and hb (0) in H that are close to each other, the corresponding
solutions ha (t) and hb (t) of Eq. 93 remain close to each other at all t ≥ 0 as well. In fact, all

solutions converge to the steady state hs , computed as 0 = 2.8−21 hs ⇒ hs = 1.96 m.

Example 56 Stability of ODE with time-varying forcing term

Consider again the tank in Example 32.


ODE system values: A = 1 m2 , K = 2 m5/2/min, Fi (t) = 2.8(1 + sin t) m3/min ⇒

dh q
= 2.8(1 + sin t) − 2 h(t) (94)
dt

ODE solution for initial conditions {1.5, 1.7, 1.9, 2.1, 2.3, 2.5} (Mathematica code in
Apprendix B):

h(t)

t
2 4 6 8

Example 57 Linear ODE stability (Recall Theorem 1)

Solve Eq. 30 in Example 9, in terms of initial condition x(0) for general forcing term u(t):
Z t
dx
= −3x(t) + u(t) ⇒ x(t) = e−3t x(0) + e−3(t−τ ) u(τ )dτ (95)
dt 0

Consider |x1 (t) − x2 (t)|, where x1 (t), x2 (t) are solutions of Eq. 95 corresponding to different
initial conditions x1 (0) 6= x2 (0): Eq. 95 ⇒:

|x1 (t) − x2 (t)| = e−3t |x1 (0) − x2 (0)| (96)

-71-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Eq. 96 implies:
|x1 (0) − x2 (0)| < ε ⇒ |x1 (t) − x2 (t)| < ε (97)

i.e.
|x1 (0) − x2 (0)| small ⇒ |x1 (t) − x2 (t)| small (98)

(Why?)
ODE solution for u(t) = 2.8(1 + sin[20t]) and initial conditions {1.5, 1.7, 1.9, 2.1, 2.3, 2.5}
(Mathematica code in Apprendix C):

x(t)

2.5

2.0

1.5

1.0
t
0.2 0.4 0.6 0.8 1.0

Example 58 Linear ODE instability (Recall Theorem 1)


Z t
dx
= 3x(t) + u(t) ⇒ x(t) = e3t x(0) + e3(t−τ ) u(τ )dτ (99)
dt 0

Consider |x1 (t) − x2 (t)|, where x1 (t), x2 (t) are solutions of Eq. 99 corresponding to different
initial conditions x1 (0) 6= x2 (0): Eq. 99 ⇒:

|x1 (t) − x2 (t)| = e3t |x1 (0) − x2 (0)| (100)


Eq. 100 implies:

|x1 (t) − x2 (t)| → ∞ as t → ∞, regardless if |x1 (0) − x2 (0)| < ε (101)

-72-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

x(t)

70

60

50

40

30

20

10

t
0.2 0.4 0.6 0.8 1.0

(Mathematica code similar to code for Example 57 in Appendix C)

-73-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 59 (In)Stability of nonlinear ODE for CSTR dynamics

Consider the CSTR in Example 2, assuming V = constant. Then, Eq. 10 implies Fi = F ,


and Eqs. 11 and 12 along with the constitutive Eqs. 13 and 14 yield the ODEs
!
dCA F (t) −E
= (CAi − CA (t)) − k0 exp CA (t)
dt V RT (t)
! (102)
dT F (t) −E U At
= (Ti − T (t)) + Jk0 exp CA (t) − (T (t) − Tc (t))
dt V RT (t) ρcp V

Consider parameter values:


V = 1.36 m3 (volume of liquid in reactor)
71
k0 = 7.08 × 10 /h (reaction rate constant)
J = 0.02775 m3 K/mol (heat of reaction per mole per density per specific heat)
E/R = 8375 K (activation energy over gas constant)
U At 3
ρcp
= 0.7 m /h (heat transfer coefficient × heat exchange area per density per
specific heat)
CAi = 8008 mol/m3 (inlet concentration of A)
Ti = 373.3 K (inlet temperature)

Assume that the flowrate F (t) and coolant temperature Tc (t) are held constant at values:
F = 1.133 m3/h
(103)
Tc = 420 K
Then, the ODEs in Eq. 102 are stable around the equilibrium points (steady states)
(CA = 7496.08, T = 399.915)
(104)
(CA = 1126.66, T = 509.167)
and unstable around the equilibrium point (steady state)

(CA = 4241.65, T = 455.737) (105)

(ÒP Hwnthi: How are the steady states of Eqs. 102 shown in Eqs. 104 and 105 defined
and computed?)

ODE solution for initial values around each of the three steady states in Eqs. 104 and 105
(See Appendix D for the Mathematica code used for numerical solution and to create the
following figures)

-74-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Steady states in Eq. 104 are locally stable: (What does that mean?)

(T (0), CA (0)) around (T, CA ) = (399.9, 7496.)

(T (0), CA (0)) around (T, CA ) = (509.2, 1127.)

Steady state in Eq. 105 is locally unstable: (What does that mean?)

(T (0), CA (0)) around (T, CA ) = (455.7, 4242.)

-75-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Definition 4: Loose definition of chaos


A set of nonlinear differential (or difference) equations is chaotic if initial conditions close to
each other produce very different solutions.

Example 60 Chaotic dynamics of nonlinear ODE for weather forecasting∗


Lorentz equations:
dx
= σ(y(t) − x(t))
dt
dy
= rx(t) − y(t) − x(t)z(t) (106)
dt
dz
= −bz(t) + x(t)y(t)
dt
(ÒP Hwnthi: Do a web search on Edward Lorenz, chaos. Find journal article: Lorenz, E.
N., “Deterministic nonperiodic flow”, J. Atmos. Sci., 20, pp.130-141, 1963)
Let σ = 1−, b = 8/3, r = 28, and (a) x(0) = y(0) = z(0) = 5 (continuous line)
(b) x(0) = 5.001, y(0) = z(0) = 5 (dashed line)
(See Appendix E for Mathematica code)

20 20

15 15

10 10

5 5

0 0

-5 -5

-10 -10

-15 -15

0 5 10 15 20 0 5 10 15 20

Integration with accuracy goal of 4 significant Integration with accuracy goal of 10 significant
digits digits

Solution accuracy becomes increasingly smaller as time progresses. Eventually, all accuracy
is lost!

• Why does the solution of Eqs 106 appear random after a certain point, even though the
equations are deterministic?

Explain in terms of the following



Chapra & Canale, Numerical Methods for Engineers, McGraw-Hill.

-76-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 61 Chaotic discrete-time dynamics resulting from Euler’s method

Dynamics of population growth with saturation:

dx
= 3x(t) − 4x(t)2 , x(0) = 0.9331 (107)
dt

(ÒP Hwnthi: Solve Eq. 107 numerically)


Euler’s method with δt = 1 ⇒

Logistic equation: xk+1 = 4xk (1 − xk ) , x0 = given (108)

It can be shown rigorously that:


xk = [sin(πyk )]2 ∗
, k = 1, 2, 3, . . . (109)
yk = IntegerPart[2yk+1 ]

Interpretation of Eq. 109b in the binary system: If, for some k, yk−1 is known with pre-
cision of, say, 5 binary digits, e.g., yk−1 = 0.11010 (corresponding to the decimal number
1
2
+ 212 + 203 + 214 + 205 = 16
13
= 0.8125, then:

yk is known with 5-1=4 binary digits, after removal of the first digit following the “0.” in yk−1

Namely yk = 0.1010 (corresponding to the decimal number 12 + 202 + 213 + 204 = 58 = 0.625).
Continuing with the same pattern, yk+1 = 0.010, yk+2 = 0.10, yk+3 = 0.0, yk+4 = 0., and
yk+5 (hence xk+5 , via Eq. 109a will be completely random!

Therefore, to be able to know yk (hence xk via Eq. 109a) with reasonable precision as k → ∞,
the precision of the initial condition x0 (equivalently y0 ) must also go to infinity. Because x0
is only known with finite precision, xk will lose all precision after a finite number of iterations
of Eq. 108. Subsequent values of xk will be (and look) random!

Visualize in the following spreadsheet:


IntegerPart[x] is the largest integer less than x

-77-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

-78-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

-79-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

7.2 Linear ODE stability


• Easy to assess.
Recall Section 2.6 and Theorem 1-Stability of multiple linear ODEs.

7.3 Stability, instability, and chaos of numerical schemes for ODE integration
Definition 5: Loose definition of stability and instability of numerical methods∗
A numerical method is unstable if small errors made at one stage of the method are magnified
in subsequent stages, and seriously degrade the accuracy of the overall computation. A
numerical method is stable if small errors made at one stage of the method are not magnified
in subsequent stages, and the accuracy of the overall computation remains insensitive to
such errors.

Example 62 Conditional Stability of Euler’s method

Revisit Example 34, for step change in the inlet flow rate Fi (t) = 2.9 m3/min. Euler’s method:

Euler’s method is conditionally stable. Runge-Kutta has similar behavior.


Kincaid, D. and W. Cheney, Numerical Analysis, 2nd Ed., Brooks/Cole, 1996

-80-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 63 Unconditional instability of multi-step method


Revisit Example 35, for step change in the inlet flow rate Fi (t) = 2.9 m3/min. Integration
method (Eq. 60) employs derivative approximation by centered difference.

Even though (a) this numerical scheme (Eq. 60) results from derivative approximation by
centered-differences, which are more accurate than forward differences (cf. Example 62)
and (b) the integration step is smaller (δt = 0.2 vs. δt = 1 for Example 62), the resulting
numerical solution of Eq. 56 is less accurate (because of instability, i.e. eventual huge growth
of error) than the numerical solution computed by the forward-difference scheme (i.e. Euler’s
method) in Example 62. In fact, it can be shown that this scheme is unconditionally unstable:
Instability will persist for any δt.

Example 64 Explicit vs Implicit Euler


Consider the equation:
dx
= λx (110)
dt
with x(0) = x0 and λ < 0. The analytical solution is x = x0 eλt and since λ < 0, x should
drop to zero given sufficient time. Application of Euler’s method as seen previously (explicit
method) would be:

xi+1 = xi + f (xi )δt ⇒ xi+1 = xi + λxi δt = xi (1 + λδt) (111)

-81-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

where δt = h step size. For xi+1 to decrease |(1 + λδt)| < 1 or −1 < 1 + λδt < 1 and given
that λ < 0, δt < −2
λ
. Otherwise the method will be unstable!
Consider an implicit Euler’s scheme:

xi+1 = xi + f (xi+1 )δt ⇒ xi+1 = xi + λxi+1 δt (112)

The unknown xi+1 appears on both sides so bringing everything to the left side

xi
xi+1 (1 − λδt) = xi ⇒ xi+1 =
(1 − λδt)

which will make xi+1 to decrease to zero unconditionally (regardless step size)!
Notes:

• If we had a system of ODEs with f linear, the implicit method would result to a linear
system of unknowns to be solved using i.e. Gauss elimination! (check Example 50)

• For a system of ODEs, stability for the explicit method requires that δt is smaller than
the inverse of the largest (by absolute) eigenvalue of the right-hand side matrix! See
Section 2.6.

• What if f or f is nonlinear equation ? ⇒ Newton-Raphson or other non-linear solver


for implicit scheme!

• While the implicit scheme addresses stability, it is still a first order method in terms
of accuracy!

• Stability could be evaluated herein because we knew the analytical solution.

Example 65 Removing chaos from Euler’s method or improving stability

Recall chaos in Example 61. Eq. 107 can be easily integrated with δt smaller than δt = 1,
e.g. δt = 0.1. (ÒP Hwnthi: verify that!)

-82-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

7.4 ODE stiffness


Definition 6: Stiff ODEs
A set of ODE is stiff if different dependent variables vary at very different rates as functions
of the independent variable. To determine stiffness check Definition 2.

Example 66 Stiff ODEs

Revisit Example 1: Eqs 2 and 4 along with the constitutive equations Q = U At (Tc − T ) and

F =a V ⇒
dV Fi (t) q
= − a V (t) (113)
dt ρ
dT Fi (t) U At (Tc (t) − T (t))
= (Ti (t) − T (t)) + (114)
dt V (t)ρ ρcp V (t)

Fi (t)
Parameter values: ρ
= 0.3 m3/min Initial conditions: V (0) = 1 m3
a = 0.2 m3/2/min T (0) = 300 K
Ti (t) = 300 K
U At
ρcp
= 0.2 m3/min
Tc (t) = 500 K

Numerical integration results (See Mathematica code in Appendix F):

2.2 380

2.0
360
1.8
V (m^3)

T(K)

1.6 340

1.4
320
1.2

1.0 300
0 20 40 60 80 0 20 40 60 80
t (min) t (min)

-83-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

7.5 Software for numerical solution of stiff ODE


- MATLAB: >> help ode15s
>> help ode23s
>> help ode23t
>> help ode23tb
(ÒP Hwnthi: What methods does MATLAB use?)

- Mathematica: NDSolve

(ÒP Hwnthi: What methods does Mathematica use?)

-84-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

8 HIGHER-ORDER DIFFERENTIAL EQUATIONS & BOUNDARY-VALUE


PROBLEMS
Course Learning Objectives
• Know how to apply numerical techniques for boundary-value problems

Numerical solution technique for


- Linear boundary-value ODE problems are “reasonable”
- Nonlinear boundary-value ODE problems can be “arbitrarily difficult”!
Solution Techniques

Shooting Method Collocation


(unreliable) Finite differences (very robust)
(reasonable) PDE based

8.1 The shooting method for boundary-value problems (BVP)


Example 67 The Shooting Method for Heat conduction two-point BVP
Revisit Example 15:

d2 T
= U (T (x) − Ta ), T (0) = T0 , T (L) = TL (115)
dx2
with U = 100, T0 = 350, TL = 550, Ta = 75, L = 1.1.

Define

dz1
 
z1 (x)=T
ˆ (x) 
 
 = z2 (x),
XX 
z1 (0) = T0 ,  (L)
z1 X
X=XTX
L
dx

 

⇒ (116)
dT   dz2
z2 (x)=
ˆ (x)
 

 
 = U (z1 (x) − Ta ), z2 (0) = guess
dx dx
-85-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Yes
Guess Integrate Eqs 113 from 0 to L OK

No

(117)

Let z2 (0)(1) = −2700, z2 (0)(2) = −2800


(ÒP Hwnthi: What does z2 (0) represent geometrically on the z1 (x) vs. x plot?)

⇒ z2 (0)(3) = −2749.721 by Eq. 117. (ÒP Hwnthi: Verify z2 (0)(3) )

(For the following figures see MATLAB code in Appendix G)


10 5

z (1 ) (x)
1
(2 )
1 z 1 (x)

z (3
1
)
(x)
(x)

0.5
(3 )
1
z (x),

0
(2 )
1
(x), z

-0.5
)
z (1
1

-1

0 0.2 0.4 0.6 0.8 1


x

-86-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Final solution T(x) in detail


550

500

450

400

350
z 1 (x)

300

250

200

150

100

0 0.2 0.4 0.6 0.8 1


x

ÒP Hwnthi: Why is the shooting method so sensitive to z2 (0) guesses? (Hint: See Eq. 42).

Example 68 Limitations of the Shooting Method

Resolve Example 67 for U = 100, T0 = 350, TL = 550, Ta = 75, L = 100 .


The method crashes as shown below.
10 305

z (1
)
1
(x)
15
z (2 )
(x)
1

z (3 )
(x)
1
(x)

10
(3 )
1
z
(2 )
1
(x),

5
z (1
1
)
(x), z

-5

0 20 40 60 80 100
x

ÒP Hwnthi: Why does the shooting method crash for this example? (Hint: See Eq. 42)

-87-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Generalize:
Solve
d00 y 
0 00 (n−1)

= f x, y, y , y , . . . , y , 0≤x≤L (118)
dx00
n o
with boundary conditions on y, y 0 , y 00 , . . . , y (n−1) at x = 0 and at x = L.

• Step 1: Create a set of first-order ODE equivalent to Eq. 118 , by defining new variables
{y1 (x), . . . , yn (x)}:
 
y10 = [y 0 =]y2
 


 y1 =y
ˆ 










0 00
   
ˆ 0
y2 =y y2 = [y =]y3

 
 
 


 
 
 

   
0 000
   
ˆ 00
y3 =y y3 = [y =]y4

 
 
 

   
.. ⇔ ..



 .







 . 



   
0 (n−1)
ˆ (n−2)
   



 yn−1 =y







 yn−1 = [y =]yn 



   h  i 
0 (n) 0 (n−1)
ˆ (n−1)
   

yn =y
 
 yn = y = f x, y, y , . . . , y = f (x, y1 , y2 , . . . , yn ) 

(119)
n o
0 (n−1)
Convert boundary conditions for y, y , . . . , y to boundary conditions for
{y1 , y2 , . . . , yn }.

• Step 2: Use the available initial conditions on {y1 , y2 , . . . , yn } and guess the missing
ones, to integrate Eqs. 119 forward, from 0 to L.

• Stwp 3: Compare the given boundary values on {y1 , y2 , . . . , yn } at L with the values
calculated in Step 2. If there are discrepancies, adjust the missing initial values on
{y1 , y2 , . . . , yn } (how?) and repeat Step 2. Else, stop.

- Three iterations are needed for linear problems.

- More iterations are needed for nonlinear problems (nonlinear ODE or boundary con-
ditions). Eq. 117 can be used recursively.

-88-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

8.2 Finite-difference methods for boundary-value problems


• Substitute derivatives by finite differences to convert ODE to a set of algebraic equations.

Example 69 Finite-difference solution of the heat conduction TPBVP

Revisit Example 15:

d2 T
= U (T (x) − Ta ), T (0) = T0 , T (L) = TL (120)
dx2
with U = 100, T0 = 350, TL = 550, Ta = 75, L = 1.1.

Use centered differences:

d2 T Ti+1 − 2Ti + Ti−1


2
≈ = U (Ti − Ta ) ⇒
dx h2
⇒ −Ti−1 + (2 + U h2 )Ti − Ti+1 = U h2 Ta , i = 1, . . . , n − 1, T0 = T0 , Tn = TL (121)

i.e.  
λ −1 · · · 0  T   T + U h2 T 
0
1 0 a
−1 λ −1 . . .
.. 
 
    
2
 .   T2   U h Ta 
   


. . .

. 
. . . . . . 0   ..  = 

.
..

(122)
     
 0 
    
 .
 . ..  
 Tn−2   U h2 Ta 
 
 . . −1 λ −1     
2

0 · · · 0 −1 λ Tn−1 TL + U h Ta

ˆ + U h2 .
where, λ=2

May solve Eqs. 122


- using Gaussian elimination or iterative methods (e.g. Gauss-Seidel, Jacobi),
- for increasing values of n, until no improvement in solution is observed.
(See Appendix H for MATLAB code used to create the following figure).

-89-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

550

500

450

400

T(x) 350

300

250

200

150

100

0 0.2 0.4 0.6 0.8 1


x

Example 70 Finite-difference solution of the heat conduction TPBVP

Revisit Example 69 with U = 100, T0 = 350, TL = 550, Ta = 75, L = 100 .

550

500

450

400

350
T(x)

300

250

200

150

100

0 20 40 60 80 100
x

The calculations appear to converge stably to the solution.

-90-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• In general:
 
y 00 = f (x, y, y 0 ) 







y0 = α
 
−yi−1
y(a) = α 
⇒ 1
(y
h2 i−1
− 2yi + yi+1 ) = f xi , yi , yi+12h , i = 1, . . . , n − 1
 
y(b) = β yn = β

 

• Linear problems:
 
y 00 =u(x) + ν(x)y + w(x)y 0 







y0 =α
yi−1 −2yi +yi+1 −yi−1
y(a)=α 

 h2
=u(xi ) + ν(xi )yi + w(xi ) yi+12h
 
y(b)=β yn =β, i = 1, . . . , n − 1

 

 




a i
|{z}
xi + |{z}
d i xi + ci
|{z}
xi+1 = bi 
|{z}



 
⇒ −(1+ h w ) (2+h2 ν i) −(1− h w ) −h2 ui ⇒
2 i 2 i

 

 
y0 = α, yn = β

 

 
y1

d 1 c1
 
   b1 − a1 α
a d
 2 2 c2 
 y2 


 b2


 ..
  
..

   
. b3

 . 







⇒ =


..

 .. 
  .. 

. . .

    
   
..

   
bn−2


 an−2 dn−2 cn−2 


.






 
an−1 dn−1 yn−1 bn−1 − cn−1 β

-91-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

9 PARTIAL DIFFERENTIAL EQUATIONS (PDE)


Example 71 PDE commonly found in transport phenomena

2D temperature distribution during the heat transfer at steady state:

∂ 2T ∂ 2T
+ =0 (Laplace equation)
∂x2 ∂y 2

Velocity profile in viscous fluid flow during unsteady state:

d2 ν dν
n 2 = (simple case of Navier-Stokes equations)
dx dt

Concentration profile due to diffusion:

∂c ∂ 2c
=D 2 (Fick’s law)
∂t ∂x

9.1 Classification of PDE


• Order classification:
First-order, e.g.
∂u ∂u
−α =0
∂x ∂y
Second-order, e.g.
∂ 2u ∂u
2
+u =0
∂x ∂y
Third-order, e.g. !2
∂ 3u ∂ 2u ∂u
+ + =0
∂x3 ∂x∂y ∂y

• Linearity classification

Definition 7: Linear equations


An equation L(u) = f (where L is any mapping, u is the unknown and f is the forcing term)
is linear if the mapping L is linear, i.e.

L(αu1 + βu2 ) = αL(u1 ) + βL(u2 ) (123)

for any numbers α, β and unknowns u1 , u2 .

-92-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

PDE:
" #
∂2 ∂2 ∂2 ∂ 2u ∂ 2u ∂ 2u
a(·) 2 + b(·) + c(·) 2 u = a(·) 2 + b(·) + c(·) 2 = −d(·) (124)
∂y ∂x∂y ∂x ∂y ∂x∂y ∂x
| {z }
L

(·) ≡ (x, y) ⇒ Linear (ÒP Hwnthi: Verify Eq. 123 for the above L)
!
∂u ∂u
(·) ≡ x, y, u, , ⇒ Quasilinear
∂x ∂y
!
∂ 2u ∂ 2u ∂ 2u
(·) ≡ x, y, u, 2 , 2 , ⇒ Nonlinear
∂x ∂y ∂x∂y

• Canonical-form classification:
PDE:
∂ 2u ∂ 2u ∂ 2u ∂u ∂u
a 2 +b +c 2 +d +e + fu = g
∂x ∂x∂y ∂y ∂x ∂y

b2 − 4ac <0 >0 =0


Equation elliptic hyperbolic parabolic

g = 0 ⇒ Homogeneous

-93-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

9.2 Developing PDEs


Recall Section 1 and Example 4 - Mathematical model for a tubular heat exchanger

- Conservation principles over vanishingly small control volume

- Constitutive equations

Example 72 PDE for heat conduction through an infinite slab

Perfect
conductor

Insulator

Conservation of energy:

(Rate of heat in) − (Rate of heat out) = (Energy accumulation rate)


lim∆x→0
∂ Qx − Qx+∆x ∂T
⇒ Qx@
A − Qx+∆x@
A' A∆xCp (T − Tref )] ⇒
[ρ@ = ρCp =⇒
∂t ∆x ∂t

lim∆x→0
∂Q ∂T 
=⇒ − = ρCp


∂ 2T ∂T

∂x ∂t 
⇒k 2
= ρCp ⇒
∂T 
 ∂x ∂t
Fourier’s Law: Q = −k



∂x

∂ 2T ρCp ∂T
2
= , 0<x<L (125)
∂x k ∂t

Eq. 125 is parabolic (Why?).

∂ 2T
At steady state: = 0 (Compare with Section 2.5)
∂x2

-94-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Boundary Conditions:

Perfect conductor: T (x = 0, t) = T0 , t > 0 (126)


dT
Insulator: (x = L, t) = 0 , t > 0 (127)
dx

Initial Condition:
T (x, t = 0) = T1 , 0 ≤ x ≤ L (128)

-95-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 73 PDE for steady-state diffusion in a reservoir

Vanishingly
small
control
volume

Conservation of mass:

(Mass In) − (Mass Out) = 0 ⇒

(Rx,y ∆y + Qx,y ∆x) − (Rx+∆x ∆y + Qx,y+∆y ∆x) = 0 ⇒

Rx+∆x,y − Rx,y Qx,y+∆y − Qx,y


+ =0⇒
∆x ∆y

lim∆x→0
∂R ∂Q
∆y→0


=⇒ + =0


∂ 2c ∂ 2c

∂x ∂y

⇒ + =0 (129)
∂c ∂c

 ∂x2 ∂y 2
Fick’s Law: R = −D , Q = −D



∂x ∂y

• Eq. 129 is elliptic (Why?)


• Eq. 129 is at steady state.
Boundary conditions: c(x, y) = f (x, y), given for all x, y on the boundary.
Solution c(x, y) may look like this for different (x, y)-grids.

(ÒP Hwnthi: How do the above two plots differ?)

-96-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10 NUMERICAL TECHNIQUES FOR PDE

Course Learning Objectives


• Solve partial differential equations with applications in heat transfer.

FINITE DIFFERENCES SPECTRAL


Approximate derivatives Approximate solution as infinite
(Large variety of formulas) series over entire solution domain
PDE solve algebraic eqs. PDE compute coefficients of
infinite series
Elliptic PDE

Parabolic PDE
(Explicit, Implicit,
Crank−Nicolson, ADI)
FINITE ELEMENTS
Approximate solution locally
and assemble pieces
PDE solve algebraic eqs.

-97-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.1 Finite-difference (FD) solution of elliptic PDE


Laplace equation:
∂ 2T ∂ 2T
+ =0 (130)
∂x2 ∂y 2
Poisson equation:
∂ 2T ∂ 2T
+ = f (x, y) (131)
∂x2 ∂y 2
(ÒP Hwnthi: What does the term f (x, y) refer to?)

Example 74 FD solution of PDE for steady-state temperature 2D-profile on an non-insulated


plate

Solve Eq. 130 for T (x, y) in the rectangular domain defined by 0 < x < a, 0 < y < b.
Dirichlet boundary conditions:
T (0, y) = W (given), 0 < y < b
T (a, y) = E (given), 0 < y < b
T (x, 0) = S (given), 0 < x < a
T (x, b) = N (given), 0 < x < a

• Step 1: Define grid {xi , yi } i = 0, . . . , m, j = 0, . . . , n, over solution domain.


Notation Tij =T
ˆ (xi , yi )

-98-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Step 2: Substitute second derivatives in Eq. 130 by centered divided differences at all
points {xi , yi } where Tij is not known, i.e. for i = 1, . . . , m − 1, j = 1, . . . , n − 1
∂ 2T Ti+1,j − 2Ti,j + Ti−1,j
(x i , yi ) ≈ (132)
∂x2 ∆x2

∂ 2T Ti,j+1 − 2Ti,j + Ti,j−1


2
(xi , yi ) ≈ (133)
∂y ∆y 2

with truncation errors Rx = O(∆x2 ) and Ry = O(∆y 2 ), respectively. ∆x = ∆y ⇒

Ti+1,j + Ti−1,j + Ti,j+1 + Ti,j−1 − 4Ti,j = 0 , 1 ≤ i ≤ m − 1, 1 ≤ j ≤ n − 1 (134)

(m − 1) × (n − 1) equations, (m − 1) × (n − 1) unknowns. (ÒP Hwnthi: Verify Eq. 134.


What are the (m − 1) × (n − 1) equations and (m − 1) × (n − 1) unknowns?)

• Step 3: Solve the resulting system of equations Ax = b

Application: Let W = 75, N = 100, E = 50, S = 0. Consider m = n = 4 ⇒ grid:

      
(i, j) = (1, 1) ⇒ −4 1 1 T11 −W − S −75
(i, j) = (2, 1) ⇒  1 −4 1 1 T21   −S   0 
      

      
(i, j) = (3, 1) ⇒ 
 1 −4 1 
 T31   −E − S   −50 
    
(i, j) = (1, 2) ⇒  1 −4 1 1 T12   −W   −75 
      

      
(i, j) = (2, 2) ⇒ −4 = 0 ⇒
1 1 1 1 =
T22  0
  
    
(i, j) = (3, 2) ⇒ 1 1 −4 1  T32   −E   −50 
      

      
(i, j) = (1, 3) ⇒ 1 −4 1  −W − N  −175
T13 
     
 
(i, j) = (2, 3) ⇒ 1 1 −4 1  T23   −N  −100
      

(i, j) = (3, 3) ⇒ 1 1 −4 T33 −E − N −150
| {z }| {z } | {z }
A x b
   
T11 42.8571
T21  33.2589
   

   

 T31  33.9286
  
T12  63.1696
   

   
⇒
 T22  = 56.2500
  
T32  52.4554
   

   
 T13  78.5714
   
T23 76.1161
   
   
T33 69.6429

-99-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

MATLAB
W = 75;
N = 100;
E = 50;
S = 0;

DD = [ -4 1 0; 1 -4 1; 0 1 -4];
II = eye (3);
OO = zeros (3 ,3);

A = [ DD II OO ; II DD II ; OO II DD ];
b = [ -W - S ; -S ; -E - S ; -W ; 0; -E ; -W - N ; -N ; -E - N ];
T = A\b

Mathematica
WW = 75;
NN = 100;
EE = 50;
SS = 0;
DD = {{ -4 , 1 , 0} , {1 , -4 , 1} , {0 , 1 , -4}};
II = IdentityMatrix [3];
OO = Table [0 , {3} , {3}];
A = ArrayFlatten [{{ DD , II , OO } , { II , DD , II } , { OO , II , DD }}];
MatrixForm [%]
b = {{ - SS - WW } , { - SS } , { - EE - SS } , { - WW } , {0} , { - EE } , { - NN -
WW } , { - NN } , { - EE - NN }}
MatrixForm [%]
T = LinearSolve [A , b ]
MatrixForm [%]

(* Plot results in 3 D *)
m = 4;
n = 4;
rows = Table [
Flatten [{ WW , Table [ T [[ i + j - 1]] , {i , 1 , m - 1}] , EE }] , {j ,
1 , ( n - 1) ( m - 1) , m - 1}];
rowSS = Table [ SS , {j , 0 , m }];
rowNN = Table [ NN , {j , 0 , m }];
Tlist = Join [{ rowSS } , rows , { rowNN }];
MatrixForm [%];
ListPlot3D [ Tlist , Ticks -> { None , None , Automatic } , PlotRange -> All ,
BoxRatios -> {1 , 1 , 1} , AxesLabel -> { " x " , " y " , " T (x , y ) " } ,
Mesh -> Full , ColorFunction -> " TemperatureMap " ]

-100-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Generalize for any m, n:

(i, j) = (1, 1) ⇒
    
−4 1 1 T1,1 −W − S
..
. .. ..  −S 
(i, j) = (2, 1) ⇒  1
 . .. . 
 .

  
.. . . .. ..
  −S 
    
.⇒  .. .. 1 .  .
    
(i, j) = (m − 1, 1) ⇒ 
 1 −4 1   Tm−1,1

  −E − S 
  
..  .. .. ..  ..   −W 
.⇒  1 . . .  .   
.. ..
    
.. .. .. .. ..   0 
.⇒ . . . . .
 
  .   
..  . .. . .
.. .. ... ..  ..  
  0 

.⇒ 
 . 
 .   
..  .. .. ..  ..   −E 
.⇒  1 . . .   .   
  = 
..  .. .. ..  ..   −W 
.⇒  . . . 1  .   
.. ..
    
.. .. .. .. ..   0 
.⇒ . . . . .
  
  .   
..  .. .. .. .. ..  ..   0 
.⇒ 
 . . . . . 

 .  
 


..  . . . . ..  ..   −E 
.⇒  . . . 1  .   
    
(i, j) = (1, n − 1) ⇒  1 −4 1   T1,n−1  −W − N 
    
(i, j) = (2, n − 1) ⇒  .. .. ..  ..   −N 
 . 1 . .  .   
.. ..
.. .. ..   −N 
     
.⇒  . . . 1   .
(i, j) = (m − 1, n − 1) ⇒ 1 1 −4 Tm−1,n−1 −E − N
| {z }| {z } | {z }
A x b

-101-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

m = n = 20 (see Mathematica code in Appendix J) ⇒

m = n = 40 (see MATLAB code in Appendix K) ⇒


Laplace equation in 2D
100

90

100
80

80 70

60 60
T(x,y)

40 50

40
20

30
0
40 20
30 40
20 30 10
20
10
10 0
y x

For comparison purposes, theanalytical solution of Eq. 130 for a slab of height H and width
 N = T̄
w with boundary conditions  is
S=E=W =0


2T̄ X (−1)n+1 + 1 nπx sinh(nπy/w)
 
T (x, y) = sin (135)
π n=1 n w sinh(nπH/w)

The superposition principle is used to get T (x, y) when the boundary conditions at the sides
E, W , and S are nonzero.
- Is there any insight that the “analytical” solution provides that the “numerical” solution
does not?
- Would you prefer the “analytical” or the “numerical” solution?
- Is there a real difference between the “analytical” and the “numerical” solution?

-102-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 75 FD solution of PDE for steady-state temperature 2D profile on a partially insulated


plate
Solve Eq. 130 for T (x, y) in the rectangular domain defined by 0 < x < a, 0 < y < b.
Dirichlet boundary conditions:
T (0, y) = W (given), 0 < y < b
T (a, y) = E (given), 0 < y < b
T (x, b) = N (given), 0 < x < a
von Neumann boundary conditions:
∂T
(x, 0) = 0, 0 < x < a (South side is insulated)
∂y

• Step 1: Define grid {xi , yj } i = 0, . . . , m, j = 0, . . . , n over solution domain.


Notation Tij =T
ˆ (xi , yi ).

• Step 2: Substitute second derivatives in Eq. 130 by centered divided differences at all
points (xi , yi ) where Tij is not known, i.e. for i = 1, . . . , m − 1, j = 0, 1, . . . , n − 1.

As before:

Ti+1,j + Ti−1,j + Ti,j+1 + Ti,j−1 − 4Ti,j = 0 , 1 ≤ i ≤ m − 1, 1 ≤ j ≤ n − 1 (136)

(m − 1) × n equations, (m − 1) × (n) unknowns.

-103-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

(ÒP Hwnthi: Verify, using j = 0 ⇒ Ti+1,0 + Ti−1,0 + Ti,1 + Ti,−1 − 4Ti,0 = 0 i = 1, . . . , m − 1.


Where is Ti,−1 ?)

Need m − 1 equations! Use von Neumann boundary condition to set up m − 1 additional


equations:

∂T ∂T Ti,1 − Ti,−1
(x, 0) = 0 ⇒ (xi , 0) = 0 ⇒ ≈ 0 ⇒ Ti,1 − Ti,−1 = 0 , i = 1, . . . , m − 1
∂y ∂y 2∆y

Resulting system of equations:

(i, j) = (1, 0) ⇒ −4 T1,−1 −W


1 1 1
   
(i, j) = (2, 0) ⇒ .. .. .. .. ..
 . 1 . . .  .   0 
.. ..
.⇒
 .. .. .. ..    0 
 . . . 1 .  .   
(i, j) = (m − 1, 0) ⇒  1 1 −4 1
  Tm−1,−1   −E 
    
(i, j) = (1, 1) ⇒  .. .. ..   T1,0   .. 
 1 . . .    . 
.. .. .. .. .. ..   . 
(i, j) = (2, 1) ⇒
 
 . . . . .  .   .. 
..
 .. .. .. .. ..
 ..
  . 
.⇒
 . . . . .  .
  .. 
    
(i, j) = (m − 1, 1) ⇒ .. .. ..   . 
  m−1,0   .. 
 1 . . .
 T

..  .. .. ..  ..   −W 
. ⇒  . . . 1  .   
..  .. .. .. .. ..
 ..   
. ⇒  . . . . .  .  =  0 
.. ..
 .. .. .. .. ..
   
. ⇒ . . . . . .   0 
    
 
..  .. .. ..  ..   −E 
. ⇒  . . . 1  .   
(i, j) = (1, n − 1) ⇒  1 −4 1  ..  −W − N 
  .   
(i, j) = (2, n − 1) ⇒ .. .. .. ..   −N 
. . .
 1

.
..
    
.. .. .. ..   −N 
.⇒
 
 . . . 1  .   
(i, j) = (m − 1, n − 1) ⇒ .   −E − N 
1 1
  T ..
−4  

von Neumann B.C. ⇒
   
1 −1   1,n−1   0 
..  .. ..  ..   .. 
.⇒  . .  .   . 
..  .. ..
 .   . 
.⇒ . . .. ..
von Neumann B.C. ⇒ 1 −1 Tm−1,n−1 0
| {z }| {z } | {z }
A x b

• Solve the resulting system of equations Ax = b. (See Appendix L for MATLAB code)

-104-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Laplace equation in 2D
100

95

100
90

90 85

80 80

T(x,y)
70 75

70
60

65
50
40 60
30 40
20 30 55
20
10
10 50
y x

10.2 Solution of large systems of equations in elliptic PDE


Equations Ax = b in Example 74 and 75 involve structured (multi-diagonal) A. To solve
Ax = b, use

• Standard Gaussian elimination: Requires O(n3 ) computations (n = dimension(A)).

• Special Gaussian elimination: Takes advantage of many zeros in A.

• Iterative methods: Require O(n2 ) × (number of iterations) computations.

– Gauss-Seidel
– Jacobi

- If (number of iterations) << n iterative methods can be more efficient.


- Relaxation is a technique for iterative methods that can help convergence speed or robust-
ness.

-105-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.3 Explicit finite-difference (FD) methods for numerical solution of parabolic PDE
Example 76 Transient temperature profile on an infinite slab: Explicit FD

Recall Example 72. Solve


∂T ∂ 2T
=k 2 (137)
∂t ∂x
Dirichlet Boundary Conditions:

Perfect conductor: T (x = 0, t) = T0 , t > 0 (138)


T (x = L, t) = TL , t > 0 (139)

Initial Condition:
T (x, t = 0) = Tinit , 0 ≤ x ≤ L (140)

• Step 1: Define spatial grid {x0 =0,ˆ x1 , . . . , xn−1 , xn =L}


ˆ and temporal grid {t0 =0,
ˆ t1 , t2 , . . .}.
Notation Ti` =T
ˆ (xi , t` ).

-106-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Step 2: Substitute temporal and spatial derivatives in Eq. 137 by forward and centered
divided differences, respectively, at all points xi where Ti` is not known, i.e. for i = 1, . . . , n−
1, and for ` = 0, 1, . . ..
`
∂ 2T Ti+1 `
− 2Ti` + Ti−1
 h i 
2
(x , t ) = + O (∆x)

 

 i ` 
∂x2 ∆x2

 


 | {z } 

Eq. 137 and Truncation error ⇒
 ∂T T `+1 − Ti` 
(xi , t` ) = i + O(∆t)

 

 
∂t ∆t

 

 | {z } 
Truncation error

∆t
Ti`+1 = Ti` + k 2
`
(Ti+1 `
− 2Ti` + Ti−1 ) , i = 1, . . . , n − 1, ` = 0, 1, 2, . . . (141)
(∆x)
| {z }
λ

• Step 3: Use initial and boundary conditions to compute Ti`+1 , ` = 0, 1, 2, . . . , i = 1, . . . , n−1


recursively using direct substitution in Eq. 141. Very simple!
B.C. Eq. 138 ⇒ T0` = T0 , ` = 0, 1, 2, . . .
B.C. Eq. 139 ⇒ Tn` = TL , ` = 0, 1, 2, . . .
I.C. Eq. 140 ⇒ Ti0 = Tinit , i = 1, . . . , n − 1
Application: k = 0.835 cm2/s, L = 10 cm.
Boundary conditions: T (x = 0, t) = 100 C, T (x = 10, t) = 50 C, t > 0
Initial condition: T (x, t = 0) = 0, 0 ≤ x ≤ 10
Solution for ∆x = 2 (n = 5), various ∆t > 0 on spreadsheet:

-107-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

∆x = 2 (n = 5), ∆t = 0.5 ⇒ numerical stability

ÒP Hwnthi: What is lim T (x, t)? (Hint: lim [Eq. 137] = Eq. 39, with lim [B.C.s 138
t→∞ t→∞ t→∞
and 139]

∆x = 2 (n = 5), ∆t = 3 ⇒ numerical instability

-108-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Example 77 Transient temperature profile on an insulated infinite slab

Recall Example 72. Solve


∂T ∂ 2T
=k 2 (142)
∂t ∂x
Dirichlet Boundary Condition:

Perfect conductor: T (x = 0, t) = T0 , t > 0 (143)

von Neumann Boundary Condition:

∂T
Insulator: (x = L, t) = 0 , t > 0 (144)
∂x

Initial Condition:
T (x, t = 0) = Tinit , 0 ≤ x ≤ L (145)

• Step 1: Define spatial grid {x0 =0,


ˆ x1 , . . . , xn−1 , xn =L}
ˆ and temporal grid {t0 =0,
ˆ t1 , t2 , . . .}.
`
Notation Ti =T
ˆ (xi , t` ). (Similar to Example 76)

• Step 2: Substitute temporal and spatial derivatives in Eq. 142 by forward and cen-
tered divided differences, respectively, at all points xi where Ti` is not known, i.e. for
i = 1, . . . , n − 1, n, and for ` = 0, 1, . . ..

As in Example 76:

∆t
Ti`+1 = Ti` + k (T ` − 2Ti` + Ti−1
`
) , i = 1, . . . , n − 1, n , ` = 0, 1, 2, . . . (146)
(∆x)2 i+1
| {z }
λ

-109-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

For i = n Eq. 146 becomes:

∆t
Tn`+1 = Tn` + k (T ` − 2Tn` + Tn−1
`
) ` = 0, 1, 2, . . . (147)
(∆x)2 n+1
| {z }
λ

` `
Tn+1 is outside the solution domain! Use von Neumann B.C. to substitute Tn+1 :

∂T `
T ` − Tn−1
Eq. 144 ⇒ (x = L, t) = 0 ⇒ n+1 `
= 0 ⇒ Tn+1 `
= Tn−1 , ` = 0, 1, 2, . . . (148)
∂x 2∆x

• Step 3: Use initial and boundary conditions to compute Ti`+1 , ` = 0, 1, 2, . . . , i = 1, . . . , n−1


recursively using Eq. 146. Very simple!
B.C. Eq. 143 ⇒ T0` = T0 , ` = 0, 1, 2, . . .
B.C. Eq. 144 ⇒ Eq. 148
I.C. Eq. 145 ⇒ Ti0 = Tinit , i = 1, . . . , n − 1, n
Application: k = 0.835 cm2/s, L = 10 cm.
∂T
Boundary conditions: T (x = 0, t) = 100 C, (x = 10, t) = 0, t > 0
∂x
Initial condition: T (x, t = 0) = 0, 0 ≤ x ≤ 10
Solution for ∆x = 2 (n = 5), various ∆t > 0 on spreadsheet:

-110-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

∆x = 2 (n = 5), ∆t = 1

ÒP Hwnthi: What is lim T (x, t)? (Hint: lim [Eq. 137] = Eq. 39, with lim [B.C.s 138
t→∞ t→∞ t→∞
and 139]

∆x = 2 (n = 5), ∆t = 3

-111-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.4 Numerical stability of explicit methods


Recall Definition 5, Numerical stability ⇒ errors are not amplified as computation progresses.

Theorem 12: When does the numerical solution of a linear equation converge to the
true solution?
If the numerical method is (a) stable, and (b) consistent (i.e. as the discretization becomes
finer the original equation is obtained) the numerical solution converges to the true solution
as the discretization becomes increasingly finer.
Therefore: 
∆x → 0 
⇒ Ti` → Ttrue
∆t → 0 
if numerical stability is guaranteed (consistency is trivial).

Theorem 13: Stability of explicit FD method for PDE


The explicit method of Section 10.3 is conditionally stable, when

∆t 1
λ=k
ˆ ≤ (149)
∆x2 2

However, to avoid spurious oscillations in the solution, use

∆t 1
λ=k
ˆ 2
≤ (150)
∆x 4

Illustrated in Example 76 and Example 77.

Theorem 13 constraints the maximum time-step ∆t for explicit methods, possibly creating
inefficiencies.

Example 78 Transient temperature profile on an insulated infinite slab

An engineer using the explicit FD method to integrate Eq. 137 found that his (her) compu-
tations produced numbers that grew without bounds (instability). To remedy the situation
(s)he decided to cut both the time and space integration steps (∆t and ∆x, respectively) by
half and repeat the numerical integration of Eq. 137. Is that a good idea?

-112-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.5 Implicit finite-difference (FD) methods for numerical solution of parabolic PDE
• Why implicit?
To accelerate computations by avoiding the bounds on the time step ∆t posed in
Theorem 13 for explicit methods.

• What are implicit methods?


They compute the value of the unknown variable at each time step recursively, not by
direct substitution of the solution at the previous time step, but by solving a set of
algebraic equations.

Example 79 Transient temperature profile on an infinite slab: Implicit FD


Revisit Example 76
• Step 1: Define spatial grid {x0 =0,
ˆ x1 , . . . , xn−1 , xn =L}
ˆ and temporal grid {t0 =0,
ˆ t1 , t2 , . . .}.
Notation: Ti` =T
ˆ (xi , t` ). (Same as before).
• Step 2: Substitute temporal and spatial derivatives in Eq. 137 by backward and centered
divided differences, respectively, at all points xi where Ti` is not known,i.e. for i = 1, . . . , n−1,
and for ` = 0, 1, . . .

`+1
 ∂ 2T Ti+1 − 2Ti`+1 + Ti−1
`+1 h
2
i
(x , t ) = + O (∆x)


 i `+1)
∂x2 ∆x2



 | {z }
Eq. 137 and Truncation error ⇒
 ∂T T `+1 − Ti`
(xi , t`+1 ) = i O(∆t)


 +
∂t ∆t



 | {z }
Truncation error

`+1
−λTi−1 + (1 + 2λ)Ti`+1 − λTi+1
`+1
= Ti` , i = 1, . . . , n − 1, ` = 0, 1, 2, . . . (151)

i.e.
   `+1   
1 + 2λ −λ T1 λT0 + T1`
 T `+1 
    
 −λ
 1 + 2λ −λ  2 

 T2` 

 i = 1, . . . , n − 1
.. .. ..   ..  ..
   
= , (152)

. . .
 
  .  .
 
  `+1 
   ` = 0, 1, 2, . . .
`
  

 −λ 1 + 2λ −λ  T
  n−2 
 
 Tn−2


`+1 `
−λ 1 + 2λ Tn−1 λTL + Tn−1
| {z } | {z } | {z }
A x b

∆t
where λ=k
ˆ
(∆x)2

-113-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

In contrast to explicit methods (Eq. 141), Eq. 151 requires the simultaneous solution of
multiple equations at each time step. But it can be shown that Eq. 151 allows much larger
integration time step.

• Step 3: Use initial and boundary conditions to compute Ti`+1 , ` = 0, 1, 2, . . . , i = 1, . . . , n−1


recursively by solving the linear system of equations Ax = b at each time step, per Eq. 152.
B.C. Eq. 138 ⇒ T0` = T0 , ` = 0, 1, 2, . . .
B.C. Eq. 139 ⇒ Tn` = TL , ` = 0, 1, 2, . . .
I.C. Eq. 140 ⇒ Ti0 = Tinit , i = 1, . . . , n − 1

MATLAB
% Numerical solution of the transient heat equation
% using an implicit numerical method
%
% Define problem data
L = 10;
T0 = 100;
TL = 50;
Tinit = 0;
k = 0.835

% Define number of time steps and step size


nSteps = 30;
Dt = 3;

% Define grid size


n = 5;

% Set up matix A
clear A
clear b
clear T
Dx = L / n ;
lambda = k * Dt / Dx ˆ2
A = diag ( ones (n -1 ,1)*(1+2* lambda )) + ...
diag ( ones (n -2 ,1)*( - lambda ) ,1) + ...
diag ( ones (n -2 ,1)*( - lambda ) , -1);

% Perform recursive computation of PDE solution


T (: ,1) = ones (n -1 ,1)* Tinit ;
for i = 1: nSteps
b = [ lambda * T0 ; zeros (n -3 ,1); lambda * TL ] + T (: , i );
T (: , i +1) = A \ b ;
end

-114-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Application: k = 0.835 cm2/s, L = 10 cm.


Boundary conditions: T (x = 0, t) = 100 C, T (x = 10, t) = 50 C, t > 0
Initial condition: T (x, t = 0) = 0, 0 ≤ x ≤ 10

Solution for ∆x = 2 (n = 5), ∆t = 3 (See Appendix M for full MATLAB code, including
plotting code):
100 100

90 90

80 80

70 70

60 60
T(x i ,t )

T(x,t l )
50 50

40 T(x , t) 40
0
T(x , t)
1
30 30
T(x 2 , t)
20 T(x 3 , t) 20
T(x 4 , t)
10 T(x 5 , t) 10

0 0
time, t , 0...to...93 x , 0...to...10

100

90

100
80

80 70

60 60
T(x,t )

40 50

40
20

30
0
20

10

0
x , 0...to...10
time, t , 0...to...93

Contrast stability of above plots to instability of explicit method for ∆x = 2 (n = 5), ∆t = 3


in Example 76.

-115-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Solution for ∆x = 10/25 (n = 25), ∆t = 3 (See Appendix M for full MATLAB code, includ-
ing plotting code):
100 100

90 90

80 80

70 70

60 60
T(x i ,t )

T(x,t l )
50 50

40 40

30 30

20 20

10 10

0 0
time, t , 0...to...93 x , 0...to...10

100

90

100
80

80 70

60 60
T(x,t )

40 50

40
20

30
0
20

10

0
x , 0...to...10
time, t , 0...to...93

Compare the above figure to its previous counterpart, for n = 5. Note that the time grid is
the same.

10.6 Numerical stability of implicit methods


Theorem 14: Stability of implicit FD method for PDEs
The implicit method of Section 10.5 is unconditionally stable, i.e. stable for any value of
∆t
λ=k
ˆ ∆x 2.

-116-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.7 The Crank-Nicolson implicit finite difference (FD) method


• Why?
To improve accuracy over explicit and implicit methods, by using finite differences
with second-order truncation error for both ∂ 2 T /∂x2 and ∂T /∂t, without jeopardizing
numerical stability.

• Why would numerical stability be jeopardized if second-order derivatives were used for
∂T /∂t ?

Example 80 Transient temperature profile on an infinite slab: Instability of Richardson’s Multi-


step FD scheme (Unconditionally unstable)
Revisit Example 76:

• Step 1: Define spatial grid {x0 =0,


ˆ x1 , . . . , xn−1 , xn =L}
ˆ and temporal grid {t0 =0,
ˆ t1 , t2 , . . .}.
`
Notation Ti =T
ˆ (xi , t` ). Same as before.

• Step 2: Substitute both temporal and spatial derivatives in Eq. 137 by centered divided
differences at all points xi where Ti` is not known, i.e. for i = 1, . . . , n−1, and for ` = 0, 1, . . ..

`
∂ 2T − 2Ti` + Ti−1
`
 
Ti+1 h
2
i
O
 

 (x i , t` ) = + (∆x) 

∂x2 ∆x2

 


 | {z } 

 
Truncation error
Eq. 137 and ⇒
∂T T `+1 − Ti`−1 h i
(xi , t` ) = i + O (∆t)2

 


 




 ∂t 2∆t | {z }




Truncation error

2∆t
Ti`+1 = Ti`−1 + k (T ` − 2Ti` + Ti−1
`
) , i = 1, . . . , n − 1, ` = 0, 1, 2, . . . (153)
(∆x)2 i+1
| {z }

• Step 3: Use initial and boundary conditions to compute (a) Ti1 , i = 1, . . . , n − 1 using
a one-step method (e.g. explicit or implicit) and (b) Ti`+1 , ` = 1, 2, . . . . i = 1, . . . , n − 1
recursively using direct substitution in Eq. 153. Appears very simple and more accurate (has
O [(∆t)2 ] truncation error) than one-step explicit (Section 10.3) or implicit (Section 10.5)
methods. BUT it can be shown that Eq. 153 is unconditionally unstable.
B.C. Eq. 138 ⇒ T0` = T0 , ` = 0, 1, 2, . . .
B.C. Eq. 139 ⇒ Tn` = TL , ` = 0, 1, 2, . . .

-117-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

I.C. Eq. 140 ⇒ Ti0 = Tinit , i = 1, . . . , n − 1

• Application: k = 0.835 cm2/s, L = 10 cm.


Boundary conditions: T (x = 0, t) = 100 C, T (x = 10, t) = 50 C, t > 0
Initial condition: T (x, t = 0) = 0, 0 ≤ x ≤ 10

Solution for ∆x = 2 (n = 5), ∆t = 0.5 on spreadsheet illustrates numerical instability.

Illustrate how numerical stability is not jeopardized even though second-order derivatives

-118-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

are used for ∂T /∂t in the unconditionally stable Crank-Nicolson scheme.


Example 81 Transient temperature profile on an infinite slab: Crank-Nicolson FD Scheme
(Uconditionally Stable)
Revisit Example 76:

• Step 1: Define spatial grid {x0 =0, ˆ x1 , . . . , xn−1 , xn =L}


ˆ and temporal grid {t0 =0,
ˆ t1/2 ,
t1 , t3/2 , . . .}. Notation Ti` =T
ˆ (xi , t` ).

• Step 2: Substitute temporal and spatial derivatives in Eq. 137 by centered and average
centered divided differences, respectively, at all points xi where Ti` is not known, i.e. for
i = 1, . . . , n − 1, and for ` = 0, 21 , 1, 32 , . . .. Define ∆t=t
ˆ `+1 − t` , λ=k ∆t
ˆ (∆x) 2 and approximate

derivatives at midpoint (xi , t`+1/2 ):

`+1
`
− 2Ti` + Ti−1
` `+1
− 2Ti`+1 + Ti−1
 " # 
 ∂ 2T 1 Ti+1 Ti+1 h
2
i 

 = + + O (∆x) 

∂x2 2 ∆x2 ∆x2

 


 | {z } 

 
Eq. 137 and  Truncation error ⇒
∂T Ti`+1 − Ti` h
2
i 
= + O (∆t)

 

 
 ∂t


 ∆t | {z }




Truncation error

`+1 `+1) `+1 ` i = 1, . . . , n − 1,


−λTi−1 + 2(1 + λ)Ti − λTi+1 = λTi−1 + 2(1 − λ)Ti` + λTi+1
`
, (154)
` = 0, 1, 2, . . .

   `+1 
2(1 + λ) −λ T1
 T `+1 
  
 −λ 2(1 + λ) −λ
  2 
.. .. ..   .. 
  


 . . .  . 
 
   `+1 

 −λ 2(1 + λ) −λ  T
  n−2 

`+1
−λ 2(1 + λ) Tn−1
| {z } | {z }
A x

    
2(1 − λ) λ T` 2λT0
  1   
 λ 2(1 − λ) λ  T`   0 
  2   

.. .. ..

.  
. 
=   ..  +  ..  (155)

. . .
    
    
  `   

 λ 2(1 − λ) λ  Tn−2   0 
    
`
λ 2(1 − λ) Tn−1 2λTL
| {z }
b

-119-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Step 3: Use initial and boundary conditions to compute Ti`+1 , ` = 0, 1, 2, . . . . i =


1, . . . , n − 1 recursively by solving the linear system of equations Ax = b at each time step,
per Eq. 155.
B.C. Eq. 138 ⇒ T0` = T0 , ` = 0, 1, 2, . . .
B.C. Eq. 139 ⇒ Tn` = TL , ` = 0, 1, 2, . . .
I.C. Eq. 140 ⇒ Ti0 = Tinit , i = 1, . . . , n − 1

MATLAB
% Numerical solution of the transient heat equation
% using the Crank - Nicolson numerical method
%
% Define problem data
L = 10;
T0 = 100;
TL = 50;
Tinit = 0;
k = 0.835

% Define number of time steps and step size


nSteps = 30;
Dt = 0.5;

% Define grid size


n = 10

% Set up matices A , Mtemp


clear A
clear b
clear T
clear Mtemp
Dx = L / n ;
lambda = k * Dt / Dx ˆ2
A = diag ( ones (n -1 ,1)*2*(1+ lambda )) + ...
diag ( ones (n -2 ,1)*( - lambda ) ,1) + ...
diag ( ones (n -2 ,1)*( - lambda ) , -1);
Mtemp = diag ( ones (n -1 ,1)*2*(1 - lambda )) + ...
diag ( ones (n -2 ,1)* lambda ,1) + ...
diag ( ones (n -2 ,1)* lambda , -1);

% Perform recursive computation of PDE solution


T (: ,1) = ones (n -1 ,1)* Tinit ;
for i = 1: nSteps
b = [2* lambda * T0 ; zeros (n -3 ,1); 2* lambda * TL ] + Mtemp * T (: , i );
T (: , i +1) = A \ b ;
end

-120-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

• Application: k = 0.835 cm2/s, L = 10 cm.


Boundary conditions: T (x = 0, t) = 100 C, T (x = 10, t) = 50 C, t > 0
Initial condition: T (x, t = 0) = 0, 0 ≤ x ≤ 10

Solution for ∆x = 1 (n = 10), ∆t = 0.5 (See Appendix N for full MATLAB code, including
plotting code):

100 100

90 90

80 80

70 70

60 60
T(xi,t)

T(x,tl)
50 50

40 40

30 30

20 20

10 10

0 0
time, t, 0...to...50.5 x, 0...to...10

100

90

100
80

80 70

60 60
T(x,t)

50
40

40
20

30
0

20

10

0
time, t, 0...to...50.5
x, 0...to...10

-121-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.8 Numerical stability of Crank-Nicolson implicit methods


Theorem 15: Stability of Crank-Nicolson implicit FD method for PDE
The Crank-Nicolson implicit method of Section 10.7 is unconditionally stable, i.e. stable for
∆t
any value of λ=k
ˆ (∆x)2 . However, spurious oscillations appear in the solution for large values

of λ.

Example 82 Spurious oscillations in Crank-Nicolson implicit FD Scheme

Revisit Example 81 for ∆x = 10/25 (n = 25), ∆t = 3


140 140

120 120

100 100

80 80
T(x i ,t )

T(x,t l )

60 60

40 40

20 20

0 0
time, t , 0...to...303 x , 0...to...10

140

120

100
100
T(x,t )

80

50
60

0 40

20

0
x , 0...to...10
time, t , 0...to...303

-122-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

10.9 Understanding transport dynamics using numerical integration of PDE (OP-


TIONAL)
How fast does T change in the infinite slab of Section 10.3?

We will answer the above question using a “numerical” approach:

Discretize T (x, t) in the spatial direction x only in Eq. 137 with B.C Eqs. 138 and 139. Let
2
ˆ (x1 , t), i = 0, . . . , n, and approximate ∂∂xT2 (xi , t) at xi = x1 , . . . , xn−1 by {(Ti+1 (t) −
T1 (t)=T
2Ti (t) + Ti−1 (t))/∆x2 } (where ∆x = Ln ), to get the following n − 1 first-order ordinary
differential equations:
      
dT1
dt
−2 1 ··· 0
0 T1 (t) T0
.. .. . ..   . 
      

 .



 1 −2 1 . .  
.  . 
   .. 
     
2  2

..  kn  .. .. ..

..  kn  .. 
 
= + 2 (156)
 
. . . 0


 . 
 L2 
 0 
 .  L
 . 
 
 ..   .. ..  ..   . 
 . 
. 1 −2 1 
    

 . 


 . 
 . 

 . 
 
dTn−1
dt
0 ··· 0 1 −2 Tn−1 (t) TL
| {z } | {z }| {z } | {z }
dx A x(t) f (t)
dt

with initial conditions Ti (0) = Tinit , by Eq. 140.

According to Eqs. 45 and 47, the solution of Eq. 156 is


     
 T1 (t)   T1 (0)  Z t 2 
T0 
. At  . kn . 
..  ..  eA(t−τ ) 2  ..  dτ ⇒
    
x(t)=
ˆ =e  +


    0 L  
Tn−1 (t) Tn−1 (0) TL
| {z } | {z }
x(t0 ) f (τ )

 
 T1 (t) 
 ..  λ1 t λn−1 t − τt − t
.  = e| {za1} + . . . + e| {zan−1} +b=
ˆ e| {z1 a + . . . + e τn−1 a
1 n−1 +b (157)
 
 } | {z }
 
mode 1 mode n−1 mode 1 mode n−1
Tn−1 (t)

where λj = − τ1j , j = 1, . . . , n − 1, are the eigenvalues of the above tridiagonal matrix A. It

-123-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

can shown∗ that:

kn2 jπ kn2 jπ
 
λj = 2
2 cos − 1 = − 2
4 sin2 , j = 1, . . . , n − 1 (158)
L n L 2n

Therefore all eigenvalues λj are negative, hence the system is stable (as physically expected).

The dominant time constant, τ1 , of the system (corresponding to the slowest mode in
Eq. 157) is the negative of the inverse of the smallest eigenvalue, i.e.

1 kn2 π kn2 π
 
− = λ1 = 2 2 cos − 1 = − 2 4 sin2 (159)
τ1 L n L 2n

When the spatial grid {x0 , . . . , xn } is refined as n → ∞ (continuum approximation) the


smallest eigenvalue λ1 converges to:

kπ 2
lim λ1 = − (160)
n→∞ L2

Proof : Taylor series expansion of cos πn − 1 in Eq. 159 yields:


!
π π2 π4 π2
 
2 n→∞
n cos − 1 = −n2 − + ... −→ − (161)
n 2!n2 4!n4 2

Therefore the dominant time constant of the system, τ1 (corresponding to the slowest mode
e−t/τ1 a1 ), is
L2
τ1 = (162)
kπ 2
namely  
 T 1 (t) 
 ..  −λ1 t − t
. ≈e a1 + b=e
ˆ τ1 a1 + b (163)
 

 
Tn−1 (t)
This result is in agreement with the analytical solution of the spectral method; e.g., for
T (0, t) = 0, T (L, t) = T̄ , Tinit = 0, the analytical solution (given without proof) is:
 !

x X 2 jx −j 2 π 2 k 
 
T (x, t) = T̄  + (−1)j sin exp t (164)
L j=0 jπ L L2

Kincaid and Cheney, Numerical Analysis: Mathematics of Scientific Computing, Brooks/Cole, 1991, p.
578

-124-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Similar analysis can be carried out for the explicit method in discrete time.

Comments

• In contrast to Eq. 164, which contains sinusoidal terms, Eq. 157 does not mislead into
believing that the solution may be oscillatory. In fact, it does not introduce artificial
oscillations to the numerical result, something that the spectral method does, unless a
huge number of terms are summed.

• Despite the fact that Eq. 157 provides a “numerical” solution, it does not conceal any
of the fundamentals of the solution (e.g., stability or dominant time constant). Eq. 157
is based on solid mathematics and is as “analytical” as any other. It does not merely
produce numbers. It produces insight.

• Eq. 157 is extremely easy to program. (A spreadsheet is enough; any procedural


language can also be easily used). The student can produce and visualize results very
quickly and confidently.

• A variety of boundary solutions and solution domains can be easily handled with finite
differences, something that spectral methods have a very hard time accomplishing.

10.10 Parabolic PDE in 2-D


!
∂T ∂ 2T ∂ 2T
=k +
∂t ∂x2 ∂y 2
Finite differences ⇒ stability problems when explicit; not tri-diagonal systems, when implicit.
Solution: Alternating-direction implicit (A.D.I) scheme.

-125-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

APPENDICES

A Mathematica code for Example 55

Mathematica
A = 1;
K = 2;
Fi [ t_ ] := 2.8;
Do [ h0 = 1.5 + 0.2 i ;
temp = NDSolve [{ h ’[ t ] == ( Fi [ t ] - K * Sqrt [ h [ t ]])/ A , h [0] == h0 } ,
h [ t ] , {t , 0 , 8}];
ploth [ i ] =
Plot [ h [ t ] /. temp [[1]] , {t , 0 , 8} , PlotRange -> All ,
DisplayFunction -> Identity ] , {i , 0 , 5}];
Show [ ploth [0] , ploth [1] , ploth [2] , ploth [3] , ploth [4] , ploth [5] ,
AxesLabel -> { " t " , " h ( t ) " } , DisplayFunction -> $DisplayFunction ]

h(t)

2.4

2.2

2.0

1.8

1.6
t
2 4 6 8

-126-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

B Mathematica code for Example 56

Mathematica
A = 1;
K = 2;
Fi [ t_ ] := 2.8*(1 + Sin [ t ]);
Do [ h0 = 1.5 + 0.2 i ;
temp = NDSolve [{ h ’[ t ] == ( Fi [ t ] - K * Sqrt [ h [ t ]])/ A , h [0] == h0 } ,
h [ t ] , {t , 0 , 8}];
ploth [ i ] =
Plot [ h [ t ] /. temp [[1]] , {t , 0 , 8} , PlotRange -> All ,
DisplayFunction -> Identity ] , {i , 0 , 5}];
Show [ ploth [0] , ploth [1] , ploth [2] , ploth [3] , ploth [4] , ploth [5] ,
AxesLabel -> { " t " , " h ( t ) " } , DisplayFunction -> $DisplayFunction ]

h(t)

t
2 4 6 8

-127-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

C Mathematica code for Example 57

Mathematica
u [ t_ ] := 2.8*(1 + Sin [20 t ]);
Do [ x0 = 1.5 + 0.2 i ;
temp = NDSolve [{ x ’[ t ] == -3 x [ t ] + u [ t ] , x [0] == x0 } ,
x [ t ] , {t , 0 , 8}];
plotx [ i ] =
Plot [ x [ t ] /. temp [[1]] , {t , 0 , 1} , PlotRange -> All ,
DisplayFunction -> Identity ] , {i , 0 , 5}];
Show [ Table [ plotx [ i ] , {i , 0 , 5}] , AxesLabel -> { " t " , " x ( t ) " } ,
DisplayFunction -> $DisplayFunction ]

x(t)

2.5

2.0

1.5

1.0
t
0.2 0.4 0.6 0.8 1.0

-128-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

D Mathematica code for Example 59

Mathematica
(* CSTR equations *)
Clear [V , k0 ,J , EoverR , UAtoverRhoCp , CAi , Ti , Tc ,F , CA , T ]
f1 ( CA_ , T_ , F_ , Tc_ ):=( F ( CAi - CA ))/ V - k0 E ˆ( -( EoverR / T )) CA
f2 ( CA_ , T_ , F_ , Tc_ ):= CA E ˆ( -( EoverR / T )) J k0 +( F ( Ti - T ))/ V -
( UAtoverRhoCp (T - Tc ))/ V

(* Calculation of steady state for given parameter values *)


V = 1.36;
k0 = 7.08 10ˆ7;
J = 0.02775;
EoverR = 8375;
UAtoverRhoCp = 0.7;
CAi = 8008;
Ti = 373.3;
Tcs = 420;
Fs = 1.133;
ss1 = FindRoot [{ f1 [ CA1 , T1 , Fs , Tcs ] == 0 ,
f2 [ CA1 , T1 , Fs , Tcs ] == 0} , {{ CA1 , CAi } , { T1 , Ti }}]
ss2 = FindRoot [{ f1 [ CA2 , T2 , Fs , Tcs ] == 0 ,
f2 [ CA2 , T2 , Fs , Tcs ] == 0} , {{ CA2 , 800} , { T2 , 460}}]
ss3 = FindRoot [{ f1 [ CA3 , T3 , Fs , Tcs ] == 0 ,
f2 [ CA3 , T3 , Fs , Tcs ] == 0} , {{ CA3 , 800} , { T3 , 510}}]

{ CA1 -> 7496.08 , T1 -> 399.915}

{ CA2 -> 4241.65 , T2 -> 455.737}

{ CA3 -> 1126.66 , T3 -> 509.167}

-129-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

(* Prepare graph for assessment of number of steady states *)


f [ T_ ] := -(( UAtoverRhoCp ( T - Tcs ))/ V ) + (( Ti - T ) Fs )/ V + (
J k0 CAi )/( E ˆ( EoverR / T ) (( V k0 )/( E ˆ( EoverR / T ) Fs ) + 1))

f[T]

(1.57333*10ˆ10 E ˆ( -8375/ T ))/(1 + 8.4985*10ˆ7 E ˆ( -8375/ T )) +


0.833088 (373.3 - T ) - 0.514706 ( -420 + T )

Plot [{(1.573*ˆ10 E ˆ( -8375/ T ))/(


1 + 8.4985*ˆ7 E ˆ( -8375/ T )) , -(0.833088 (373.3 - T ) -
0.514706 ( -420 + T ))} , {T , 350 , 550}]

200

150

100

50

400 450 500 550

-50

(* Integrate ODEs forward starting from initial conditions


close to each of the three steady states *)

(* Set first options for 3 D graphics formatting *)


SetOptions [{ ParametricPlot3D , ListPointPlot3D } ,
AxesLabel -> { " t " , " T ( t ) " , " CA ( t ) " } ,
PlotRange -> All ,
BoxRatios -> {1 , 1 , 1} ,
FaceGrids -> {{ -1 , 0 , 0}} ,
ViewPoint -> {1.475 , 2.388 , 1.889}];

-130-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

(* Integrate ODE forwards starting from


initial conditions close to steady state #3 *)
iMin = 95; iMax = 105; iIncrement = 5;
jMin = 99; jMax = 101; jIncrement = 1;
tMax = 6;
Do [
Do [
Clear [ CA , T ];
iCA = i /100.;
jT = j /100.;
temp3 = NDSolve [{ CA ’[ t ] == f1 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
T ’[ t ] == f2 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
CA [0] == iCA ( CA3 /. ss3 ) ,
T [0] == jT ( T3 /. ss3 )} ,
{ CA [ t ] , T [ t ]} ,
{t , 0 , tMax }];
Conc [ t_ ] := CA [ t ] /. temp3 [[1]];
Temp [ t_ ] := T [ t ] /. temp3 [[1]];
plotCAT [i , j ] = ParametricPlot3D [{ t , Temp [ t ] , Conc [ t ]} , {t , 0 , tMax } ,
DisplayFunction -> Identity ] ,
{i , iMin , iMax , iIncrement }] ,
{j , jMin , jMax , jIncrement }];

listIC = Table [{0 , j /100 ( T3 /. ss3 ) , i /100 ( CA3 /. ss3 )} ,


{i , iMin , iMax , iIncrement } ,
{j , jMin , jMax , jIncrement }];
listIC = Flatten [ listIC , 1];
plotIC = ListPointPlot3D [ listIC , PlotStyle -> PointSize [0.02] ,
DisplayFunction -> Identity ];
Show [{ Table [
plotCAT [i , j ] , {i , iMin , iMax , iIncrement } , {j , jMin , jMax ,
jIncrement }] , plotIC } , DisplayFunction -> $DisplayFunction ]

-131-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

(* Integrate ODE forwards starting from


initial conditions close to steady state #1 *)
iMin =95; iMax =105; iIncrement =5;
jMin =99; jMax =101; jIncrement =1;
tMax =6;
Do [
Do [
Clear [ CA , T ];
iCA = i /100.;
jT = j /100.;
temp1 = NDSolve [{ CA ’[ t ]== f1 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
T ’[ t ]== f2 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
CA [0]== iCA ( CA1 /. ss1 ) ,
T [0]== jT ( T1 /. ss1 )} ,
{ CA [ t ] , T [ t ]} ,
{t ,0 , tMax }];
Conc [ t_ ]:= CA [ t ]/. temp1 [[1]];
Temp [ t_ ]:= T [ t ]/. temp1 [[1]];
plotCAT [i , j ]= ParametricPlot3D [{ t , Temp [ t ] , Conc [ t ]} ,{ t ,0 , tMax } ,
DisplayFunction - > Identity ] ,
{i , iMin , iMax , iIncrement }] ,
{j , jMin , jMax , jIncrement }];

listIC = Table [{0 , j /100( T1 /. ss1 ) , i /100( CA1 /. ss1 )} ,


{i , iMin , iMax , iIncrement } ,
{j , jMin , jMax , jIncrement }];
listIC = Flatten [ listIC ,1];
plotIC = ListPointPlot3D [ listIC , PlotStyle -> PointSize [0.02] ,
DisplayFunction -> Identity ];
Show [{ Table [
plotCAT [i , j ] , {i , iMin , iMax , iIncrement } , {j , jMin , jMax ,
jIncrement }] , plotIC } , DisplayFunction -> $DisplayFunction ]

-132-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

(* Integrate ODE forwards starting from


initial conditions close to steady state #2 *)
iMin =95; iMax =105; iIncrement =5;
jMin =99; jMax =101; jIncrement =1;
tMax =6;
Do [
Do [
Clear [ CA , T ];
iCA = i /100.;
jT = j /100.;
temp2 = NDSolve [{ CA ’[ t ]== f1 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
T ’[ t ]== f2 [ CA [ t ] , T [ t ] , Fs , Tcs ] ,
CA [0]== iCA ( CA2 /. ss2 ) ,
T [0]== jT ( T2 /. ss2 )} ,
{ CA [ t ] , T [ t ]} ,
{t ,0 , tMax }];
Conc [ t_ ]:= CA [ t ]/. temp2 [[1]];
Temp [ t_ ]:= T [ t ]/. temp2 [[1]];
plotCAT [i , j ]= ParametricPlot3D [{ t , Temp [ t ] , Conc [ t ]} ,{ t ,0 , tMax } ,
DisplayFunction - > Identity ] ,
{i , iMin , iMax , iIncrement }] ,
{j , jMin , jMax , jIncrement }];

listIC = Table [{0 , j /100( T2 /. ss2 ) , i /100( CA2 /. ss2 )} ,


{i , iMin , iMax , iIncrement } ,
{j , jMin , jMax , jIncrement }];
listIC = Flatten [ listIC ,1];

plotIC = ListPointPlot3D [ listIC , PlotStyle - > PointSize [0.02] ,


DisplayFunction - > Identity ];
Show [{ Table [ plotCAT [i , j ] ,{i , iMin , iMax , iIncrement } ,
{j , jMin , jMax , jIncrement }] , plotIC } ,
DisplayFunction - >$DisplayFunction ]

-133-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

E Mathematica code for Example 60

Mathematica
\[ Sigma ]=10;
b =8/3;
r =28;
tMax =20;

tempLorenz = NDSolve [{ x ’[ t ]== -\[ Sigma ]( x [ t ] - y [ t ]) ,


y ’[ t ]== - x [ t ] z [ t ]+ r * x [ t ] - y [ t ] ,
z ’[ t ]== x [ t ] y [ t ] - b * z [ t ] , x [0]== z [0]== y [0]==5} ,
{x ,y , z } ,{t ,0 , tMax } , AccuracyGoal - >4];

t em p L o re n z Pe r t ur b e d = NDSolve [{ x ’[ t ]== -\[ Sigma ] ( x [ t ] - y [ t ]) ,


y ’[ t ]== - x [ t ] z [ t ]+ r * x [ t ] - y [ t ] ,
z ’[ t ]== x [ t ] y [ t ] - b * z [ t ] ,
x [0]==5.001 , z [0]== y [0]==5} ,
{x ,y , z } , {t ,0 , tMax } , AccuracyGoal - >4];

Plot [ Evaluate [{ x [ t ]/. tempLorenz [[1]] , x [ t ]/. t e mp L o re n z Pe r t ur b e d [[1]]}] ,


{t ,0 , tMax } , PlotStyle - >{ Dashing [{0 ,0}] , Dashing [{0.01 ,0.01}]} , Frame - > True ]

20

15

10

-5

-10

-15

0 5 10 15 20

tempLorenz = NDSolve [{ x ’[ t ]== -\[ Sigma ] ( x [ t ] - y [ t ]) ,


y ’[ t ]== - x [ t ] z [ t ]+ r * x [ t ] - y [ t ] ,
z ’[ t ]== x [ t ] y [ t ] - b * z [ t ] , x [0]== z [0]== y [0]==5} ,
{x ,y , z } ,{t ,0 , tMax } , AccuracyGoal - >10];

t em p L o re n z Pe r t ur b e d = NDSolve [{ x ’[ t ]== -\[ Sigma ] ( x [ t ] - y [ t ]) ,


y ’[ t ]== - x [ t ] z [ t ]+ r * x [ t ] - y [ t ] ,
z ’[ t ]== x [ t ] y [ t ] - b * z [ t ] ,
x [0]==5.001 , z [0]== y [0]==5} ,
{x ,y , z } , {t ,0 , tMax } , AccuracyGoal - >10];

Plot [ Evaluate [{ x [ t ]/. tempLorenz [[1]] , x [ t ]/. t e mp L o re n z Pe r t ur b e d [[1]]}] ,


{t ,0 , tMax } , PlotStyle - >{ Dashing [{0 ,0}] , Dashing [{0.01 ,0.01}]} , Frame - > True ]

-134-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

20

15

10

-5

-10

-15

0 5 10 15 20

-135-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

F Mathematica code for Example 66

Mathematica
FiOverRho [ t_ ]:=0.3; (* m ˆ3/ min *)
a =0.2; (* m ˆ3/2/ min *)
Ti [ t_ ]:=300; (* K *)
UAtOverRhoCp =0.2; (* m ˆ3/ min *)
(* U =500 -: - 10 ,000 W / m ˆ2/ K
A =4 m ˆ2 rho =1000 kg / m ˆ3 cp =4.2 joule / g / K *)
Tc [ t_ ]:=500; (* K *)
V0 =1;
T0 =300;

tMin =0;
tMax =90;

temp = NDSolve [{ V ’[ t ]== FiOverRho [ t ] - a * Sqrt [ V [ t ]] ,


T ’[ t ]== FiOverRho [ t ]/ V [ t ]*( Ti [ t ] - T [ t ])+ UAtOverRhoCp / V [ t ]*( Tc [ t ] - T [ t ]) ,
V [0]== V0 ,
T [0]== T0 } ,
{ V [ t ] , T [ t ]} ,{ t , tMin , tMax }]

SetOptions [ Plot ,
PlotRange - > All ,
Frame - > True ];

Plot [ Evaluate [ V [ t ]/. temp ] ,{t , tMin , tMax } ,


FrameLabel - >{ " t ␣ ( min ) " , " V ␣ ( m ˆ3) " }]

Plot [ Evaluate [ T [ t ]/. temp ] ,{t , tMin , tMax } ,


FrameLabel - >{ " t ␣ ( min ) " , " T ( K ) " }]

2.2 380

2.0
360
1.8
V (m^3)

T(K)

1.6 340

1.4
320
1.2

1.0 300
0 20 40 60 80 0 20 40 60 80
t (min) t (min)

-136-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

G MATLAB code for Example 67


Create the m-file HeatTransferODE.m first:

MATLAB
function RHS = HeatTransferODE ( time , z )
%
% define variables U , T0 , TL , Ta , L as global ,
% to assign numerical values to them once
% outside this function
%
global U Ta
%
% compute right - hand side of vector equations dy / dt = f (t , y ( t ))
%
RHS = [ z (2); U *( z (1) - Ta )];

Then run the following MATLAB code:

MATLAB

% assign numerical values to global variables U , Ta ,


% to be able to use such values in all sub - programs
% that include the global command
%
global U Ta
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 1.1;
%
% guess two values for z2 (0)
z20one = -2700;
z20two = -2800;
%
% call ODE solver , for one initial condition , plot solution , and label plot
%
% guess one
z0 = [ T0 ; z20one ];
[X , Z1 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
figure (1)
plot (X , Z1 (: ,1) , ’o - ’ );
hold on
%
% % guess two
z0 = [ T0 ; z20two ];

-137-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

[X , Z2 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z2 (: ,1) , ’s - ’ );
%
% final guess
z20new = z20two + ( TL - Z2 ( end ,1))*( z20two - z20one )/( Z2 ( end ,1) - Z1 ( end ,1))
z0 = [ T0 ; z20new ]
[X , Z ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z (: ,1) , ’ˆ - ’ ); xlabel ( ’x ’ );
ylabel ( ’ z_1 ˆ(ˆ1ˆ)( x ) , ␣ z_1 ˆ(ˆ2ˆ)( x ) , ␣ z_1 ˆ(ˆ3ˆ)( x ) ’ ); axis tight ;
legend ( ’ z_1 ˆ(ˆ1ˆ)( x ) ’ , ’ z_1 ˆ(ˆ2ˆ)( x ) ’ , ’ z_1 ˆ(ˆ3ˆ)( x ) ’ , ’ Location ’ , ’ northwest ’)
hold off
%
figure (2)
plot (X , Z (: ,1) , ’ˆ - ’ ); xlabel ( ’x ’ ); ylabel ( ’ z_1 ( x ) ’ ); axis tight ;
title ( ’ Final ␣ solution ␣ T ( x ) ␣ in ␣ detail ’)

-138-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

H MATLAB code for Example 69

MATLAB
clear T
clear x

% Parameter values
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 1.1;

% Initiate selection of grid size


n = 2;

% Selection of bound for iterative refinement of solution


% by grid size increase
maxiter = 5;

% Set up and solve system of linear algebraic equations


for i = 1: maxiter

n = 2* n ;
h = L/n;

A = diag ((2+ U * h ˆ2)* ones (n -1 ,1)) ...


+ diag ( - ones (n -2 ,1) ,1) ...
+ diag ( - ones (n -2 ,1) , -1);
b = U * h ˆ2* Ta * ones (n -1 ,1) + [ T0 ; zeros (n -3 ,1); TL ];

Ti = A \ b ;

T = [ T0 ; Ti ; TL ]
for i = 1: n +1
x ( i ) = (i -1)* h ;
end

% Graphic representation of the solution


figure (1)
plot (x , T )
xlabel ( ’x ’)
ylabel ( ’T ( x ) ’)
axis tight
hold on
end
hold off

-139-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

I MATLAB code for Example ??

MATLAB
clear T
clear x
clear c

% Parameter values
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 100;

% Initiate selection of grid size


n = 1;

% Selection of bound for iterative refinement of solution by grid size increase


maxiter = 6;

% Set up and solve system of linear algebraic equations


% for increasingly refined grids
for index = 1: maxiter

n = 2* n ;
h = L/n;

% Calculate grid points


for i = 1: n
x(i) = i*h;
end

% Set up and solve linear system of equations


clear A11 ;
A11 = [1 0 ; 1 L ];

clear A12 ;
clear temp ;
temp = L ˆ2;
for i = 1: n -2
temp = [ temp L ˆ( i +2)];
end
A12 = [ zeros (1 ,n -1); temp ];

clear A21
A21 = [( - U ) ( - U * x (1))];
for i = 1: n -2
A21 = [ A21 ; ( - U ) ( - U * x ( i +1))];
end

-140-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

clear A22
A22 = [];
clear temp
for i = 1: n -1
temp = 1*2 - U * x ( i )ˆ2;
for j = 1: n -2
temp = [ temp ( j +1)*( j +2)* x ( i )ˆ j - U * x ( i )ˆ( j +2)];
end
A22 = [ A22 ; temp ];
end

clear A
clear b
clear c

A = [ A11 A12 ; A21 A22 ];

b = [ T0 ; TL ; -U * Ta * ones (n -1 , 1)];

c = A\b;

% Graphic representation of the coefficients c_i


% figure ( n )
figure (10)
subplot ( maxiter ,2 ,2* index -1)
plot (0: n , c , ’o - ’)
xlabel ( ’i ’)
ylabel ( ’ c_i ’)
axis tight
hold off

% Calculation and graphic representation of the solution T ( x )


m = 10* n ;
for i = 1: m -1
T ( i ) = c (1);
for j = 1: n
T ( i ) = T ( i ) + c ( j +1)*( i * L / m )ˆ j ;
end
end
subplot ( maxiter ,2 ,2* index )
plot ((0: m )* L /m , [ T0 T TL ] , ’ - ’)
xlabel ( ’x ’)
ylabel ( ’T ( x ) ’)
axis tight
hold off

end

-141-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

J Mathematica code for Example 74

Mathematica
(* Set grid size
x_i , i =0 ,... , m
y_j , j =0 ,... , n
There are (n -1)*( m -1) interior points *)
WW =75; NN =100; EE =50; SS =0;
m =20; n =20;

(* Concstruct matrix A *)
Clear [ AA ]
(* Do [
Do [
AA [i , j ]=0 ,
{i ,1 ,( m -1)*( n -1)}] ,
{j ,1 ,( m -1)*( n -1)}]; *)
Do [
Do [
If [ i == j , AA [i , j ]= -4. ,
If [ j == i +m -1 , AA [i , j ]=1. ,
If [ i == j +m -1 , AA [i , j ]=1. ,
If [ j == i +1 ,
If [i -( m -1)* IntegerPart [ i /( m -1)]==0 , AA [i , j ]=0. , AA [i , j ]=1.] ,
If [ i == j +1 ,
If [j -( m -1)* IntegerPart [ i /( m -1)]==0 , AA [i , j ]=0. , AA [i , j ]=1.] ,
AA [i , j ]=0.]]]]] ,
{i ,1 ,( m -1)*( n -1)}
],
{j ,1 ,( m -1)*( n -1)}
]
matrixA = Table [ AA [i , j ] ,{i ,1 ,( m -1)*( n -1)} ,{ j ,1 ,( m -1)*( n -1)}];
MatrixForm [ matrixA ];
M a t r i x C o n d i t i o n N u m b e r [ matrixA ];

(* Construct right - hand - side vector b *)


Clear [ bb ]
Do [
Do [
If [ i +1== m ,
If [ j +1== n , bb [i , j ]= - EE - NN ,
If [j -1==0 , bb [i , j ]= - EE - SS , bb [i , j ]= - EE ]
],

If [i -1==0 ,
If [ j +1== n , bb [i , j ]= - WW - NN ,
If [j -1==0 , bb [i , j ]= - WW - SS , bb [i , j ]= - WW ]
],

-142-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

If [ j +1== n , bb [i , j ]= - NN ,
If [j -1==0 , bb [i , j ]= - SS , bb [i , j ]=0]
]
]
],
{i ,1 ,m -1}
],
{j ,1 ,n -1}
]
vectorb = Flatten [ Table [ bb [i , j ] ,{j ,1 ,n -1} ,{ i ,1 ,m -1}]];
MatrixForm [ vectorb ];

(* Solve equations *)
T = LinearSolve [ matrixA , vectorb ]// N ;
MatrixForm [%];

(* Plot results *)
rows = Table [
Flatten [{ WW , Table [ T [[ i +j -1]] ,{ i ,1 ,m -1}] , EE }] ,
{j ,1 ,( n -1)*( m -1) , m -1}
];
rowSS = Table [ SS ,{ j ,0 , m }];
rowNN = Table [ NN ,{ j ,0 , m }];
Tlist = Join [{ rowSS } , rows ,{ rowNN }];
MatrixForm [%];
ListPlot3D [ Tlist ,
Ticks - >{ None , None , Automatic } ,
PlotRange - > All ,
BoxRatios - >{1 ,1 ,1} ,
AxesLabel - >{ " x " ," y " ," T (x , y ) " } , Mesh - > Full ,
ColorFunction - > " TemperatureMap " ]

-143-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

K MATLAB code for Example 74

MATLAB
% Solution of Laplace equation over rectangular domain
% Dirichlet boundary conditions
%
clear A
clear b
clear bb
clear T

WW = 75;
NN = 100;
EE = 50;
SS = 0;

% Select grid size


m = 40; % x - direction , index i
n = 40; % y - direction , index j

% Construct A as a multi - diagonal matrix


Diagonal = -4* eye (( m -1)*( n -1));
temp = ones (( m -1)*( n -1) -1 ,1);
for i =1: n -2
temp ( i *( m -1) ,1)=0;
end
DiagonalPlusOne = diag ( temp ,1);
DiagonalMinusOne = diag ( temp , -1);
D i a g o n a l P l u s m M i n u s O n e = diag ( ones (( m -1)*( n -1) -( m -1) ,1) , m -1);
D i a g o n a l M i n u s m M i n u s O n e = diag ( ones (( m -1)*( n -1) -( m -1) ,1) , -( m -1));
clear A
A = Diagonal +...
DiagonalPlusOne +...
DiagonalMinusOne +...
D i a g o n a l P l u s m M i n u s O n e +...
DiagonalMinusmMinusOne ;

% Construct b
for j = 1: n -1
for i = 1: m -1
if i +1 == m
if j +1 == n
bb (i , j ) = -EE - NN ;
elseif j -1 == 0
bb (i , j ) = -EE - SS ;
else
bb (i , j ) = - EE ;
end
elseif i -1 == 0

-144-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

if j +1 == n
bb (i , j ) = -WW - NN ;
elseif j -1 == 0
bb (i , j ) = -WW - SS ;
else
bb (i , j ) = - WW ;
end
elseif j +1 == n
bb (i , j ) = - NN ;
elseif j -1 == 0
bb (i , j ) = - SS ;
else
bb (i , j ) = 0;
end
end
end
clear b
b = bb (: ,1);
for j = 2: n -1
b = [ b ; bb (: , j )];
end

% Solve linear equations


clear T
T = A\b;

% Plot results
clear Z
for i = 1: m -1
for j = 1: n -1
Z ( j +1 , i +1) = T (( j -1)*( m -1)+ i ,1);
end
end

for i = 1: m +1
Z (1 , i ) = SS ;
Z ( n +1 , i ) = NN ;
end

for j = 2: n
Z (j ,1) = WW ;
Z (j , m +1) = EE ;
end
colormap ( jet )
surf ( Z )
axis tight
xlabel ’x ’
ylabel ’y ’
zlabel ’T (x , y ) ’
title ’ Laplace ␣ equation ␣ in ␣ 2 D ’
colorbar

-145-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

L MATLAB code for Example 75

MATLAB
% Solution of Laplace equation over rectangular domain
% Dirichle boundary conditions
%
clear A
clear b
clear T

WW = 75;
NN = 100;
EE = 50;
SS = 0;

% Select grid size


mgrid = 40; % x - direction , index i
ngrid = 40; % y - direction , index j

m = mgrid ;
n = ngrid + 1;

% Construct A
Diagonal = -4* eye (( m -1)*( n -1));
temp = ones (( m -1)*( n -1) -1 ,1);
for i =1: n -2
temp ( i *( m -1) ,1)=0;
end
DiagonalPlusOne = diag ( temp ,1);
DiagonalMinusOne = diag ( temp , -1);
D i a g o n a l P l u s m M i n u s O n e = diag ( ones (( m -1)*( n -1) -( m -1) ,1) , m -1);
D i a g o n a l M i n u s m M i n u s O n e = diag ( ones (( m -1)*( n -1) -( m -1) ,1) , -( m -1));
clear A
clear ANeumann
A = Diagonal +...
DiagonalPlusOne +...
DiagonalMinusOne +...
D i a g o n a l P l u s m M i n u s O n e +...
DiagonalMinusmMinusOne ;

temp1 = [ eye (m -1); zeros (( m -1)*( n -2) , m -1); eye (m -1)];


temp2 = [ A ; [ zeros (m -1 ,m -1) - eye (m -1) zeros (m -1 ,( m -1)*( n -3))] ];
ANeumann = [ temp1 temp2 ];

% Construct b
clear bb
for j = 1: n -1
for i = 1: m -1

-146-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

if i +1 == m
if j +1 == n
bb (i , j ) = -EE - NN ;
elseif j -1 == 0
bb (i , j ) = -EE - SS ;
else
bb (i , j ) = - EE ;
end
elseif i -1 == 0
if j +1 == n
bb (i , j ) = -WW - NN ;
elseif j -1 == 0
bb (i , j ) = -WW - SS ;
else
bb (i , j ) = - WW ;
end
elseif j +1 == n
bb (i , j ) = - NN ;
elseif j -1 == 0
bb (i , j ) = - SS ;
else
bb (i , j ) = 0;
end
end
end
clear b
clear bNeumann
b = bb (: ,1);
for j = 2: n -1
b = [ b ; bb (: , j )];
end
bNeumann = [ b ; zeros (m -1 ,1)];

% Solve linear equations


clear TNeumann
TNeumann = ANeumann \ bNeumann ;

% Plot results
clear Z
for i = 1: mgrid -1
for j = 1: ngrid
Z (j , i +1) = TNeumann ( j *( mgrid -1)+ i ,1);
end
end

for i = 1: mgrid +1
% Z (1 , i ) = SS ;
Z ( ngrid +1 , i ) = NN ;
end

for j = 1: ngrid +1

-147-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

Z (j ,1) = WW ;
Z (j , mgrid +1) = EE ;
end

colormap ( jet )
surf ( Z )
axis tight
xlabel ’x ’
ylabel ’y ’
zlabel ’T (x , y ) ’
title ’ Laplace ␣ equation ␣ in ␣ 2 D ’
colorbar

-148-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

M MATLAB code for Example 79

MATLAB
% Numerical solution of the transient heat equation
% using an implicit numerical method
%
% Define problem data
L = 10;
T0 = 100;
TL = 50;
Tinit = 0;
k = 0.835

% Define number of time steps and step size


nSteps = 30;
Dt = 3;

% Define grid size


n = 25;

% Set up matix A
clear A
clear b
clear T
Dx = L / n ;
lambda = k * Dt / Dx ˆ2
A = diag ( ones (n -1 ,1)*(1+2* lambda )) + ...
diag ( ones (n -2 ,1)*( - lambda ) ,1) + ...
diag ( ones (n -2 ,1)*( - lambda ) , -1);

% Perform recursive computation of PDE solution


T (: ,1) = ones (n -1 ,1)* Tinit ;
for i = 1: nSteps
b = [ lambda * T0 ; zeros (n -3 ,1); lambda * TL ] + T (: , i );
T (: , i +1) = A \ b ;
end

% Plot results
figure (1)
plot ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)])
axis tight
set ( gca , ’ xtick ’ ,[])
xlabel ( strcat ( ’ {\ itx } , ␣ 0... to ... ’ , num2str ( L )))
ylabel ( ’ {\ itT }({\ itx , t_l }) ’)

figure (3)
colormap ( jet )
surf ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)] ’)
axis tight

-149-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

xlabel ( strcat ( ’ {\ itx } , ␣ 0... to ... ’ , num2str ( L )))


ylabel ( strcat ( ’ time , ␣ {\ itt } , ␣ 0... to ... ’ , num2str ( Dt *( nSteps +1))))
zlabel ( ’ {\ itT }({\ itx , t }) ’)
set ( gca , ’ xtick ’ ,[])
set ( gca , ’ ytick ’ ,[])
colorbar

figure (2)
plot ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)] ’)
axis tight
xlabel ( strcat ( ’ time , ␣ {\ itt } , ␣ 0... to ... ’ , num2str ( Dt *( nSteps +1))))
ylabel ( ’ {\ itT }({\ itx_i , t }) ’)
clear stringMatrix

set ( gca , ’ xtick ’ ,[])

for i = 1: n +1
s = int2str (i -1);
stringMatrix (i ,:) = strcat ( ’T ( x_ ’ ,s , ’ ,␣ t ) ’ );
end
legend ( stringMatrix , ’ Location ’ , ’ southeast ’)

-150-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

N MATLAB code for Example 81

MATLAB
% Numerical solution of the transient heat equation
% using the Crank - Nicolson numerical method
%
% Define problem data
L = 10;
T0 = 100;
TL = 50;
Tinit = 0;
k = 0.835

% Define number of time steps and step size


nSteps = 100;
Dt = 3;

% Define grid size


n = 25;

% Set up matices A , Mtemp


clear A
clear b
clear T
clear Mtemp
Dx = L / n ;
lambda = k * Dt / Dx ˆ2
A = diag ( ones (n -1 ,1)*2*(1+ lambda )) + ...
diag ( ones (n -2 ,1)*( - lambda ) ,1) + ...
diag ( ones (n -2 ,1)*( - lambda ) , -1);
Mtemp = diag ( ones (n -1 ,1)*2*(1 - lambda )) + ...
diag ( ones (n -2 ,1)* lambda ,1) + ...
diag ( ones (n -2 ,1)* lambda , -1);

% Perform recursive computation of PDE solution


T (: ,1) = ones (n -1 ,1)* Tinit ;
for i = 1: nSteps
b = [2* lambda * T0 ; zeros (n -3 ,1); 2* lambda * TL ] + Mtemp * T (: , i );
T (: , i +1) = A \ b ;
end

% Plot results
figure (1)
plot ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)])
axis tight
set ( gca , ’ xtick ’ ,[])
xlabel ( strcat ( ’ {\ itx } , ␣ 0... to ... ’ , num2str ( L )))
ylabel ( ’ {\ itT }({\ itx , t_l }) ’)

-151-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis

figure (3)
colormap ( jet )
surf ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)] ’)
axis tight
xlabel ( strcat ( ’ {\ itx } , ␣ 0... to ... ’ , num2str ( L )))
ylabel ( strcat ( ’ time , ␣ {\ itt } , ␣ 0... to ... ’ , num2str ( Dt *( nSteps +1))))
zlabel ( ’ {\ itT }({\ itx , t }) ’)
set ( gca , ’ xtick ’ ,[])
set ( gca , ’ ytick ’ ,[])
colorbar

figure (2)
plot ([ T0 * ones (1 , nSteps +1); T ; TL * ones (1 , nSteps +1)] ’)
axis tight
xlabel ( strcat ( ’ time , ␣ {\ itt } , ␣ 0... to ... ’ , num2str ( Dt *( nSteps +1))))
ylabel ( ’ {\ itT }({\ itx_i , t }) ’)
clear stringMatrix

set ( gca , ’ xtick ’ ,[])

-152-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy