Numerical & Statistical Anylysis For Cheme's Part3
Numerical & Statistical Anylysis For Cheme's Part3
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-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
(Lumped-parameter System)
Assumptions: ...............
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
d(VρCp (T − Tref ))
= Fi Cp (Ti − Tref ) − F Cp (T − Tref ) + Q (3)
dt
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
Example 2 Mathematical model for a jacket-cooled continuous-flow stirred tank reactor (CSTR)
• 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
-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
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:
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.
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:
• Mass balance on B:
• 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 :
• Mass balance on P2 :
• Energy balance:
dH
= ρ1 F1 H1 (T1 ) + ρ2 F2 H2 (T2 ) − ρF H(T ) + Q
dt
Now,
-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
-7-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
(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
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)
-9-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
dx
α (t)x(t),
= |{z} initial condition x(t0 ) given (25)
dt const
⇒
x(t) = x(t0 )eα(t−t0 ) (26)
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
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:
-11-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
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
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
λ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 }
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 +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
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 α
-16-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
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
-18-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Eq. 31 ⇒ λ2 + 4λ + 4 = 0 ⇒ λ1 = λ2 = −2
1 1 3 1
P= ⇒ P−1 =
−2 −3 −2 −1
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
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.
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
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
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
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
√ }}
λ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
-22-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
-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 }| {
| {z } | {z }
Mode 1 Mode n
-24-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
“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
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
-27-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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.
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
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?)
-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
Numerically Analytically
Integration: Easy Difficult
Differentiation: Difficult Easy
-30-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
-31-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
f (b) − f (a)
f (x) ≈ p(x) = a0 + a1 x = f (a) + (x − a) ⇒
b−a
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
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
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
For h = 0.5, n = 6:
-34-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
f (x) ≈ p(x) = a0 + a1 x + a2 x2 + a3 x3 ⇒
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
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
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
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
Numerical Differentiation
-37-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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!
-38-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
f (x0 + h) − f (x0 − h)
• Centered difference: f 0 (x0 ) ≈
2h
f (x0 + h) − f (x0 − h)
⇒ f 0 (x0 ) = + O(h2 )
2h
Geometric interpretation:
-39-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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 ⇒
Geometric interpretation:
-40-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Note: A large number of finite-difference formulas exist. Check textbook for comprehensive
list.
-41-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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)
Consider points {t0 , t1 , t2 , . . .} and calculate values h1 (t), h(t2 ), . . . h(tn ), given
h(t0 ) = h(0) and Fi (t)
-42-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
-43-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
-44-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
Numerical solution
where
ˆ i+1 − xi
h=x (62)
• What is F ?
where
ˆ i+1 − xi
h=x (64)
-47-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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.)
• Sources of inaccuracy:
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
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).
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 !
Elocal = O(h2 )
Eglobal = O(h)
-49-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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 ) = . . .
dy
Solve: = 3x2 , y(0) = 1 (Exact solution: y = (x3 + 1))
dx
Euler’s method: yi+1 = yi + 3x2i h
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
Graphical interpretation:
Fill in the graph.
• 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
Graphical interpretation:
Fill in the graph.
-51-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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=?
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
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
• 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)
• 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
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
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
Theorem 11: Local and global error of the fourth-order Runge-Kutta method
dy
Solve: = −y(x) + cos x, y(0) = 3
dx
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
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
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
h
yi+1 = yi + [55fi − 59fi−1 + 37fi−2 − 9fi−3 ] (77)
24
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.
h
yi+1 = yi + [9fi+1 + 19fi − 5fi−1 + fi−2 ] (79)
24
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 .
Mathematica: NDSolve
(ÒP Hwnthi: What methods does Mathematica use?)
-57-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
function F = RightHandSideODE(x,y)
F = - y + cos(x);
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
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
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 ;
-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
1 2 3 4 5 6 7
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
2 4 6 8
Observe Example 43 and Example 45. Why are some points spaced less densely than others?
-61-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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.
-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)
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?
-63-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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.
-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
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
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))]
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
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
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.
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
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 ⇒:
-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
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 ⇒:
-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
-73-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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)
(Ò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?)
Steady state in Eq. 105 is locally unstable: (What does that mean?)
-75-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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?
-76-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
dx
= 3x(t) − 4x(t)2 , x(0) = 0.9331 (107)
dt
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!
∗
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.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.
Revisit Example 34, for step change in the inlet flow rate Fi (t) = 2.9 m3/min. Euler’s method:
∗
Kincaid, D. and W. Cheney, Numerical Analysis, 2nd Ed., Brooks/Cole, 1996
-80-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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.
-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:
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.
• While the implicit scheme addresses stability, it is still a first order method in terms
of accuracy!
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
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
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
- Mathematica: NDSolve
-84-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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)
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
-86-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
500
450
400
350
z 1 (x)
300
250
200
150
100
ÒP Hwnthi: Why is the shooting method so sensitive to z2 (0) guesses? (Hint: See Eq. 42).
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.
- 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
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.
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
-89-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
550
500
450
400
T(x) 350
300
250
200
150
100
550
500
450
400
350
T(x)
300
250
200
150
100
0 20 40 60 80 100
x
-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
∂ 2T ∂ 2T
+ =0 (Laplace equation)
∂x2 ∂y 2
d2 ν dν
n 2 = (simple case of Navier-Stokes equations)
dx dt
∂c ∂ 2c
=D 2 (Fick’s law)
∂t ∂x
• Linearity classification
-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
g = 0 ⇒ Homogeneous
-93-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
- Constitutive equations
Perfect
conductor
Insulator
Conservation of energy:
∂ 2T ρCp ∂T
2
= , 0<x<L (125)
∂x k ∂t
∂ 2T
At steady state: = 0 (Compare with Section 2.5)
∂x2
-94-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Boundary Conditions:
Initial Condition:
T (x, t = 0) = T1 , 0 ≤ x ≤ L (128)
-95-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Vanishingly
small
control
volume
Conservation of mass:
-96-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
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
-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
(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
(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
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, theanalytical 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
• 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:
-103-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
∂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
• 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
– Gauss-Seidel
– Jacobi
-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
Initial Condition:
T (x, t = 0) = Tinit , 0 ≤ x ≤ L (140)
-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 }
λ
-107-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Ò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]
-108-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
∂T
Insulator: (x = L, t) = 0 , t > 0 (144)
∂x
Initial Condition:
T (x, t = 0) = Tinit , 0 ≤ x ≤ L (145)
• 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
∆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
-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
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).
∆t 1
λ=k
ˆ ≤ (149)
∆x2 2
∆t 1
λ=k
ˆ 2
≤ (150)
∆x 4
Theorem 13 constraints the maximum time-step ∆t for explicit methods, possibly creating
inefficiencies.
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.
`+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.
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
% 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);
-114-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
-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.
-116-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
• Why would numerical stability be jeopardized if second-order derivatives were used for
∂T /∂t ?
• 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 }
2λ
• 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
Illustrate how numerical stability is not jeopardized even though second-order derivatives
-118-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
• 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
`+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
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
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
-120-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
of λ.
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
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
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)
-123-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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
kπ 2
lim λ1 = − (160)
n→∞ L2
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.
• 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.
-125-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
APPENDICES
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
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
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
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
-129-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
f[T]
200
150
100
50
-50
-130-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
-131-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
-132-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
-133-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
Mathematica
\[ Sigma ]=10;
b =8/3;
r =28;
tMax =20;
20
15
10
-5
-10
-15
0 5 10 15 20
-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
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;
SetOptions [ Plot ,
PlotRange - > All ,
Frame - > True ];
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
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 )];
MATLAB
-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
MATLAB
clear T
clear x
% Parameter values
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 1.1;
n = 2* n ;
h = L/n;
Ti = A \ b ;
T = [ T0 ; Ti ; TL ]
for i = 1: n +1
x ( i ) = (i -1)* h ;
end
-139-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
MATLAB
clear T
clear x
clear c
% Parameter values
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 100;
n = 2* n ;
h = L/n;
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
b = [ T0 ; TL ; -U * Ta * ones (n -1 , 1)];
c = A\b;
end
-141-
CBE301 Lecture Notes - Part 3 Manolis Doxastakis
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 ];
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
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;
% 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
% 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
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;
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 ;
% 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)];
% 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
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
% 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);
% 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
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
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
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
% 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
-152-