21 Numerical Differentiation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MTH 510 Numerical Analysis 7/30/2020

Objective
Solving the problem of Rate of Change of physical quantities
df d  df  f   f 
f ' ( x) = , f ' ' ( x) =  , etc f ' ( x, y ) = , f ' ' ( x, y ) =  , etc
x x  x 
Numerical Differentiation dx dx  dx 

• Numerical Differentiation for equi-spaced &


Ch 21 unequally spaced data
• Improvement of differentiation using Richardson’s
Extrapolation (not included)
• Sensitivity of Numerical differentiation to data error
• Evaluating derivatives in Matlab
1 2

1 2

Introduction Derivative Formulas: Derivation


• Methods Formula Derivation: f (x) f ( xi +1 )
– Simple Function- Analytical Equispaced data points
f ( xi )
f ( xi −1 )
– Complex & Discrete Data – Numerical
h h
• Numerical Method Taylor Series Expansion
xi −1 xi xi +1 x
– Can be Unstable – process associated with Forward Expansion;
subtractive cancellation f ' ' ( xi )h 2 f ' ' ' ( xi )h 3
f ( xi +1 ) = f ( xi ) + f ' ( xi )h + + + .... (1a)
2! 3!
Backward Expansion;
f ' ' ( xi )h 2 f ' ' ' ( xi )h 3
f ( xi −1 ) = f ( xi ) − f ' ( xi )h + − + .... (1b)
3
2! 3! 4

3 4

1st Derivative-2 point/node Formula Example 21.1:


Forward Diff. Approx. -from (1a) and using 2 terms Estimate 1st derivative of f(x)=cos(x) at x=0.8 using
central difference approximation and the resulting
f ( xi +1 ) − f ( xi ) hf ' ' ( xi ) f ( xi +1 ) − f ( xi )
f ' ( xi ) = − = + O ( h) (2) error with a step of h= 0.02 , 0.01
h 2! h

