Back

ODE Solvers: Euler vs RK4 vs RK45

What you are seeing: all three methods integrate x¨=ω2x\ddot x = -\omega^2 x from the same initial condition. Euler's explicit step pumps energy upward without bound; RK4 stays close to the true orbit at fourth-order accuracy; adaptive RK45 chooses its own step size from local error estimates. The phase-space plot at right shows each trajectory in (x,v)(x, v).

Figure 1. Three ODE solvers integrating the same simple harmonic oscillator.
system
Δt0.05
ω0.50

WHAT TO TRY

  • Watch the energy: explicit Euler spirals outward, pumping energy in without bound, while RK4 hugs the true orbit and RK45 adapts its step to a tolerance.
  • Enlarge the time step: every method degrades, but Euler blows up first. Accuracy and stability are bought with smaller steps or higher order.
  • Read the energy-drift readout: for a Hamiltonian system the conserved energy is the honest scorecard, exposing which integrator is quietly lying.
  • Change the system: the large-angle pendulum bends into an anharmonic orbit, the damped oscillator spirals inward, and Van der Pol settles onto a relaxation limit cycle. Each is scored against a converged reference, so the same Euler-versus-RK4-versus-RK45 gap shows up on any equation.