]>
A particle is moving through space, and you record its position as a function of time in a table. Your problem is to determine its velocity when all you have is this table of versus .
You probably did rather well in your first calculus course and feel competent at taking derivatives. However, you probably did not take derivatives of a table of numbers using the elementary definition:
In fact, even a computer runs into errors with this kind of limit because it is wrought with subtractive cancellation; the computer's finite word length causes the numerator to fluctuate between and the machine precision as the denominator approaches zero.The most direct method for numerical differentiation of a function starts by expanding it in a Taylor series. This series advances the function one small step forward:
where is the step size . This is illustrated in Fig. ?? . We obtain the forward-difference derivative algorithm by solving ( 8.2 ) for :where the subscript denotes a computed expression. You can think of this approximation as using two points to represent the function by a straight line in the interval from to .
The approximation ( 8.3 ) has an error proportional to (unless the "ultrauser" looks down kindly upon you and makes vanish). We can make the approximation error smaller and smaller by making smaller and smaller. For too small an , however, all precision will be lost through the subtractive cancellation on the LHS of ( 8.4 ), and the decreased approximation error becomes irrelevant.
![]()
|
An improved approximation to the derivative starts with the basic definition ( 8.1 ). Rather than making a single step of forward, we form a central difference by stepping forward by and backward by :
Here is the symbol for the central difference. The central-difference approximation is illustrated in Fig. ?? . When the Taylor series for are substituted into ( 8.8 ), we obtain The important difference from ( 8.3 ) is that when is subtracted from , all terms containing an odd power of in the Taylor series cancel. Therefore, the central-difference algorithm becomes accurate to one order higher in ; that is, . If the function is well behaved; that is, if , then you can expect the error with the central-difference method to be smaller than with the forward difference ( 8.4 ). If we now return to our polynomial example ( 8.5 ), we find that for a parabola, the central difference gives the exact answer regardless of the size of :Because the Taylor series provides an analytic expression for the error, we can be even more clever. While the central difference ( 8.8 ) makes the error term proportional to vanish, we can also make the term proportional to vanish by algebraically extrapolating from relatively large (and, because of this, small roundoff error) to :
We introduce the additional information by forming the central difference with step size :We now eliminate the quadratic error term as well as the linear error term in ( 8.9 ) with the combination
If , there is only one place of roundoff error, and if , the truncation error in ( 8.15 ) is now the same size as machine precision , which is the best you can hope for.
A good way of computing ( 8.14 ) is to group the terms as
The advantage to ( 8.16 ) is that it reduces the loss of precision that occurs when large and small numbers are added together, only to be subtracted from other large numbers; it is better to first subtract the large numbers from each other and then add the difference to the small numbers. When working with these and similar higher-order methods, it is important to remember that while they may work very well for well-behaved functions, they may fail badly for computed or measured functions containing noise. In these difficult cases it may be better to first fit the data with some analytic function using the techniques of Chapter ?? , Data Fitting , and then differentiate the fit.But regardless of the algorithm, you may remember that evaluating the derivative of at requires you to know the values of surrounding . We shall use this same idea in Chapter ?? , Differential Equations and Oscillations .
The approximation errors in numerical differentiation decrease with decreasing step size while roundoff errors increase with a smaller step size (you have to take more steps and do more calculations). We know from our discussion in Chapter ?? , Errors and Uncertainties in Computations , that the best approximation occurs for an that makes the total error a minimum. As a rough guide, this occurs when .
Because differentiation subtracts two numbers very close in value, the limit of roundoff error is essentially machine precision:
The approximation error with the forward-difference algorithm ( 8.4 ) is an term, while that with the central-difference algorithm ( 8.9 ) is an term;
The value for which roundoff and approximation errors are equal is therefore
We take (which is crude, although not really so crude for or ), and assume a single precision calculation . In this case we obtain
This may seem backward because the better algorithm leads to a larger value. It is not. The ability to use a large means that the error in the central-difference method is some 20 times smaller than the error in the forward-difference method. For the forward-difference algorithm, approximately half of the machine precision is lost.
Let's say that you have measured the position versus time for a particle. Your problem is to determine the force on the particle.
Newton's second law tells us that the force and acceleration are linearly related:
where is the force, is the particle's mass, and is the acceleration. So if we can determine the acceleration from the table, we can determine the force.The concern about errors we expressed for first derivatives is even more of a concern for second derivatives because the additional subtractions lead to more cancellations. Let's go back to the central-difference method:
This algorithm gives the derivative at by moving forward and backward from by . The second derivative is taken to be the central difference of the first derivative:As was true for first derivatives, by evaluating a function in the region surrounding , it is possible to determine the second derivative at . But some care is required to preserve the grouping in ( 8.29 ) and not convert it to the neater form:
This latter form increases subtractive cancellation because the computer first stores the "large" number [well, if does not change sign, then the sum is larger than either or ] only to subtract another large number from it.Write a program to calculate the second derivative of using the central-difference algorithm ( 8.29 ). Test it over four cycles. Start with and keep reducing until you reach machine precision.