ce27. notes. numerical differentiation
TRANSCRIPT
-
8/9/2019 Ce27. Notes. Numerical Differentiation
1/28
Numerical Differentiation
eatingeatingeatingeating
-
8/9/2019 Ce27. Notes. Numerical Differentiation
2/28
Introduction Some numerical algorithms require
evaluation of the derivative of a function.
When the function is difficult todifferentiate analytically or when thefunction is given empirically as in table of
values, approximate calculation of thederivative is necessary.
-
8/9/2019 Ce27. Notes. Numerical Differentiation
3/28
Approximating the derivativeThe definition of the derivative is, the limit as
h0 of
practically suggests the program to pick asmall value of h.
Example. Compute the derivative off(x)=ex
atx=1.
( )( ) ( )
h
xfhxfxf
+' )1(
-
8/9/2019 Ce27. Notes. Numerical Differentiation
4/28
-
8/9/2019 Ce27. Notes. Numerical Differentiation
5/28
Approximating the derivative When h is large, the difference quotient is
the slope of the secant line joining twopoints that are not close enough to eachother.
When h is too small, the differencequotient can exhibit the problem of loss of
significance due to the subtraction ofquantities that are nearly equal.
-
8/9/2019 Ce27. Notes. Numerical Differentiation
6/28
Approximating the derivative To determine the best approximation
before the terms start to move away fromthe limit is to computeDk until
In the example,
11 + NNNN DDDD
00012.00007.0 4556 =>= DDDD
-
8/9/2019 Ce27. Notes. Numerical Differentiation
7/28
Sources of error The truncation error t comes from higher
terms in the Taylor series expansion:
Round-off error r must be expected sincereal numbers dont have exact
representation in binary.
( ) ( )L
+= "
2
1' hf
h
xfhxff
-
8/9/2019 Ce27. Notes. Numerical Differentiation
8/28
Central-difference formulas Assume thatfC3[a,b] and thatx-h,x, x+h
[a,b]. Then
where, there exists a number c=c(x) [a,b]such that
( )( ) ( )
th
hxfhxfxf +
+
2'
( )( ) ( )232
6h
cfht O==
)2(
-
8/9/2019 Ce27. Notes. Numerical Differentiation
9/28
Centered-formula of order O(h4
)
Assume thatfC5[a,b] and thatx-2h,x-h, x
x+h, x+2h [a,b]. Then
where( )( ) ( )454
30h
cfht O==
( )( ) ( ) ( ) ( )
t
h
hxfhxfhxfhxfxf +
++++
12
2882'
)3(
-
8/9/2019 Ce27. Notes. Numerical Differentiation
10/28
ExampleLetf(x)=cos(x). Calculate approximations for
f(0.8) using Eqs. (2) and (3) with stepsizes h=0.1, 0.01, 0.001, 0.0001 andcompare with true valuef(0.8) = -sin(0.8).
Carry out nine decimal places in
calculations.
-
8/9/2019 Ce27. Notes. Numerical Differentiation
11/28
ExampleEq. (2)
Eq. (3)
( )
( ) ( ) ( ) ( )
h
hxfhxfhxfhxf
xf 12
2882
'
++++
( )( ) ( )
h
hxhxf
xf 2'
+
-
8/9/2019 Ce27. Notes. Numerical Differentiation
12/28
Differentiating an
Interpolating Polynomial The Lagrange quadratic polynomial P2(x)
that passes throughx=0.7,0.8, and 0.9.
Differentiating
( ) 22 348063157.0159260044.0046875165.1 xxxP =
( ) 716161095.08.0'2 =P
-
8/9/2019 Ce27. Notes. Numerical Differentiation
13/28
Differentiating an
Interpolating Polynomial The Lagrange quadric polynomial P4(x)
that passes throughx=0.6,0.7,0.8, 0.9 and1.0.
Differentiating
( )2
4 523291341.000963839.0998452927.0 xxxP +=43
028981100.0026521229.0 xx ++
( ) 717353703.08.0'4 =P
-
8/9/2019 Ce27. Notes. Numerical Differentiation
14/28
Differentiation using LimitsTo approximatef(x) numerically, generate a
sequence
for k=0,..,n until |Dn+1
Dn
| |Dn-1
Dn
| or|DnDn-1| tolerance.
( )
( ) ( )
( )hhxfhxf
Dxf k
kk
k
+
=
102
1010
'
-
8/9/2019 Ce27. Notes. Numerical Differentiation
15/28
Richardsons ExtrapolationLet
andD0(h) andD0(2h) denote approximations
tof(x0) that are obtained using Eq. (2)with step sizes h and 2h.
( ) ( )khxfxff kk +== 0
( ) ( )( ) ( ) 2
00
2
00
42''
ChhDxfChhDxf+
+
( )4
-
8/9/2019 Ce27. Notes. Numerical Differentiation
16/28
Richardsons ExtrapolationManipulating these two equations, we obtain
which is the central difference formulaEq.(3).
This method of obtaining a formula forf(x0)of higher order from a formula of lowerorder is called extrapolation.
( ) ( ) ( )h
ffffhDhDxf
12
88
3
24' 211200
0
++=
-
8/9/2019 Ce27. Notes. Numerical Differentiation
17/28
Richardsons ExtrapolationSuppose that two approximations of order
O(h2k) forf(x0) areDk-1(h) andDk-1(2h) andthat they satisfy
Then an improved approximation is
( ) ( )( ) ( ) L
L
+++
+++
++
+
22
2
12
110
22
2
2
110
442''
kkkk
k
kk
k
hchchDxfhchchDxf
( ) ( ) ( ) ( ) ( ) ( )221122014
24'
+++
=+=
k
k
kk
kk
k hhDhD
hhDxf OO
-
8/9/2019 Ce27. Notes. Numerical Differentiation
18/28
ExampleLet
f(x)=cos(x).
Use Eq. (4) with h=0.01 to show how the
linear combination (4D0(h) -D0(2h))/3 canbe used to obtain the approximation to
f(0.8).
Carry nine decimal places in allcalculations.
-
8/9/2019 Ce27. Notes. Numerical Differentiation
19/28
ExampleUsing Eq. (4), we obtain
The linear combination becomes
( ) ( ) ( )
( )( ) ( )
717308275.0
04.0
78.082.02
717344150.002.0
79.081.0
0
0
=
=
ffhD
ffhD
( ) ( ) ( ) 717356108.03
248.0' 00 = hDhDf
-
8/9/2019 Ce27. Notes. Numerical Differentiation
20/28
Higher Derivatives Taylor series can be used to obtain
central-difference formulas for higherderivatives.
The popular choices are those of order
O(h2) and O(h4).
-
8/9/2019 Ce27. Notes. Numerical Differentiation
21/28
Higher DerivativesTo derivef(x) of order O(h2), we write
adding
( ) ( ) ( )( ) ( )( )
( ) ( ) ( ) ( ) ( )( ) L
L
++=
++++=+
62"'
62
"'
332
332
xfhxfhxfhxfhxf
xfhxfhxfhxfhxf
( ) ( ) ( )( ) ( )( )
L+++=++24
2
2
"22
442xfhxfh
xfhxfhxf
-
8/9/2019 Ce27. Notes. Numerical Differentiation
22/28
Higher DerivativesSolving forf(x) yields
If truncated at the fourth derivative
( ) ( ) ( ) ( )2
2"
h
hxfxfhxfxf
++=
( )
( )( )
( )( )
( )( ) LL
!22
!62
!42
2226442
kxfhxfhxfh
kk
( )( )( )
12
2"
42
2
101 cfh
h
fffxf
+=
-
8/9/2019 Ce27. Notes. Numerical Differentiation
23/28
Central difference formulas
of Order O(h2)
( )
( )
( )( )
( )( )4
210120
4
3
21120
3
2
1010
11
0
464 2
2
2"
2'
h
fffffxf
h
ffffxf
h
fffxf
h
ffxf
++
+
+
-
8/9/2019 Ce27. Notes. Numerical Differentiation
24/28
Central difference formulas
of Order O(h4)
( )
( )
( )( )
( )( )4
32101230
4
3
3211230
3
2
210120
2112
0
61239563912
8
81313812
163016"
12
88
'
hfffffffxf
h
ffffffxf
h
fffffxf
h
ffff
xf
+++
+++
++
++
-
8/9/2019 Ce27. Notes. Numerical Differentiation
25/28
ExampleLetf(x)=cos(x). Use the formula
with h=0.1, 0.01, and 0.001 to findapproximations to f(0.8).
Compare with the true value of f(0.8) =
-cos(0.8).
( )2
101 2"h
fffxf
+=
-
8/9/2019 Ce27. Notes. Numerical Differentiation
26/28
Example The calculation for h=0.01 is
with an error of -0.000016709 The other calculations are summarized:
( ) ( ) ( ) ( ) 696690000.00001.0
79.080.0281.08.0" =
+
ffff
-
8/9/2019 Ce27. Notes. Numerical Differentiation
27/28
Exercise 1Letf(x)=sin(x), wherex is measured in
radians.(a) Calculate approximations tof(0.8)using Eq.(3) with h=0.1, 0.01, andcompare withf( 0.8)=cos(0.8).
(b) Use extrapolation formula to compute
the approximation tof( 0.8)
-
8/9/2019 Ce27. Notes. Numerical Differentiation
28/28
Exercise 2Use numerical differentiation formula
and h=0.1 to approximatef(1) for thefunctions
(a)f(x) =x2 (b)f(x) =x4
( )2
210120
12
163016"
h
fffffxf
++