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.

Spectral Methods

Open in Colab

A finite difference formula approximates a derivative from a few neighboring values, and its error is O(hp)O(h^p) for some fixed order pp.
A spectral method instead fits one global function through all the grid values using Fourier transform, and differentiates that function exactly.
For smooth periodic functions the error then decreases faster than any power of hh.
The fast Fourier transform makes every operation cost O(Nlog⁡N)O(N \log N).

Fourier series on a grid

Take NN equally spaced points xj=jL/Nx_j = jL/N, j=0,…,N−1j = 0, \dots, N-1, on a periodic interval [0,L)[0, L). Recall that any vector of values uju_j can be written exactly as a combination of NN complex exponentials,

uj=1N∑ku^k eikxj,u_j = \frac{1}{N} \sum_{k} \hat u_k\, e^{i k x_j},

where u^=\hat u = fft(u) and the wavenumbers kk are integer multiples of 2π/L2\pi/L. The FFT returns the coefficients in the order 0,1,…,N/2−1,−N/2,…,−10, 1, \dots, N/2 - 1, -N/2, \dots, -1 (times 2π/L2\pi/L), and fftfreq produces the wavenumbers in the same order.

Since

ddxeikx=ik eikx,\frac{d}{dx} e^{ikx} = ik\, e^{ikx},

differentiating takes three steps: transform, multiply coefficient kk by ikik, transform back.

One detail needs care. For even NN, the value for the mode k=−N/2k = -N/2 is used by both eiNx/2e^{iNx/2} and e−iNx/2e^{-iNx/2}, so the actual interpolant is cos⁡(Nx/2)\cos(Nx/2), whose first derivative vanishes at every grid point. For odd derivatives we therefore set that coefficient to zero; otherwise a real function would get a complex derivative.

The finite difference derivatives fd2 and fd4 are sparse matrices, built like the periodic laplacian_1d in Time-Dependent PDEs: sparse.diags for the bands, plus the entries that wrap around the corners. Compare the three on u(x)=esin⁡xu(x) = e^{\sin x}, whose derivative is cos⁡x esin⁡x\cos x\, e^{\sin x}.

The finite difference errors fall like N−2N^{-2} and N−4N^{-4}, straight lines on a log-log plot. The spectral error falls faster than any straight line, and this behavior is called spectral accuracy.

Past that point the spectral error creeps up slowly from round-off error.

Smoothness sets the rate

The error of the spectral derivative is roughly the size of the Fourier coefficients that the grid cannot represent, so it is governed by how fast the coefficients u^k\hat u_k decay.

That is decided by smoothness. A jump in uu gives coefficients of size about 1/k1/k, a jump in the pp-th derivative gives about 1/kp+11/k^{p+1}, and an analytic function like esin⁡xe^{\sin x} has coefficients that decay geometrically.

The derivative inherits these rates. For ∣sin⁡x∣3|\sin x|^3 the spectral derivative converges only algebraically:

Each doubling of NN divides the error by 4, so it is O(N−2)O(N^{-2}). This is roughly the same as a second-order finite difference.

Spectral methods are good for smooth periodic problems and lose most of their advantage when the solution has kinks, jumps, or shocks. A truncated Fourier series of a discontinuous function also overshoots near the jump by about 9%, however many modes you keep (the Gibbs phenomenon).

Linear PDEs: exact evolution in Fourier space

For a linear PDE with constant coefficients on a periodic domain, all the Fourier modes evolve independently. For the heat equation

ut=Duxx,u_t = D u_{xx},

we can Fourier transform on both sides

ddt∑ku^k=D∑k(−k2)u^k.\frac{\mathrm d}{\mathrm d t} \sum_k \hat u_k = D \sum_k (-k^2) \hat u_k.

So that the Fourier mode with the coefficient eikxe^{ikx} satisfies

ddtu^k=−Dk2u^k,\frac{\mathrm d}{\mathrm d t} \hat u_k = -Dk^2 \hat u_k,
u^k(t)=e−Dk2t u^k(0)\hat u_k(t) = e^{-Dk^2 t}\, \hat u_k(0)

This solves the semi-discrete problem exactly in time, at any tt, with one FFT and one inverse FFT. There is no time step and no stability limit. The high wavenumbers decay fastest with decay rate Dk2Dk^2.

Advection

The advection equation

ut+c ux=0u_t + c\,u_x = 0

moves any profile to the right at speed cc without changing its shape u(x,t)=u0(x−ct)u(x,t) = u_0(x-ct).

In Fourier space

ddtu^k=c(−ik)u^k,u^k(t)=e−icktu^k(0).\frac{\mathrm{d}}{\mathrm{d}t} \hat u_k = c(-ik) \hat u_k, \quad \hat u_k(t) = e^{-ickt}\hat u_k(0).

We move a narrow bump once around the domain, after which the exact solution is the initial condition again, and compare

  • second-order differences with RK4 in time

  • spectral differentiation with RK4 in time

all with the same time step Δt=0.5 h/c\Delta t = 0.5\,h/c.

The finite difference fails because waves of different lengths travel at different speeds.
Applied to eikxe^{ikx}, the centered difference gives i sin⁡khh eikxi\,\frac{\sin kh}{h}\, e^{ikx} instead of ik eikxik\, e^{ikx}, so mode kk moves at speed csin⁡khkh<cc \frac{\sin kh}{kh} < c. The short waves lag behind and leave a trail. This is also called numerical dispersion.

The spectral derivative is exact for every mode the grid can represent, so the only error left is from RK4 in time.

Finite differences are accurate only for well-resolved modes, kh≪1kh \ll 1, which means many points per wavelength. The spectral derivative is exact up to the grid’s limit kh=πkh = \pi, two points per wavelength.

