Computational Physics - Department of Physics

(Axel Boer) #1

60 3 Numerical differentiation and interpolation


most likely the point where roundoff errors take over. If we had used single precision, we
would geth≈ 10 −^2. Due to the subtractive cancellation in the expression forf′′there is a
pronounced detoriation in accuracy ashis made smaller and smaller.
It is instructive in this analysis to rewrite the numerator of the computed derivative as


(fh−f 0 )+ (f−h−f 0 ) = (exp(x+h)−expx)+ (exp(x−h)−expx),

as
(fh−f 0 )+ (f−h−f 0 ) =exp(x)(exp(h)+exp(−h)− 2 ),


since it is the difference(exp(h)+exp(−h)− 2 )which causes the loss of precision. The results,
still forx= 10 are shown in the Table 3.2. We note from this table that ath≈× 10 −^8 we have


h exp(h) +exp(−h) exp(h) +exp(−h)− 2
10 −^1 2.0100083361116070 1.0008336111607230× 10 −^2
10 −^2 2.0001000008333358 1.0000083333605581× 10 −^4
10 −^3 2.0000010000000836 1.0000000834065048× 10 −^6
10 −^4 2.0000000099999999 1.0000000050247593× 10 −^8
10 −^5 2.0000000001000000 9.9999897251734637× 10 −^11
10 −^6 2.0000000000010001 9.9997787827987850× 10 −^13
10 −^7 2.0000000000000098 9.9920072216264089× 10 −^15
10 −^8 2.0000000000000000 0.0000000000000000× 100
10 −^9 2.0000000000000000 1.1102230246251565× 10 −^16
10 −^10 2.0000000000000000 0.0000000000000000× 100

Table 3.2Result for the numerically calculated numerator of the second derivative as function of the step
sizeh. The calculations have been made with double precision.


essentially lost all leading digits.
From Fig. 3.2 we can read off the slope of the curve and therebydetermine empirically
how truncation errors and roundoff errors propagate. We sawthat for− 4 <log 10 (h)<− 2 ,
we could extract a slope close to 2 , in agreement with the mathematical expression for the
truncation error.
We can repeat this for− 10 <log 10 (h)<− 4 and extract a slope which is approximately equal
to− 2. This agrees again with our simple expression in Eq. (3.6).


3.2 Numerical Interpolation and Extrapolation


Numerical interpolation and extrapolation are frequentlyused tools in numerical applications
to physics. The often encountered situation is that of a functionfat a set of pointsx 1 ...xn
where an analytic form is missing. The functionfmay represent some data points from ex-
periment or the result of a lengthy large-scale computationof some physical quantity that
cannot be cast into a simple analytical form.
We may then need to evaluate the functionfat some pointxwithin the data setx 1 ...xn,
but wherexdiffers from the tabulated values. In this case we are dealing with interpolation.
Ifxis outside we are left with the more troublesome problem of numerical extrapolation.
Below we will concentrate on two methods for interpolation and extrapolation, namely poly-
nomial interpolation and extrapolation. The cubic spline interpolation approach is discussed
in chapter 6.

Free download pdf