Discretizing: FD Stencils
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
the weights over . 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 over , for a more accurate estimate at the price of a wider reach.
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 ) and the second-order error falls roughly with : 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.