Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Spectral methods

ProblemSolve differential equations with smooth solutions to very high accuracy: two-point problems , derivatives of periodic functions, and the periodic heat equation
Devised byFourier series for the heat equation: Joseph Fourier (1807–1822). Collocation methods: Cornelius Lanczos (1938). Pseudospectral methods: Heinz-Otto Kreiss and Joseph Oliger, Steven Orszag (early 1970s). Chebyshev differentiation matrices in their modern form: Lloyd N. Trefethen (2000)
AccuracyExponential in the number of points for analytic solutions; faster than any power for smooth ones
CostChebyshev: a dense system. Fourier: two FFTs,
Read firstPolynomial interpolation, The fast Fourier transform
APIchebyshev_points, chebyshev_differentiation_matrix, chebyshev_bvp, fourier_derivative, periodic_heat

Overview

Finite differences and finite elements approximate a solution locally, from a few neighbouring values, and converge at a fixed algebraic rate such as . Spectral methods approximate it globally, by a single polynomial or trigonometric series through all the grid values, and differentiate that. For smooth solutions the error then falls exponentially with the number of points: where a second-order method needs a thousand points for ten digits, a spectral method may need thirty.

The same principle underlies Gaussian quadrature and the trapezoid rule for periodic functions. This entry covers its use for differential equations: Fourier methods for periodic problems, where the FFT makes them extremely fast, and Chebyshev collocation for problems on an interval.

Where it is used

Spectral methods reach high accuracy with few unknowns when the solution is smooth.

  • Fluid dynamics and weather. Turbulence simulations and global weather and climate models use Fourier and spherical-harmonic expansions.
  • Quantum mechanics and optics. Schrodinger-equation solvers and beam-propagation methods.
  • Boundary value problems. Chebyshev collocation solves two-point problems to near machine precision with a small matrix.
  • Contrast with finite elements. Global polynomials give fast convergence for smooth problems, but need simple geometry.

History

Spectral ideas go back to Joseph Fourier, who solved the heat equation in his Théorie analytique de la chaleur (1822) by expanding the temperature in sines and cosines, each of which simply decays in time. Cornelius Lanczos (1938) introduced the use of Chebyshev polynomials and collocation at selected points to solve differential equations approximately.

The methods became practical with fast computers and the fast Fourier transform (Cooley and Tukey, 1965). Heinz-Otto Kreiss and Joseph Oliger (1972) and Steven Orszag (1971–72) developed pseudospectral methods for fluid dynamics and weather prediction, which compute derivatives in spectral space and nonlinear terms on the grid. David Gottlieb and Orszag’s monograph (1977) established the theory, and Lloyd N. Trefethen’s Spectral Methods in MATLAB (2000) made the Chebyshev differentiation matrix, implemented here, a few lines of code.

The idea

Global interpolation and exponential convergence

Through points there is exactly one interpolating polynomial (or, on a periodic grid, one trigonometric polynomial). Differentiating it gives approximations to at every point at once, each using all the values. If is analytic, its interpolant converges exponentially, and so do its derivatives. Algebraic methods, by contrast, are limited by the degree of the local polynomial, whatever the smoothness of .

Fourier differentiation

For a periodic function sampled at equispaced points on , the discrete Fourier transform gives coefficients such that . Differentiation multiplies each coefficient by with :

So: transform, multiply, transform back, operations with the FFT. One subtlety: for even the mode is the same on the grid as , so an odd derivative of it is ambiguous; the library sets it to zero. The rate of convergence is governed by how fast the Fourier coefficients decay, which is governed by smoothness:

Figure 2.30. Fourier differentiation.

For the analytic the error reaches machine precision with 32 points. For , whose third derivative jumps, the coefficients decay only like and the derivative error like : no better than finite differences.

The heat equation in Fourier space

Fourier’s own insight solves the periodic heat equation exactly: each mode satisfies , so

periodic_heat applies this factor in one step, with no time-stepping error and no stability restriction: high-frequency modes are damped fastest, which is why diffusion smooths. The mean, mode , is conserved.

Chebyshev points and differentiation matrices

On a non-periodic interval, equispaced polynomial interpolation fails (the Runge phenomenon of the Newton-Cotes entry). The remedy is to cluster the points towards the ends, at the Chebyshev points

the projections onto of equally spaced points on a semicircle. Interpolation at these points converges for every analytic function. Differentiating the interpolant is a linear map from the values to the derivative values, a matrix with explicit entries:

with and otherwise. Computing the diagonal as minus the row sum, rather than from its closed form, makes exact on constants and much more accurate in floating point.

Chebyshev collocation for boundary value problems

To solve with and given, map to , replace by and by (scaled by the map), require the equation at the interior points, and replace the first and last rows by the boundary conditions. The result is a dense linear system of size , solved by LU factorisation. For :

Figure 2.31. Chebyshev collocation against finite elements.

Chebyshev collocation reaches with about 25 points, where linear finite elements need a thousand points for . The plateau beyond is rounding error, amplified by the ill-conditioning of , whose entries grow like .

Algorithm

  • Fourier derivative: FFT; multiply mode by (zeroing the Nyquist mode for odd and even ); inverse FFT; take the real part.
  • Periodic heat: FFT; multiply mode by ; inverse FFT.
  • Chebyshev BVP: build and ; assemble at the interior points, with boundary rows , ; solve by LU.

Implementation

