ME 261
Session: January 2023
Dr. Sumon Saha
Professor
Department of Mechanical Engineering
Bangladesh University of Engineering and Technology
ME 261 .. Numerical Analysis
Lecture # 02
Solution of Numerical Differentiation
ME 261 .. Numerical Differentiation
Problem #01:
a) Derive a backward difference formula for the first derivative with an
error term O(h3)
b) Use this formula to evaluate f′(x) at x = 2 for the following function
f(x) (consider step size h = 0.1):
f ( x ) = e−2x − x
c) Compute the relative error from the analytical derivative for the
numerical result obtained in part (b).
ME 261 .. Numerical Differentiation
a) Finite difference approximations of derivatives are derived
from the approximations of Taylor’s series expansion.
i−3 i−2 i−1 i i+1 i+2 i+3
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
• y = f(x), yi−1 = y(xi−1) = y(x−h) = f(xi−1) = f(x−h)
• Using Taylor’s series expansion of f(xi−1) = f(x−h),
f ( xi ) − O ( h5 )
h2 h3 h4 iv
f ( xi −1 ) f ( xi ) − hf ( xi ) + f ( xi ) − f ( xi ) + (1)
2! 3! 4!
• Similarly, using Taylor’s series expansion of f(xi−2) = f(x−2h),
f ( xi −2 ) f ( xi ) − ( 2h ) f ( xi ) +
( 2h )
2
f ( xi ) −
( 2h )
3
f ( xi ) + O ( h 4 ) (2)
2! 3!
ME 261 .. Numerical Differentiation
• Similarly, using Taylor’s series expansion of f(xi−3) = f(x−3h),
f ( xi −3 ) f ( xi ) − ( 3h ) f ( xi ) +
( 3h ) 2
f ( xi ) −
( 3h ) 3
f ( xi ) + O ( h 4 ) (3)
2! 3!
• From 4×(1) − (2)
4 f ( xi −1 ) − f ( xi −2 ) = 3 f ( xi ) − 2hf ( xi ) + 4 f ( xi ) + O ( h 4 )
h3
(4)
3!
• From 9×(1) − (3)
9 f ( xi −1 ) − f ( xi −3 ) = 8 f ( xi ) − 6hf ( xi ) + 18 f ( xi ) + O ( h 4 )
h3
(5)
3!
• From 9×(4) − 2× (5)
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 formula for f’(x)
ME 261 .. Numerical Differentiation
b) Now, f ( x ) = e−2x − x
Given, xi = x = 2, h = 0.1, then f ( 2 ) = e−2( 2) − 2 = −1.982
xi−1 = x – h = 2 – 0.1 = 1.9
i i−1 i−2 i−3
xi 2 1.9 1.8 1.7
f(xi) −1.982 −1.878 −1.773 −1.667
• Backward Difference formula for f’(x) with error O(h3)
11 f ( xi ) − 18 f ( xi −1 ) + 9 f ( xi −2 ) − 2 f ( xi −3 )
f ( xi ) =
6h
11 f ( 2 ) − 18 f (1.9 ) + 9 f (1.8) − 2 f (1.7 )
f ( 2) =
6 0.1
11( −1.982 ) − 18 ( −1.878) + 9 ( −1.773) − 2 ( −1.667 )
=
0.6
( 2 ) = −1.035
f num Note: use 3 significant digits after decimals
if number has less than 4 significant digits.
ME 261 .. Numerical Differentiation
c) Now, f ( x ) = e−2 x − x
f ( x ) = −2e−2 x − 1
Analytical (exact) result, exact ( )
−2( 2)
f 2 = −2 e − 1 = −1.037
• Relative error
( x ) − f num
fexact . ( x)
Error , E = 100%
( x)
fexact
( 2 ) − f num
fexact . ( 2)
= 100%
( 2)
fexact
−1.037 + 1.035
= 100%
−1.037
= 0.193%
Note: Memorize all formulae up to O(h2) for FDS and BDS, and O(h4)
for CDS.
ME 261 .. Numerical Differentiation
Problem #02: Find f ′(x) and f ″(x) of the function f(x):
f ( x ) = −0.1x 4 − 0.158x3 − 0.5x 2 − 0.25x + 12.ABC
at x = 0.5 with a step size of h = 0.25 using
a) Forward finite-divided-difference formula with an error of O(h2),
b) Backward finite-divided-difference formula with an error of O(h2),
c) Centered finite-divided-difference formula with an error of O(h4).
Find the accuracy of your results. ABC is the last three digit of your
student number. For example, if you student number is 1610123, then
ABC is 123 and the corresponding function will be
f ( x ) = −0.1x 4 − 0.158x3 − 0.5x 2 − 0.25x + 12.123
ME 261 .. Numerical Differentiation
Now, f ( x ) = −0.1x 4 − 0.158x3 − 0.5x 2 − 0.25 x + 12.ABC
f ( x ) = −0.4 x3 − 0.158 3x 2 − 0.5 2 x − 0.25
= −0.4 x3 − 0.474 x 2 − x − 0.25
f ( x ) = −0.4 3x 2 − 0.474 2 x − 1
= −1.2 x 2 − 0.948 x − 1
Analytical (exact) result,
( 0.5) = −0.4 ( 0.5) − 0.474 ( 0.5) − ( 0.5) − 0.25 = −0.919
3 2
fexact
( 0.5) = −1.2 ( 0.5) − 0.948 ( 0.5) − 1 = −1.774
2
fexact
Given, xi = x = 0.5, h = 0.25, then xi−1 = x – h = 0.5 – 0.25 = 0.25
xi+1 = x + h = 0.5 + 0.25 = 0.75
Let, ABC = 000 f ( 0.5) = −0.1( 0.5) − 0.158 ( 0.5) − 0.5 ( 0.5) − 0.25 ( 0.5) + 12.000 = 11.724
4 3 2
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(a) Forward finite-divided-difference formula with an error of O(h2)
−3 f ( xi ) + 4 f ( xi +1 ) − f ( xi +2 ) Relative error
f ( xi ) =
2h ( x ) − f num
fexact ( x)
−3 f ( 0.5) + 4 f ( 0.75) − f (1) E=
( x)
100%
f ( 0.5) =
fexact
2 ( 0.25)
( 0.5) − f num
fexact ( 0.5)
−3 (11.724 ) + 4 (11.433) − 10.992 = 100%
= ( 0.5)
fexact
0.5
( 0.5) = −0.864 −0.919 + 0.864
f num = 100%
−0.919
= 5.985%
Accuracy of the result is (100−5.985) = 94.015%
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(a) Forward finite-divided-difference formula with an error of O(h2)
2 f ( xi ) − 5 f ( xi +1 ) + 4 f ( xi +2 ) − f ( xi +3 )
f ( xi ) =
Relative error
h2 ( 0.5) − f num
fexact ( 0.5)
E= 100%
2 f ( 0.5) − 5 f ( 0.75) + 4 f (1) − f (1.25) ( 0.5)
fexact
f ( 0.5) =
( 0.25) 2
=
−1.774 + 1.648
100%
−1.774
2 (11.724 ) − 5 (11.433) + 4 (10.992 ) − 10.354 = 7.103%
=
( 0.25) 2
( 0.5) = −1.648
f num
Accuracy of the result is (100−7.103) = 92.897%
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(b) Backward finite-divided-difference formula with an error of O(h2)
3 f ( xi ) − 4 f ( xi −1 ) + f ( xi −2 ) Relative error
f ( xi ) =
2h ( 0.5) − f num
fexact ( 0.5)
3 f ( 0.5) − 4 f ( 0.25) + f ( 0 ) E= 100%
f ( 0.5) =
( 0.5)
fexact
2 ( 0.25)
−0.919 + 0.88
−3 (11.724 ) + 4 (11.903) − 12.000 = 100%
= −0.919
0.5 = 4.244%
( 0.5) = −0.88
f num
Accuracy of the result is (100−4.244) = 95.756%
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(b) Backward finite-divided-difference formula with an error of O(h2)
2 f ( xi ) − 5 f ( xi −1 ) + 4 f ( xi −2 ) − f ( xi −3 )
f ( xi ) =
Relative error
h2 ( 0.5) − f num
fexact ( 0.5)
E= 100%
2 f ( 0.5) − 5 f ( 0.25) + 4 f ( 0 ) − f ( −0.25) ( 0.5)
fexact
f ( 0.5) =
( 0.25) 2
=
−1.774 + 1.6
100%
−1.774
2 (11.724 ) − 5 (11.903) + 4 (12.000 ) − 12.033 = 9.808%
=
( 0.25) 2
( 0.5) = −1.6
f num
Accuracy of the result is (100−9.808) = 90.192%
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(c) Centered finite-divided-difference formula with an error of O(h4)
f (x ) −8 f (x ) +8 f (x ) − f (x ) Relative error
f ( xi ) = i −2 i −1 i +1 i+2
12h ( 0.5) − f num
fexact ( 0.5)
E= 100%
f ( 0 ) − 8 f ( 0.25) + 8 f ( 0.75) − f (1) fexact ( 0.5)
f ( 0.5) =
12 ( 0.25) −0.919 + 0.917
= 100%
12.000 − 8 (11.903) + 8 (11.433) − 10.992 −0.919
= = 0.218%
3
( 0.5) = −0.917
f num
Accuracy of the result is (100−0.218) = 99.782%
ME 261 .. Numerical Differentiation
i−3 i−2 i−1 i i+1 i+2 i+3
xi −0.25 0 0.25 0.5 0.75 1.0 1.25
f(xi) 12.033 12.000 11.903 11.724 11.433 10.992 10.354
(c) Centered finite-divided-difference formula with an error of O(h4)
− f ( xi −2 ) + 16 f ( xi −1 ) − 30 f ( xi ) + 16 f ( xi +1 ) − f ( xi + 2 )
f ( xi ) =
12h2
− f ( 0 ) + 16 f ( 0.25) − 30 f ( 0.5) + 16 f ( 0.75) − f (1)
f ( 0.5) =
12 ( 0.25)
2
−12.000 + 16 (11.903) − 30 (11.724 ) + 16 (11.433) −10.992
=
0.75
( 0.5) = −1.781
f num
( 0.5) − f num
fexact ( 0.5) −1.774 + 1.781
Relative error E= 100% = 100% = 0.395%
fexact ( 0.5)
−1.774
Accuracy of the result is (100−0.395) = 99.605%
ME 261 .. Numerical Differentiation
Problem #03: The electrical voltage drops across an inductor, according
to Faraday's law, is given by: di
VL = L
dt
where VL = voltage drop (V), L = inductance (Henry), i = current (A), t =
time (sec).
i 0 0.15 0.3 0.55 0.8 1.9
t 0 0.1 0.2 0.3 0.5 0.7
From the above data set, determine the voltage drop at the following time
steps for an inductance of 4 Henry: (a) t = 0, (b) t = 0.15, (c) t = 0.4, (d) t
= 0.7. All the results should conform to the accuracy level of O(h2).
ME 261 .. Numerical Differentiation
(a) t = 0, h = ti+1 – ti = 0.1 − 0 = 0.1, L = 4 Henry
i 0 0.15 0.3 0.55 0.8 1.9
t 0 0.1 0.2 0.3 0.5 0.7
di
Voltage drop, VL ( t = 0 ) = L
dt ti =0
−3i ( ti ) + 4i ( ti +1 ) − i ( ti +2 ) Using FDS with
=L error of O(h2)
2h
−3i ( 0 ) + 4i ( 0.1) − i ( 0.2 )
=4
2 0.1
−3 ( 0 ) + 4 ( 0.15) − ( 0.3)
=4 = 6V
0.2
ME 261 .. Numerical Differentiation
(b) t = 0.15, h = ti+1 – ti = 0.2 − 0.15 = 0.05, L = 4 Henry
i 0 0.15 0.3 0.55 0.8 1.9
t 0 0.1 0.2 0.3 0.5 0.7
di
Voltage drop, VL ( t = 0.15) = L
dt ti =0.15
i ( ti +1 ) − i ( ti −1 ) Using CDS with
=L
2h error of O(h2)
i ( 0.2 ) − i ( 0.1)
=4
2 0.05
0.3 − 0.15
=4 = 6V
0.1
ME 261 .. Numerical Differentiation
(c) t = 0.4, h = ti+1 – ti = 0.5 − 0.4 = 0.1, L = 4 Henry
i 0 0.15 0.3 0.55 0.8 1.9
t 0 0.1 0.2 0.3 0.5 0.7
di
Voltage drop, VL ( t = 0.4 ) = L
dt ti =0.4
i ( ti +1 ) − i ( ti −1 ) Using CDS with
=L
2h error of O(h2)
i ( 0.5) − i ( 0.3)
=4
2 0.1
0.8 − 0.55
=4 = 5V
0.2
ME 261 .. Numerical Differentiation
(d) t = 0.7, h = ti – ti−1 = 0.7 − 0.5 = 0.2, L = 4 Henry
i 0 0.15 0.3 0.55 0.8 1.9
t 0 0.1 0.2 0.3 0.5 0.7
di
Voltage drop, VL ( t = 0.7 ) = L
dt ti =0.7
3i ( ti ) − 4i ( ti −1 ) + i ( ti −2 ) Using BDS with
=L
2h error of O(h2)
3i ( 0.7 ) − 4i ( 0.5) + i ( 0.3)
=4
2 0.2
3 (1.9 ) − 4 ( 0.8) + 0.55
=4 = 30.5V
0.4
ME 261 .. Numerical Differentiation
Problem #04: When a fluid flows over a surface, the shear stress τ
(N/m2) at the surface is given by the following expression:
du
=
dy surface
where viscosity, μ = 0.00024 Pa.s, u is the velocity parallel to the surface
(m/s), and y is the distance normal to the surface (m). Measurements of
the velocity of an air stream flowing above the surface are given in the
following table
y (m) 0.0 0.01 0.02 0.03 0.04
u (m/s) 0.00 45.56 70.16 90.38 100.00
Calculate the shear at the surface (y = 0) based on first-, second-, and
third-order accurate approximations.
ME 261 .. Numerical Differentiation
(a) y = 0, h = yi+1 – yi = 0.01 − 0.0 = 0.01, μ = 0.00024 Pa.s
y (m) 0.0 0.01 0.02 0.03 0.04
u (m/s) 0.00 45.56 70.16 90.38 100.00
du du
Shear stress, ( y = 0 ) = =
dy surface dy y =0
u ( yi +1 ) − u ( yi ) Using FDS first order
=
h approximation
u ( 0.01) − u ( 0.0 )
= 0.00024
0.01
45.56 − 0.00
= 0.00024 = 1.09344N/m2
0.01
ME 261 .. Numerical Differentiation
(b) y = 0, h = yi+1 – yi = 0.01 − 0.0 = 0.01, μ = 0.00024 Pa.s
y (m) 0.0 0.01 0.02 0.03 0.04
u (m/s) 0.00 45.56 70.16 90.38 100.00
Shear stress,
du du
( y = 0) = =
dy surface dy y =0
−3u ( yi ) + 4u ( yi +1 ) − u ( yi +2 ) Using FDS second
=
2h order approximation
−3u ( 0.0 ) + 4u ( 0.01) − u ( 0.02 )
= 0.00024
2 0.01
−3 ( 0.00 ) + 4 ( 45.56 ) − 70.16
= 0.00024 = 1.34496N/m2
0.02
ME 261 .. Numerical Differentiation
(c) y = 0, h = yi+1 – yi = 0.01 − 0.0 = 0.01, μ = 0.00024 Pa.s
y (m) 0.0 0.01 0.02 0.03 0.04
u (m/s) 0.00 45.56 70.16 90.38 100.00
Shear stress,
du du
( y = 0) = =
dy surface dy y =0
−11u ( yi ) + 18u ( yi +1 ) − 9u ( yi +2 ) + 2u ( yi +3 ) Using FDS third
=
6h order approximation
−11u ( 0.0 ) + 18u ( 0.01) − 9u ( 0.02 ) + 2u ( 0.03)
= 0.00024
6 0.01
−11( 0.00 ) + 18 ( 45.56 ) − 9 ( 70.16 ) + 2 (90.38)
= 0.00024 = 1.4776N/m2
0.06
ME 261 .. Numerical Differentiation
Problem #05: The displacement of an instrument subjected to a random
vibration test, at different instants of time, is found to be as follows:
Station, i Time, ti (s) Displacement, yi (inch)
1 0.05 0.144
2 0.10 0.172
3 0.15 0.213
4 0.20 0.296
5 0.25 0.070
6 0.30 0.085
7 0.35 0.525
8 0.40 0.110
Determine the velocity, dy/dt and acceleration, d2y/dt2 at t = 0.05, 0.10,
0.15, 0.20, 0.25, 0.30, 0.35, and 0.40 using suitable finite difference
formulae.
ME 261 .. Numerical Differentiation
h = ti+1 – ti = 0.10 − 0.05 = 0.05 Using FDS with error of O(h2)
dy −3 y ( ti ) + 4 y ( ti +1 ) − y ( ti +2 )
Velocity, =
dt t =0.05 2h
−3 y ( 0.05) + 4 y ( 0.10 ) − y ( 0.15)
=
2 0.05
−3 ( 0.144 ) + 4 ( 0.172 ) − 0.213
= = 0.43inch/s
0.1
d2y 2 y ( ti ) − 5 y ( ti +1 ) + 4 y ( ti +2 ) − y ( ti +3 )
Acceleration, 2 =
dt t =0.05 h2
2 y ( 0.05) − 5 y ( 0.10 ) + 4 y ( 0.15) − y ( 0.20 )
=
( 0.05)
2
2 ( 0.144 ) − 5 ( 0.172 ) + 4 ( 0.213) − 0.296
= = −6.4inch/s2
( 0.05)
2
ME 261 .. Numerical Differentiation
h = ti – ti−1 = 0.40 − 0.35 = 0.05 Using BDS with error of O(h2)
dy 3 y ( ti ) − 4 y ( ti −1 ) + y ( ti −2 )
Velocity, =
dt t =0.40 2h
3 y ( 0.40 ) − 4 y ( 0.35) + y ( 0.30 )
=
2 0.05
3 ( 0.110 ) − 4 ( 0.525) + 0.085
= = −16.85inch/s
0.1
d2y 2 y ( ti ) − 5 y ( ti −1 ) + 4 y ( ti −2 ) − y ( ti −3 )
Acceleration, 2 =
dt t =0.40 h2
2 y ( 0.40 ) − 5 y ( 0.35) + 4 y ( 0.30 ) − y ( 0.25)
=
( 0.05)
2
2 ( 0.110 ) − 5 ( 0.525) + 4 ( 0.085) − 0.070
= = −854inch/s2
( 0.05)
2
ME 261 .. Numerical Differentiation
h = ti+1 – ti = 0.25 − 0.20 = 0.05 Using CDS with error of O(h2)
dy y ( ti +1 ) − y ( ti −1 )
Velocity, =
dt t =0.20 2h
y ( 0.25) − y ( 0.15)
=
2 0.05
0.070 − 0.213
= = −1.43inch/s
0.1
d2y y ( ti −1 ) − 2 y ( ti ) + y ( ti +1 )
Acceleration, 2
=
dt t =0.20 h2
y ( 0.15) − 2 y ( 0.20 ) + y ( 0.25)
=
( 0.05)
2
0.213 − 2 ( 0.296 ) + 0.070
= = −123.6inch/s2
( 0.05)
2
ME 261 .. Numerical Differentiation
Calculated velocity, dy/dt and acceleration, d2y/dt2 using FDM of O(h2)
Displacement, Velocity, Acceleration,
Station, i Time, ti (s)
yi (inch) dy/dt (inch/s) d2y/dt2 (inch/s2)
1 0.05 0.144 0.43 −6.4
2 0.10 0.172 0.69 5.2
3 0.15 0.213 1.24 16.8
4 0.20 0.296 −1.43 −123.6
5 0.25 0.070 −2.11 96.4
6 0.30 0.085 4.55 170
7 0.35 0.525 0.25 −342
8 0.40 0.110 −16.85 −854
ME 261 .. Numerical Differentiation
Homework / Practice
Use three-point central difference formula with h = 0.1 to
approximate the second derivative of
1
f ( x) = at x = 2
x
Also use Richardson’s Extrapolation Scheme to approximate the
same derivative with O(h4) at x = 2.