Poisson’s equation

On the square [0,2π)2[0, 2\pi)^2 with periodic boundary conditions, the Poisson equation −Δu=f-\Delta u = f becomes, mode by mode,

(kx2+ky2) u^k=f^k.(k_x^2 + k_y^2)\, \hat u_{k} = \hat f_{k}.

Every mode except k=0k = 0 can be solved by division. For the k=0k = 0 mode,

u^0=∫02π∫02πu(x,y) dxdy\hat u_0 = \int_0^{2\pi}\int_0^{2\pi} u(x, y) \,\mathrm d x \mathrm d y

is the average of uu over the periodic square. Adding a constant to uu also satisfy the Poisson equation, so we can safely fix u^0=0\hat u_0 = 0 (zero mean).

Check it with a manufactured solution: pick u=esin⁡xcos⁡2yu = e^{\sin x}\cos 2y, compute f=−Δuf = -\Delta u by hand, and see if we get uu back.

For comparison, here is a sparse direct solve of the second-order finite difference version of the same problem.

The sparse solve is already hundreds of times slower at N=256N = 256, and it is only second-order accurate. Fast Poisson solvers based on the FFT, and on its relatives the discrete sine and cosine transforms for Dirichlet and Neumann boundaries are standard building blocks in fluid dynamics and electrostatics codes.

Time splitting for nonlinear problems

We have shown that derivatives are cheap in Fourier space, while products and other nonlinear functions of uu are cheap on the real space grid. Many PDEs have the form

ut=Lu+N(u),u_t = \mathcal L u + \mathcal N(u),

where L\mathcal L is linear with constant coefficients, so that it can be solved exactly in Fourier space as above, and N\mathcal N acts point by point. Time splitting (also called operator splitting) advances the two parts one after the other. Write SL(τ)S_{\mathcal L}(\tau) for the map that takes uu to the solution of ut=Luu_t = \mathcal L u a time τ\tau later, and SN(τ)S_{\mathcal N}(\tau) for the same map for ut=N(u)u_t = \mathcal N(u). One step of size Δt\Delta t is

  • Lie splitting: un+1=SL(Δt) SN(Δt) unu^{n+1} = S_{\mathcal L}(\Delta t)\, S_{\mathcal N}(\Delta t)\, u^n, a full nonlinear step followed by a full linear step;

  • Strang splitting: un+1=SN(Δt/2) SL(Δt) SN(Δt/2) unu^{n+1} = S_{\mathcal N}(\Delta t/2)\, S_{\mathcal L}(\Delta t)\, S_{\mathcal N}(\Delta t/2)\, u^n, a half nonlinear step, a full linear step, and another half nonlinear step. (The order can be changed depending on which is easier)

If the two parts commute, both are exact. In general they do not. For matrices AA and BB, expanding the exponentials gives

eΔtAeΔtB=eΔt(A+B)+Δt22(AB−BA)+O(Δt3),e^{\Delta t A} e^{\Delta t B} = e^{\Delta t (A + B)} + \frac{\Delta t^2}{2}(AB - BA) + O(\Delta t^3),

so Lie splitting makes an error of order Δt2\Delta t^2 in every step, and of order Δt\Delta t after the T/ΔtT/\Delta t steps needed to reach a time TT. Strang splitting is symmetric, which cancels the Δt2\Delta t^2 term, and it is second order.

Example: the KdV equation

The Korteweg-de Vries (KdV) equation

ut+u ux+uxxx=0u_t + u\,u_x + u_{xxx} = 0

is a model for shallow water waves. It has soliton solutions, solitary waves that keep their shape,

u(x,t)=3c sech⁡2 ⁣(c2 (x−ct)),u(x, t) = 3c\, \operatorname{sech}^2\!\left(\tfrac{\sqrt c}{2}\,(x - ct)\right),

which move at a speed cc proportional to their height. The linear part is solved exactly in Fourier space,

ut+uxxx=0,u^k(t+τ)=eik3τ u^k(t)u_t + u_{xxx} = 0, \quad \hat u_k(t + \tau) = e^{ik^3\tau}\, \hat u_k(t)

The nonlinear part ut+u ux=0u_t + u\,u_x = 0 has no such formula, so we advance it with one step of rk4_steps from above. We also write u ux=12(u2)xu\,u_x = \tfrac12 (u^2)_x. Here the half steps of the Strang splitting are the cheap derivatives, so each step needs only one RK4 step.

One more detail. If uu contains wavenumbers up to N/2N/2, then u2u^2 contains wavenumbers up to NN, which the grid cannot represent. They show up as lower wavenumbers instead (this is called aliasing) and can make the computation blow up. The standard fix is the 2/3 rule: drop the top third of the wavenumbers when differentiating u2u^2.

Before using it, check it. A soliton with speed c=100c = 100 goes once around the domain of length 2π2\pi in time 2π/1002\pi/100 and should come back to where it started.

Now a tall soliton starts behind a short one. The tall one is faster, catches up, and passes through.

After the collision both solitons come out with their original shapes and speeds.

Exercises

Exercise 1

Real data has conjugate-symmetric Fourier coefficients, so half of them are redundant. Rewrite spectral_derivative using rfft, irfft, and rfftfreq from scipy.fft. Check that it agrees with the version above to rounding error, and time both for N=220N = 2^{20}.

Exercise 2

Compute the second derivative of esin⁡xe^{\sin x} spectrally (order=2) and with the second-order formula (uj+1−2uj+uj−1)/h2(u_{j+1} - 2u_j + u_{j-1})/h^2. How many points does each need for an error below 10-8?