You are on page 1of 28

Chemical Engineering Modelling (ENCH610014; ENCH600011)

Lecture 7:
Ordinary Differential Equations with
Boundary Value Problems
(Finite Difference Methods)
Muhammad Ibadurrohman, Ph.D (DIC)
▪ ODE: ▪ Boundary conditions:

𝑦𝑖 𝑥0 = 𝑦𝑖,0
𝑑𝑛 𝑦 𝑑 𝑛−1 𝑦 𝑑 𝑛−2 𝑦
=𝑓 , , … , 𝑦, 𝑥
𝑑𝑥 𝑛 𝑑𝑥 𝑛−1 𝑑𝑥 𝑛−2 𝑦𝑗 𝑥𝑓 = 𝑦𝑗,𝑓

in canonical forms: or

𝑑𝑦1
= 𝑓1 𝑥, 𝑦1 , 𝑦2 , … , 𝑦𝑛 𝑑𝑦𝑖 𝑑𝑦𝑖
𝑑𝑥 = ቤ
𝑑𝑥 𝑑𝑥 𝑥=𝑥
0
𝑑𝑦2
= 𝑓2 𝑥, 𝑦1 , 𝑦2 , … , 𝑦𝑛
𝑑𝑥 𝑑𝑦𝑗 𝑑𝑦𝑗
= ቤ
⋮ 𝑑𝑥 𝑑𝑥 𝑥=𝑥
𝑓

𝑑𝑦𝑛
= 𝑓𝑛 𝑥, 𝑦1 , 𝑦2 , … , 𝑦𝑛 Or combination thereof!
𝑑𝑥
▪ General form of second order ODE:
𝑑2𝑦 𝑑𝑦 𝑑2𝑦 𝑑𝑦
𝑎 𝑥, 𝑦 + 𝑏 𝑥, 𝑦 + 𝑐 𝑥, 𝑦 = 𝑑(𝑥) or 𝑎 𝑥, 𝑦 + 𝑏 𝑥, 𝑦 + 𝑐 𝑥, 𝑦 = 0
𝑑𝑥 2 𝑑𝑥 𝑑𝑥 2 𝑑𝑥

▪ Boundary conditions:
▪ Initial condition
𝑑𝑦 𝑑𝑦
𝑏𝑖 𝑥𝑖 , 𝑦𝑖 + 𝑐𝑖 𝑥𝑖 , 𝑦𝑖 = 𝑑𝑖 𝑥𝑖 or 𝑏𝑖 𝑥𝑖 , 𝑦𝑖 + 𝑐𝑖 𝑥𝑖 , 𝑦𝑖 = 0
𝑑𝑥 𝑑𝑥

▪ Final condition

𝑑𝑦 𝑑𝑦
𝑏𝑓 (𝑥𝑓 , 𝑦𝑓 ) + 𝑐𝑓 (𝑥𝑓 , 𝑦𝑓 ) = 𝑑𝑓 𝑥𝑓 or 𝑏𝑓 (𝑥𝑓 , 𝑦𝑓 ) + 𝑐𝑓 (𝑥𝑓 , 𝑦𝑓 ) = 0
𝑑𝑥 𝑑𝑥
▪ A more generic form of ODE

𝑑𝑦 𝑑 2 𝑦 𝑑𝑦
𝑎(𝑥, 𝑦, ) 2 + 𝑏(𝑥, 𝑦) + 𝑐(𝑥, 𝑦) = 𝑑(𝑥)
𝑑𝑥 𝑑𝑥 𝑑𝑥

or
2 𝑗 𝑘
𝑑𝑦 𝑑 𝑦 𝑑𝑦
𝑎 𝑥, 𝑦, + 𝑏(𝑥, 𝑦) + 𝑐(𝑥, 𝑦) = 𝑑(𝑥)
𝑑𝑥 𝑑𝑥 2 𝑑𝑥
▪ Using grid points (nodes) at the domain of the independent
variable:

▪ The dependent variable 𝑦(𝑥) at the grid point 𝑥𝑖 is denoted


as 𝑦𝑖 = 𝑦(𝑥𝑖 )
Recall that any function can be approximated with a Taylor series:
2

