Stiff Equations
Some ODEs contain processes on very different time scales: a fast reaction that settles in microseconds next to a slow one that evolves over hours. Once the fast part has decayed, the solution is smooth, yet explicit methods are still forced to take tiny steps, because larger ones are unstable. Such problems are called stiff, and they need implicit methods.
A simple stiff problem
, . The solution is : after a fast transient of a few milliseconds it simply follows . Euler's method needs for stability no matter how smooth the solution is.
import numpy as np
lam = 1000.0
f = lambda t, u: -lam * (u - np.cos(t)) - np.sin(t)
exact = lambda t: np.cos(t) - np.exp(-lam * t)
for h in (0.0019, 0.0021, 0.01):
n = int(round(2 / h)); u = 0.0
with np.errstate(over="ignore", invalid="ignore"):
for i in range(n):
u = u + h * f(i * h, u)
print(f"explicit Euler, h = {h}: u(2) = {u:.4e}, exact {exact(n * h):.4f}")
# backward Euler: u_{i+1} = u_i + h f(t_{i+1}, u_{i+1}); f is linear in u, so solve directly
for h in (0.01, 0.1, 0.5):
n = int(round(2 / h)); u = 0.0
for i in range(n):
t1 = (i + 1) * h
u = (u + h * (lam * np.cos(t1) - np.sin(t1))) / (1 + h * lam)
print(f"backward Euler, h = {h}: u(2) = {u:.4f}, exact {exact(n * h):.4f}")explicit Euler, h = 0.0019: u(2) = -4.1678e-01, exact -0.4168
explicit Euler, h = 0.0021: u(2) = -2.5459e+39, exact -0.4154
explicit Euler, h = 0.01: u(2) = -7.0551e+190, exact -0.4161
backward Euler, h = 0.01: u(2) = -0.4161, exact -0.4161
backward Euler, h = 0.1: u(2) = -0.4161, exact -0.4161
backward Euler, h = 0.5: u(2) = -0.4161, exact -0.4161
Backward Euler is accurate with steps 250 times larger than explicit Euler's stability limit.
Stiffness and eigenvalues
For a system the local behaviour is governed by the eigenvalues of the Jacobian . A problem is stiff when some eigenvalues have large negative real parts (fast, decayed modes) while the solution itself varies slowly. The stiffness ratio can be or more in chemistry and circuit simulation.
A-stability
Applied to , backward Euler gives , which decays for every when . A method with this property is A-stable. No explicit method is A-stable; backward Euler, the trapezoid rule and BDF2 are. (Dahlquist's barrier: A-stable linear multistep methods have order at most 2; BDF3 to BDF6 are stable in large wedges, enough in practice.)
Implicit methods need nonlinear solves
For nonlinear , each implicit step solves by Newton's method (Newton's Method for Systems), with the Jacobian . Production codes reuse Jacobians and factorizations across steps to keep this cheap.
The Robertson chemical kinetics problem
A classic stiff test: three species with rate constants spanning nine orders of magnitude.
import numpy as np
from scipy.integrate import solve_ivp
def robertson(t, y):
y1, y2, y3 = y
return [-0.04 * y1 + 1e4 * y2 * y3,
0.04 * y1 - 1e4 * y2 * y3 - 3e7 * y2**2,
3e7 * y2**2]
for method in ("RK45", "BDF", "Radau", "LSODA"):
sol = solve_ivp(robertson, (0, 100), [1, 0, 0], method=method, rtol=1e-6, atol=1e-10)
print(f"{method:6s}: success {sol.success}, steps {len(sol.t) - 1:5d}, evaluations {sol.nfev:6d}, y1(100) = {sol.y[0, -1]:.6f}")RK45 : success True, steps 104142, evaluations 729668, y1(100) = 0.617235
BDF : success True, steps 163, evaluations 431, y1(100) = 0.617236
Radau : success True, steps 88, evaluations 726, y1(100) = 0.617235
LSODA : success True, steps 240, evaluations 496, y1(100) = 0.617234
The explicit RK45 takes over 100,000 steps (all dictated by stability, not accuracy), while BDF, Radau and LSODA need between about 90 and 240.
In software
solve_ivp(method="BDF"),"Radau"(implicit Runge-Kutta, order 5),"LSODA"(detects stiffness and switches). Supplyingjac=(or a sparsity patternjac_sparsity=) speeds them up greatly.- If an explicit solver takes absurdly many steps on a smooth-looking solution, suspect stiffness and switch methods.
- Stiff ODEs arise from discretizing diffusion: the semi-discrete heat equation is stiff, which is why implicit time stepping is used for it (The Heat Equation: Implicit Methods).
Exercises
Exercise 1: Stability limit
The Jacobian of a system has eigenvalues , and . What is the largest stable step for explicit Euler?
Answer
.
Solution
The fastest mode sets the limit: . To reach , explicit Euler needs over 500,000 steps.
Exercise 2: Backward Euler's amplification factor
What is the amplification factor of backward Euler for ? Of explicit Euler?
Answer
and .
Solution
Backward: (strong damping, correct qualitatively). Explicit: (explosive oscillation).
Exercise 3: Trapezoid versus backward Euler (code)
For , , take 10 steps of with backward Euler and with the trapezoid rule . What happens to the transient?
Answer
Backward Euler reaches at once; the trapezoid rule's error flips sign every step and decays only very slowly (factor about ).
Solution
lam, h = -1e6, 0.1
ube = utr = 0.0
for _ in range(10):
ube = (ube - h * lam) / (1 - h * lam) # f = lam (u - 1)
utr = (utr * (1 + h * lam / 2) - h * lam) / (1 - h * lam / 2)
print(f"backward Euler error {ube - 1:.2e}, trapezoid error {utr - 1:.6f}")backward Euler error 0.00e+00, trapezoid error -0.999600
The trapezoid rule is A-stable but its amplification factor tends to for very stiff modes (it is not L-stable), so transients ring. Backward Euler and BDF damp them.
Test yourself
A problem is stiff when…
- the solution blows up
- fast decaying modes force an explicit method to take steps far smaller than accuracy needs
- the equation is nonlinear
- the solution oscillates
Which method is A-stable?
- explicit Euler
- RK4
- backward Euler
- Adams-Bashforth 2
Implicit methods for nonlinear systems need at each step…
- nothing extra
- a nonlinear solve, usually by Newton's method
- a matrix exponential
- an FFT