Hexa Scholar

Finite differences on a grid

A gold curve on a dark base, with a long dark ruler held above it and a tiny ruler half buried among gold grains as large as itself.
A long ruler that misses the bend, and a tiny one lost among the grains.

The question

A computer estimates the slope of √x at 2 as (f(2 + h) − f(2))/h in double precision. Which step h is most accurate?

Pick a step and lock it in; then the page computes every step.

  1. 1 SeeLock in the step you think is best.
  2. 2 TryPredict the best step for other formulas.
  3. 3 ProveBuild the error bound, line by line.
  4. 4 ApplyProblems with tables, steps and bond prices.
Before you start · 3 quick checks on derivatives, rounding and powers of two, about 3 min

Two ideas before you start

2 ideas

A difference quotient is a secant slope

Figure (needs JavaScript): √x near 2, its tangent at 2 (dotted) and the secant from 2 to 2 + h (dashed).

Step h

h = 2: (2 − √2)/2 ≈ 0.293 f′(2) = √2/4 ≈ 0.354

Here f(x) = √x. The forward difference (f(2 + h) − f(2))/h is the slope of the secant from 2 to 2 + h. In exact arithmetic it tends to the tangent slope f′(2) as h shrinks.

Exact definition

Forward difference. Let \(f\) be defined on an interval containing \(x\) and \(x + h\), with \(h \gt 0\). The forward difference quotient is

\[D_hf(x) = \frac{f(x + h) - f(x)}{h}.\]

If \(f\) is differentiable at \(x\), then \(D_hf(x) \to f'(x)\) as \(h \to 0\). The difference \(D_hf(x) - f'(x)\), computed with exact numbers, is the truncation error.

Neighbors on a grid

Figure (needs JavaScript): √x with grid points 1.5, 2 and 2.5, and a dashed secant through the points a formula uses.

Formula

Forward: (f₁ − f₀)/h

On a grid only the values at the grid points are known, and a difference formula combines neighbors. This card draws h = 0.5 so the points are easy to see.

Exact definition

Grid. Fix a point \(x_0\) and a spacing \(h \gt 0\). The grid points are \(x_j = x_0 + jh\) for whole numbers \(j\), and \(f_j = f(x_j)\).

Difference formulas at \(x_0\). Forward \((f_1 - f_0)/h\), backward \((f_0 - f_{-1})/h\) and central \((f_1 - f_{-1})/(2h)\) estimate \(f'(x_0)\). The second difference

\[\frac{f_1 - 2f_0 + f_{-1}}{h^2}\]

estimates \(f''(x_0)\).

Fine print: names, scope and notation

The spacing \(h\) is also called the step. The page treats one variable and an evenly spaced grid.

A tilde marks a computed number: \(\tilde f(t)\) is the stored value of \(f(t)\), and \(\tilde D\) the computed quotient. Rounding of stored values is the subject of floating point; how fast a Taylor error shrinks, of Taylor remainders.

1 See

Lock in the most accurate step

The target is \(f'(2) = \sqrt{2}/4\) for \(f(x) = \sqrt{x}\). The computer forms \(\tilde D = (\tilde f(2 + h) - \tilde f(2))/h\), where \(\tilde f(t)\) is the value of \(\sqrt{t}\) stored in double precision. Its error is \(|\tilde D - f'(2)|\).

2 Try

Other difference formulas

The explorer, its goals and one more question open after you answer the prediction.

3 Prove

The error bound and the best step

Theorem

Assumptions. \(f\) is twice continuously differentiable and positive on \(J = [x, x + \tfrac{1}{2}]\), and \(f''(t) \ne 0\) for every \(t\) in \(J\). Let \(M_0\) be the largest value of \(f\) on \(J\) and \(M_2\) the largest value of \(|f''|\) on \(J\). Take a step \(h\) with \(0 \lt h \le \tfrac{1}{2}\) and a number \(u \gt 0\). The computer returns \(\tilde f(x) = f(x)(1 + \delta_0)\) and \(\tilde f(x + h) = f(x + h)(1 + \delta_1)\) with \(|\delta_0| \le u\) and \(|\delta_1| \le u\), and forms \(\tilde D = (\tilde f(x + h) - \tilde f(x))/h\) with no further rounding.

Conclusion. Write \(s = f(x) + f(x + h)\). There is a \(\xi\) in \((x, x + h)\) such that, over all such \(\delta_0\) and \(\delta_1\), the largest possible error is

\[\begin{aligned} &\max_{\delta_0, \delta_1}|\tilde D - f'(x)| \\ &= \tfrac{1}{2}h|f''(\xi)| + \frac{us}{h}. \end{aligned}\]

So every rounding gives \(|\tilde D - f'(x)| \le g(h)\), where \(g(h) = \tfrac{1}{2}M_2h + 2uM_0/h\). The function \(g\) on \(h \gt 0\) is smallest at \(h^* = 2\sqrt{uM_0/M_2}\), where its two terms are equal and \(g(h^*) = 2\sqrt{uM_0M_2}\).

Why \(f \gt 0\) and \(f'' \ne 0\). The result does not need them; they keep the proof short (\(s\) is a plain sum, the truncation part is never 0) and make each wrong option below false in every allowed case. For any twice continuously differentiable \(f\) the same argument gives the largest error \(\tfrac{1}{2}h|f''(\xi)| + u\bar s/h\), where \(\bar s = |f(x)| + |f(x + h)|\), so \(|\tilde D - f'(x)| \le \tfrac{1}{2}M_2h + u\bar s/h\) with \(M_2\) the largest value of \(|f''|\) on \([x, x + h]\).

On this page. For \(\sqrt{x}\) at 2 in binary64 the assumptions hold with \(u = 2^{-53}\): \(\sqrt{x} \gt 0\), \(f''(t) = -\tfrac{1}{4}t^{-3/2} \ne 0\), and only the stored square roots are rounded (stage 1, How the page computes).

Facts the proof uses.

  • Taylor's theorem with the Lagrange remainder, from the lesson on Taylor polynomials: if \(f\) is twice continuously differentiable on \([x, x + h]\), there is a \(\xi\) in \((x, x + h)\) with \[\begin{aligned} f(x + h) &= f(x) + hf'(x) \\ &\quad + \tfrac{1}{2}h^2f''(\xi); \end{aligned}\]
  • a function \(a\delta_0 + b\delta_1\) of two numbers \(\delta_0, \delta_1\) in \([-u, u]\) takes every value between its smallest and largest, and those occur at corners of the square;
  • where \(g'\) is negative \(g\) falls, and where it is positive \(g\) rises.

Fix \(f\), \(x\), \(h\) and \(u\) as in the theorem, and write \(D = (f(x + h) - f(x))/h\) for the exact quotient: \(D - f'(x)\) is the truncation part and \(\tilde D - D\) the rounding part.

Each option is one step about the fixed \(f\), \(x\), \(h\) and \(u\). Pick the one that is true for every \(f\), \(x\), \(h\) and \(u\) the theorem allows.

4 Apply

Problems

Each problem has hints and a full solution. Exact answers can be typed as a decimal or a fraction.

Key point A finite difference replaces a derivative by a combination of grid values. Its truncation error falls like a power of \(h\) that the formula decides, and combining two steps can cancel the leading term. Rounding in the values is divided by a power of \(h\), so with rounded data, as in P6, the step must not be too small.
Level Intro Prerequisites Taylor remainders and floating point; derivatives (refreshed in Before you start) Time ~35 min

Related: numerical integration (error also shrinks like a power of the spacing); Newton's method (a finite difference can stand in for its derivative).

Further reading