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.

Part 2: Building Better Methods - Runge-Kutta

Module 3: ODE Methods & Conservation | ASTR 596

San Diego State University

Learning Outcomes

By the end of this section, you will be able to:


The Key Insight: Sample Multiple Points

Euler’s fatal flaw is assuming the derivative stays constant over the entire timestep. Real functions curve. Runge-Kutta methods evaluate the derivative at carefully chosen intermediate points, then combine these samples with specific weights to achieve higher-order accuracy.

This is exactly the same principle as quadrature from Module 2! We’re essentially performing numerical integration on:

xn+1=xn+tntn+1f(x(t),t)dtx_{n+1} = x_n + \int_{t_n}^{t_{n+1}} f(x(t), t) dt

But now the integrand depends on the unknown solution x(t)x(t) itself, requiring us to estimate intermediate values.

RK2 - The Midpoint Method

Geometric Intuition

Instead of blindly following the tangent from the starting point, we:

  1. Take a half-step using the initial derivative

  2. Evaluate the derivative at this midpoint

  3. Use this midpoint derivative for the full step

This is analogous to the midpoint rule for integration, which we know from Module 2 is second-order accurate!

Complete Mathematical Formulation

The RK2 midpoint method is a predictor-corrector method:

Stage 1 - Predictor:

k1=f(xn,tn)k_1 = f(x_n, t_n)

xmid=xn+h2k1x_{mid} = x_n + \frac{h}{2}k_1

Stage 2 - Corrector:

k2=f(xmid,tn+h2)k_2 = f(x_{mid}, t_n + \frac{h}{2})

xn+1=xn+hk2x_{n+1} = x_n + h k_2
RK2 midpoint method: First evaluate derivative at start (k₁), use it to estimate midpoint, evaluate derivative there (k₂), then use midpoint derivative for full step. This captures solution curvature much better than Euler’s single-point sampling, achieving O(h²) global accuracy. The midpoint derivative (green) provides a better approximation to the average slope over the interval than the initial derivative alone.

Figure 1:RK2 midpoint method: First evaluate derivative at start (k₁), use it to estimate midpoint, evaluate derivative there (k₂), then use midpoint derivative for full step. This captures solution curvature much better than Euler’s single-point sampling, achieving O(h²) global accuracy. The midpoint derivative (green) provides a better approximation to the average slope over the interval than the initial derivative alone.

Error Analysis via Taylor Series

Using multivariate Taylor expansion, the midpoint derivative becomes:

f(xmid,tmid)=f+h2(ft+ffx)+O(h2)f(x_{mid}, t_{mid}) = f + \frac{h}{2}\left(\frac{\partial f}{\partial t} + f\frac{\partial f}{\partial x}\right) + O(h^2)

The RK2 update matches the true solution through O(h2)O(h^2):

RK2 is second-order accurate — halving the timestep reduces error by a factor of 4.

Implementation

def rk2_step(x, t, h, f):
    """Single RK2 (midpoint) step"""
    k1 = f(x, t)
    
    # Predictor to midpoint
    x_mid = x + 0.5 * h * k1
    t_mid = t + 0.5 * h
    
    # Evaluate at midpoint
    k2 = f(x_mid, t_mid)
    
    # Full step using midpoint derivative
    return x + h * k2

RK4 - The Classical Workhorse

RK4 samples the derivative at four carefully chosen points:

Stage 1: k1=f(xn,tn)k_1 = f(x_n, t_n) (derivative at start)

Stage 2: k2=f(xn+h2k1,tn+h2)k_2 = f(x_n + \frac{h}{2}k_1, t_n + \frac{h}{2}) (derivative at midpoint using k1k_1)

Stage 3: k3=f(xn+h2k2,tn+h2)k_3 = f(x_n + \frac{h}{2}k_2, t_n + \frac{h}{2}) (derivative at midpoint using k2k_2)

Stage 4: k4=f(xn+hk3,tn+h)k_4 = f(x_n + hk_3, t_n + h) (derivative at endpoint using k3k_3)

Final update:

xn+1=xn+h6(k1+2k2+2k3+k4)\boxed{x_{n+1} = x_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)}

The Connection to Simpson’s Rule

The weights 16,26,26,16\frac{1}{6}, \frac{2}{6}, \frac{2}{6}, \frac{1}{6} are precisely Simpson’s rule weights from Module 2! This is not a coincidence:

RK4 is essentially applying Simpson’s quadrature to the integral tntn+1f(x(t),t)dt\int_{t_n}^{t_{n+1}} f(x(t), t) dt, but with the complication that we must estimate x(t)x(t) at intermediate points.

Error Analysis

Through Taylor series expansion to fourth order:

This means halving the timestep reduces error by a factor of 16!

The General Runge-Kutta Framework

All explicit RK methods follow this pattern:

