The system
A double pendulum is just two rods and two masses — yet it is one of the simplest mechanical systems that exhibits deterministic chaos. There is no closed-form solution: the only way to know where the pendulum will be is to simulate it, step by step.
Why Euler fails
Euler's method is one line of code and it is quietly wrong: every step injects a little energy. After a minute your pendulum swings higher than it started, violating conservation of energy.
Runge-Kutta 4
RK4 samples the derivative four times per step and blends them. The error shrinks with the fourth power of the step size:
const k1 = f(t, y); const k2 = f(t + h/2, y + h/2 * k1); const k3 = f(t + h/2, y + h/2 * k2); const k4 = f(t + h, y + h * k3); y += h/6 * (k1 + 2*k2 + 2*k3 + k4);
Try it yourself
Open the simulation, set the integrator to Euler and watch the energy graph climb. Switch to RK4 — it stays flat for hours.
▶ Open Double Pendulum simulation