Numerical Differentiation
Numerical Differentiation
Chapter 19
Numerical Differentiation
Prof. Hae
Hae--Jin Choi
' f (x i+1 ) - f (x i )
f (x i ) = + O(h)
h
l Backward:
' f (x i ) - f (x i-1 )
f (x i ) = + O(h)
� h
l Centered:
' f (x i+1 ) - f (x i-1 )
f (x i ) = + O(h 2 )
� 2h
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 6
High Accuracy Differentiation
l Forward Taylor series expansion
f ¢¢( xi ) 2
f ( xi +1 ) = f ( xi ) + f ¢( xi )h + h +L
2!
f ( xi +1 ) - f ( xi ) f ¢¢( xi )
f ¢( xi ) = - h + O(h 2 )
h 2!
l Forward-difference approximation of 1st derivative excluding the
second and higher derivative term (In chapter 4)
f ( xi +1 ) - f ( xi )
¢
f ( xi ) = + O ( h)
h
l Forward-difference approximation of 2nd derivative
f ( xi + 2 ) - 2 f ( xi +1 ) + f ( xi )
f ¢¢( xi ) = 2
+ O ( h)
h
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 7
High Accuracy Differentiation
l Forward-difference approximation of 1st derivative including
2nd derivative term
f ( xi +1 ) - f ( xi ) f ( xi + 2 ) - 2 f ( xi +1 ) + f ( xi ) 2
f ¢( xi ) = - 2
h + O ( h )
h 2h
- f ( xi + 2 ) + 4 f ( xi +1 ) - 3 f ( xi )
f ¢( xi ) = + O(h 2 )
2h
l Notice that inclusion of second-derivative term has
improved the accuracy to O(h2) .
4 1
D= (-0.934375) - (-1) = -0.9125
3 3
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 15
Unequally Spaced Data
• One way to calculated derivatives of
unequally spaced data is to determine a
polynomial fit and take its derivative at a
point.
• As an example, using a second-order
Lagrange polynomial to fit three points and
taking its derivative yields:
2x - x1 - x2 2x - x0 - x2 2x - x0 - x1
f ¢(x) = f (x0 ) + f (x1 ) + f (x2 )
(x0 - x1 )(x0 - x2 ) (x1 - x0 )(x1 - x2 ) (x2 - x0 )(x2 - x1 )
>> diff(x)
ans =
Columns 1 through 5
0.1000 0.1000 0.1000 0.1000 0.1000
Columns 6 through 8
0.1000 0.1000 0.1000
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 19
Numerical Differentiation with
MATLAB
• diff(y)./diff(x)
– Returns the difference between adjacent values in y divided
by the corresponding difference in adjacent values of x
>> d=diff(y)./diff(x)
Columns 1 through 5
10.8900 -0.0100 3.1900 8.4900 8.6900
Columns 6 through 8
1.3900 -11.0100 -21.3100
>> n=length(x);
>> xm=(x(1:n-1)+x(2:n))./2;
% vector d contains derivative estimates corresponding to the
% midpoint between adjacent elements
>> xa=0: .01 : .8 ;
>> ya=25-400*xa+3*675*xa.^2-4*900*xa.^3+5*400*xa.^4;
>> xplot(xm, d, 'o', xa, ya)