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.

2026-11-16 Stiff equations

Last time

  • Notes on integration

  • Ordinary differential equations (ODE)

  • Explicit methods for solving ODE

  • Stability

Today

  • Implicit and explicit methods

  • Stiff equations

  • PDE examples

using LinearAlgebra
using Plots
default(linewidth=4, legendfontsize=12)

struct RKTable
    A::Matrix
    b::Vector
    c::Vector
    function RKTable(A, b)
        s = length(b)
        A = reshape(A, s, s)
        c = vec(sum(A, dims=2))
        new(A, b, c)
    end
end

function rk_stability(z, rk)
    s = length(rk.b)
    1 + z * rk.b' * ((I - z*rk.A) \ ones(s))
end

rk4 = RKTable([0 0 0 0; .5 0 0 0; 0 .5 0 0; 0 0 1 0], [1, 2, 2, 1] / 6)
heun = RKTable([0 0; 1 0], [.5, .5])
Rz_theta(z, theta) = (1 + (1 - theta)*z) / (1 - theta*z)

function ode_rk_explicit(f, u0; tfinal=1, h=0.1, table=rk4)
    u = copy(u0)
    t = 0.
    n, s = length(u), length(table.c)
    fY = zeros(n, s)
    thist = [t]
    uhist = [u0]
    while t < tfinal
        tnext = min(t+h, tfinal)
        h = tnext - t
        for i in 1:s
            ti = t + h * table.c[i]
            Yi = u + h * sum(fY[:,1:i-1] * table.A[i,1:i-1], dims=2)
            fY[:,i] = f(ti, Yi)
        end
        u += h * fY * table.b
        t = tnext
        push!(thist, t)
        push!(uhist, u)
    end
    thist, hcat(uhist...)
end
ode_rk_explicit (generic function with 1 method)

Ordinary Differential Equations

Given initial condition y0=y(t=0)y_0 = y(t=0), find y(t)y(t) for t>0t > 0 that satisfies

y′≡y˙≡∂y∂t=f(t,y)y' \equiv \dot y \equiv \frac{\partial y}{\partial t} = f(t, y)
Applicationyyff
Orbital dynamicsposition, momentumconservation of momentum
Chemical reactionsconcentrationconservation of atoms
Epidemiologyinfected/recovered populationtransmission and recovery
  • yy can be a scalar or a vector

Solving differential equations

Linear equations

y′=A(t)y+source(t)y' = A(t) y + \text{source}(t)
  • Autonomous if A(t)=AA(t) = A and source independent of tt

  • Suppose yy and a=Aa = A are scalars: y(t)=eaty0y(t) = e^{at} y_0

Can do the same for systems

y(t)=eAty0y(t) = e^{A t} y_0

What does it mean to exponentiate a matrix?

Taylor series!

eA=1+A+A22+A33!+⋯e^A = 1 + A + \frac{A^2}{2} + \frac{A^3}{3!} + \dotsb

and there are many practical ways to compute it.

Question

Suppose that the diagonalization A=XΛX−1A = X \Lambda X^{-1} exists and derive a finite expression for the matrix exponential using the scalar exp function.

Forward Euler Method

The simplest method for solving y′(t)=f(t,y)y'(t) = f(t,y) is to use numerical differentiation to write

y′≈y(h)−y(0)hy' \approx \frac{y(h) - y(0)}{h}

which yields the solution estimate

y~(h)=y(0)+hf(0,y(0))\tilde y(h) = y(0) + h f(0, y(0))

where hh is the step size. Let’s try this on a scalar problem

y′=−k(y−cos⁡t)y' = -k (y - \cos t)

where kk is a parameter controlling the rate at which the solution u(t)u(t) is pulled toward the curve cos⁡t\cos t.

function ode_euler(f, y0; tfinal=10., h=0.1)
    y = copy(y0)
    t = 0.
    thist = [t]
    yhist = [y0]
    while t < tfinal
        tnext = min(t+h, tfinal)
        h = tnext - t
        y += h * f(t, y)
        t = tnext
        push!(thist, t)
        push!(yhist, y)
    end
    thist, hcat(yhist...)
end
ode_euler (generic function with 1 method)
f1(t, y; k=41) = -k * (y .- cos(t))

thist, yhist = ode_euler(f1, [0.0], tfinal=20, h=.05)
plot(thist, yhist[1,:], marker=:circle)
plot!(cos)
Loading...

Forward Euler on a linear system

[y1y2]′=[01−10][y1y2]\begin{bmatrix} y_1 \\ y_2 \end{bmatrix}' = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}
f2(t, y) = [0 1; -1 0] * y

