Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Big Idea

Derivatives can be approximated using function values at discrete points. Taylor’s theorem tells us exactly how accurate these approximations are and reveals the fundamental trade-off between truncation error and round-off error.

The Forward Difference Formula

Suppose we have a function f(x)f(x) and we want to compute its derivative at a point x0x_0 (i.e., the slope of the tangent line to the curve y=f(x)y = f(x) at x0x_0).

Recall the limit definition of the derivative:

f(x0)=limh0f(x0+h)f(x0)hf'(x_0) = \lim_{h\to 0} \frac{f(x_0 + h) - f(x_0)}{h}

The left-hand side is the slope of the tangent line, while the right-hand side is the limit of the slope of the secant line connecting the points (x0,f(x0))(x_0, f(x_0)) and (x0+h,f(x0+h))(x_0 + h, f(x_0 + h)).

This suggests we can approximate the derivative using:

f(x0)f(x0+h)f(x0)hf'(x_0) \approx \frac{f(x_0 + h) - f(x_0)}{h}

for hh “small”. A major question is: how accurate is this approximation?

Error Analysis

Proposition 1 (Forward Difference Error)

The forward difference approximation satisfies:

f(x0+h)f(x0)h=f(x0)+E(h)\frac{f(x_0 + h) - f(x_0)}{h} = f'(x_0) + E(h)

where the error is:

E(h)=h2f(ξ)=O(h)E(h) = -\frac{h}{2} f''(\xi) = \mathcal{O}(h)

for some ξ(x0,x0+h)\xi \in (x_0, x_0 + h). The error is first-order in hh.

Proof 1

Using Taylor’s theorem with k=1k = 1:

f(x0+h)=f(x0)+f(x0)h+f(ξ)2h2f(x_0 + h) = f(x_0) + f'(x_0) h + \frac{f''(\xi)}{2} h^2

where ξ(x0,x0+h)\xi \in (x_0, x_0 + h).

Substituting into our secant line approximation:

