This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · posts/finite-differences-and-spectral-methods

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.

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.

[claim: spectral-derivative]

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 .

[claim: central-difference-near-a-jump]

What the two claims cover

The two claims, [claim: spectral-derivative] and [claim: central-difference-near-a-jump], 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.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 52–190