(∆𝑥) ′′
(∆𝑥) 𝑛
∆𝑥
𝑓 𝑥𝑖+1 = 𝑓 𝑥𝑖 + 𝑓 𝑥𝑖 + 𝑓 𝑥𝑖 + ⋯ + 𝑓 𝑥𝑖 +⋯
1! 2! 𝑛!
Rearranging the above expression results in:

𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
𝑓 𝑥𝑖 = + 𝑶 ∆𝑥
∆𝑥
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
𝑓 ′ 𝑥𝑖 ≈
∆𝑥

where 𝑶 ∆𝑥 represents the order of the first term in the truncated portion of
the series:
𝑛−1
∆𝑥 (∆𝑥)
𝑶 ∆𝑥 = 𝑓 ′′ 𝑥𝑖 + ⋯ + 𝑓 𝑛 𝑥𝑖 +⋯
2! 𝑛!
▪ Error of order ∆𝒙 𝑓(𝑥)
𝑑𝑓(𝑥𝑖 ) 1
= 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1 + 𝑶 ∆𝑥
𝑑𝑥 ∆𝑥
𝑑𝑓(𝑥𝑖 ) 1
≈ 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
𝑑𝑥 ∆𝑥

▪ Error of order (∆𝒙)𝟐


∆𝑥
𝑑𝑓(𝑥𝑖 ) 1
= 3𝑓 𝑥𝑖 − 4𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2 + 𝑶(∆𝑥)2
𝑑𝑥 2∆𝑥
𝑑𝑓(𝑥𝑖 ) 1 𝑥
≈ 3𝑓 𝑥𝑖 − 4𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2 𝑥𝑖−1 𝑥𝑖
𝑑𝑥 2∆𝑥
▪ Error of order(∆𝒙)𝟐
𝑑𝑓(𝑥𝑖 ) 1 𝟐
= 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 + 𝑶 ∆𝑥
𝑑𝑥 2∆𝑥
𝑑𝑓(𝑥𝑖 ) 1
≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
𝑑𝑥 2∆𝑥

▪ Error of order (∆𝒙)𝟒


𝑑𝑓(𝑥𝑖 ) 1
= −𝑓 𝑥𝑖+2 + 8𝑓 𝑥𝑖+1 − 8𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2 + 𝑶(∆𝑥)4
𝑑𝑥 12∆𝑥
𝑑𝑓(𝑥𝑖 ) 1
≈ −𝑓 𝑥𝑖+2 + 8𝑓 𝑥𝑖+1 − 8𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2
𝑑𝑥 12∆𝑥
▪ Error of order ∆𝒙
𝑓(𝑥)
𝑑𝑓(𝑥𝑖 ) 1
= 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 + 𝑶 ∆𝑥
𝑑𝑥 ∆𝑥
𝑑𝑓(𝑥𝑖 ) 1
≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
𝑑𝑥 ∆𝑥

▪ Error of order (∆𝒙)𝟐


𝑑𝑓(𝑥𝑖 ) 1 ∆𝑥
= −𝑓 𝑥𝑖+2 + 4𝑓 𝑥𝑖+1 − 3𝑓 𝑥𝑖 + 𝑶(∆𝑥)2
𝑑𝑥 2∆𝑥
𝑑𝑓(𝑥𝑖 ) 1 𝑥
≈ −𝑓 𝑥𝑖+2 + 4𝑓 𝑥𝑖+1 − 3𝑓 𝑥𝑖 𝑥𝑖 𝑥𝑖+1
𝑑𝑥 2∆𝑥
▪ Error of order ∆𝒙
𝑑 2 𝑓(𝑥𝑖 ) 1
2
= 2
𝑓 𝑥𝑖 − 2𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2 + 𝑶 ∆𝑥
𝑑𝑥 (∆𝑥 )
𝑑 2 𝑓(𝑥𝑖 ) 1
≈ 𝑓 𝑥𝑖 − 2𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2
𝑑𝑥 2 (∆𝑥)2

▪ Error of order (∆𝒙)𝟐


