How do you simulate a pendulum in code?
Turn the pendulum equation into a state you can step forward in time. Compare explicit Euler, semi-implicit Euler and RK4 with real energy-drift numbers, in JavaScript.
How do you simulate a pendulum in code?
Write the state as two numbers — the angle and the angular velocity — and advance them in small time steps using the acceleration . The choice of integrator matters more than it looks: explicit Euler makes the pendulum gain energy without limit, while semi-implicit Euler and RK4 keep it stable.
From one equation to a state you can step
This article is the code companion to How a pendulum works, where the equation is derived. A computer steps first-order systems, so introduce :
Each step of length moves a little forward. The three integrators below differ only in how they do that.
Three integrators in a few lines
const g = 9.81, L = 1;
const accel = (theta) => -(g / L) * Math.sin(theta);
// Energy per unit mass — used to check each method
const energy = ({ theta, omega }) =>
0.5 * L * L * omega ** 2 + g * L * (1 - Math.cos(theta));
// 1. Explicit Euler: both updates use the OLD state
function explicitEuler(s, dt) {
const a = accel(s.theta);
s.theta += s.omega * dt;
s.omega += a * dt;
}
// 2. Semi-implicit Euler: velocity first, then position with the NEW velocity
function semiImplicitEuler(s, dt) {
s.omega += accel(s.theta) * dt;
s.theta += s.omega * dt;
}
// 3. Classic 4th-order Runge–Kutta
function rk4(s, dt) {
const f = (th, om) => [om, accel(th)];
const [k1t, k1o] = f(s.theta, s.omega);
const [k2t, k2o] = f(s.theta + 0.5 * dt * k1t, s.omega + 0.5 * dt * k1o);
const [k3t, k3o] = f(s.theta + 0.5 * dt * k2t, s.omega + 0.5 * dt * k2o);
const [k4t, k4o] = f(s.theta + dt * k3t, s.omega + dt * k3o);
s.theta += (dt / 6) * (k1t + 2 * k2t + 2 * k3t + k4t);
s.omega += (dt / 6) * (k1o + 2 * k2o + 2 * k3o + k4o);
}
// Run it
const state = { theta: 1, omega: 0 }; // released from rest at 1 rad (~57°)
for (let t = 0; t < 100; t += 0.02) semiImplicitEuler(state, 0.02);
console.log(energy(state));How much energy does each method lose or gain?
A frictionless pendulum must conserve energy, so drift in is a direct measure of numerical error. We released the bob from rad on a m string and ran s (about 50 swings):
| Method | Δt = 0.02 s | Δt = 0.005 s |
|---|---|---|
| Explicit Euler | +2,175% (blows up) | +566% |
| Semi-implicit Euler | wobbles within ±3% | wobbles within ±0.7% |
| RK4 | 0.0003% | 0.0000003% |
Explicit Euler pushes the bob slightly further out on every swing, so the amplitude keeps growing. Semi-implicit Euler is symplectic: the energy error oscillates around the true value instead of drifting away, which is why it is the default in most game and physics engines. RK4 is dramatically more accurate per step, but it does not have that structural guarantee, so its error still creeps upward over very long runs.
What PhysicsHub does
- The Simple Pendulum simulation does not integrate an angle at all: the bob is a free body under gravity and the string is a
Distanceconstraint to the pivot, stepped with semi-implicit Euler at a fixed 1/120 s. - The Double Pendulum integrates the exact Lagrangian equations for the two angles with
rk4, because chaos amplifies any error and a constraint solver on free masses would not be accurate enough.
Frequently asked questions
Why does explicit Euler make a pendulum gain energy?
What is a symplectic integrator?
When should I use RK4 instead of semi-implicit Euler?
How small should the time step be?
Keep exploring
- The physics behind this code: How does a pendulum work?.
- See both methods running: Simple Pendulum and Double Pendulum.
- The same idea for a falling ball with drag: Free fall and air resistance.