anthropocene
Computational Physics / Derivatives Without Calculus
◆ core25 min

Differentiating a function you can only sample

Taylor gives you the formula and the error term in one line. Then floating point takes the error term back, and the result is the most important curve in the subject.

assumes you already have

The definition of a derivative is a limit:

A machine cannot take a limit. It can only pick some small and evaluate the quotient. The entire question is: which , and how wrong are you?

Where the error term comes from

Taylor expand about :

Rearranging for :

So the forward difference is off by — an error proportional to . Halve the step, halve the error. That is what first order means, and the constant is not mysterious: it is literally half the second derivative.

Now do the same thing with a step in each direction and subtract:

Every even-powered term cancels by symmetry — including the term that was the leading error before. Divide through:

The central difference is second order, for the same two function evaluations. That is not a small improvement. At it is roughly ten thousand times more accurate, at identical cost.

predictcommit first

Both schemes cost two evaluations of f. If you shrink h by a factor of 10, what happens to each error?

The curve that breaks the intuition

Watch what the error actually does as shrinks over fifteen orders of magnitude.

d/dx of sin(x) at x = 1
h* for O(h^1)h* for O(h^2)h* for O(h^4)10⁻¹⁶10⁻¹⁴10⁻¹²10⁻¹⁰10⁻⁸10⁻⁶10⁻⁴10⁻²10⁻¹10⁻¹⁴10⁻¹²10⁻¹⁰10⁻⁸10⁻⁶10⁻⁴10⁻²10⁰STEP SIZE H|ERROR|
Forward — O(h^1)Central — O(h^2)Five-point — O(h^4)
schemes
Forward · best h4.6e-9 → 1.9e-9
Central · best h3.2e-6 → 6.0e-13
Five-point · best h0.00 → 1.4e-14
Error against step size, log-log. Left branch: truncation error, falling as h^p exactly as Taylor promises. Right branch: roundoff, rising as ε/h. The amber rules mark the theoretically optimal step for each order.

The left half is the theory working perfectly — straight lines whose slopes are the orders we just derived, 1 and 2 and 4.

Then every curve turns around.

The reason is the previous lesson. The numerator is a difference of nearly equal numbers, and as they become more nearly equal. Each is known to a relative precision of , so the absolute error in the numerator is about no matter how small gets. Dividing by then amplifies it:

Total error is the sum of the two effects, and they pull opposite ways:

Differentiate, set to zero, and you get the optimal step and the best error you can possibly achieve:

For the forward difference () that is and an error around you can never get more than half your digits back, no matter how carefully you choose . For the central difference (), with error near .

tunefind the threshold

Find the step size where the forward difference is at its most accurate. You are looking for the bottom of the valley, not the left edge of the plot.

-16-14-12-10-8-6-4-2-8-6-4-20LOG₁₀ HLOG₁₀ |ERROR|
forward differenceyour h
h1.00e-2
|error|4.216e-3
recallscheduled for review
Why does making h smaller eventually make a finite-difference derivative worse?

What to carry forward

  • Order of accuracy is a slope on a log-log plot, and you should always measure it rather than trust it. A method claiming fourth order that measures 2.1 has a bug.
  • Symmetry buys accuracy for free. The central difference costs the same as the forward difference and is an order better. That idea reappears constantly.
  • Smaller is not always better. Every method with a subtraction in it has a floor.

Next: the same machinery, but instead of estimating one derivative, we use it to follow a trajectory forward in time.