Chapter 19 Numerical Differentiation Estimate the derivatives (slope, curvature, etc.) of a function by using the function values at only a set.
Download ReportTranscript Chapter 19 Numerical Differentiation Estimate the derivatives (slope, curvature, etc.) of a function by using the function values at only a set.
Chapter 19 Numerical Differentiation
Estimate the derivatives (slope, curvature, etc.) of a function by using the function values at only a set of discrete points Ordinary differential equation (ODE) Partial differential equation (PDE) Represent the function by Taylor polynomials or Lagrange interpolation Evaluate the derivatives of the interpolation polynomial at selected (unevenly distributed) nodal points
Differentiation
dy dx
Difference
y
x
lim 0 (
x
(
x x x x x x x
dy dx
A smaller step
x
(or h) results in a smaller error
Evenly distributed points along the x-axis
x
h
x i-3 x i-2 x i-1 x i x i+1 x i+2 x i+3 Distance between two neighboring points is the same, i.e. h.
Unevenly distributed points along the x-axis
x 1 x 2 x 3
Numerical Differentiation
Forward difference Backward difference Centered difference
Forward difference
x i
1 x i h x i+1 x
Backward difference
x i
1 h x i x i+1 x
Centered difference
x i
1 2h x i x i+1 x
y
First Derivatives
f
( x )
i-2 i-1 i i+1 i+2
Forward difference Backward difference Central difference
f f
( ( x x ) )
f
( x )
f f f ( ( ( x i
1 )
f ( x x i x i
1 )
x i
x f ( x i
1 i x i
1 i x i
1 )
f ( x ) i
1 ) x i
1
x i
1 )
y i
1
y x i
1 y i
x i
y x x i
1 i
1 i i y x i
1 i
1
y i
1 x i
1 x
Truncation Errors
Uniform grid spacing
f ( x i
1 )
f ( x i
1 )
f ( x i
h )
f ( x i
h )
f f ( x i ( x i ) )
h f
( x i
h f
( x i ) )
h 2 !
h 2 2 2 !
f f
( x i ( x i )
h 3 3 !
)
h 3 3 !
f f
( x i )
( x i )
forward
:
f
(
x i
)
backward central
:
f
:
(
f x i
( )
x i
)
f
(
f
(
f x
(
x i
1 i
1 x i
) ) )
h
2 h h f f
(
f
( (
x x x i
1 i i
)
1
) )
h 2 6 h h 2 2 f f f
(
1
)
O(h)
(
(
2 3
)
O(h)
)
O(h 2
)
Example: First Derivatives
Use forward and backward difference approximations to estimate the first derivative of
f ( x )
0 .
1 x 4
0 .
15 x 3
0 .
5 x 2
0 .
25 x
1 .
2
at x = 0.5 with h = 0.5 and 0.25 (exact sol. = -0.9125)
Forward Difference
h h
0
.
5
,
f 0
.
25
,
(
0
.
5
)
f
(
0
.
5
)
f f
(
1
)
f
(
0
.
5
(
1 0
0
.
5
.
75
)
0
.
75
f
) (
0
.
5 0
.
5 0
) .
2
0
.
925 0 0
.
.
5
63632813 0
.
1 25
.
45
,
0
.
925 t
58
.
9
%
1
.
155
,
t
26
.
5
% Backward Difference
h h
0
.
5
,
f 0
.
25
,
f
(
0
.
5
)
(
0
.
5
)
f f
(
0
.
5
)
0
.
5
(
0
.
5
)
0 0
.
5
f f
(
0
( )
0 0
.
25
.
25 0
) .
925
0 0
.
5 1
.
925
.
2
1
.
0
.
25 0
.
55
,
10351563 t
39
.
7
%
0
.
714
,
t
21
.
7
%
Example: First Derivative
Use central difference approximation to estimate the first derivative of
f ( x )
0 .
1 x 4
0 .
15 x 3
0 .
5 x 2
0 .
25 x
1 .
2
at x = 0.5 with h = 0.5 and 0.25 (exact sol. = -0.9125)
Central Difference
h
0 .
5 , f
( 0 .
5 )
f ( 1 )
1
0 f ( 0 )
0 .
2
1 .
2 1
1 .
0 ,
t
9 .
6 % h
0 .
25 , f
( 0 .
5 )
f ( 0 .
75 0 .
75 )
f ( 0 .
25 0 .
25 )
0 .
63632813
1 .
10351563 0 .
5
0 .
934 ,
t
2 .
4 %
Second-Derivatives
Taylor-series expansion Uniform grid spacing
f ( x i
1 )
f ( x i )
h f
( x i )
h 2 2 !
f
( x i )
h 3 3 !
f
( x i )
h 4 4 !
f
( x i )
f ( x f ( i
1 x ) i
1
)
f ( x f ( i x )
h f
( i
1 )
x 2
i f )
( x i h 2 2 !
)
f h 2
( x i 2 !
f
)
( x i h 3 3 !
)
f h 4
( x i 4 !
f )
h
( x i 4 f
( x i 4 !
)
)
Second-order accurate O(h
2 ) f
(
x i
)
f
(
x i
1
)
2 f
(
x i
)
h 2 f
(
x i
1
)
h 2 4
!
f
(
)
Centered Finite-Divided Differences
Forward Finite-divided differences
Backward finite-divided differences
First Derivatives
f
( x )
Parabolic curve
i-2 i-1 i i+1 i+2
3 -point Forward difference
f
(
x
)
f
(
x i
2
)
4 x i
2 f
(
x i
1 x i
) 3 -point Backward difference
3 f
(
x i
)
f
(
x
)
3 f
(
x i
)
4 f x i
(
x i
1 x
)
i
2
3 f
(
x i
2
)
Example: First Derivatives
Use forward and backward difference approximations of O(h 2 ) to estimate the first derivative of
f ( x )
0 .
1 x 4
0 .
15 x 3
0 .
5 x 2
0 .
25 x
1 .
2
at x = 0.5 with h = 0.25 (exact sol. = -0.9125)
Forward Difference
f
( 0 .
5 )
f ( 1 )
4 f ( 0 .
75 )
3 f ( 0 .
5 ) 2 ( 0 .
25 )
0 .
2
4 ( 0 .
6363281 0 .
5
Backward Difference
)
3 ( 0 .
925 )
0 .
859375 ,
t
f
( 0 .
5 )
3 f ( 0 .
5 )
4 f ( 0 .
25 0 .
5 )
f ( 0 ) 2 ( 0 .
25 ) 3 ( 0 .
925 )
4 ( 1 .
035156 )
1 .
2
0 .
878125 ,
t
5 .
82 %
3 .
77 %
Higher Derivatives
All second-order accurate O(h
2 ) f f
( x i
( )
x i f f )
( x i f ( f )
x (
x i i
2 )
f ( x i
1 )
f 2 h f ( )
x 2 i
1 f ( )
2 x i
1 f ( x i ) h 2
2 ( x )
f ( i
1 x f ( ) i
1 x i
1 )
h 3 ( x i
2 )
4 f ( x i
1 )
6 f ( x i )
4 f ( x f ) i
1 ( ) x i
2
f ( ) h 4 x i
2 )
More nodal points are needed for higher derivatives Higher order formula may be derived
19.3 Richardson Extrapolation
D
D h
1 2 4 ........
D h
( 1 2 ) (1)
D
is the true value but unknown and
D(h 1 )
is an approximation based on the step size
h 1
. Reducing the step size to half,
h 2 =h 1 /2
, we obtained another approximation
D(h 2 ).
D
( 2 ) 2 4 ( 2 ) ( 2 2 ) (2) 1 ( 1 2 ) ( 2 2 ) 4 Hence 4*(2) - (1),
D
4 ( 2 )
D h
3 + ( 1 4 ) By properly combining the two approximations,
D(h 1 ) & D(h 2
), the error is reduced to
O(h 4 ).
f f
Example of using Richardson Extrapolation
Central Difference Scheme
1 2
h
/ 2 1
h
4*(2) - (1),
h
)
h
/ 2)
h
)]
h
2 6
f
h
/ 2)]
h
2 24
f f
' 3 4 ) (1) 4 ) (2) 4 ) By combining the two approximations,
D(h/2) & D(h
), the error of
f’(h)
is reduced to
O(h 4 ).
Ex19.2: Richardson Extrapolation
Use central difference approximation to estimate the first derivative of
f ( x )
0 .
1 x 4
0 .
15 x 3
0 .
5 x 2
0 .
25 x
1 .
2
at x = 0.5 with h = 0.5 and 0.25 (exact sol. = -0.9125)
D h
1 ( 0.5)
f
(1)
f
(0) 1.0, t 9.6%
D h
2 ( 0.25)
f
(0.75)
f
(0.25) 0.934, t 2.4%
D
4
D
2
D
1 3 0.9125, t 0%
General Three-Point Formula
Lagrange interpolation polynomial for unequally spaced data
f
(
x
)
L i
1
(
x
)
(
x i
1
(
x
(
x
(
i x
x x i i
)( )(
x x
i
1 x
i
1 x
)
i
1 x i
1
)(
x i
1
)(
x x i
x i x i
1 1
) ) )
f f
( (
x i x i
1
) )
(
x
(
x i
1
f x x i i
(
x i
1
)
1
1
)( )(
x
x i
1 L i
(
x
)
x
i
)
x i
)
f f
(
x i
(
x i
)
1
)
L i
1
(
x
)
f
(
x i
1
)
Lagrange Interpolation
1st-order Lagrange polynomial
f 1
(
x
)
L 0 f
(
x 0
)
L 1
(
x
)
f
(
x 1
)
x x 0
x 1 x 1 f
(
x 0
)
x x 1
x 0 x 0 f
(
x 1
)
Second-order Lagrange polynomial
f 2 ( x )
( ( ( ( x x 0 x x 1
x x 1 1 x 0 x 0 )( )( )( )( x x 0
x 2 x 2 ) ) x x 1
x 2 x 2 ) )
( ( x x 2
x x 0 0 )( )( x x 2
x 1 x 1 ) ) f f f ( ( ( x 0 x x 1 2 ) ) )
Lagrange Interpolation
Third-order Lagrange polynomial
f 3 ( x )
( ( ( x 0 x
x x 1 1 )( )( x x 0
x 2 x 2 )( )( x x 0
x 3 x ) 3 ( x 1 x
x x 0 0 )( )( x x 1
x 2 x 2 )( )( x x 1
x 3 x 3 ) ) )
( ( ( x 2 x
x x 0 0 )( )( x x 2
x 1 x 1 )( )( x x 2
x 3 x 3 ) ( x 3 x
x x 0 0 )( )( x x 3
x 1 x 1 )( )( x x 3
x 2 x 2 ) ) ) f f f f ( ( ( ( x 0 x x x 1 2 3 ) ) ) )
Lagrange Interpolation
L 0 (x)f(x 0 ) L 2 (x)f(x 2 ) x 0 L 1 (x)f(x 1 ) x 1 x 2
General Three-Point Formula
Lagrange interpolation polynomial for unequally spaced data
f
(
x
)
L i
1
(
x
)
f
(
x i
1
)
L i
(
x
)
f
(
x i
)
f f
(
x i
1
) (
x i
1
) ( (
x
(
x i x i
1
(
x
1
x x x x i i i i
)( )(
1
1 x x
)( )(
i
1 x x x
i
1 x i
)
1
i
1 x
i
)
x i
) )
L i
1
(
x
)
f
(
x i
)
f
( ( (
x i
1
)
x
x i
x i
1
)(
x i
1
)(
x x i
x i x i
1
1
) )
First derivative
f
(
x
)
f f
(
x i
1
) (
x i
1
) (
x i
1 2
x
x i x i
)(
x i
1 x i
1 x i
1
) (
x i
1 2 x
x
x i
1 i
1
)(
x i
1 x i
x i
)
f
(
x i
) (
x i 2 x
x i x i
1
1
)(
x i
x i
1
x i
1
)
Second Derivative
First Derivative for unequally spaced data
f
(
x
)
f f
(
x i
1
) (
x i
1
) (
x i
1 2 x
x i x i
)(
x
i
1 x i
1 x i
1
) (
x i
1 2 x
x i x i
1
1
)(
x i
1 x i
x i
)
f
(
x i
) (
x i 2 x
x i x
1 i
1
)(
x i
x i
1 x i
1
)
Second Derivative for unequally spaced data
f
(
x
)
f
(
x i
1
) (
x i
1 2
x i
)(
x i
1
x i
1
)
f
(
x i
) (
x i 2
x i
1
)(
x i
x i
1
)
f
(
x i
1
) (
x i
1 2
x i
1
)(
x i
1
x i
)
Differentiation of Noisy Data
MATLAB’s Methods
Derivatives are sensitive to the noise Use least square fit before taking derivatives
p = polyfit(x, y, n) - coefficients of P n (x) polyfit(p, x) - evaluation of P n (x) polyder(p) - differentiation
x dy
diff
(
x ( x ) 1
),
x x ( ( 2 2 ),
)
x , ( x 1 ( n )
), x ( diff(y)./d iff(x)
3 )
x ( 2 ),
dy/dx at x(i)
dy/dx at x(i
1)
, x ( n )
x ( forward n
1 )
difference backward difference