thist, yhist = ode_euler(f2, [0., 1], h=.1, tfinal=100)
scatter(thist, yhist')
plot!([cos, sin])
Loading...
eigvals([0 1; -1 0])
2-element Vector{ComplexF64}: 0.0 - 1.0im 0.0 + 1.0im

Runge-Kutta 4

thist, yhist = ode_rk_explicit(f2, [0., 1], h=2, tfinal=50)
scatter(thist, yhist')
plot!([cos, sin], size=(800, 500))
Loading...
  • Apparently it is possible to integrate this system using large time steps.

  • This method evaluates f(y)f(y) four times per stepso the cost is about equal when the step size hh is 4x larger than forward Euler.

Linear Stability Analysis

Why did Euler diverge (even if slowly) while RK4 solved this problem accurately? And why do both methods diverge if the step size is too large? We can understand the convergence of methods by analyzing the test problem

y′=λyy' = \lambda y

for different values of λ\lambda in the complex plane. One step of the Euler method with step size hh maps

y↦y+hλy=(1+hλ)⏟R(hλ)y.y \mapsto y + h \lambda y = \underbrace{(1 + h \lambda)}_{R(h \lambda)} y .

When does this map cause solutions to “blow up” and when is it stable?

function plot_stability(Rz, method; xlim=(-3, 2), ylim=(-1.5, 1.5))
    x = xlim[1]:.02:xlim[2]
    y = ylim[1]:.02:ylim[2]
    plot(title="Stability: $method", aspect_ratio=:equal, xlim=xlim, ylim=ylim)
    heatmap!(x, y, (x, y) -> abs(Rz(x + 1im*y)), c=:bwr, clims=(0, 2))
    contour!(x, y, (x, y) -> abs(Rz(x + 1im*y)), color=:black, linewidth=2, levels=[1.])
    plot!(x->0, color=:black, linewidth=1, label=:none)
    plot!([0, 0], [ylim...], color=:black, linewidth=1, label=:none)
end
plot_stability (generic function with 1 method)
plot_stability(z -> 1 + z, "Forward Eulor")
Loading...

Stability for RK4

plot_stability(z -> rk_stability(4z, rk4), "RK4")
Loading...
plot_stability(z -> rk_stability(2z, heun), "Heun's method")
Loading...

Implicit methods

Recall that forward Euler is the step

y~(h)=y(0)+hf(0,y(0)).\tilde y(h) = y(0) + h f(0, y(0)) .

This can be evaluated explicitly; all the terms on the right hand side are known so the approximation y~(h)\tilde y(h) is computed merely by evaluating the right hand side. Let’s consider an alternative, backward Euler (or “implicit Euler”),

y~(h)=y(0)+hf(h,y~(h)).\tilde y(h) = y(0) + h f(h, \tilde y(h)) .

This is a (generally) nonlinear equation for y~(h)\tilde y(h). For the test equation y′=λyy' = \lambda y, the backward Euler method is

y~(h)=y(0)+hλy~(h)\tilde y(h) = y(0) + h \lambda \tilde y(h)

or

y~(h)=11−hλ⏟R(hλ)y(0).\tilde y(h) = \underbrace{\frac{1}{1 - h \lambda}}_{R(h\lambda)} y(0) .
plot_stability(z -> 1/(1-z), "Backward Euler")
Loading...

Computing with implicit methods

y~(h)=y(0)+hf(y~(h))\tilde y(h) = y(0) + h f\big(\tilde y(h) \big)
  • Linear solve for linear problem

  • Nonlinear (often Newton) solve for nonlinear problem

  • Need Jacobian or finite differencing

plot_stability(z -> Rz_theta(z, .5), "Midpoint method")
Loading...

The θ\theta method

Forward and backward Euler are bookends of the family known as θ\theta methods.

u~(h)=u(0)+hf(θh,θu~(h)+(1−θ)u(0))\tilde u(h) = u(0) + h f\Big(\theta h, \theta\tilde u(h) + (1-\theta)u(0) \Big)

which, for linear problems, is solved as

(I−hθA)u(h)=(I+h(1−θ)A)u(0).(I - h \theta A) u(h) = \Big(I + h (1-\theta) A \Big) u(0) .

θ=0\theta=0 is explicit Euler, θ=1\theta=1 is implicit Euler, and θ=1/2\theta=1/2 are the midpoint or trapezoid rules (equivalent for linear problems). The stability function is

R(z)=1+(1−θ)z1−θz.R(z) = \frac{1 + (1-\theta)z}{1 - \theta z}.
Rz_theta(z, theta) = (1 + (1-theta)*z) / (1 - theta*z)
theta = 1
plot_stability(z -> Rz_theta(z, theta), "\$\\theta=$theta\$", 
    xlim=(-10, 1))
Loading...

Extend the limits on the figure.

θ\theta method for the oscillator

function ode_theta_linear(A, u0; forcing=zero, tfinal=1, h=0.1, theta=.5)
    u = copy(u0)
    t = 0.
    thist = [t]
    uhist = [u0]
    while t < tfinal
        tnext = min(t+h, tfinal)
        h = tnext - t
        rhs = (I + h*(1-theta)*A) * u .+ h*forcing(t+h*theta)
        u = (I - h*theta*A) \ rhs
        t = tnext
        push!(thist, t)
        push!(uhist, u)
    end
    thist, hcat(uhist...)
end
ode_theta_linear (generic function with 1 method)
# Test on oscillator
A = [0 1; -1 0]
theta = .5
thist, uhist = ode_theta_linear(A, [0., 1], h=.5,
    theta=theta, tfinal=20)
scatter(thist, uhist')
plot!([cos, sin])
Loading...

θ\theta method for the cosine decay

k = 2000
theta = .5
thist, uhist = ode_theta_linear(-k, [.2], forcing=t -> k*cos(t), tfinal=5, h=.1, theta=theta)
scatter(thist, uhist[1,:], title="\$\\theta = $theta\$")
plot!(cos, size=(800, 500))
Loading...

Stability classes and the θ\theta method

Definition: AA-stability

A method is AA-stable if the stability region

{z:∣R(z)∣≤1}\{ z : |R(z)| \le 1 \}

contains the entire left half plane

ℜ[z]≤0.\Re[z] \le 0 .

This means that the method can take arbitrarily large time steps without becoming unstable (diverging) for any problem that is indeed physically stable.

Definition: LL-stability

A time integrator with stability function R(z)R(z) is LL-stable if

lim⁡z→∞R(z)=0.\lim_{z\to\infty} R(z) = 0 .

For the θ\theta method, we have

lim⁡z→∞1+(1−θ)z1−θz=1−θθ.\lim_{z\to \infty} \frac{1 + (1-\theta)z}{1 - \theta z} = \frac{1-\theta}{\theta} .

Evidently only θ=1\theta=1 is LL-stable.

Heat equation as linear ODE

  • How do different θ∈[0,1]\theta \in [0, 1] compare in terms of stability?

  • Are there artifacts even when the solution is stable?

using SparseArrays

function heat_matrix(n)
    dx = 2 / n
    rows = [1]
    cols = [1]
    vals = [0.]
    wrap(j) = (j + n - 1) % n + 1
    for i in 1:n
        append!(rows, [i, i, i])
        append!(cols, wrap.([i-1, i, i+1]))
        append!(vals, [1, -2, 1] ./ dx^2)
    end
    sparse(rows, cols, vals)
end
heat_matrix(5)
5×5 SparseMatrixCSC{Float64, Int64} with 15 stored entries: -12.5 6.25 ⋅ ⋅ 6.25 6.25 -12.5 6.25 ⋅ ⋅ ⋅ 6.25 -12.5 6.25 ⋅ ⋅ ⋅ 6.25 -12.5 6.25 6.25 ⋅ ⋅ 6.25 -12.5
n = 200
A = heat_matrix(n)
x = LinRange(-1, 1, n+1)[1:end-1]
u0 = exp.(-200 * x .^ 2)
@time thist, uhist = ode_theta_linear(A, u0, h=.01, theta=.5, tfinal=1);
nsteps = size(uhist, 2)
plot(x, uhist[:, 1:5])
  0.007510 seconds (10.92 k allocations: 14.522 MiB, 31.83% gc time)
Loading...

Advection as a linear ODE

function advect_matrix(n; upwind=false)
    dx = 2 / n
    rows = [1]
    cols = [1]
    vals = [0.]
    wrap(j) = (j + n - 1) % n + 1
    for i in 1:n
        append!(rows, [i, i])
        if upwind
            append!(cols, wrap.([i-1, i]))
            append!(vals, [1., -1] ./ dx)
        else
            append!(cols, wrap.([i-1, i+1]))
            append!(vals, [1., -1] ./ 2dx)
        end
    end
    sparse(rows, cols, vals)
end
advect_matrix(5)
5×5 SparseMatrixCSC{Float64, Int64} with 11 stored entries: 0.0 -1.25 ⋅ ⋅ 1.25 1.25 ⋅ -1.25 ⋅ ⋅ ⋅ 1.25 ⋅ -1.25 ⋅ ⋅ ⋅ 1.25 ⋅ -1.25 -1.25 ⋅ ⋅ 1.25 ⋅
n = 50
A = advect_matrix(n, upwind=false)
x = LinRange(-1, 1, n+1)[1:end-1]
u0 = exp.(-9 * x .^ 2)
@time thist, uhist = ode_theta_linear(A, u0, h=.02, theta=.5, tfinal=1.);
nsteps = size(uhist, 2)
plot(x, uhist[:, 1:(nsteps÷8):end])
  0.001382 seconds (6.46 k allocations: 4.237 MiB)
Loading...

Spectrum of operators

theta=.5
h = .1
plot_stability(z -> Rz_theta(z, theta), "\$\\theta=$theta, h=$h\$")
ev = eigvals(Matrix(h*advect_matrix(20, upwind=false)))
scatter!(real(ev), imag(ev))
Loading...
theta=.5
h = .2 / 4
plot_stability(z -> Rz_theta(z, theta), "\$\\theta=$theta, h=$h\$", xlim=(-30, 1))
ev = eigvals(Matrix(h*heat_matrix(20)))
scatter!(real(ev), imag(ev), label=:none)
Loading...