The Chebyshev differentiation matrix:

    let c: Vec<f64> = (0..=n).map(|i| if i == 0 || i == n { 2.0 } else { 1.0 } * if i % 2 == 0 { 1.0 } else { -1.0 }).collect();
    let mut d = vec![vec![0.0; n + 1]; n + 1];
    for i in 0..=n {
        for j in 0..=n {
            if i != j {
                d[i][j] = c[i] / c[j] / (x[i] - x[j]);
            }
        }
        d[i][i] = -d[i].iter().sum::<f64>();
    }
  • The Chebyshev points run from down to , so the returned solution runs from to .
  • The Fourier routines use the library’s FFT, whose forward transform is unscaled and whose inverse divides by .
  • The Chebyshev system is dense, so the cost is ; that is no burden, because spectral accuracy rarely needs more than a few hundred points.

In practice

//! Spectral methods: Chebyshev collocation and Fourier differentiation.
//!
//! Run with `cargo run --release -p lib_jsl_integration --example pde_spectral`.

use lib_jsl_integration::pde::spectral::{chebyshev_bvp, fourier_derivative, periodic_heat};
use lib_jsl_special_functions::airy_functions::airy_ai;
use std::f64::consts::PI;

fn main() {
    // Airy's equation u'' = x u on [−8, 2] with u(−8) = Ai(−8), u(2) = Ai(2): the error against
    // the Airy function at every collocation point.
    for n in [16, 24, 32, 40, 48] {
        let (xs, u) = chebyshev_bvp(|_| 0.0, |x| -x, |_| 0.0, -8.0, 2.0, airy_ai(-8.0), airy_ai(2.0), n).unwrap();
        let error = xs.iter().zip(&u).map(|(&x, &v)| (v - airy_ai(x)).abs()).fold(0.0, f64::max);
        println!("Airy equation with {:2} Chebyshev points: max error {error:.1e}", n + 1);
    }

    // The derivative of a smooth periodic function from 32 samples.
    let n = 32;
    let x: Vec<f64> = (0..n).map(|i| 2.0 * PI * i as f64 / n as f64).collect();
    let u: Vec<f64> = x.iter().map(|x| (x.cos()).exp()).collect();
    let du = fourier_derivative(&u, 1, 2.0 * PI).unwrap();
    let error = x.iter().zip(&du).map(|(x, d)| (d + x.sin() * x.cos().exp()).abs()).fold(0.0, f64::max);
    println!("Fourier derivative of e^(cos x) with {n} points: max error {error:.1e}");

    // The periodic heat equation, solved exactly in Fourier space: a square pulse smooths out.
    let pulse: Vec<f64> = x.iter().map(|&x| if (x - PI).abs() < 1.0 { 1.0 } else { 0.0 }).collect();
    let later = periodic_heat(&pulse, 1.0, 0.5, 2.0 * PI).unwrap();
    let mean_before = pulse.iter().sum::<f64>() / n as f64;
    let mean_after = later.iter().sum::<f64>() / n as f64;
    println!("heat: peak {:.4} → {:.4}, mean {mean_before:.4} → {mean_after:.4} (conserved)", 1.0, later.iter().cloned().fold(f64::MIN, f64::max));
}

Strengths

  • Exponential convergence for smooth solutions: a handful of points give many digits.
  • Fourier methods cost only two FFTs per derivative, and solve linear constant-coefficient periodic problems exactly in time.

Weaknesses

  • Accuracy collapses to an algebraic rate if the solution is not smooth (shocks, kinks, interfaces), and discontinuities cause Gibbs oscillations.
  • Simple geometries only: intervals, periodic domains and their products. Complex domains call for finite elements or spectral elements.
  • Dense matrices with large condition numbers; very high derivatives amplify rounding.

Exercises

  1. Compute for and by hand and check that it differentiates exactly (for ).
  2. Show that the Fourier derivative of sampled at points is zero, and explain why zeroing the Nyquist mode is the right choice.
  3. Solve , with chebyshev_bvp (Trefethen’s Program 13) and find the smallest that gives 12 correct digits.
  4. Use periodic_heat on a square pulse and plot the result at several times. Why do the corners round off first?
  5. Replace the Chebyshev points by equally spaced points in the differentiation matrix formula (using the general barycentric weights) and repeat the convergence experiment for . What happens?
Hints and answers
  1. For the nodes are and (the slope of the line through two points). For the nodes are and must give , and ; check the rows against these three conditions.
  2. At the equispaced points , , and the derivative vanishes at every grid point, so the sampled derivative is 0. The Nyquist mode cannot be distinguished from its alias, and its derivative would be imaginary, so setting it to zero keeps the derivative of a real function real.
  3. Increase until the error stops falling; the solution is smooth, so the error falls geometrically and 12 digits typically need a few dozen points.
  4. The corners of the pulse are made of high wavenumbers, and each wavenumber decays like , so the sharp features smooth out first.
  5. Equally spaced points suffer from the Runge phenomenon: the error in the derivative grows exponentially with instead of decaying, and the differentiation matrix becomes extremely ill-conditioned.

Further reading

  • J. Fourier, Théorie analytique de la chaleur, Paris, 1822.
  • C. Lanczos, “Trigonometric interpolation of empirical and analytical functions”, Journal of Mathematics and Physics 17 (1938) 123–199.
  • S. A. Orszag, “Comparison of pseudospectral and spectral approximation”, Studies in Applied Mathematics 51 (1972) 253–259.
  • D. Gottlieb and S. A. Orszag, Numerical Analysis of Spectral Methods: Theory and Applications, SIAM, 1977.
  • L. N. Trefethen, Spectral Methods in MATLAB, SIAM, 2000. The source of the differentiation matrix and many of the examples here.
  • J. P. Boyd, Chebyshev and Fourier Spectral Methods, 2nd ed., Dover, 2001.