How a Pendulum Teaches You Differential Equations
You don't need a math degree to understand ODE solvers. You need a pendulum, a screen, and 20 minutes. Here's how Euler, RK4, and adaptive methods actually work — with the real code.
Start With What You Can See
Hang a weight from a string. Pull it to one side. Let go. It swings.
You just created a system governed by a differential equation:
dθ/dt = ω dω/dt = -(g/L) · sin(θ)
θ is the angle. ω is the angular velocity. g is gravity (9.81 m/s²). L is the string length. These two lines say: the angle changes at a rate equal to the velocity, and the velocity changes at a rate that depends on gravity, length, and the current angle.
The problem: we can't solve this equation exactly. The sin(θ) makes it nonlinear. There's no formula that gives you θ at any time t. So we approximate — we step forward in small increments, computing the next state from the current one.
That's what an ODE solver does. And there are better and worse ways to do it.
The Euler Method: Obvious but Flawed
The simplest idea: if I know the state now, and I know the rate of change, I can estimate the state a small time step later.
next_angle = current_angle + velocity × dt
next_velocity = current_velocity + acceleration × dt
This is Euler's method. It's like walking through fog: you can see one step ahead, so you take that step, then look again. In code:
function euler(f, t, y, dt) {
const dy = f(t, y);
return [
y[0] + dt * dy[0],
y[1] + dt * dy[1]
];
}
The problem: Euler's method is first-order accurate. That means if you halve the step size, you halve the error. For a pendulum, this error accumulates — the simulated pendulum slowly gains energy and swings wider and wider. After a few minutes, it's spinning in full circles. A real pendulum never does this.
RK4: The Workhorse
In 1901, Carl Runge and Martin Kutta published a better method. Instead of looking at the rate of change once per step, look at it four times:
- Measure the slope at the start of the step → k₁
- Step halfway using k₁, measure the slope there → k₂
- Step halfway using k₂, measure again → k₃
- Step to the end using k₃, measure one more time → k₄
- Combine: weighted average (k₁ + 2k₂ + 2k₃ + k₄) / 6
This is fourth-order accurate. Halve the step size and the error drops by a factor of 16. The pendulum conserves energy correctly for thousands of swings.
function rk4(f, t, y, dt) {
const k1 = f(t, y);
const k2 = f(t + dt/2, [y[0] + dt/2 * k1[0], y[1] + dt/2 * k1[1]]);
const k3 = f(t + dt/2, [y[0] + dt/2 * k2[0], y[1] + dt/2 * k2[1]]);
const k4 = f(t + dt, [y[0] + dt * k3[0], y[1] + dt * k3[1]]);
return [
y[0] + (dt/6) * (k1[0] + 2*k2[0] + 2*k3[0] + k4[0]),
y[1] + (dt/6) * (k1[1] + 2*k2[1] + 2*k3[1] + k4[1])
];
}
This is the method PinePaper uses for pendulum, spring-mass, and Van der Pol simulations. It's the same method used in aerospace trajectory calculations. 22 lines of code.
Why This Matters Outside Physics
The pendulum is a teaching example. But the same technique — step forward, measure, correct — applies anywhere you have a rate of change:
- Population growth: dx/dt = r·x·(1 - x/K). Logistic growth with carrying capacity. Same solver.
- Chemical reactions: concentrations change at rates proportional to current concentrations. Same solver.
- Neural networks: gradient descent is a discretized ODE. Each training step is an Euler step along the loss surface.
- Economics: interest compounds continuously via dy/dt = r·y. Exponential growth is the simplest ODE.
- Animation timing: easing curves are solutions to spring-damper ODEs. The "elastic" and "bounce" easings in CSS are physical simulations.
The math doesn't change. The domain does. That's what makes it a language — the same grammar describes sheep counting, pendulum swings, and neural network training.
Try It Yourself
Open PinePaper and select the Dynamic System generator. Choose "pendulum." The bob swings. Now change the parameters:
- Increase gravity → faster swing (shorter period)
- Increase rod length → slower swing (longer period)
- Start at a larger angle → watch how the period increases (nonlinear effect that the textbook small-angle approximation misses)
Every change you make is a measurement. You changed a parameter and observed the result. The RK4 solver recomputed 30 frames per second, and the pendulum showed you what the equation predicts.
That's the whole point. Mathematics is measurement. PinePaper makes it visible.
References
- Butcher, J.C. (2016). Numerical Methods for Ordinary Differential Equations (3rd ed.). Wiley.
- Dormand, J.R. & Prince, P.J. (1980). A family of embedded Runge-Kutta formulae. Journal of Computational and Applied Mathematics, 6(1), 19-26.
- Euler, L. (1768). Institutionum calculi integralis, Vol. 1. Impensis Academiae Imperialis Scientiarum.
- Hairer, E., Nørsett, S.P., & Wanner, G. (1993). Solving Ordinary Differential Equations I: Nonstiff Problems (2nd ed.). Springer.
- Kutta, W. (1901). Beitrag zur näherungsweisen Integration totaler Differentialgleichungen. Zeitschrift für Mathematik und Physik, 46, 435-453.
- Newton, I. (1687). Philosophiæ Naturalis Principia Mathematica. London: Joseph Streater.
- Runge, C. (1895). Über die numerische Auflösung von Differentialgleichungen. Mathematische Annalen, 46(2), 167-178.
- Strogatz, S.H. (2015). Nonlinear Dynamics and Chaos (2nd ed.). Westview Press.
PinePaper's ODE solver covers Euler, RK4, and adaptive Dormand-Prince RK45 in about 200 lines. Try the pendulum simulation free at pinepaper.studio/editor.
Ready to create?
Start making animated GIFs, videos, and graphics — free, no signup.
Open PinePaper Editor