Chapter 3 Numerical Differentiation
Chapter 3 Numerical Differentiation
1. Theory summary
1.1. Numerical Differentiation
Finite-divided-difference approximations of derivatives are derived from Taylor
series expansions:
f ' ' (x i) 2
f (x i+1 )=f (xi )+f ' (x i). h+ . h + .. .
2
High-accuracy divided-difference formulas can be generated by including
additional terms from the Taylor series expansion. For examples, for first order
derivatives:
Forward finite-divided-difference formulas:
f ( x i+1 )−f (xi )
f ' (x i)=
h
−f (x i+2 )+ 4 f (x i+1 )−3 f ( xi )
f ' (x i)=
2h
Centered finite-divided-difference formulas:
f ( x i+1 )−f (xi −1 )
f ' (x i)=
2h
−f (x i+2 )+ 8 f (x i +1)−8 f (x i−1)+ f (x i−2)
f ' (x i)=
12 h
Backward finite-divided-difference formulas:
f ( x i)−f (x i−1)
f ' (x i)=
h
3 f (x i)−4 f (x i−1)+f ( x i−2 )
f ' (x i)=
2h
Higher order derivatives can also be derived. For instance, for second order:
Forward finite-divided-difference formulas:
f (x i+2 )−2 f ( x i +1)+ f (x i)
f ' ' (x i)= 2
h
Then : y i+ 1= y i+ f ( x i , y i ) . h
1.2.2. Heun’s method
One method to improve the estimate of the slope involves the determination of
two derivatives for the interval—one at the initial point and another at the end point. The
two derivatives are then averaged to obtain an improved estimate of the slope for the
entire interval. This approach, called Heun’s
method.
The slope at the beginning of an interval is
similar to Euler’s method:
'
y i=f ( x i , y i )
y i+ 1= y i+ .h
2
k 2=f ( x i + p1 . h , y i +q11 . k 1 . h )
…
k n=f ( x i + pn−1 .h , y i +q n−1 ,1 . k 1 . h+q n−1 ,2 .k 2 . h+. . .+q n−1 , n−1 . k n −1 . h )
With k 1=f ( x i , y i )
1
2( 1
k 2=f xi + . h , y i+ . k 1 . h
2 )
k 3=f ( x i +h , y i−k 1 . h+2 k 2 h )
With k 1=f ( x i , y i )
1
2 ( 1
k 2=f xi + . h , y i+ . k 1 . h
2 )
1
2 ( 1
k 3=f xi + h , y i + . k 2 .h
2 )
k 4=f ( x i+ h , y i +k 3 h )
2. Numerical examples
2.1. Numerical differentiation
*Example 1 (Polynomial): Evaluate the first and the second derivatives of
4 3 2
f (x)=−x −2 , 5 x −5 x −4 , 5 x+3
Numerically:
Centered finite-divided-difference formula with 3 points:
f ( x i+1 )−f (xi −1 )
f ' (x i)=
2h
4 3 2
f ( x i ) =f ( 1)=−1 −2 , 5 .1 −5 .1 −4 ,5.1+3=−10
4 3 2
f ( x i +1 )=f ( 1+0 ,25 )=f ( 1 , 25 )=−1 , 25 −2 ,5 .1 , 25 −5 .1 ,25 −4 ,5.1 , 25+3
4547
¿−
256
4 3 2
f ( x i−1 )=f ( 1−0 , 25 ) =f ( 0 , 75 )=−0 ,75 −2 , 5 .0 ,75 −5.0 ,75 −4 , 5.0 , 75+3
1167
¿−
256
−4547 1167
+
f (1 , 25)−f (0.75) 256 256
f ' (1)= = =−26,40625
2h 2.0 , 25
Error: ε = |−26,40625−(−26)
−26
.100 %=1,5625 % |
''
f ( xi +1 )−2 f ( x i ) + f ( xi−1 )
f ❑( x i )= 2
h
−4547 1167
−2.(−10)−
'' f (1 , 25 )−2 f (1 )+ f ( 0 , 75 ) 256 256
f ❑( 1 )= 2
= 2
=−37,125
h 0 ,25
Error: ε = |−37.125−(−37)
−37 |
.100 %=0,3378 %
¿−12,7916
4 3 2
f ( x i−1 )=f ( 1−0 , 1 )=f ( 0 , 9 )=−0 ,9 −2 , 5 .0 , 9 −5 .0 , 9 −4 , 5.0 , 9+ 3
¿−7,5786
f (1 , 1 )−f ( 0 , 9 ) −12,7916 +7,5786
f ❑' ( 1 )= = =−26,065
2h 2.0 ,1
Error: ε = |−26,065−(−26)
−26 |
.100 %=0 ,25 %
−f ( 1, 5 ) +8 f (1 , 25 )−8 f ( 0 , 75 ) +f ( 0 , 5 )
f ❑' ( 1 )=
12 h
¿
−(−28 ,5 )+ 8. ( −4547
256 )
−8. (
256 )
−1167
−0,875
12.0 , 25
¿−26
Error: ε = |
−26−(−26)
−26
.100 %=0 %|
−f (x i+2 )+ 16 f (x i +1)−30 f (x i)+16 f (x i−1)−f (x i−2)
f ' ' (x i)= 2
12 h
−f (1 , 5)+ 16 f (1 , 25)−30 f (1)+16 f (0 , 75)−f (0 , 5)
f ' ' (1)=
12 .0 , 252
''
f ❑( 1 )=
−(−28.5 )+ 16. (−4547
256 )−30. (−10 ) +16. (
256 )
−1167
−(−0,875 )
=−37
2
12 .0 ,25
Error: ε = |
−37−(−37)
−37
.100 %=0 %|
*Example 2 (Not a polynomial): Evaluate the first derivatives of
2
f (x)=e−x sin2 x
−f ( 2 )+ 8 f ( 1 ,5 )−8 f ( 0 , 5 ) + f ( 0 )
f ❑' ( 1 )= =−0,8516422098
12 h
Error: ε = | −0,8516422098−(−0,9752073898)
−0,9752073898 |
.100 %=12, 67 %
With step size h=0.25 and the initial condition at x=0, y=2
Solution
Analytically
−1 4 10 3 15 2
y= x + x − x +7 , 5 x+C
4 3 2
−1 4 10 3 15 2
y ( 0 )=2 ⇒ C=2 ⇒ y = x + x − x +7 ,5 x +2
4 3 2
−1 4 10 3 15 2
y ( 0 , 25 )= .0 , 25 + .0 , 25 − .0 , 25 +7 , 5.0 , 25+2=3,457356771
4 3 2
Euler’s method
y i+ 1= y i+ f ( x i , y i ) . h
f ( x i , y i )=f ( 0 )=7 , 5
Error: ε = |3,875−3,457356771
3,457356771 |.100 % ≈ 12 , 08 %
Heun’s method
0
y i+ 1= y i+ f ( x i , y i ) . h
0
¿> y (0 , 25)= y (0)+7 , 5.0 ,25=2+1,875=3,875
279
f ( xi +1 , y i +1) =f (0 , 25)=−0 , 25 +10 .0 , 25 −15.0 , 25+7 , 5=
0 3 2
64
f ( xi , y i ) +f ( xi +1 , y i+ 1)
0
y i+ 1= y i+ .h
2
279
7 ,5+
f (0)+f (0 , 25) 64
y (0 , 25)= y (0)+ .0 ,25=2+ .0 , 25=3,482421875
2 2
Error: ε = |3,482421875−3,457356771
3,457356771 |.100 % ≈ 0 , 72 %
Runge-Kutta method (4th order)
k 1=f ( x i , y i ) =f (0)=7 , 5
( 1 1
)
k 2=f xi + . h , y i+ . k 1 . h =f (0+
2 2
0 , 25
2
)=f (0,125)=−0,1253 +10 .0,1252−15.0,125+7 ,5=
2959
512
k =f ( x + h , y + . k .h )=f (0,125)=
1 1 2959
3 i i 2
2 2 512
3 2 279
k 4=f ( x i+ h , y i +k 3 h )=f (0 , 25)=−0 , 25 +10 .0 , 25 −15.0 , 25+7 , 5=
64
1
y i+ 1= y i+ (k 1+ 2 k 2+ 2 k 3+ k 4 ). h
6
y ( 0 , 25 )= y ( 0 ) +
1
6(7 , 5+2.
2959
512
+2.
2959 279
+
512 64 )
.0 , 25=2+1,457356771
¿ 3,457356771
Error: ε = |3,457356771−3,457356771
3,457356771 |.100 %=0 %
3. Solving using software (Octave-7.3.0)
3.1. Numerical differentiation
*Example 1 (Polynomial): Evaluate the first and the second derivatives of
4 3 2
f (x)=−x −2 , 5 x −5 x −4 , 5 x+3
at xi = 1, step size h = 0,25
*Example 2 (Not a polynomial): Evaluate the first derivatives of
2
f (x)=e−x sin2 x