articleAINumerics
Finite differences and spectral methods
Two ways to differentiate a function you only know at grid points. A central difference uses the two neighbors; a spectral derivative uses every point. On a smooth periodic function the spectral one reaches rounding error with a few dozen points. Put a jump in the function and, away from the jump, the central difference is the accurate one.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Take a periodic function sampled at equally spaced points, with , and ask for its derivative at those points. The central difference looks at two neighbors. Its error is , so doubling divides it by 4. The spectral derivative fits the trigonometric polynomial through all samples and differentiates that exactly: transform, multiply mode by , transform back. Every sample contributes to every derivative value.
A smooth function
For , which is smooth and periodic, the Fourier coefficients fall off faster than any power of , and so does the spectral derivative's error. The two lines for in the figure show it: the spectral error reaches rounding by 32 points, while the central difference is still losing a factor of 4 per doubling.
A jump
Now add a unit step to , so that the function jumps at and . Next to a jump neither method is right, and the largest error over all points grows with for both. Away from the jumps they behave differently. The central difference at a point or farther from a jump only sees , and its error falls like as before. The spectral derivative at that point still includes the jump: in the spectral differentiation matrix the weight of a sample at distance is in size, and that does not shrink as grows. The two lines marked far points show it: the central difference's error keeps falling, the spectral one stays near .
What the two claims cover
The two claims, claim spectral-derivative ✓ checked and claim central-difference-near-a-jump ✓ checked, cover one smooth function and one function with jumps, both periodic, on equally spaced points. Functions on an interval that are not periodic, which spectral methods handle with Chebyshev points, and the cost of each method per time step, are not tested here.
Printed
Tests 3 of 3 pass
- the spectral error on exp(sin x) is below 1e-12 by N = 32 (passes)
- the central difference error falls by 3.9 to 4.1 per doubling from N = 32 on both functions (passes)
- the spectral error at the points far from the jumps stays above 0.7 (passes)