Documente Academic
Documente Profesional
Documente Cultură
Numerical Differentiation
a d teg at o /
and Integration/
Numerical Differentiation
By
Dr Ali Jawarneh
Dr Ali Jawarneh 1
Ch.23:
Ch 23: Numerical Differentiation and
Integration/ Numerical Differentiation
Dr Ali Jawarneh 2
NUMERICAL DIFFERENTIATION
High-Accuracy Differentiation Formulas
Taylor series
It provides a mean to predict a function value at one point in terms of the
function value and it’s derivative at another point. Taylor series expansions
are used to derive finite-divided-difference approximations of derivatives.
True derivative
f (x)
h=xi+1-xi=xi-xi-1
forward
backward centered
h h
x
x i −1 xi x i +1
(old) (present) (new)
Dr Ali Jawarneh 3
Taylor ∞
f ( x) = ∑ am ( x − x 0 ) n 1 (n )
where a n = f ( x 0 )
Series n=0
n= n!
(x − x0 ) ( x − x 0 )2 ( x − x 0 )3
f ( x) = f ( x 0 ) + f ' (xo ) + f " (xo ) + f " ' ( x o ) + ...
1! 2! 3!
n: is order of the series
Forward Taylor
f "(xi )
f ( x i +1 ) = f ( x i ) + f ' ( x i )( x i +1 − x i ) + ( x i +1 − x i )2 +
2!
(n) h = x i +1 − x i
f "' (xi ) f (xi )
( x i +1 − x i ) 3 + ... + ( x i + 1 − x i )n
3! n!
Backward Taylor
f "(xi )
f ( x i −1 ) = f ( x i ) − f ' ( x i )( x i − x i −1 ) + ( x i − x i −1 ) 2 −
2!
(n )
h = x i − x i −1
f "' (xi ) f (xi )
( x i − x i −1 ) 3 + ... − ... + ( x i − x i −1 )n
3! n!
Dr Ali Jawarneh 4
Forward Taylor
f " (xi ) 2 f "' (xi ) 3 f (n ) (xi ) n
f ( x i +1 ) = f ( x i ) + f ' ( x i )h + h + h + ... + h (1)
2! 3! n!
Zero order ((n=0): ) f ( x i+ i + 1 ) = f ( x i ) + O(h ) O(h): Truncation
error of order h
1st order (n=1): f ( x i +1 ) = f ( x i ) + f ' ( x i )h + O(h 2 )
f " (xi ) 2
2nd order (n=2): f ( x i +1 ) = f ( x i ) + f ' ( x i )h + h + O( h 3 )
2!
Backward Taylor
f " (xi ) 2 f "' (xi ) 3 f (n ) (xi ) n
f ( x i −1 ) = f ( x i ) − f ' ( x i )h + h − h + ... − ... + h (2)
2! 3! n!
Zero order: f ( x i −1 ) = f ( x i ) + O(h )
1st order: f ( x i −1 ) = f ( x i ) − f ' ( x i )h + O(h 2 ) Dr Ali Jawarneh
f ( x i + 2 ) − 2f ( x i +1 ) + f ( x i ) Forward
F d , 2nd
2 d derivative
d i ti
f "(xi ) = 2
+ O(h ) of order h
h
Dr Ali Jawarneh 6
from (1) f " (xi ) 2 f "' (xi ) 3
f ( x i +1 ) = f ( x i ) + f ' ( x i )h + h + h + ...
2! 3!
Use also Forward , 2nd derivative of order h
f ( x i +1 ) − f ( x i ) f " ( x i ) h 2
f ' (xi ) = − + O( h 2 )
h 2! h
f ( x i +1 ) − f ( x i ) [f ( x i + 2 ) − 2f ( x i +1 ) + f ( x i )] h 2
= − 2
+ O ( h 2
)
h h 2! h
Collecting terms:
Dr Ali Jawarneh 7
Backward Approximation
From (2) f ( x i −1 ) = f ( x i ) − f ' ( x i )h +
f " (xi ) 2 f "' (xi ) 3
h − h + ... (5)
2! 3!
f ( x i ) − f ( x i −1 )
f ' (xi ) = + O(h ) Backward , 1stt derivative of order h
h
f " (xi )
Similar to (2)
f ( x i − 2 ) = f ( x i ) − f ' ( x i )( 2h ) + ( 2h ) 2 − ... (6)
2!
Multiplication Eq.(5) with 2, then subtract the result from (6), you will get:
f ( x i ) − 2f ( x i −1 ) + f ( x i − 2 ) b k
backwardd , 2nd
2 d derivative
d i ti off
f " (xi ) = = O ( h )
h2 order h
Dr Ali Jawarneh 8
from (2) f " (xi ) 2 f "' (xi ) 3
f ( x i −1 ) = f ( x i ) − f ' ( x i )h + h − h +
2! 3!
Use also Backward , 2nd derivative of order h
f ( x i ) − f ( x i −1 ) f ( x i ) − 2f ( x i −1 ) + f ( x i − 2 ) h 2 1
f ' (xi ) = +[ 2
]
h h 2! h
Collecting terms:
3f ( x i ) − 4f ( x i −1 ) + f ( x i − 2 )
f ' (xi ) = 2 + O( h 2 )
h
Dr Ali Jawarneh 9
Centered Approximation
f ( x i + 1 ) − f ( x i −1 )
f ' (xi ) = + O( h 2 )
2h
Dr Ali Jawarneh 10
Summary of Finite-Divided-Difference Formulas
(1) Forward Finite-Divided-Difference
Finite Divided Difference Formulas
f ( x i +1 ) − f ( x i )
1st derivative: f ' (xi ) = + O( h )
h
− f ( x i + 2 ) + 4f ( x i +1 ) − 3f ( x i )
f ' (xi ) = + O( h 2 ) More accurate
2h
2nd derivative:
f ( x i + 2 ) − 2f ( x i +1 ) + f ( x i )
f "(xi ) = 2
+ O( h )
h
− f ( x i + 3 ) + 4f ( x i + 2 ) − 5f ( x i +1 ) + 2f ( x i )
f "(xi ) = 2
+ O ( h 2
)
h
More accurate
Dr Ali Jawarneh 11
(2) Backward Finite-Divided-Difference
Finite Divided Difference Formulas
f ( x i ) − f ( x i −1 )
1st derivative: f ' (xi ) = + O( h )
h
3f ( x i ) − 4f ( x i −1 ) + f ( x i − 2 )
f ' (xi ) = + O( h 2 ) More accurate
2h
2nd derivative:
f ( x i ) − 2f ( x i −1 ) + f ( x i − 2 )
f "(xi ) = 2
+ O( h )
h
2f ( x i ) − 5f ( x i −1 ) + 4f ( x i − 2 ) − f ( x i − 3 )
f "(xi ) = 2
+ O ( h 2
)
h
More accurate
Dr Ali Jawarneh 12
(3) Centered Finite-Divided-Difference
Finite Divided Difference Formulas
f ( x i + 1 ) − f ( x i −1 )
1st derivative: f ' (xi ) = + O( h 2 )
2h
− f ( x i + 2 ) + 8f ( x i +1 ) − 8f ( x i −1 ) + f ( x i − 2 ) More accurate
f ' (xi ) = + O( h )
4
12h
2nd derivative:
f ( x i +1 ) − 2f ( x i ) + f ( x i −1 )
f "(xi ) = 2
+ O ( h 2
)
h
Dr Ali Jawarneh 14
Example: Find the first derivative of y= cos(x) at x=π/4
using a step size of h= π/12.
/12 Use forward and backward
approximations of O(h) & O(h2) , and central difference
approximation of O(h2) and O(h4). Also, estimate the true
l ti error εt for
percentt relative f each h approximation.
i ti
Solution f ( x)
Forward f ' ( x i ) = f ( x i +1 ) − f ( x i ) + O(h )
h
h
− f ( x i + 2 ) + 4f ( x i +1 ) − 3f ( x i )
f ' (xi ) = + O( h 2 )
2h x
Backward f ( x i ) − f ( x i −1 ) x i − 2 x i −1 xi x i +1 x i + 2
f ' (xi ) = + O( h )
h
f ' (xi ) =
3f ( x i ) − 4f ( x i −1 ) + f ( x i − 2 )
+ O( h 2 ) π π π π 5π
2h
12 6 4 3 12
Centered f ( x i + 1 ) − f ( x i −1 )
f ' (xi ) = + O( h 2 )
2h
− f ( x i + 2 ) + 8f ( x i +1 ) − 8f ( x i −1 ) + f ( x i − 2 ) Dr Ali Jawarneh
f ' (xi ) = + O( h 4 ) 15
12h
π π
x f(x) (radian) f ' ( ) |true = − sin( ) = −0.70710678
4 4
Xi‐2 π/12 0 965925826
0.965925826 (radian
f ( x i +1 ) − f ( x i )
Forward f ' ( x i ) = + O( h )
Xi‐1 π/6 0.866025404 h
Dr Ali Jawarneh 16
Centered
f ( x i + 1 ) − f ( x i −1 ) cos( π / 3) − cos( π / 6)
f ' (xi ) = + O( h 2 ) f ' ( π / 4) = = −0.69905703
2h 2π / 12
− f ( x i + 2 ) + 8f ( x i +1 ) − 8f ( x i −1 ) + f ( x i − 2 )
f ' (xi ) = + O( h 4 )
12h
First Order Second Order
Forward ‐0.79108963 ‐0.72601275
εt=11.877% εt=2.67%
Backward ‐0.60702442 ‐0.71974088
εt=14.154% εt=1.787%
Centered ‐0.69905703 ‐0.70699696
εt=1.138% εt=0.016%
Dr Ali Jawarneh 17
Example: The following data was collected for the
distance traveled versus time of a rocket
rocket. Use
numerical differentiation of order O(h2) to estimate the
rocket’s velocity (V) and acceleration (a) at each time
t, s 0 25 50 75 100 125
y, km 0 32 58 78 92 100
Solution − f ( x i + 2 ) + 4f ( x i +1 ) − 3f ( x i )
Forward f ' (xi ) = + O( h 2 ) Forward Backward
2h
Centered
− f ( x i + 3 ) + 4f ( x i + 2 ) − 5f ( x i +1 ) + 2f ( x i )
f " (xi ) = 2
+ O( h 2 )
h
3f ( x i ) − 4f ( x i −1 ) + f ( x i − 2 )
B k
Backward
d f ' (xi ) = + O( h 2 )
2h
2f ( x i ) − 5f ( x i −1 ) + 4f ( x i − 2 ) − f ( x i − 3 )
f " (xi ) = 2
+ O( h 2 )
h
Centered f ' ( x ) = f ( x i +1 ) − f ( x i −1 ) + O(h 2 ) f " ( x ) = f ( x i +1 ) − 2f ( x i ) + f ( x i −1 ) + O(h 2 )
i i 2
2h h
18
Dr Ali Jawarneh
Do not forget that our question is of order O(h2)
t=0 The only option is Forward, O(h) and O(h2)
− f ( x i + 2 ) + 4f ( x i +1 ) − 3f ( x i ) − 58 + 4( 32) − 3(0)
Forward f ' (xi ) = + O( h 2 ) f ' ( 0 ) = = 1.4 km / s
2h 2( 25)
t=25
2 You have
Y h many options:
(1) Forward, O(h) and O(h2)
ti
25 2
Dr Ali Jawarneh 20
t=100 You have many options:
(1) Forward, Only 1 derivative of O(h) stt
2f ( x i ) − 5f ( x i −1 ) + 4f ( x i − 2 ) − f ( x i − 3 )
f " (xi ) = 2
+ O( h 2 )
h
2(100) − 5(92) + 4(78) − (58)
f " (125) = 2
= −0.0096 km / s 2
25
Dr Ali Jawarneh 21
t y V a
0 0 1.4 ‐0.0096
0.0096
25 32 1.16 ‐0.0096
50 58 0 92
0.92 ‐0.0096
0 0096
75 78 0.68 ‐0.0096
100 92 0.44 ‐0.0096
125 100 0.20 ‐0.0096
0.0096
Dr Ali Jawarneh 22
Example: for the following available data:
(1) What is the least accurate value of f’(1
f’(1.399)
399)
(2)What is the value of f”(1.399)
(3)What is the best available estimate value of f’(1.400)
4 1 h1
D ≅ D(h 2 ) − D(h1 ) h2 =
3 3 2
For centered difference approximation with
O(h2), the application of this formula will yield
a new derivative estimate O(h ( 4)
Dr Ali Jawarneh 25
Three ways to improve derivative
estimates when employing finite
f
divided differences
Dr Ali Jawarneh 26
Example: Use Richardson extrapolation to estimate
th fi t d i ti
the first derivative of y= cos(x) at x=π/4 using step
f ( ) t /4 i t
sizes of π/3 and π/6. Employ centered differences of
O(h2) for the initial estimates.
) for the initial estimates
f (x ) − f (x )
Solution: f ' (x ) = i
2h
i +1
+ O( h )
i −1 2
D≅
4 1
D(h 2 ) − D(h1 ) h2 h2 h2 = π / 6
3 3
x i −1 xi x i +1
4 1
D≅ ( −0.67524) − ( −0.58477) = −0.70530 π / 12 π / 4 5π / 12
3 3
Differentiate:
2x − x i − x i + 1 2x − x i −1 − x i + 1 2x − x i −1 − x i
f ' ( x ) = f ( x i −1 ) + f (xi ) + f ( x i +1 )
( x i −1 − x i )( x i −1 − x i +1 ) ( x i − x i −1 )( x i − x i +1 ) ( x i +1 − x i −1 )( x i +1 − x i )
Dr Ali Jawarneh 28
Example: The heat flux at the oil-air interface can be
computed with Fourier law: dT
q |z = 0 = −kρc |z = 0
dz
Where: q
q= heat flux (W/m
( 2)
T(oC) 13.5 12 10
z 0 1.25 3.75
Dr Ali Jawarneh 29
Solution: x i −1 = 0
x i = 1.25
x i +1 = 3.75
x i −1 xi x i +1
2x − x i − x i + 1 2x − x i −1 − x i + 1 2x − x i −1 − x i
f ' ( x ) = f ( x i −1 ) + f (xi ) + f ( x i +1 )
( x i −1 − x i )( x i −1 − x i +1 ) ( x i − x i −1 )( x i − x i +1 ) ( x i +1 − x i −1 )( x i +1 − x i )
dT
q |z = 0 = − kρc |z = 0
dz
q |z = 0 = −( 3.5x10 −7 )(1800)(840)( −133.3333) = 70.56 W / m 2
Dr Ali Jawarneh 30