Numerical Differentiation
Numerical Differentiation
Priom Das
Lecturer
Department of Mechanical Engineering, BUET
Lecture # 01
Numerical Differentiation
ME 261 .. Introduction to Differentiation
Definition: Example:
f f ( x + x ) − f ( x )
= lim
x x → 0 x
f ( x + h) − f ( x)
= lim
h →0 h
y
= lim
x → 0 x
ME 261 .. Application of Differentiation
ME 261 .. Application of Differentiation
ME 261 .. Application of Differentiation
ME 261 .. Application of Differentiation
ME 261 .. Differentiation vs Integration
ME 261 .. Application of Numerical Differentiation
f ( xi + 2 ) f ( xi ) + ( 2h ) f ( xi ) + f ( xi ) + f ( xi ) + O ( h 4 ) (3)
2! 3!
• Using Taylor’s series expansion of f(xi−2) = f(x−2h),
( 2h ) ( 2h )
2 3
f ( xi − 2 ) f ( xi ) − ( 2h ) f ( xi ) + f ( xi ) − f ( xi ) + O ( h 4 ) (4)
2! 3!
ME 261 .. Numerical Differentiation
f ( xi +3 ) f ( xi ) + ( 3h ) f ( xi ) + f ( xi ) + f ( xi ) + O ( h 4 ) (5)
2! 3!
• Using Taylor’s series expansion of f(xi−3) = f(x−3h),
( 3h ) ( 3h )
2 3
f ( xi −3 ) f ( xi ) − ( 3h ) f ( xi ) + f ( xi ) − f ( xi ) + O ( h 4 ) (6)
2! 3!
ME 261 .. Numerical Differentiation
h2 h3 h 4 iv
f ( xi +1 ) f ( xi ) + hf ( xi ) + f ( xi ) + f ( xi ) + f ( xi ) + O ( h 5 ) (1)
2! 3! 4!
h2 h3 h 4 iv
f ( xi −1 ) f ( xi ) − hf ( xi ) + f ( xi ) − f ( xi ) + f ( xi ) − O ( h 5 ) (2)
2! 3! 4!
( 2h ) ( 2h ) ( 2h )
2 3 4
f ( xi + 2 ) f ( xi ) + ( 2h ) f ( xi ) + f ( xi ) + f ( xi ) + f iv ( xi ) + O ( h 5 ) (3)
2! 3! 4!
( 2h ) ( 2h ) ( 2h )
2 3 4
f ( xi − 2 ) f ( xi ) − ( 2h ) f ( xi ) + f ( xi ) − f ( xi ) + f iv ( xi ) − O ( h 5 ) (4)
2! 3! 4!
( 3h ) ( 3h ) ( 3h )
2 3 4
f ( xi +3 ) f ( xi ) + ( 3h ) f ( xi ) + f ( xi ) + f ( xi ) + f iv ( xi ) + O ( h 5 ) (5)
2! 3! 4!
( 3h ) ( 3h ) ( 3h )
2 3 4
f ( xi −3 ) f ( xi ) − ( 3h ) f ( xi ) + f ( xi ) − f ( xi ) + f iv ( xi ) − O ( h 5 ) (6)
2! 3! 4!
ME 261 .. Numerical Differentiation
Techniques:
f ( xi +1 ) − f ( xi )
Thus, f ( xi ) = + O (h) Error
h
f ( xi ) − f ( xi −1 )
Thus, f ( xi ) = + O (h) Error
h
f ( xi +1 ) − f ( xi −1 )
Thus, f ( xi ) = + O ( h2 ) Error
2h
Thus, −11 f ( xi ) + 18 f ( xi +1 ) − 9 f ( xi + 2 ) + 2 f ( xi +3 )
f ( xi ) = + O ( h3 ) Error
6h
This is called Forward Difference Scheme (FDS) / Approximation for f’(x)
[4 points formula]
ME 261 .. Numerical Differentiation
(First Derivative)
i−3 i−2 i−1 i i+1 i+2 i+3
x
xi−1 f’(xi) xi xi+1
h
• From 9×(2) − (6)
h3
9 f ( xi −1 ) − f ( xi −3 ) = 8 f ( xi ) − 6hf ( xi ) + 18 f ( xi ) + O ( h 4 ) (11)
3!
• From 9×(9) − 2× (11)
18 f ( xi −1 ) − 9 f ( xi − 2 ) + 2 f ( xi −3 ) = 11 f ( xi ) − 6hf ( xi ) + O ( h 4 )
Thus, 11 f ( xi ) − 18 f ( xi −1 ) + 9 f ( xi − 2 ) − 2 f ( xi −3 )
f ( xi ) = + O ( h3 ) Error
6h
This is called Backward Difference Scheme (BDS) / Approximation for f’(x)
[4 points formula]
ME 261 .. Numerical Differentiation
(First Derivative)
i−3 i−2 i−1 i i+1 i+2 i+3
x
xi−1 f’(xi) xi xi+1
h h3
• From (3) − (4) f ( xi+2 ) − f ( xi −2 ) = 4 hf ( xi ) + 16 f ( xi ) + O (
3!
h 5
) (12)
• From (12) − 8 ×(7)
f ( xi + 2 ) − f ( xi − 2 ) − 8 f ( xi +1 ) + 8 f ( xi −1 ) = −12hf ( xi ) + O ( h 5 )
f ( xi − 2 ) − 8 f ( xi −1 ) + 8 f ( xi +1 ) − f ( xi + 2 )
Thus, f ( xi ) = + O ( h4 ) Error
12h
Important Note
1. Minimum number of terms required for nth derivative = n + 1.
2. In all the finite difference formulas, the sum of all the
coefficients of the function values, f(xi) in the numerator can be
observed to be zero. Physically this implies that the derivatives
becomes zero if f(x) is a constant.
Numerical Differentiation
ME 261 ..
(Third Derivative)
Question 1: Derive the finite difference formula of third derivative
with an error of O(h2) using central finite difference approximation.
i−2 i−1 i i+1 i+2
x
xi−1 f’(xi) xi xi+1 Independent variable: xi = x0 + ih,
Dependent variable: fi = f(xi) = f(x0 + ih),
h i = 0, 1, 2, ….., n
dx
d 2 f ( xi −1 ) − 2 f ( xi ) + f ( xi +1 )
= 2
dx h2
1
= f ( xi −1 ) − 2 f ( xi ) + f ( xi +1 )
h2
1 f ( xi − 2 ) − 2 f ( xi −1 ) + f ( xi ) f ( xi −1 ) − 2 f ( xi ) + f ( xi +1 ) f ( xi ) − 2 f ( xi +1 ) + f ( xi + 2 )
= 2 − 2 +
h h2 h 2
h 2
1
= 4 f ( xi − 2 ) − 2 f ( xi −1 ) + f ( xi ) − 2 f ( xi −1 ) + 4 f ( xi ) − 2 f ( xi +1 ) + f ( xi ) − 2 f ( xi +1 ) + f ( xi + 2 )
h
f ( xi − 2 ) − 4 f ( xi −1 ) + 6 f ( xi ) − 4 f ( xi +1 ) + f ( xi + 2 )
f iv ( xi ) = 4
+ O ( h2 ) Error
h
ME 261 .. Numerical Differentiation
Sample Questions
1. Derive the finite difference formula of first derivative with an
error of O(h4) using central finite difference approximation.
2. Derive the finite difference formula of first derivative with an
error of O(h2) using forward finite difference approximation.
3. Derive the finite difference formula of first derivative with an
error of O(h3) using forward finite difference approximation.
4. Derive the backward finite difference formula of first derivative
with an error of O(h3).
5. Derive the finite difference formula of second derivative with an
error of O(h4) using central finite difference approximation.
6. Derive a three-point central difference formula for second
derivative.
ME 261 .. Numerical Differentiation
−1
h2
ME 261 .. Numerical Differentiation
dX 2 dX
With reference to Taylor Series expansions, derive the equivalent
central-difference approximations to the ODE for the following two
different levels of truncation error.
a) Order of error of ΔX2 (O(h2))
b) Order of error of ΔX4 (O(h4))
dY Y ( X i +1 ) − Y ( X i −1 )
Thus, = + O ( h2 ) Error (3)
dX 2h
d 2Y Y ( X i −1 ) − 2Y ( X i ) + Y ( X i +1 )
Thus,
dX 2
=
h2
+ O ( h 2
) Error (4)
dX 2 dX
• Using (3) and (4)
Y ( X i −1 ) − 2Y ( X i ) + Y ( X i +1 ) Y ( X i +1 ) − Y ( X i −1 )
10 2 + 4 + 6Y ( X i ) = 2 X i
2
h 2 h
10 Y ( X i −1 ) − 2Y ( X i ) + Y ( X i +1 ) + 2h Y ( X i +1 ) − Y ( X i −1 ) + 6h 2Y ( X i ) = 2 X i2 h 2
10Y ( X i −1 ) − 20Y ( X i ) + 10Y ( X i +1 ) + 2hY ( X i +1 ) − 2hY ( X i −1 ) + 6h 2Y ( X i ) = 2 X i2 h 2
( 2h + 10 ) Y ( X i +1 ) + ( 6h 2 − 20 ) Y ( X i ) + (10 − 2h ) Y ( X i −1 ) = 2 X i2 h 2
Solution
3
( r + r h ) ( r + 2h ) − 4r + 2r h + rh − 2 ( r + h ) + ( 6r 3 + 2rh 2 ) ( r )
3 2 3 2 2 h
3 h3
− 4r − 2r h + rh + ( r − h ) + ( r 3 − r 2 h ) ( r − 2h ) = 0
2 2
2