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 | COMP 521

Learning Objectives

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

Initial Value Problem (IVP): Finding a function given its derivative and starting value — the fundamental problem of dynamics.

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. Matched intermediate stages (Runge-Kutta): Evaluate ff at predicted states so the update matches the solution Taylor expansion to a chosen order

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 an oscillatory system, the dimensionless quantity ωh\omega h compares the step with the dynamical timescale. If T=2π/ωT=2\pi/\omega, then h=0.1Th=0.1T gives 10 steps per period and h=0.01Th=0.01T gives 100. Neither choice is an accuracy guarantee.

Sampling a prescribed band-limited signal above its Nyquist rate is a different problem from integrating its dynamics. An ODE method must also be stable: forward Euler is unstable on a pure oscillator for every nonzero hh, while leapfrog’s oscillator stability interval is 0<ωh<20<\omega h<2.

For gravitational systems, compare the orbital timescale T=2πa3/(GM)T=2\pi\sqrt{a^3/(GM)} with encounter scales such as R/vrelR/v_{\rm rel} and R3/[G(m1+m2)]\sqrt{R^3/[G(m_1+m_2)]}. Close encounters can introduce a much shorter scale than the main orbit. No fixed fraction of one timescale guarantees accuracy for every orbit, method, or requested observable.

Example — a one-year period: a one-day step gives about 365 steps per period; a tenth-day step gives about 3650. Both are candidate resolutions to compare, not labels meaning “ordinary” and “high” accuracy. Measure trajectory, energy, and phase errors under refinement over the actual duration you need.


Euler’s Method - The Simplest Approach

Mathematical Formulation

Euler’s method: The simplest ODE integration scheme, using a constant derivative over each timestep.

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

Figure 1:Euler’s method extends the tangent line at each point. This dimensionless example solves y′=−2y+ty'=-2y+t, y(0)=1y(0)=1, with exact reference y(t)=54e−2t+12t−14y(t)=\tfrac54e^{-2t}+\tfrac12t-\tfrac14 (the figure labels the independent variable xx). The left panel compares single steps; the right compares repeated steps through t=2t=2. The final absolute errors are about 0.01534 for h=0.2h=0.2 and 0.02249 for h=0.4h=0.4. Error is not monotone in time here: it grows initially and then decreases. For a smooth problem over a fixed interval, Euler’s one-step error is O(h2)O(h^2) and its global error is O(h)O(h).

Open full-size figure

Taylor Series Analysis of Error

Local truncation error: Error introduced in a single step, assuming perfect starting values.

Global error: Total accumulated error over the entire integration interval.

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 one-step defect is O(h2)O(h^2); Euler is called a first-order method because its global error is O(h)O(h) on a fixed time interval under smoothness and stability assumptions. Global error is not generally the unweighted sum of the local defects: a wrong state changes the next derivative.

If ff is Lipschitz in the state with constant LL and each defect is bounded by Ch2Ch^2, then, starting from an exact initial value,

∣en+1∣≤(1+hL)∣en∣+Ch2,|e_{n+1}|\le(1+hL)|e_n|+Ch^2,

and summing this geometric bound gives, for L>0L>0,

∣eN∣≤ChL[(1+hL)N−1]≤ChL(eLT−1),T=Nh.|e_N|\le\frac{Ch}{L}\big[(1+hL)^N-1\big] \le\frac{Ch}{L}(e^{LT}-1),\qquad T=Nh.

For L=0L=0, the bound is CThCTh. This explains the O(h)O(h) global order at fixed TT and why the duration matters. Halving hh approximately halves the leading error only while that term dominates.

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 energy increases at every step. For this oscillator we can derive the complete growth law, so there is no need to attach an unexplained percentage to a particular number of periods. The apparent linear growth at short times is the beginning of an exponential.

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=vn−hω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[(vn−hω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[vn2−2hω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:

EN=E0(1+h2ω2)N≈E0eNh2ω2=E0etfinalhω2E_N = 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}

Take an oscillator with a one-year period and express time in days: ω=2π/365.25 day−1\omega=2\pi/365.25\ \mathrm{day}^{-1}, h=1 dayh=1\ \mathrm{day}, and T=1000(365.25) dayT=1000(365.25)\ \mathrm{day}. Then

log⁡(EN/E0)=365250log⁡[1+(2π/365.25)2]≈108.1.\log(E_N/E_0)=365250\log\left[1+(2\pi/365.25)^2\right]\approx108.1.

The ratio is of order 1047, not 1.1. This is an oscillator benchmark with Earth’s period, not a numerical solution of the nonlinear Earth–Sun problem. The formula cannot be transferred to a Kepler orbit after its radius changes.

Euler's oscillator trajectory spirals outward and its energy grows to about 3.51 times its initial value.

Figure 2:The dimensionless oscillator uses ω=1\omega=1, h=0.02h=0.02, and 3142 steps (approximately ten periods). Left: Euler expands the scaled phase-plane trajectory relative to the exact unit circle. Right: the exact discrete energy factor compounds to about 3.51. This is an oscillator diagnostic, not a computed spatial orbit in an inverse-square force.

Open full-size figure

Why Euler Fails: The Geometric View

Phase space: The space of all possible states, with position and momentum coordinates. Trajectories follow energy contours.

Liouville’s theorem: Phase space volume must be preserved in Hamiltonian systems

In scaled phase coordinates (x,v/ω)(x,v/\omega), the harmonic oscillator traces a circle; in unscaled position-velocity coordinates it generally traces an ellipse. 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

Energy and phase diagnose different errors. In scaled coordinates, one Euler step rotates by arctan⁡(ωh)\arctan(\omega h) while expanding the radius. Therefore

ωnum=arctan⁡(ωh)h=ω−ω3h23+O(h4).\omega_{\rm num}=\frac{\arctan(\omega h)}h =\omega-\frac{\omega^3h^2}{3}+O(h^4).

After N=T/hN=T/h steps, the unwrapped phase discrepancy is

Δϕ=T(ωnum−ω)=−Tω3h23+O(Th4).\Delta\phi=T(\omega_{\rm num}-\omega) =-\frac{T\omega^3h^2}{3}+O(Th^4).

At fixed duration the oscillator’s phase error is second order even though Euler’s overall state error is first order: amplitude growth dominates the state error. At fixed hh, the phase lag accumulates linearly with time. This special diagnostic order does not make Euler a second-order integrator.

Predict: when hh is halved at fixed TT, should the leading energy error halve or quarter? What about the phase error?

n=100: relative energy error=4.82911e-01, phase lag=-8.24881e-03 rad
n=200: relative energy error=2.18103e-01, phase lag=-2.06586e-03 rad
n=400: relative energy error=1.03717e-01, phase lag=-5.16695e-04 rad

When Does Euler Work?

Despite these failures, Euler has legitimate uses:

  1. Very short integrations where N(ωh)2≪1N(\omega h)^2 \ll 1 for this oscillator

  2. Resolved dissipative systems where the step also satisfies the stability limit; stiffness can make explicit Euler impractical

  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 has a second-order one-step defect (O(h2)O(h^2)) and first-order global error, 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