𝑑 2 𝑓(𝑥𝑖 ) 1 𝟐
= 2𝑓 𝑥𝑖 − 5𝑓 𝑥𝑖−1 + 4𝑓 𝑥𝑖−2 − 𝑓 𝑥𝑖−3 + 𝑶 ∆𝑥
𝑑𝑥 2 (∆𝑥 )2
𝑑 2 𝑓(𝑥𝑖 ) 1
2
≈ 2
2𝑓 𝑥𝑖 − 5𝑓 𝑥𝑖−1 + 4𝑓 𝑥𝑖−2 − 𝑓 𝑥𝑖−3
𝑑𝑥 (∆𝑥 )
▪ Error of order(∆𝒙)𝟐
𝑑 2 𝑓(𝑥𝑖 ) 1 𝟐
2
= 2
𝑓 𝑥𝑖+2 − 2𝑓 𝑥𝑖+1 + 𝑓 𝑥𝑖 + 𝑶 ∆𝑥
𝑑𝑥 (∆𝑥)
𝑑 2 𝑓(𝑥𝑖 ) 1
2
≈ 2
𝑓 𝑥𝑖+1 − 2𝑓 𝑥𝑖 + 𝑓 𝑥𝑖−1
𝑑𝑥 (∆𝑥)

▪ Error of order (∆𝒙)𝟒


𝑑 2 𝑓(𝑥𝑖 ) 1 𝟒
= −𝑓 𝑥𝑖+2 + 16𝑓 𝑥𝑖+1 − 30𝑓 𝑥𝑖 + 16𝑓 𝑥𝑖−1 − 𝑓 𝑥𝑖−2 + 𝑶 ∆𝑥
𝑑𝑥 2 12(∆𝑥)2
𝑑 2 𝑓(𝑥𝑖 ) 1
2
≈ 2
−𝑓 𝑥𝑖+2 + 16𝑓 𝑥𝑖+1 − 30𝑓 𝑥𝑖 + 16𝑓 𝑥𝑖−1 − 𝑓 𝑥𝑖−2
𝑑𝑥 12(∆𝑥)
▪ Error of order ∆𝒙
𝑑 2 𝑓(𝑥𝑖 ) 1
2
= 2
𝑓 𝑥𝑖+2 − 2𝑓 𝑥𝑖+1 + 𝑓 𝑥𝑖 + 𝑶 ∆𝑥
𝑑𝑥 (∆𝑥)
𝑑 2 𝑓(𝑥𝑖 ) 1
≈ 𝑓 𝑥𝑖+2 − 2𝑓 𝑥𝑖+1 + 𝑓 𝑥𝑖
𝑑𝑥 2 (∆𝑥)2

▪ Error of order (∆𝒙)𝟐


𝑑 2 𝑓(𝑥𝑖 ) 1 𝟐
= −𝑓 𝑥𝑖+3 + 4𝑓 𝑥𝑖+2 − 5𝑓 𝑥𝑖+1 + 2𝑓 𝑥𝑖 + 𝑶 ∆𝑥
𝑑𝑥 2 (∆𝑥)2
𝑑 2 𝑓(𝑥𝑖 ) 1
2
≈ 2
−𝑓 𝑥𝑖+3 + 4𝑓 𝑥𝑖+2 − 5𝑓 𝑥𝑖+1 + 2𝑓 𝑥𝑖
𝑑𝑥 (∆𝑥)
𝑥𝑛 − 𝑥1 𝑥𝑓 − 𝑥𝑖
∆𝑥 = =
𝑛−1 𝑛−1
FFD BFD
CFD

▪ There are 𝑛 − 2 finite difference equations and 2 boundary-condition

equations

▪ Solve the system of algebraic equations of 𝑛 equations and 𝑛 unknowns.


▪ Finite difference equations (Error of order (∆𝒙)𝟐 )

At the initial node, 𝑖 = 1 (FFD) → boundary condition At final nodes, 𝑖 = 𝑛 (BFD) → boundary condition

𝑑𝑦 1 𝑑𝑦 1
= −3𝑦1 + 4𝑦2 − 𝑦3 = 3𝑦𝑛 − 4𝑦𝑛−1 + 𝑦𝑛−2
𝑑𝑥 2∆𝑥 𝑑𝑥 2∆𝑥

At intermediate nodes, 𝑖 = 2 to 𝑛 − 1 (CFD)

𝑑2𝑦 1
= 𝑦 − 2𝑦𝑖 + 𝑦𝑖−1
𝑑𝑥 2 (∆𝑥)2 𝑖+1

𝑑𝑦 1
= 𝑦 − 𝑦𝑖−1
𝑑𝑥 2∆𝑥 𝑖+1
Compound A diffuses through a 4-cm pipe and reacts as it diffuses. The governing equation for
diffusion with reaction is:
𝑥=0 𝑥 = 4 𝑐𝑚