f(x0+h)f(x0)h=1h(f(x0)+hf(x0)+12h2f(ξ)f(x0))=f(x0)+12hf(ξ)\begin{split} \frac{f(x_0 + h) - f(x_0)}{h} &= \frac{1}{h}\left( f(x_0) + h f'(x_0) + \frac{1}{2} h^2 f''(\xi) - f(x_0) \right) \\ &= f'(x_0) + \frac{1}{2} h f''(\xi) \end{split}

Remark 1 (Computational Verification)

To verify the error theory: if E(h)=ChE(h) = Ch, then logE=logC+logh\log E = \log C + \log h.

Plot logE\log E vs. logh\log h — you should see a straight line with slope 1.

The absolute error of the forward difference approximation of the derivative for \sin(x_0) at x_0 = 1.2.

The absolute error of the forward difference approximation of the derivative for sin(x0)\sin(x_0) at x0=1.2x_0 = 1.2.

The Round-off Error Trade-off

The plot above shows something surprising: the error increases for very small hh!

This is our first encounter with a fundamental phenomenon in scientific computing: round-off error. When hh becomes very small, we’re subtracting two nearly equal numbers (f(x0+h)f(x0)f(x_0 + h) \approx f(x_0)), and then dividing by a tiny hh. This amplifies the small errors inherent in floating-point arithmetic.

Tip

In the next chapter on floating-point arithmetic, we’ll learn:

  • Why computers can’t represent most real numbers exactly

  • What machine epsilon is and why it’s approximately 10-16 for double precision

  • How to find the optimal step size that balances truncation and round-off error

For now, the practical takeaway is: smaller hh is not always better. For first-order finite differences, h108h \approx 10^{-8} is often optimal.

The Central Difference Formula

The forward difference only uses information to the right of x0x_0. From geometric intuition, using points on both sides should give a better approximation to the tangent line—the secant line through (x0h,f(x0h))(x_0 - h, f(x_0 - h)) and (x0+h,f(x0+h))(x_0 + h, f(x_0 + h)) is more symmetric.

Proposition 2 (Central Difference Approximation)

The central difference approximation:

f(x0)f(x0+h)f(x0h)2hf'(x_0) \approx \frac{f(x_0 + h) - f(x_0 - h)}{2h}

has error E(h)=O(h2)E(h) = \mathcal{O}(h^2)second-order in hh. Squaring hh squares the error!

Proof 2

We expand both f(x0+h)f(x_0 + h) and f(x0h)f(x_0 - h) using Taylor series:

f(x0±h)=f(x0)±hf(x0)+h22f(x0)±h36f(ξ±)+O(h4)f(x_0 \pm h) = f(x_0) \pm h f'(x_0) + \frac{h^2}{2}f''(x_0) \pm \frac{h^3}{6} f'''(\xi^{\pm}) + O(h^4)

Substituting into the central difference formula:

f(x0+h)f(x0h)2h=12h(2hf(x0)+h36(f(ξ+)+f(ξ))+O(h5))=f(x0)+h212(f(ξ)+f(ξ+))+O(h4)\begin{split} \frac{f(x_0 + h) - f(x_0 - h)}{2h} &= \frac{1}{2h}\left( 2hf'(x_0) + \frac{h^3}{6}\left( f'''(\xi^+) + f'''(\xi^-) \right) + O(h^5)\right) \\ &= f'(x_0) + \frac{h^2}{12} \left( f'''(\xi^-) + f'''(\xi^+) \right) + O(h^4) \end{split}

Why the improvement? The even-powered terms (h2,h4,h^2, h^4, \ldots) cancel due to symmetry.

Central finite difference error for approximating \tan'(0.2). The error follows O(h^2) (red line) until roundoff error dominates at h \approx 10^{-6}.

Central finite difference error for approximating tan(0.2)\tan'(0.2). The error follows O(h2)O(h^2) (red line) until roundoff error dominates at h106h \approx 10^{-6}.

Comparison: Forward vs Central

MethodFormulaErrorOrder
Forwardf(x0+h)f(x0)h\frac{f(x_0 + h) - f(x_0)}{h}O(h)O(h)1st
Centralf(x0+h)f(x0h)2h\frac{f(x_0 + h) - f(x_0 - h)}{2h}O(h2)O(h^2)2nd

Example 1 (Accuracy Comparison)

For h=0.01h = 0.01:

  • Forward difference error: 0.01\sim 0.01

  • Central difference error: 0.0001\sim 0.0001

The central difference is 100× more accurate for the same step size!

Remark 2 (Optimal Step Size for Central Differences)

The total error (truncation + roundoff) is:

E(h)=M0μh+M1h2E(h) = \frac{M_0 \mu}{h} + M_1 h^2

Minimizing gives hoptμ1/36×106h_{\text{opt}} \approx \mu^{1/3} \approx 6 \times 10^{-6} for double precision.

This is larger than for forward differences because truncation error decreases faster (h2h^2 vs hh).

Second Derivative Approximation

Proposition 3 (Second Derivative Formula)

The central difference approximation for the second derivative:

f(x0)f(x0+h)2f(x0)+f(x0h)h2f''(x_0) \approx \frac{f(x_0 + h) - 2f(x_0) + f(x_0 - h)}{h^2}

has error O(h2)O(h^2).

Proof 3

Adding the Taylor expansions (instead of subtracting):

f(x0+h)+f(x0h)=2f(x0)+h2f(x0)+O(h4)f(x_0 + h) + f(x_0 - h) = 2f(x_0) + h^2 f''(x_0) + O(h^4)

Solving for f(x0)f''(x_0) gives the formula above.

This formula is fundamental in numerical PDEs—it’s the discrete Laplacian in one dimension.

Summary

MethodFormulaError Order
Forward differencef(x+h)f(x)h\frac{f(x+h) - f(x)}{h}O(h)O(h)
Central differencef(x+h)f(xh)2h\frac{f(x+h) - f(x-h)}{2h}O(h2)O(h^2)
Second derivativef(x+h)2f(x)+f(xh)h2\frac{f(x+h) - 2f(x) + f(x-h)}{h^2}O(h2)O(h^2)

Key principle: Using symmetric stencils improves accuracy, but there are diminishing returns due to round-off error. The optimal step size balances truncation and round-off errors.