Escolar Documentos
Profissional Documentos
Cultura Documentos
2017
Numerical differentiation
Formulas for numerical derivatives are important in developing
algorithms for solving boundary value problems for ordinary differential
equations and partial differential equations. Standard examples of
numerical differentiation often use known functions so that the
numerical approximation can be compared with the exact answer. For
illustration, we use Bessel function J1 (x), whose tabulated values can
be found in standard reference books. Eight equally spaced points over
[0, 7] are (0, 0.0000), (1, 0.4400), (2, 0.5767), (3, 0.3391), (4, −0.0660),
Figure: (a) the tangent to p2 (x) at (2, 0.5767)with the slope p02 (2) = −0.0505 (b) the
tangent to p4 (x) at (2, 0.5767) with slope p04 (2) = −0.0618
The quadratic polynomial P4 and its tangent line at (2, J1 (2)) are shown
in figure 6.1(b). the true value for the derivative is J10 (2) = −0.06445,
and the errors in p2 (x)andP4 (x) are -0.0140 and -0.0026, respectively.
In this chapter we develop the introductory theory needed to
investigate the accuracy of numerical differentiation.
hk fk = f (1 + hk ) fk − e Dk = (fk − e)/hk
h1 = 0.1 3.004166024 0.285884196 2.858841960
h2 = 0.01 2.745601015 0.027319187 2.731918700
h3 = 0.001 2.721001470 0.002719642 2.719642000
h4 = 0.0001 2.718553670 0.000271842 2.718420000
h5 = 0.00004 2.718309011 0.000027183 2.718300000
h6 = 10−6 2.718284547 0.000002719 2.719000000
h7 = 10−7 2.718282100 0.000000272 2.720000000
h8 = 10−8 2.718281856 0.000000028 2.800000000
h9 = 10−9 2.718281831 0.000000003 3.000000000
h1 0 = 10−10 2.718281828 0.000000000 0.000000000
f (x + h) − f (x − h)
f 0 (x) ≈ (3)
2h
Furthermore, there exist a number c = c(x) ∈ [a, bsuchthat]
f (x + h) − f (x − h)
f 0 (x) = + Etrunc (f , h) (4)
2h
h2 f (3) (c)
where Etrunc (f , h) = − = O(h2 )
6
Professor Henry Arguello Numerical methods 2017 11 / 66
Numerical Differentiation
The term E(f , h) is called the truncation error
proof start with the second-degree Taylor expansions
f (x) = p2 (x) + E2 (x), about x, for f (x + h) and f (x − h):
f (x + h) − f (x − h) f (3) (c)
f 0 (x) = − (9)
2h 3!
The first term in (9) is the central-difference formula (3),the second
term is the truncation error, and the proof is complete.
suppose that the value of the third derivative f (3) (c) does not change
too rapidly; then the truncation error in (4) goes to zero in the same
manner as h2 , which is expressed by using the notation O(h2 ).when
computer calculations are used, it i not desirable to choose h too small.
For this reason it is useful to have a formula for approximating f 0 (x) that
has a truncation error term of the order O(h4 ).
−f (x + 2h) + 8f (x + h) − 8f (x − h) + f (x − 2h)
f 0 (x) = (10)
12h
furthermore, there exist a number c = c(x) ∈ [a, b]such that
−f (x + 2h) + 8f (x + h) − 8f (x − h) + f (x − 2h)
f 0 (x) = + Etrunc (f , h)
12h
(11)
where
h4 f (5) (c)
Etrunc (f , h) = − = O(h4 )
30
proof
One way to derive formula (10) is as follows. Start with the difference
between the fourth-degree Taylor expansions f (x) = P4 (x) + E4 (x),
about x, of f (x + h) and f (x − h):
Next multiply the terms in equation (12) by 8 and substract (13) form it.
The terms involving f 3 (x)will be delimited and we get
After(15) is substituted into (14) and the result is solved for f 0 (x), we
obtain
example 6.2
Let f (x) = cos(x) (a) Use formulas (3) and (10) with step sizes
h = 0.1, 0.01, 0.001 and 0.0001and calculate the aproximationsfor
f 0 (0.8). Carry nine decimal placesin all the calculations.
(b) Compare with the true calue f 0 (0.8) = −sin(0.8).
(a) using th formula (3) with h =0.01, we get
.
f (0.81) − f (0.79) 0.689498433 − 0.703845316
f 0 (0.8) ≈ ≈ ≈
0.02 0.02
−0.717344150
.
using formula (10) with h = 0.01
.
−f (0.82) + 8f (0.81) − 8f (0.79) + f (0.78)
f 0 (0.8) ≈ ≈ −0.717356108
0.12
step size aproximation by formula(3) error using formula (3) aproximationby formula(10) error using formula (10)
0.1 -0.716161095 -0.001194996 -0.717353703 -0.000002389
0.01 -0.717344150 -0.000011941 -0.717353108 -0.000000017
0.001 -0.717356000 -0.000000091 -0.717356167 -0.000000076
0.0001 -0.717360000 -0.000003909 -0.717360833 -0.000004742
table6.2 Numerical differentiation using formular (3) and (10)
e1 − e−1 h2 f (3)(c)
= − (19)
2h 6
where the total error term E(f,h) has a part due round-off error pplus a
part due to truncation error
Professor Henry Arguello Numerical methods 2017 22 / 66
Numerical Differentiation
Mh2
|E(f , h)| ≤ + (20)
h 6
and the value of h that minimizes the right-hand side of (20)is
3 1/3
h=( ) (21)
M
When h is small. the portion of (19) involving (e1 − e−1 )/2h can be
relatively large. In Example 6,2, when h=0.0001, this difficulty was
encountered. The round-off errors
figure 6.3
Finding the optimal step size h = 0.0011447714 when formula (21) is
applied to f (x) = cos(x) in example 6.2.
Professor Henry Arguello Numerical methods 2017 26 / 66
Numerical Differentiation
figure 6.4
Finding the optimal step size h = 0.022388475 when formula (26) is
applied to f (x) = cos(x) in example 6.2.
figure 6.5 (a) The graph of y = cos(x) and the interpolating polynomial
p2 (x) used to estimate f 0 (0.8) ≈ p02 (0.8) = −0.716161095 (b) The graph
of y = cos(x) and the interpolating polynomial p4 (x) used to estimate
f 0 (0.8) ≈ p04 (0.8) = −0.717353703
Professor Henry Arguello Numerical methods 2017 31 / 66
Numerical Differentiation
Richardson’s Extrapolation
In this section we emphasize the relationship between formulas (3)
and (10). Let fk = f (xk ) = f (x0 + kh), and use the notation D0 (h) and
D0 (2h) to denote the approximations to f 0 (x0 ) that are obtained from (3)
with step sizes h and 2h, respectively :
and
f 0 (x0 ) ≈ D0 (2h) + 4Ch2 (28)
If we multiply relation (27) by 4 subtract relation (28) from this product,
the the terms involving C cancel and the result is
Example 6.3
Let f (x) = cos(x).Use (27) and (28) with h=0.01, and show how
thelinear combination (4D0 (h) − D0 (2h))/3 in (30) can be used to
obtain the approximation to f 0 (0.8) given in (10). Carry nine decimal
places in all the calculations.
and
f (0.82) − f (0.78) 0.682221207 − 0.710913538
D0 (2h) ≈ ≈
0.04 −0.717308275
≈ −0.717308275
This is exactly the same as the solution in Example 6.2 that used (10)
directly to approximate f 0 (0.8).
The method of obtaining a formula for f 0 (x0 ) of higher order from a for-
mula of lower order is called extrapolation. The proof requires that
the error term for (3) can be expanded in a series containing only even
powers of h. we have already seen how to use step sizes h and 2h to
remove the term involving h2 . To see how h4 is removed, let D1 (h) and
D1 (2h) denote the approximations to f 0 (x0 ) of order O(h4 ) obtained with
formula (16) using step sizes h and 2h, respectively. Then
and
−f4 + 8f2 − 8f−2 + f−4 16h4 f (5) (c2 )
(32) f 0 (x0 ) = + ≈ D1 (2h) + 16Ch4 .
12h 30
Suppose that f (5) (x) has one sign and does not change too rapidly;
then the assumption that f (5) (c1 ) ≈ f (5) (c2 ) can be used to eliminate the
terms involving h4 in (31) and (32), and the result is
16D1 (h) − D1 (2h)
(33) f 0 (x0 ) ≈
15
The general pattern for improving calculations is stated in the next re-
sult.
The formulas for f 0 (x0 ) in the preceding section required that the func-
tion can be computed at abscissas that lie on both sides of x, and they
were referred to as central-difference formulas. Taylor series can be
used to obtain central-difference formulas for the higher derivatives.
2 4
The popular choices are those of order O(h ) and O(h ) and are given
in Tables 6.3 and 6.4. In these tables we use the convention that
fk = f (x0 + kh) for k = −3, −2, −1, 0, 1, 2, 3. For illustration, we will derive
the formula for f 00 (x) of order 0(h2 ) in Table 6.3. Start with the Taylor
expansions
h2 f 00 (x) h3 f (3) (x) h4 f (4) (x)
(1) f (x + h) = f (x) + hf 0 (x) + + + + ...
2 6 24
f1 − f−1
f 0 (x0 ) ≈
2h
f1 − 2f0 + f−1
f 00 (x0 ) ≈
h2
f2 − 2f1 + 2f−1 − f−2
f (3) (x0 ) ≈
2h3
f2 − 4f1 + 6f0 − 4f−1 + f−2
f (4) (x0 ) ≈
h4
(a) Use formula (6) with h = 0.1, 0.01, and 0.001 and find
approximations to f 00 (0.8). Carry nine decimal place in all
calculations.
Error Analysis
When formula (11) is applied to Example 6.4, use the bound | f 4 |≤|
cos(x) |≤ 1 = M and the value ∈= 0.5 × 10−9 . The optimal step size
is h = (24 × 10−9 /1)1/4 = 0.01244666, and we see that h = 0.01 was
closest to the optimal value.
Since the portion of the error due to round off is inversely proportional
to the square of h, this term grows when h gets small. this is sometimes
referred to as the step-size dilemma. One partial solution ti this prob-
lem is to use a formula of higher order in that a larger value of h will
produce the desired accuracy. The formula for f 00 (x0 ) of order O(h4 ) in
Table 6.4 is
−f2 + 16f1 − 30f0 + 16f−1 − f−2
(12) f 00 (x0 ) = + E(f , h)
12h2
Professor Henry Arguello Numerical methods 2017 47 / 66
Numerical Differentiation
(a) Use formula (12) with h = 1.0, 0.1, and 0.01 and find
approximations to f 00 (0.8). Carry nine decimal places in all the
calculations.
(b) Compare with the true value f 00 (0.8) = −cos(0.8).
(c) Determine the optimal step size.
f 00 (0.8)
−f (1.0) + 16f (0.9) − 30f (0.8) + 16f (0.7) − f (0.6)
≈
0.12
≈ −0.696705958.
(c) When formula (15) is applied, we can use the bound | f (6) (x) |≤|cos(x) |
1 = M and the value ∈= 0.5 × 10−9 . These values give the optimal step
size h = (120 × 10−9 /1)1/6 = 0.070231219.
Step size Approximation by formula (12) Error using formula (12)
h = 0.1 -0.689625413 -0.007081296
h = 0.01 -0.696705958 -0.000000751
h = 0.001 -0.696690000 -0.000016709
−3f0 + 4f1 − f2
f 0 (x0 ) ≈ (forward difference)
2h
f 00 (t) ≈
2((t − x1 ) + (t − x2 ) + (t − x3 )) 2((t − x0 ) + (t − x2 ) + (t − x3 ))
f0 + f1
(x0 − x1 )(x0 − x2 )(x0 − x3 ) (x1 − x0 )(x1 − x2 )(x1 − x3 )
2((t − x0 ) + (t − x1 ) + (t − x3 )) 2((t − x0 ) + (t − x1 ) + (t − x2 ))
+f2 + f3
(x2 − x0 )(x2 − x1 )(x0 − x3 ) (x3 − x0 )(x3 − x1 )(x3 − x2 )
Start with the Lagrange interpolation polynomial for f (t) based on the
five x0 , x1 , x2, x3 , and x4 ,
(t − x1 )(t − x2 )(t − x3 )(t − x4 )
f (t) ≈ f0
(x0 − x1 )(x0 − x2 )(x0 − x3 )(x0 − x4 )
(t − x0 )(t − x2 )(t − x3 )(t − x4 )
+f1
(x1 − x0 )(x1 − x2 )(x1 − x3 )(x1 − x4 )
(t − x0 )(t − x1 )(t − x3 )(t − x4 )
+f2
(x2 − x0 )(x2 − x1 )(x2 − x3 )(x2 − x4 )
(t − x0 )(t − x1 )(t − x2 )(t − x4 )
+f3
(x3 − x0 )(x3 − x1 )(x3 − x2 )(x3 − x4 )
(t − x0 )(t − x1 )(t − x2 )(t − x3 )
+f4
(x4 − x0 )(x4 − x1 )(x4 − x2 )(x4 − x3 )
f 000 (x0 ) ≈
6((−h) + (−2h) + (−3h) + (−4h)) 6((0) + (−2h) + (−3h) + (−4h))
f0 4
+f1
24h −6h4
6((0) + (−h) + (−3h) + (−4h)) 6((0) + (−h) + (−2h) + (−4h))
+f2 4
+ f3
4h −6h4
6((0) + (−h) + (−2h) + (−3h))
+f4
24h4
−60h 54h −48h 42h −36h
= f0 4
+ f1 4 + f2 4
f3 4 + f4
24h 6h 4h 6h 24h4
−5f0 + 18f1 − 24f2 + 14f3 − 3f4
= f1 ,
2h3
and the formula is established.
Observe that the nodes tk do not need to be equally spaced for formulas
(16) through (18) to hold. Choosing the abscissas in different orders will
produce different formulas for approximating f 0 (x).
The kth partial sum on the right side if equation (24) is the derivative of
the Newton polynomial of degree k based on the first k nodes. If
forms a set of N +1 equally spaced points on the real axis, the kth partial
sum is an approximation to f 0 (t0 ) of order O(hk−1 ).
Suppose that N = 5. If the five nodes are tk = x + hk for k = 0, 1, 2, 3,
and 4, then (24) is an equivalent way to compute the forward-difference
formula for f 0 (x) of order O(h4 ). If the five nodes {tk } are chosen to be
t0 = x, t1 = x + h, t2 = x − h, t3 = x + 2h, andt4 = x − 2h, then (24) is the
central-difference formula for f 0 (x) of order O(h4 ). When the five nodes
are tk = x − kh, then (24) is the backward-difference formula for f 0 (x) of
order O(h4 ).