Lesson 3.2 - Numeric Differentiation
Lesson 3.2 - Numeric Differentiation
2:
Numeric Differentiation
• Explain the principle behind numeric differentiation
• Perform definite differentiation using numeric methods
Meaning of derivative
Calculus definition:
Line defining 𝐵
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥) 𝑓(𝑥 + ∆𝑥)
′
𝑓 𝑥 = lim Slope at A
∆𝑥→0 ∆𝑥
or 𝐴
𝑓(𝑥)
“𝑓 ′ 𝑥 is the slope of the tangent line at 𝑥.”
Numeric approach: ∆𝑥
𝑥 𝑥 + ∆𝑥
′
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥)
𝑓 𝑥 ≅ Line defining
∆𝑥
Slope A to B
where ∆𝑥 ≅ 0
This is called finite-divided-difference method.
Example
Determine
1
𝑑 1 𝑓 𝑥 =
1 − 𝑥2
𝑑𝑥 1− 𝑥2 𝑑𝑦
𝑑𝑥
at 𝑥 = 0.5
Analytic Solution:
𝑥
𝑓′ 𝑥 = 3
1− 2
𝑥 2
𝒇′ 𝟎. 𝟓 = 𝟎. 𝟕𝟔𝟗𝟖𝟎𝟎𝟑𝟓𝟖𝟗
Example
1 𝑑𝑦
𝑓 𝑥 =
Numeric Solution: 1 − 𝑥2 𝑑𝑥
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥) 𝐵 𝑓(0.500001)
′
𝑓 𝑥 ≅
∆𝑥
𝐴
Say we use ∆𝑥 = 1 × 10−6 : 𝑓(0.5)
Backward: 𝑓(𝑥)
𝐴
𝑓(𝑥 − ∆𝑥)
𝑓 𝑥 − 𝑓(𝑥 − ∆𝑥)
𝑓′ 𝑥 ≅ ∆𝑥 ∆𝑥
∆𝑥
𝑥 − ∆𝑥 𝑥 + ∆𝑥
Centered: 𝑥
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥 − ∆𝑥)
𝑓′ 𝑥 ≅ 2∆𝑥
2∆𝑥
Example
1
Centered: 𝑓 𝑥 =
1 − 𝑥2 𝑑𝑦
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥 − ∆𝑥) 𝑑𝑥
′
𝑓 𝑥 ≅ 𝐵
2∆𝑥 𝑓(0.5 + 0.000001)
′
1.154701308 … − 1.154699769 …
𝑓 0.5 ≅
0.000002 0.5 − 0.000001 0.5 + 0.000001
𝒇′ 𝟎. 𝟓 ≅ 𝟎. 𝟕𝟔𝟗𝟖𝟎𝟎𝟑𝟔 0.5
1
𝑓 𝑥 =
1 − 𝑥2
Analytic: 𝒇′ 𝟎. 𝟓 = 𝟎. 𝟕𝟔𝟗𝟖𝟎𝟎𝟑𝟓𝟖𝟗
Forward:
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥)
𝑓′ 𝑥 ≅
∆𝑥
Backward:
′
𝑓 𝑥 − 𝑓(𝑥 − ∆𝑥)
𝑓 𝑥 ≅
∆𝑥
Centered:
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥 − ∆𝑥)
′
𝑓 𝑥 ≅ Analytic solution:
2∆𝑥
𝒇′ 𝟎. 𝟐 = 𝟏. 𝟐𝟎𝟒𝟔𝟏𝟓𝟏𝟒𝟗 …
Second derivative 𝐵𝐶
𝑓 𝑥
“the rate of change of the first derivative” 𝐴𝐵
Forward FDM: 𝐵
𝑓(𝑥 + 2∆𝑥)
𝐴
𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥) 𝑓(𝑥 + ∆𝑥)
𝑓′ 𝑥 ≅ 𝑋
∆𝑥 𝑓(𝑥)
′
𝑓′ 𝐴 − 𝑓′(𝑋) ∆𝑥 ∆𝑥
𝑓′ 𝑥 ≅
∆𝑥
𝑥
𝑥 + ∆𝑥
𝑓 𝐴 − 𝑓(𝐵) 𝑓 𝐵 − 𝑓(𝑋)
−
𝑓 ′′ 𝑥 ≅ ∆𝑥 ∆𝑥 𝑥 + 2∆𝑥
∆𝑥
𝑓 𝑥 − 2𝑓 𝑥 + ∆𝑥 + 𝑓(𝑥 + 2∆𝑥)
𝑓 ′′ 𝑥 ≅
∆𝑥 2
Example 𝐵𝐶
Approximate 𝑓 𝑥
𝐴𝐵
𝑑2 1 𝐵
𝑓(𝑥 + 2∆𝑥)
𝑑𝑥 2 1− 𝑥2 𝑥=0.5
𝐴
𝑓(𝑥 + ∆𝑥)
𝑋
using Forward FDM: 𝑓(𝑥)
𝑓(𝑥 − 2∆𝑥)
′
𝑓 𝑥 − 2𝑓 𝑥 + ∆𝑥 + 𝑓(𝑥 + 2∆𝑥) ∆𝑥 ∆𝑥
𝑓′ 𝑥 ≅
∆𝑥 2
𝑥
𝑥 + ∆𝑥
Analytic Solution: 𝑥 + 2∆𝑥
2
1 + 2𝑥
𝑓 ′′ 𝑥 = 5
1 − 𝑥2 2
𝒇′′ 𝟎. 𝟓 = 𝟑. 𝟎𝟕𝟗𝟐𝟎𝟏𝟒𝟑𝟔 …
1
𝐵𝐶
Example 𝑓 𝑥 =
1−𝑥 2
𝐴𝐵
𝒇′ ′ 𝟎. 𝟓 ≅ 𝟑. 𝟎𝟔 Analytic Solution:
𝒇′′ 𝟎. 𝟓 = 𝟑. 𝟎𝟕𝟗𝟐𝟎𝟏𝟒𝟑𝟔 …
Second derivative 𝑓 𝑥
Centered FDM: 𝐷
𝑓(𝑥 + 2∆𝑥)
′
′
𝑓 (𝐶) − 𝑓′(𝐵) 𝐶
𝑓′ 𝑥 ≅
2∆𝑥 𝑋
𝐵 𝑓(𝑥)
where: 𝐴
𝑓(𝑥 − 2∆𝑥)
∆𝑥 ∆𝑥 ∆𝑥 ∆𝑥
𝑓 𝐷 − 𝑓(𝑋) 𝑓 𝑥 + 2∆𝑥 − 𝑓(𝑥)
𝑓′ 𝐶 = = 𝑥 − 2∆𝑥
2∆𝑥 2∆𝑥 𝐴𝑋 𝑥 + 2∆𝑥
′
𝑓 𝑥 + 2∆𝑥 − 2𝑓 𝑥 + 𝑓(𝑥 − 2∆𝑥)
𝑓′ 𝑥 ≅
4∆𝑥 2
Example
Determine 𝑓 𝑥
𝑑2 1 𝐷
𝑓(𝑥 + 2∆𝑥)
𝑑𝑥 2 1 − 𝑥2 𝑥=0.5 𝐶
Using Centered FDM: 𝑋
𝐵 𝑓(𝑥)
𝐴
𝑓 𝑥 + 2∆𝑥 − 2𝑓 𝑥 + 𝑓(𝑥 − 2∆𝑥) 𝑓(𝑥 − 2∆𝑥)
𝑓 ′′ 𝑥 ≅ ∆𝑥 ∆𝑥 ∆𝑥 ∆𝑥
4∆𝑥 2
𝐴𝑋 𝑥 − 2∆𝑥 𝑥 + 2∆𝑥
𝑥 − ∆𝑥 𝑥 + ∆𝑥
Analytic Solution: 𝑋𝐷
2 𝑥
1 + 2𝑥
𝑓 ′′ 𝑥 = 5
1 − 𝑥2 2
𝒇′′ 𝟎. 𝟓 = 𝟑. 𝟎𝟕𝟗𝟐𝟎𝟏𝟒𝟑𝟔 …
Example
Centered FDM: (say use ∆𝑥 = 1 × 10−6 )
′
𝑓 𝑥 + 2∆𝑥 − 2𝑓 𝑥 + 𝑓(𝑥 − 2∆𝑥)
𝑓′ 𝑥 ≅
4∆𝑥 2
𝑓 0.5 = 1.154700538 …
𝑑2 𝑥
(𝑒 cos(0.25𝑥))𝑥=0.2
𝑑𝑥 2
Forward:
𝑓 𝑥 − 2𝑓 𝑥 + ∆𝑥 + 𝑓(𝑥 + 2∆𝑥)
𝑓 ′′ 𝑥 ≅
∆𝑥 2
Centered:
Second Derivative:
3-point
𝑓 𝑥 + 2∆𝑥 − 2𝑓 𝑥 + 𝑓(𝑥 − 2∆𝑥)
′
𝑓′ 𝑥 ≅ Centered FDM
4∆𝑥 2
For derivations, see p. 653-655 of Chapra (Numerical Methods for Engineers – 6th Edition)
Example
1
𝑓 𝑥 =
1 − 𝑥2
1
𝑓 𝑥 =
Approximate: 1 − 𝑥2
𝑑𝑦
𝑑𝑥
a) 𝑓′(0.5) using 4-point centered FDM
𝑥
𝑓′ 𝑥 = 1 + 2𝑥 2
2
3 𝑓 ′′ 𝑥 = 5
1− 𝑥 2
1− 𝑥2 2
𝒇′ 𝟎. 𝟓 = 𝟎. 𝟕𝟔𝟗𝟖𝟎𝟎𝟑𝟓𝟖𝟗
𝒇′′(𝟎. 𝟓) = 𝟑. 𝟎𝟕𝟗𝟐𝟎𝟏𝟒𝟑𝟔