𝑑2𝐴 𝑘 = 5 × 10−6 s-1


𝐷 − 𝑘𝐴 = 0 𝐴 = 0.1 𝑀 𝐷 = 1.5 × 10−6 cm2/s 𝐴=0𝑀
𝑑𝑥 2

where 𝐴 is the concentration of A, 𝑥 is the distance in the pipe, 𝐷 is diffusivity of A in the medium,
and 𝑘 is the reaction rate constant. If the reactor is designed so that A is totally consumed exactly in
the end of the pipe, determine the concentration profile of A along the pipe! (inlet concentration of
A is 0.1 M, 𝑘 = 5 × 10−6 s-1, 𝐷 = 1.5 × 10−6 cm2/s). Use FD methods with 4, 10, 50, and 100 nodes!
𝒏=𝟒

𝑥𝑓 − 𝑥𝑖 4 − 0 4
∆𝑥 = = = = 1.333 ▪ At 𝒊 = 𝟑 (CFD)
𝑛−1 4−1 3
𝑑2 𝐴
𝐷 2 − 𝑘𝐴 = 0
▪ At the initial node, 𝒊 = 𝟏 → boundary condition 𝑑𝑥
𝑑2 𝐴
𝐴1 = 0.1 .... (1) 1.5 × 10 −6
− 5 × 10−6 𝐴 = 0
𝑑𝑥 2

▪ At 𝒊 = 𝟐 (CFD) 1
1.5 × 10−6 2
𝐴4 − 2𝐴3 + 𝐴2 − 5 × 10−6 𝐴2 = 0
𝑑2 𝐴 (1.333)
𝐷 2 − 𝑘𝐴 = 0
𝑑𝑥 0.84375𝐴2 − 6.6875𝐴3 + 0.84375𝐴4 = 0 .... (3)
−6
𝑑2 𝐴
1.5 × 10 2
− 5 × 10−6 𝐴 = 0
𝑑𝑥
1
1.5 × 10−6 𝐴3 − 2𝐴2 + 𝐴1 − 5 × 10−6 𝐴2 = 0 ▪ At the final node, 𝒊 = 𝒏 → boundary condition
(1.333) 2
𝐴4 = 0 .... (4)
0.84375𝐴1 − 6.6875𝐴2 + 0.84375𝐴3 = 0 .... (2)
𝐴1 = 0.1 .... (1)
0.84375𝐴1 − 6.6875𝐴2 + 0.84375𝐴3 =0 .... (2)
0.84375𝐴2 − 6.6875𝐴3 + 0.84375𝐴4 = 0 .... (3)
𝐴4 = 0 .... (4)

Solve the system of linear equations! Choose your methods: Gaussian / Gauss-Jordan elimination, Inverse,
Cramer’s rule, LU Decomposition, Jacobi, Gauss Seidel, etc.

1 0 0 0 𝐴1 0.1 𝐴1 0.1
0.84375 −6.6875 0.84375 0 𝐴2 0 𝐴2 0.01282
= =
0 0.84375 −6.6875 0.84375 𝐴3 0 𝐴3 0.00162
0 0 0 1 𝐴4 0 𝐴4 0
𝒏 = 𝟏𝟎
▪ At 𝒊 = 𝟐 to 𝒊 = 𝟖...
𝑥𝑓 − 𝑥𝑖 4−0 4
∆𝑥 = = = = 0.444 ▪ At 𝒊 = 𝒏 − 𝟏 = 𝟗 (CFD)
𝑛−1 10 − 1 9
𝑑2 𝐴
▪ At the initial node, 𝒊 = 𝟏 → boundary condition 𝐷 2 − 𝑘𝐴 = 0
𝑑𝑥
𝐴1 = 0.1 .... (1) −6
𝑑2 𝐴
1.5 × 10 2
− 5 × 10−6 𝐴 = 0
𝑑𝑥
▪ At 𝒊 = 𝟐 (CFD) 1
1.5 × 10−6 𝐴10 − 2𝐴9 + 𝐴8 − 5 × 10−6 𝐴2 = 0
𝑑2 𝐴 (0.444) 2
𝐷 2 − 𝑘𝐴 = 0
𝑑𝑥
7.59375𝐴8 − 20.188𝐴9 + 7.59375𝐴10 = 0.... (9)
−6
𝑑2 𝐴
1.5 × 10 2
− 5 × 10−6 𝐴 = 0
𝑑𝑥
1
1.5 × 10−6 2
𝐴3 − 2𝑦2 + 𝐴1 − 5 × 10−6 𝐴2 = 0 ▪ At the final node, 𝒊 = 𝒏 → boundary condition
(0.444)
𝐴10 = 0 .... (10)
7.59375𝐴1 − 20.188𝐴2 + 7.59375𝐴3 = 0 .... (2)
Solve the system of linear equations!

