Finite differences on a grid
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 SeeLock in the step you think is best.
- 2 TryPredict the best step for other formulas.
- 3 ProveBuild the error bound, line by line.
- 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 ideasA 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).
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.
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.
Lock in the most accurate step
- Choose k with the slider. The step is h = 2⁻ᵏ, from 2⁻¹ = 0.5 for k = 1 down to 2⁻⁵¹ for k = 51.
- Press Lock in my step. The page then computes the quotient for all 51 steps and plots the errors.
- Then reach three goals by moving the slider again.
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)|\).
How the page computes
The page computes as a program does in binary64 (IEEE 754 double precision). Each stored value \(\tilde f(t)\) is \(\sqrt{t}\) rounded to the nearest double, so \(\tilde f(t) = \sqrt{t}\,(1 + \delta)\) with \(|\delta| \le u\), where \(u = 2^{-53} \approx 1.11 \times 10^{-16}\).
The steps are powers of two, so \(2 + h\) and \(2 - h\) are stored exactly for every \(k\) up to 51. Two doubles within a factor of 2 of each other subtract exactly (Sterbenz's lemma), and dividing by a power of two is exact. So the only rounding is in the stored square roots.
The exact values \(f'(2)\) and \(D = (\sqrt{2 + h} - \sqrt{2})/h\) are computed with 80 decimal digits. The error \(\tilde D - f'(2)\) is the truncation part \(D - f'(2)\) plus the rounding part \(\tilde D - D\).
Three goals
Other difference formulas
The explorer, its goals and one more question open after you answer the prediction.
Explorer: forward and central
Both curves are drawn; the buttons choose the difference for the readout and goals.
Goals
One more formula
The error bound and the best step
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.
Problems
Each problem has hints and a full solution. Exact answers can be typed as a decimal or a fraction.
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
- R. L. Burden and J. D. Faires, Numerical Analysis, chapter 4, “Numerical Differentiation and Integration.”
- N. J. Higham, Accuracy and Stability of Numerical Algorithms (SIAM, 2002), chapter 1, on rounding errors and cancellation.
- R. J. LeVeque, Finite Difference Methods for Ordinary and Partial Differential Equations (SIAM, 2007), chapter 1, “Finite Difference Approximations.”