ki=f(xn+hj=1i1aijkj,tn+cih)k_i = f\left(x_n + h\sum_{j=1}^{i-1} a_{ij}k_j, t_n + c_ih\right)
xn+1=xn+hi=1sbikix_{n+1} = x_n + h\sum_{i=1}^s b_i k_i

The coefficients (aij,bi,ci)(a_{ij}, b_i, c_i) define the method and are displayed in a Butcher tableau:

c₁ |
c₂ | a₂₁
c₃ | a₃₁ a₃₂
⋮  | ⋮
cₛ | aₛ₁ aₛ₂ ... aₛ,ₛ₋₁
---|--------------------
   | b₁  b₂  ...  bₛ

For RK4:

0   |
1/2 | 1/2
1/2 | 0   1/2
1   | 0   0   1
----|----------------
    | 1/6 2/6 2/6 1/6

Adaptive Timestep Control

Real problems have varying timescales. When a planet is far from the sun, we can take large steps. During close encounters, we need tiny steps. Adaptive methods adjust hh dynamically.

The Embedded Runge-Kutta Approach

The idea: compute two different order estimates using the same function evaluations, then use their difference to estimate error.

Runge-Kutta-Fehlberg Method (RK45)

RK45 computes both 4th and 5th order solutions:

  1. Compute six kik_i values (shared between both estimates)

  2. Combine with 4th-order weights → x(4)x_{(4)}

  3. Combine with 5th-order weights → x(5)x_{(5)}

  4. Error estimate: ϵ=x(5)x(4)\epsilon = ||x_{(5)} - x_{(4)}||

  5. Optimal timestep: hnew=h0.9(tolerance/ϵ)1/5h_{new} = h \cdot 0.9 \cdot (\text{tolerance}/\epsilon)^{1/5}

If error is acceptable, accept the step and continue. Otherwise, reject and retry with smaller hh.

Adaptive timestep control for eccentric orbit (e=0.9). Small steps near perihelion where velocity is high, large steps near aphelion where motion is slow. The top panel shows the elliptical orbit with color-coded timestep sizes, while the bottom panel shows how the timestep varies continuously around the orbit. This concentrates computational effort where dynamics are fastest while maintaining constant error throughout.

Figure 2:Adaptive timestep control for eccentric orbit (e=0.9). Small steps near perihelion where velocity is high, large steps near aphelion where motion is slow. The top panel shows the elliptical orbit with color-coded timestep sizes, while the bottom panel shows how the timestep varies continuously around the orbit. This concentrates computational effort where dynamics are fastest while maintaining constant error throughout.

When to Use Adaptive Methods

Good for:

Bad for:

Performance Comparison

Let’s compare methods on the Kepler problem (elliptical orbit with e=0.017e = 0.017):

MethodOrderSteps/OrbitEnergy Error After 100 OrbitsCPU Time
Euler11000+10%1.0×
RK22100+0.1%0.2×
RK4430+0.0001%0.12×
RK454/515-50+0.00001%0.15×

RK4 achieves 100,000× better accuracy than Euler with 8× less computation!

The Dark Side of High-Order Methods

Despite their impressive local accuracy, RK methods have a fatal flaw for long-term integration:

Energy Drift Comparison

After 10,000 orbits with timestep h=0.01×torbith = 0.01 \times t_\text{orbit}:

Even RK4, accurate to O(h4)O(h^4) per step, exhibits systematic energy drift! After millions of orbits (typical for solar system simulations), even this tiny drift becomes catastrophic.

Why RK Methods Drift

The fundamental issue: RK methods don’t preserve the geometric structure of phase space. They slightly expand or contract volumes, violating Liouville’s theorem. Each step compounds this violation until energy drifts unboundedly.

Higher order reduces the drift rate but doesn’t eliminate it. For billion-year simulations, we need qualitatively different methods.


Bridge to Part 3: The Need for Geometric Integration

The Runge-Kutta family achieves impressive local accuracy through careful sampling of derivatives. RK4’s fourth-order accuracy seems like it should be sufficient for any problem. Yet for long-term integration of Hamiltonian systems, even this isn’t enough.

The issue isn’t accuracy — it’s structure preservation. RK methods focus on minimizing the local truncation error, but they ignore the geometric properties that make physical systems special. Conservation laws, symmetries, and phase space structure are not preserved.

In Part 3, we’ll explore symplectic integrators that sacrifice local accuracy for global stability. These methods preserve the fundamental geometry of phase space, keeping energy bounded even over cosmic timescales. We’ll discover that a second-order symplectic method that keeps your solar system stable for a billion years beats a tenth-order method that spirals planets into the sun.

The transition from RK to symplectic methods represents a philosophical shift: from minimizing error to preserving physics.

Next: Part 3 - Symplectic Integration