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 1: The Failure of Naive Integration

ODE Methods & Conservation | Numerical Methods Module 3 | ASTR 596

San Diego State University

Learning Outcomes

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


From Continuous to Discrete

The fundamental ODE initial value problem asks us to find a function x(t)x(t) given its rate of change:

dxdt=f(x,t),x(t0)=x0\frac{dx}{dt} = f(x, t), \quad x(t_0) = x_0

This has the formal integral solution:

x(t)=x0+t0tf(x(s),s)dsx(t) = x_0 + \int_{t_0}^t f(x(s), s) ds

But this Initial Value Problem contains a circular dependency: to find x(t)x(t), we need to integrate f(x(s),s)f(x(s), s), but ff depends on the unknown solution x(s)x(s) itself!

Numerical methods break this circular dependency by making assumptions about how ff behaves over small time intervals:

  1. Constant derivative assumption (Euler): Assume ff stays constant over [tn,tn+1][t_n, t_{n+1}]

  2. Linear variation assumption (Trapezoidal): Assume ff varies linearly between endpoints

  3. Polynomial approximation (Runge-Kutta): Assume ff follows a polynomial of degree pp

Each assumption leads to a different family of methods with different error behaviors, stability properties, and conservation characteristics.

The Dimensional Analysis of Timesteps

Before diving into specific methods, let’s understand what constrains our choice of timestep hh from pure dimensional analysis.

For any oscillatory system with characteristic frequency ω\omega:

For gravitational N-body systems, multiple timescales compete:

Example - Earth-Sun system:


Euler’s Method - The Simplest Approach

Mathematical Formulation

Euler’s method is the most straightforward discretization possible. Given the ODE

dxdt=f(x,t),\frac{dx}{dt} = f(x,t),

at time tnt_n with solution xnx_n, and a timestep hh, we approximate:

xn+1=xn+hf(xn,tn)\boxed{x_{n+1} = x_n + h f(x_n, t_n)}

This assumes the derivative ff remains constant over the entire interval [tn,tn+1][t_n, t_{n+1}] — essentially extending the tangent line at tnt_n forward by distance hh.

Euler’s method extends the tangent line at each point, accumulating error by ignoring the solution’s curvature. The diagram shows how Euler follows straight line segments (red) that deviate increasingly from the true curved solution (blue). The local truncation error at each step is proportional to h², but these errors accumulate to give O(h) global error.

Figure 1:Euler’s method extends the tangent line at each point, accumulating error by ignoring the solution’s curvature. The diagram shows how Euler follows straight line segments (red) that deviate increasingly from the true curved solution (blue). The local truncation error at each step is proportional to h2, but these errors accumulate to give O(h)O(h) global error.

Taylor Series Analysis of Error

To understand Euler’s error precisely, we need the Taylor series. The true solution at tn+1=tn+ht_{n+1} = t_n + h is:

x(tn+h)=x(tn)+hx(tn)+h22x(tn)+h36x(tn)+O(h4)x(t_n + h) = x(t_n) + h x'(t_n) + \frac{h^2}{2}x''(t_n) + \frac{h^3}{6}x'''(t_n) + O(h^4)

Since x(tn)=f(x(tn),tn)x'(t_n) = f(x(t_n), t_n) by definition of our ODE, Euler’s method gives:

xn+1=xn+hf(xn,tn)\boxed{x_{n+1} = x_n + h f(x_n, t_n)}

The local truncation error — the error in one step — is:

τn=x(tn+1)xn+1=h22x(tn)+O(h3)\tau_n = x(t_{n+1}) - x_{n+1} = \frac{h^2}{2}x''(t_n) + O(h^3)

The leading error term is O(h2)O(h^2), making Euler locally second-order accurate. But errors accumulate! Over N=tfinal/hN = t_\text{final}/h steps to reach final time tfinalt_\text{final}, the global error becomes:

Eglobal=i=0N1τiNh22x(ξ)=tfinalhh22x(ξ)=tfinalh2x(ξ)=O(h)E_\text{global} = \sum_{i=0}^{N-1} \tau_i \approx N \cdot \frac{h^2}{2}x''(\xi) = \frac{t_\text{final}}{h} \cdot \frac{h^2}{2}x''(\xi) = \frac{t_\text{final} h}{2}x''(\xi) = O(h)

Euler is globally first-order: halving the timestep only halves the total error.

The Energy Drift Catastrophe

Let’s see Euler fail catastrophically on the harmonic oscillator:

x¨=ω2x\ddot{x} = -\omega^2 x

Converting to first-order system:

ddt(xv)=(vω2x)\frac{d}{dt}\begin{pmatrix} x \\ v \end{pmatrix} = \begin{pmatrix} v \\ -\omega^2 x \end{pmatrix}

The true solution has constant energy E=12(v2+ω2x2).E = \frac{1}{2}(v^2 + \omega^2 x^2).

Implementation and Analysis

import numpy as np

def euler_harmonic(x0, v0, omega, h, n_steps):
    """Integrate harmonic oscillator with Euler's method"""
    x, v = x0, v0
    E0 = 0.5 * (v0**2 + omega**2 * x0**2)
    
    energies = [E0]
    for i in range(n_steps):
        # Euler update
        a = -omega**2 * x
        x = x + h * v
        v = v + h * a
        
        # Energy (should be constant, but...)
        E = 0.5 * (v**2 + omega**2 * x**2)
        energies.append(E)
    
    return energies

The shocking result: energy grows approximately linearly with time! After 1000 orbital periods, energy has typically increased by 10%. The orbit spirals outward, violating conservation of energy.

Mathematical Analysis of Energy Growth

For the harmonic oscillator with Euler’s method, we can derive the exact energy growth rate. Starting with position xnx_n and velocity vnv_n:

