Discretizing: FD Stencils

Part 4, Part 4: The Acoustic Wave Equation

Learning objectives

  • Approximate a second derivative as a weighted sum of grid samples
  • Write the second-order and fourth-order stencils
  • See the error fall as the grid refines or the order rises
  • Recognise the accuracy-versus-cost tradeoff of the stencil

Derivatives from Samples

The wave equation asks for a second derivative, but a computer holds only a grid of samples. A finite-difference stencil bridges the gap: it estimates the derivative at a point as a fixed weighted sum of that point and its neighbours. For the second derivative the second-order stencil is

dfracpartial2upartialx2approxdfracui12ui+ui+1h2,\dfrac{\partial^2 u}{\partial x^2} \approx \dfrac{u_{i-1} - 2u_i + u_{i+1}}{h^2},i12ui+ui+1h2,

the weights (1,2,1)(1, -2, 1) over h2h^2. It is nothing more than the curvature: how far the middle sample sits below the average of its neighbours. The fourth-order stencil uses two neighbours on each side, with weights (tfrac112,tfrac43,tfrac52,tfrac43,tfrac112)(-\tfrac{1}{12}, \tfrac{4}{3}, -\tfrac{5}{2}, \tfrac{4}{3}, -\tfrac{1}{12}) over h2h^2, for a more accurate estimate at the price of a wider reach.

Discretizing: the finite-difference stencilthe sampled bumpexact (dashed) vs stencilA stencil is a weighted sum of neighbours: (1, -2, 1) over h squared. Finer grid, smaller error.

Accuracy Versus Cost

Sample the smooth bump and watch the stencil track the exact second derivative. Two levers control the error. Refine the grid (more points, smaller hh) and the second-order error falls roughly with h2h^2: halve the spacing and the error drops about fourfold. Raise the order and the estimate tightens dramatically at the same spacing, because the wider stencil cancels more of the truncation error, though it now needs two cells on each side and a little more work per point.

That is the entire numerical bargain of finite differences, and the engine you already have makes exactly this choice: a second-order scheme is cheap but leaves visible error on coarse grids, a fourth-order scheme is cleaner. The error that survives has a name and a character all its own, numerical dispersion, and it is the star of this part. Combine this spatial stencil with the same idea in time and you can march the wave equation forward, which is what the engine does.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.