3.1 Numerical Differentiation 59
Relative error
log 10 (h)
ε
-14 -12 -10 -8 -6 -4 -2 0
6
4
2
0
-2
-4
-6
-8
-10
Fig. 3.2Log-log plot of the relative error of the second derivative ofexp(x)as function of decreasing step
lengthsh. The second derivative was computed forx= 10 in the program discussed above. See text for further
details
εapprox≈
f 0 (^4 )
12
h^2.
If we were not to worry about loss of precision, we could in principle makehas small as
possible. However, due to the computed expression in the above program example
f 0 ′′=
fh− 2 f 0 +f−h
h^2 =
(fh−f 0 )+ (f−h−f 0 )
h^2 ,
we reach fairly quickly a limit for where loss of precision due to the subtraction of two nearly
equal numbers becomes crucial. If(f±h−f 0 )are very close, we have(f±h−f 0 )≈εM, where
|εM|≤ 10 −^7 for single and|εM|≤ 10 −^15 for double precision, respectively.
We have then
∣∣
f 0 ′′
∣∣
=
∣∣
∣∣(fh−f^0 )+ (f−h−f^0 )
h^2
∣∣
∣∣≤^2 εM
h^2
.
Our total error becomes
|εtot|≤
2 εM
h^2
+
f 0 (^4 )
12
h^2. (3.6)
It is then natural to ask which value ofhyields the smallest total error. Taking the derivative
of|εtot|with respect tohresults in
h=
(
24 εM
f 0 (^4 )
) 1 / 4
.
With double precision andx= 10 we obtain
h≈ 10 −^4.
Beyond this value, it is essentially the loss of numerical precision which takes over. We note
also that the above qualitative argument agrees seemingly well with the results plotted in Fig.
3.2 and Table 3.1. The turning point for the relative error atapproximatelyh≈ 10 −^4 reflects