1 0 0 0 0 0 0 0 0 0 𝐴1 0.1
7.59375 −20.188 7.59375 0 0 0 0 0 0 0 𝐴2 0
0 7.59375 −20.188 7.59375 0 0 0 0 0 0 𝐴3 0
𝐴4 0
0 0 7.59375 −20.188 7.59375 0 0 0 0 0
𝐴5 0
0 0 0 7.59375 −20.188 7.59375 0 0 0 0
= 0
0 0 0 0 7.59375 −20.188 7.59375 0 0 0 𝐴6
0
0 0 0 0 0 7.59375 −20.188 7.59375 0 0 𝐴7 0
0 0 0 0 0 0 7.59375 −20.188 7.59375 0 𝐴8 0
0 0 0 0 0 0 0 7.59375 −20.188 7.59375 𝐴9 0
0 0 0 0 0 0 0 0 0 1 𝐴10 0
𝑛 = 20 𝑛 = 50 𝑛 = 100
Unfortunately, one cannot expect to always obtain linear Finite Difference
equations since non-linear equations are often encountered in many cases.
For example, ODE representing heat distribution in a rod is expressed as
follows:

𝑑2𝑇 −7 𝑇 + 273 4
− 1 × 10 + 4 150 − 𝑇 = 0
𝑑𝑥 2

with boundary conditions, say 𝑇 0 = 200 and 𝑇 0.5 = 100, this problem will
result in a set of non-linear equations for us to solve!
▪ At the initial node, 𝒊 = 𝟏 → boundary condition

𝑇1 = 200 ... (1)

▪ At 𝒊 = 𝟐

𝑑2𝑇 −7 𝑇 + 273 4 + 4 150 − 𝑇 = 0


− 1 × 10
𝑑𝑥 2
1 −7 𝑇 + 273 4 + 4 150 − 𝑇 = 0
𝑇 3 − 2𝑇2 + 𝑇1 − 1 × 10 2 2 ... (2)
(∆𝑥)2
▪ At 𝒊 = 𝒏 − 𝟏 (CFD)

𝑑2𝑇 −7 𝑇 + 273 4
− 1 × 10 + 4 150 − 𝑇 = 0
𝑑𝑥 2
1 −7 4
2
𝑇𝑛 − 2𝑇𝑛−1 + 𝑇𝑛−2 − 1 × 10 𝑇𝑛−1 + 273 + 4 150 − 𝑇𝑛−1 = 0 ... (𝑛 − 1)
(∆𝑥)

▪ At the final node, 𝒊 = 𝒏 → boundary condition

𝑇𝑛 = 100 ... (𝑛)


𝑇1 = 200 ... (1)

1
2
𝑇3 − 2𝑇2 + 𝑇1 − 1 × 10−7 𝑇2 + 273 4 + 4 150 − 𝑇2 = 0 ... (2)
(∆𝑥)

⋮ System of non-
⋮ linear equations !!

1 −7 4
𝑇𝑛 − 2𝑇𝑛−1 + 𝑇𝑛−2 − 1 × 10 𝑇𝑛−1 + 273 + 4 150 − 𝑇𝑛−1 = 0 ... (𝑛 − 1)
(∆𝑥)2

𝑇𝑛 = 100 ... (𝑛)


▪ The non-linear algebraic FD equations are solved using the
algorithm of Fixed-point Iteration, Newton-Raphson, or Secant for
systems of non-linear equations
▪ Yet, the algorithms require rigorous programming efforts, and
extreme cautions must be taken whilst picking initial guess
▪ In excel, these issues can be circumvented using combination of
solver and macros VBA
𝑇 = 200 oC

𝑇 = 100 oC

𝑑2𝑇
2
− 1 × 10−7 𝑇 + 273 4
+ 4 150 − 𝑇 = 0
𝑑𝑥

You might also like