A sensor records the height of a drone every half second. Nobody hands you a formula, yet you want its velocity. The derivative is a limit of difference quotients, so the natural move is to stop before the limit and use a difference quotient with a small but fixed step. That simple idea works well, and it fails in two interesting ways, one from stopping early and one from the computer’s rounding.
Key terms
Forward, backward and central differences
Definition Let be defined on an open interval containing and let be a step size small enough that and lie in that interval. The forward, backward and central difference approximations to are
If is differentiable at , each of them tends to as . The central difference is the average of the other two.
In plain words These are slopes of secant lines. The forward one looks ahead, the backward one looks behind, and the central one looks both ways from the point.
Example Take at , where , and use .
| Approximation | Value | Error |
|---|---|---|
The central difference is about thirty times more accurate here at the same cost of two function values.
Truncation error of a difference formula
Definition The truncation error of a difference formula is the difference between the formula, computed in exact arithmetic, and . Taylor’s theorem gives it. If is continuous on , then for some in
In the same way, if is continuous on , then for some in
If is continuous on , then for some in
So the one-sided formulas are first order, with error proportional to , and the central formula is second order, with error proportional to .
In plain words Halving roughly halves the error of a one-sided difference but cuts the error of a central difference by about four.
Example For at every derivative is close to near , so the predicted errors are about and . With these are and , matching the table above. With the actual errors are and , again as predicted.
Cancellation
Definition Subtractive cancellation is the loss of significant digits when two nearly equal numbers, each known only to a fixed number of significant digits, are subtracted. If and agree in their first significant digits, the computed has about fewer correct significant digits than and had.
In plain words The digits the two numbers share cancel out, and only the less reliable trailing digits are left.
Example Work with 8 significant digits. Then is stored as and as . Their difference has only 5 significant digits. Dividing by gives , and the three lost digits can never be recovered. A smaller makes and agree in even more digits, so the loss gets worse.
Roundoff in difference quotients
Definition Suppose each computed value of has a relative error of at most , about in double precision, and near . Then the computed numerator of can be off by about , so the computed quotient carries a roundoff error of up to about
For the central difference the numerator is divided by , so the corresponding term is about .
In plain words Rounding errors in are tiny, but the formula divides them by . A small makes them large.
Example For at with , the roundoff term is about
The computed forward difference is in fact off by about . That is worse than its error of at the much larger step , and more than ten thousand times worse than its error of at .
Choosing the step size
Definition For the forward difference, with near , the total error is bounded by about
This is smallest at
which is of order when and are of moderate size. For the central difference, with near , the bound is smallest at
which is of order .
In plain words Truncation wants a small step, roundoff wants a big one. The best step sits in between, and below it the answers get worse, not better.
Example Here are the sizes of the actual errors of both formulas for at , computed in double precision.
| Forward error | Central error | |
|---|---|---|
The forward difference is best near and the central one near , just as the formulas for predict.
Derivatives from sampled data
Definition Suppose we know only the values at equally spaced points for . At an interior point the derivative is estimated by the central difference, and at the first and last points by a forward and a backward difference,
The second derivative at an interior point is estimated by the second central difference
If is continuous on , this estimate minus equals for some in . Measurement errors in the data are divided by or , so they act like roundoff.
In plain words With a table of numbers, use the neighbors on both sides when you have them and a one-sided difference at the ends.
Example A drone’s height in meters is recorded every half second.
| height |
At the velocity is about meters per second. At the forward difference gives , and at the backward difference gives . The acceleration at is about
meters per second squared, so the drone is slowing its climb.
Check yourself
- Compute the central difference for at with , and compare it with the error formula.
Show the answer
The central difference is
The exact derivative is , so the error is . The formula gives , exact here because is constant.
- A forward difference gives an error of with . Roughly what error do you expect with , assuming roundoff is still negligible?
Show the answer
The forward difference is first order, so halving roughly halves the error, giving about .
- Why is a bad step for a forward difference in double precision?
Show the answer
The roundoff term is about
so the result can be wrong in the first decimal place. The best step is near .