Backward Diff. Approx. -from (1b), and using 2 terms Forward Approx f ( xi +1 ) − f ( xi )
f '( xi ) = ; E_ a =
hf ( xi )
h 2!
f ( xi ) − f ( xi −1 ) hf ' ' ( xi ) f ( xi +1 ) − f ( xi ) cos(.82) − cos(.8)
f ' ( xi ) = + = + O ( h) (3) h = 0.02, f '(0.8) = = - 0.7242751
h 2! h 0.02
cos(.82) − cos(.8)
Central Diff. Approx. -from (1a-1b), and using 2 terms h = 0.01, f '(0.8) = = - 0.72082764
0.01
f ( xi +1 ) − f ( xi −1 ) 2h f '''( xi ) f ( xi +1 ) − f ( xi −1 )
2
Associated Error true & approx
f '( xi ) = +  + O(h 2 ) ( 4) 0.02 cos(.8)
2h 3! 2h h = 0.02, Ea = = .0069671
2
E t =|− sin(0.8) − f '(0.8)| = .00691901
5 6

5 6

LF_S20 1
MTH 510 Numerical Analysis 7/30/2020

Example 21.1 cont. 2nd Derivative f’’(x)


Forward Approximation
h TRUE Forward Error Rel ER (%) Approx Er
• Taylor Expansion step= 2h 2h 2h
0.02 -0.717356091 -0.724275103 0.00691901 0.96451569 0.0069671 xi −1 xi −1 xi xi +1 xi + 2
0.01 -0.717356091 -0.72082764 0.00347155 0.4839366 0.0034835

Backward Abbroximation Forward Expansion;


h TRUE Backward Error Rel ER (%) Approx Er 4 f ' ' ( xi )h 2 8 f ' ' ' ( xi )h 3
0.02 -0.717356091 -0.710341433 -0.0070147 0.97784876 0.0069671 f ( xi + 2 ) = f ( xi ) + 2 f ' ( xi )h + + + .... (1c)
0.01 -0.717356091 -0.713860631 -0.0034955 0.48726991 0.0034835
2! 3!

Central Approx
Backward Expansion;
h TRUE Central Error Rel ER (%) Approx Er
4 f ' ' ( xi )h 2 8 f ' ' ' ( xi )h 3
0.02 -0.717356091 -0.717308268 -4.782E-05 0.00666653 4.645E-05 f ( xi − 2 ) = f ( xi ) − 2 f ' ( xi )h + − + .... (1d )
0.01 -0.717356091 -0.717344135 -1.196E-05 0.00166666 1.161E-05 2! 3!

7 8

7 8

2nd Derivative f’’(x) Example 21.2:


Forward Diff. Approx. –from (1c-2x1a) Estimate 2nd derivative of f(x)=cos(x) at x=0.8 using
central difference approximation and the resulting error
f ( xi + 2 ) − 2 f ( xi +1 ) + f ( xi ) with a step of h= 0.02 , 0.01
f ' ' ( xi ) = + O ( h) (5)
h2

Backward Diff. Approx. –from (1d-2(1b)) f ( xi + 2 ) − 2 f ( xi + 1 ) + f ( xi )


Forward Approx f ''( xi ) =
h2
;

f ( xi −2 ) − 2 f ( xi −1 ) + f ( xi ) cos(.84) − 2 cos(.82) + cos(.8)


f ' ' ( xi ) = + O ( h) (6) h = 0.02, f ''(0.8) = = - 0.68219847
(0.02 2
h2
f ''(0.8) = − cos(.8) = - 0.69670671
Central Diff. Approx. –from (1a+1b) Associated Error true
f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 ) h = 0.02, E t =|− cos(0.8) − f ''(0.8)| = 0.01450824
f ' ' ( xi ) = + O( h 2 ) ( 7)
h2
9 10

9 10

Example 21.2 cont.


3rd & 4th Derivative Formula (Table
21.2-21.5)
f''(cos(x)) h=0.8 Forward Diff. Approx. (4 & 5 points)
Forward Approximation
f ( xi +3 ) − 3 f ( xi + 2 ) + 3 f ( xi +1 ) − f ( xi )
h TRUE Forward Error Rel ER (%) f ' ' ' ( xi ) = O ( h)
h3
0.02 -0.69670671 -0.68219847 -0.01450824 2.082403 f ( xi + 4 ) − 4 f ( xi +3 ) + 6 f ( xi + 2 ) − 4 f ( xi +1 ) + f ( xi )
0.01 -0.69670671 -0.68949269 -0.00721402 1.035446 f iv ( xi ) = O ( h)
h4

Back Diff. Approx. (4 and 5 points)


Backward Abbroximation
f ( xi ) − 3 f ( xi −1 ) + 3 f ( xi −2 ) − f ( xi −3 )
h TRUE Backward Error Rel ER (%) f ' ' ' ( xi ) = O ( h)
h3
0.02 -0.69670671 -0.71088984 0.014183132 2.035739 f ( xi ) − 4 f ( xi −1 ) + 6 f ( xi −2 ) − 4 f ( xi −3 ) + f ( xi −4 )
0.01 -0.69670671 -0.70383945 0.007132741 1.02378 f ( xi ) =
iv
O ( h)
h4

Central Approx Central Diff. Approx. (4 and 5 points)


h TRUE Central Error Rel ER (%) f ( xi + 2 ) − 2 f ( xi +1 ) + 2 f ( xi −1 ) − f ( xi −2 )
0.02 -0.69670671 -0.69668349 -2.3223E-05 0.003333 f ' ' ' ( xi ) = O( h 2 )
2h 3
0.01 -0.69670671 -0.6967009 -5.8059E-06 0.000833 f ( xi + 2 ) − 4 f ( xi +1 ) + 6 f ( xi ) − 4 f ( xi −1 ) + f ( xi −2 )
11 f ( xi ) =
iv
O( h 2 ) 12
h4

11 12

LF_S20 2
MTH 510 Numerical Analysis 7/30/2020

High-Accuracy Formulas (1st Example 21.3:


derivative)
• 3 & 4 point formulas-Derived including additional Estimate 1st derivative of f(x)=cos(x) at x=0.8 using 3
Taylor Series Terms and replacing the f’’ in( 1(a-c)) & 4 points formulas and the resulting error with a
Forward Diff. Approx. –sub f’’ in (1a) step of h= 0.02 , 0.01
− f ( xi + 2 ) + 4 f ( xi +1 ) − 3 f ( xi )
f ' ( xi ) = + O( h 2 ) (8) − f ( x i + 2 ) + 4 f ( xi + 1 ) − 3 f ( xi )
2h Forward Approx f '( xi ) =
2h
;

Backward Diff. Approx. –sub f’’ in (1b) h = 0.02, f ''(0.8) =


− cos(.84) + 4 cos(.82) − 3 cos(.8)
= - 0.717453118
3 f ( xi ) − 4 f ( xi −1 ) + f ( xi −2 ) 2(0.02)
f ' ( xi ) = + O( h 2 ) (9) f '(0.8) = − cos(.8) = - 0.71735609
2h
Central Diff. Approx. Associated true Error
− f ( xi + 2 ) + 8 f ( xi +1 ) − 8 f ( xi −1 ) + f ( xi −2 ) h = 0.02, E t =|− sin(08
. ) − f '(08
. )| = 9.7  10-5
f ' ( xi ) = + O( h 4 ) (10)
12h
13 14

13 14

Example 21.3 cont. Derivative of Unequal Spaced Data


f'(cos(x)) 0.8
Forward Approximation • Method seen- equi-spaced data (h=const)
h TRUE Forward Error Rel ER (%)
0.02 -0.71735609 -0.717453118 9.7E-05 0.013526
• Experimental data- unequal intervals (data with and
0.01 -0.71735609 -0.717380176 2.41E-05 0.003357 without error)
• Method- Using Lagrange Interpolating Polynomial
Backward Abbroximation – Fit a 2nd order polynomial for each adjacent 3 points P2 (x)
h TRUE Backward Error Rel ER (%)
– Find derivative of the approx.
0.02 -0.71735609 -0.717450332 9.42E-05 0.013137
0.01 -0.71735609 -0.717379828 2.37E-05 0.003309 – Accuracy –same as central difference

Central Approx
• Derivative of data with error-find best-fit (Least
h TRUE Central Error Rel ER (%) Square regression), derivative on the basis of the
0.02 -0.71735609 -0.717356087 -3.8E-09 5.33E-07 function
0.01 -0.71735609 -0.717356091 -2.4E-10 3.33E-08 15 16

15 16

Example 3: Unequally spaced Data f ' ( x), &  f ( x)dx for Data with Error
Find the derivative of the function a) Data free of error b) Data with error
30 30

f ( x) = 5 xe − x 25
f (x) 25 f (x)
20 20

@x=1.8 X 1.2 3 3.2


15 15

10 10

f(x) 1.807 0.7468 0.6522 5 5

0 0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

Use lagrange interpolation polynomial approximation df


c) Deriv of (a) d) Deriv of (b)
P2 (t ) =
( x − xi )( x − xi +1 )
f ( xi −1 ) +
( x − xi −1 )( x − xi +1 )
f ( xi ) +
( x − xi −1 )( x − xi )
f ( xi +1 )
10
9 f ' ( x)
df
10  dx dx
( xi −1 − xi )( xi −1 − xi +1 ) ( xi − xi −1 )( xi − xi +1 ) ( xi +1 − xi )( xi +1 − xi −1 ) 8 dx 9
8
f ' ( x)
7 7
2 x − xi − xi +1 2 x − xi −1 − xi +1 2 x − xi −1 − xi 6 6
P2 ' (t ) = f ( xi −1 ) + f ( xi ) + f ( xi +1 ) 5 5
( xi −1 − xi )( xi −1 − xi +1 ) ( xi − xi −1 )( xi − xi +1 ) ( xi +1 − xi )( xi +1 − xi −1 ) 4 4
3 3
2 2

P2 ' (t = 1.8) = −0.6236 f ' (t = 1.8) = −0.6612  t = 5. 7% 1


0
1
0
17 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 18
x x

17 18

LF_S20 3
MTH 510 Numerical Analysis 7/30/2020

Derivatives & Integral for Data Example 4: Unequally spaced Data


with Error
A jet fighter position on an aircraft carrier runway was timed
Effect of Numerical Differentiation & Integration during landing
Numerical Integration attenuates errors in the Data (fig b)
t[s] 1.2 3 3.2 1.74 2.36 3.24 3.82
Numerical Differentiation amplifies errors in the Data (fig d) x[m] 154 186 209 2.50 262 272 2.74

Estimate;
a) the velocity (v) and
b) the acceleration (a)

19 20

19 20

280
Measured Displacement vs time data
Using the least square polynomial

P2 (t ) = 152.6957 + 70.4297t − 10.1784t 2


260

240

220
Distance [m]

200

180

160

140
0 0.5 1 1.5 2 2.5 3 3.5 4
Time [s]

a) The velocity;
v(t ) = P2 ' (t ) = 70.4297 − 20.3568t m / s

b) The acceleration
a(t ) = P2 ' ' (t ) = 20.3568 m / s 2 21 22

21 22

Questions?

23

23

LF_S20 4

You might also like