xn+1=xn+hvnx_{n+1} = x_n + hv_n
vn+1=vnhω2xnv_{n+1} = v_n - h\omega^2x_n

The energy after one step:

En+1=12(vn+12+ω2xn+12)E_{n+1} = \frac{1}{2}(v_{n+1}^2 + \omega^2 x_{n+1}^2)

Substituting the updates:

En+1=12[(vnhω2xn)2+ω2(xn+hvn)2]E_{n+1} = \frac{1}{2}[(v_n - h\omega^2x_n)^2 + \omega^2(x_n + hv_n)^2]

Expanding:

En+1=12[vn22hω2xnvn+h2ω4xn2+ω2xn2+2hω2xnvn+h2ω2vn2]E_{n+1} = \frac{1}{2}[v_n^2 - 2h\omega^2x_nv_n + h^2\omega^4x_n^2 + \omega^2x_n^2 + 2h\omega^2x_nv_n + h^2\omega^2v_n^2]
En+1=12[vn2+ω2xn2]+h2ω22[vn2+ω2xn2]E_{n+1} = \frac{1}{2}[v_n^2 + \omega^2x_n^2] + \frac{h^2\omega^2}{2}[v_n^2 + \omega^2x_n^2]

Therefore:

En+1=En(1+h2ω2)\boxed{E_{n+1} = E_n(1 + h^2\omega^2)}

The amplification factor (1+h2ω2)>1(1 + h^2\omega^2) > 1 means energy grows exponentially! After NN steps:

ENE0(1+h2ω2)NE0eNh2ω2=E0etfinalhω2E_N \approx E_0(1 + h^2\omega^2)^N \approx E_0 e^{Nh^2\omega^2} = E_0 e^{t_\text{final}h\omega^2}

For Earth’s orbit with h=1h = 1 day and tfinal=1000t_\text{final} = 1000 years:

EfinalE0e0.11.1\frac{E_{final}}{E_0} \approx e^{0.1} \approx 1.1

A 10% energy increase means that Earth would drift into a higher orbit!

Euler’s method systematically violates energy conservation with catastrophic consequences. The figure shows a harmonic oscillator integrated over 10 periods with timestep h = 0.02: (Top left) Phase space trajectory spirals outward as energy increases ~3.5×. (Top right) Energy grows monotonically from amplification factor (1 + h²ω²) ≈ 1.0004 per step compounding over many orbits. (Bottom left) Spatial orbit expands continuously—a planet would spiral away from its star! (Bottom right) Error analysis shows systematic energy injection (red line) versus oscillating position errors (teal). This demonstrates why Euler fails for long-term dynamics despite being locally accurate.

Figure 2:Euler’s method systematically violates energy conservation with catastrophic consequences. The figure shows a harmonic oscillator integrated over 10 periods with timestep h = 0.02: (Top left) Phase space trajectory spirals outward as energy increases ~3.5×. (Top right) Energy grows monotonically from amplification factor (1 + h²ω²) ≈ 1.0004 per step compounding over many orbits. (Bottom left) Spatial orbit expands continuously—a planet would spiral away from its star! (Bottom right) Error analysis shows systematic energy injection (red line) versus oscillating position errors (teal). This demonstrates why Euler fails for long-term dynamics despite being locally accurate.

Why Euler Fails: The Geometric View

In phase space (position-velocity space), the harmonic oscillator traces a circle. Each Euler step moves along the tangent to the circle, placing the new point slightly outside. The phase space area increases, violating Liouville’s theorem that phase space volume must be preserved in Hamiltonian systems.

The Phase Error Problem

Even if we could tolerate energy drift, Euler has another fatal flaw: phase error. The frequency of oscillation is wrong:

ωnumerical=ωtrue(1+O(h2))\omega_{numerical} = \omega_{true}(1 + O(h^2))

After N=tfinal/hN = t_\text{final}/h oscillations, the phase error is:

Δϕ=NO(h2)=ThO(h2)=O(h)\Delta\phi = N \cdot O(h^2) = \frac{T}{h} \cdot O(h^2) = O(h)

This means that even with small timesteps, the phase error accumulates linearly with time.

When Does Euler Work?

Despite these failures, Euler has legitimate uses:

  1. Very short integrations where Nh21N h^2 \ll 1

  2. Highly dissipative systems where energy should decay

  3. Quick explorations before using better methods

  4. Teaching why better methods are needed!

The Fundamental Lesson

Euler’s method reveals a profound truth about numerical integration:

Local accuracy does not guarantee global stability

Euler is locally second-order accurate (O(h2)O(h^2) per step), yet it systematically violates conservation laws. This isn’t a bug — it’s a fundamental property of the discretization.

The tangent line approximation, while locally accurate, doesn’t respect the curved geometry of phase space. Each step compounds this geometric error until the qualitative behavior is wrong.


Bridge to Part 2: The Quest for Better Methods

Euler’s catastrophic failure motivates our search for better integration methods. The failure isn’t just about accuracy — it’s about systematic bias. Every Euler step pushes slightly outward from the true trajectory. This accumulation of geometric errors destroys the physics we’re trying to simulate.

What we need are methods that:

In Part 2, we’ll explore the Runge-Kutta family — methods that evaluate the derivative at carefully chosen intermediate points to achieve higher accuracy. By sampling the “curvature” of the solution, these methods can follow the true trajectory more faithfully. But as we’ll discover, even fourth-order accuracy isn’t enough to preserve energy over cosmic timescales.

The journey from Euler to modern integration methods is a journey from naive approximation to deep understanding of geometric structure. Each method we develop addresses specific failures of its predecessors, leading ultimately to symplectic integrators that preserve the fundamental geometry of physics.

Next: Part 2 - Building Better Methods