Part 4: Stability Analysis
Module 3: ODE Methods & Conservation | ASTR 596
Learning Outcomes¶
By the end of this section, you will be able to:
Analyze stability regions for different integration methods
Diagnose and handle stiff equations
Predict when numerical methods will become unstable
Choose appropriate methods based on stability requirements
Implement implicit methods for challenging problems
Linear Stability Theory¶
To understand when methods fail catastrophically, we analyze their behavior on the test equation:
where . The true solution is .
When we apply a numerical method, we get:
where is the stability function with .
For bounded solutions, we need . The stability region is the set of values where this holds.
Stability Functions for Common Methods¶
Let’s derive the stability functions for our methods:
Euler’s Method¶
Applying Euler to :
Therefore:
This is stable when , which defines a circle of radius 1 centered at .
RK2 (Midpoint)¶
Following through the RK2 steps:
Therefore:
RK4¶
Through similar analysis (but more algebra):
This matches the Taylor series of through fourth order, but is not exactly truncated!
Physical Interpretation¶
For different types of problems, we need different stability characteristics:
Oscillatory Systems (Circular Orbits)¶
For a harmonic oscillator with frequency :
Eigenvalues: (purely imaginary)
Stability requires the imaginary axis to be in the stability region
Euler: Marginally stable for , but introduces artificial damping
RK4: Stable for
Leapfrog: Stable for with no damping!
Dissipative Systems (Damped Motion)¶
For exponential decay with rate :
Eigenvalue: (negative real)
Stability requires the negative real axis in the stability region
Euler: Stable for
RK4: Stable for
Stiff Equations - When Stability Dominates¶
What Makes an Equation Stiff?¶
A stiff equation contains widely separated timescales. After fast transients die out, we’re left tracking slow evolution, but explicit methods still need tiny timesteps for stability.
Understanding Stiffness Through Examples¶
Example 1: Chemical Kinetics¶
Consider a reaction where species A slowly converts to B, but B rapidly equilibrates:
The eigenvalues are (slow) and (fast).
After the initial transient (), B has equilibrated and we’re just tracking the slow decay of A. But explicit methods need for stability, even though nothing interesting happens on this timescale!
Example 2: Stellar Interior with Radiative Cooling¶
In stellar shocks, radiative cooling can create extreme stiffness:
where cooling for high temperatures. Near equilibrium:
Heating timescale: years
Cooling timescale: seconds
Stiffness ratio: 107!
Non-Stiff vs Stiff: The Key Distinction¶
Non-stiff equations have eigenvalues with similar magnitudes. The timestep is chosen for accuracy, and stability is automatically satisfied. Examples:
Planetary orbits (all periods within a few orders of magnitude)
Pendulum oscillations
Wave propagation in uniform media
Stiff equations have eigenvalues with vastly different magnitudes. The timestep is forced by stability of the fastest mode, even after it has decayed. Examples:
Chemical reaction networks
Radiative transfer with cooling
Electrical circuits with multiple timescales
Implicit Methods for Stiff Problems¶
Implicit methods evaluate the derivative at the future point, giving superior stability at the cost of solving equations.
Backward Euler¶
Instead of , we use:
This is implicit because appears on both sides!
Stability Analysis¶
For the test equation :
Stability function:
This is stable for the entire left half-plane! We call this A-stable.
Solving the Implicit Equation¶
The challenge: we must solve for .
For nonlinear , this requires iteration. Newton’s method is typical:
def backward_euler_step(y, t, h, f, df_dy, tol=1e-10):
"""
Single backward Euler step
Requires Jacobian df/dy for Newton iteration
"""
y_new = y # Initial guess
for _ in range(10): # Newton iterations
# Residual: R(y_new) = y_new - y - h*f(y_new, t+h)
residual = y_new - y - h*f(y_new, t + h)
if np.linalg.norm(residual) < tol:
return y_new
# Jacobian of residual
J = np.eye(len(y)) - h*df_dy(y_new, t + h)
# Newton update
delta = np.linalg.solve(J, -residual)
y_new = y_new + delta
raise RuntimeError("Newton iteration failed to converge")Implicit Midpoint Rule¶
A second-order implicit method:
This is A-stable AND symplectic! It preserves geometric structure while handling stiffness.
The CFL Condition¶
The CFL condition is a necessary stability criterion for hyperbolic PDEs that also applies to ODEs from spatial discretization:
where is the spatial grid spacing and is the maximum wave speed.
For the wave equation discretized in space:
Eigenvalues:
Stability requires:
This means finer spatial grids require smaller timesteps!
Diagnosing Integration Problems¶
Systematic Debugging Approach¶
When your integration fails, follow this diagnostic sequence:
Check for NaN/Inf: Immediate indicator of instability
Plot the solution: Look for oscillations growing exponentially
Compute eigenvalues: Estimate
Check stability limit: Is ?
Test with smaller h: Does halving h fix the problem?
Monitor energy: For conservative systems, is energy bounded?
Stability Test Code¶
def stability_test(method, lambda_val, h_values):
"""Test stability for different timesteps"""
results = []
for h in h_values:
y = 1.0 # Initial condition
stable = True
for n in range(1000):
y = method(y, lambda_val, h)
if abs(y) > 1e10: # Explosion detected
stable = False
break
results.append((h, stable, abs(y)))
return resultsBridge to Part 5: From Stability to Speed¶
Understanding stability tells us what timesteps are allowed, but performance determines whether our simulations finish in reasonable time. Even the most stable method is useless if it takes years to run.
In Part 5, we’ll explore performance optimization through vectorization. Modern processors can perform multiple operations simultaneously, but only if we structure our code correctly. The difference between naive loops and optimized array operations can be 100× in execution speed—the difference between waiting hours and waiting weeks for results.
Next: Part 5 - Performance Optimization