Mass-Spring-Damper: Solving a Second-Order ODE Numerically
Rewrite m y″ + c y′ + k y = 0 as a first-order system, then step through it numerically to build the solution one small increment at a time.
The System
Applying Newton's second law (F = ma) gives:
Reducing to a First-Order System
The RK4 method used here, like MATLAB's ode45, advances a first-order system of ODEs. To handle our second-order equation we introduce a new variable:
Then g′ = y″, and substituting into the ODE gives us two coupled first-order equations:
In vector form with u = [y, g]T:
We now have two quantities to track — position y and velocity g — and two rules saying how each changes. A numerical solver can march both forward through time together.
How the Solver Works
ode45 is the name of a popular numerical ODE solver found in MATLAB and many other tools. The name breaks down like this: ode = ordinary differential equation, 4 and 5 = it uses both a 4th-order and a 5th-order approximation at each step so it can automatically judge how accurate its answer is. You don't need MATLAB to understand it — it is just one specific algorithm for the idea described below.
The core idea: small steps in time
We can't solve most ODEs with a formula, but we can approximate a solution by taking many tiny steps forward in time. At each step we know the current position and velocity, so we can calculate the acceleration from Newton's law — and use that to predict where the mass will be a moment later.
The simplest version: Euler's method
The most basic version — Euler's method, which you have already seen in this course — takes a single slope sample at the start of each step:
This works, but the error can accumulate quickly. Using a smaller step size h helps, but then you need many more steps to cover the same time interval.
A better idea: sample the slope several times per step (RK4)
The 4th-order Runge-Kutta method (RK4) — the algorithm behind ode45 — takes four slope samples within each step and combines them in a weighted average. This gives a much more accurate estimate without shrinking h:
ode45 extends this by also computing a 5th-order estimate and comparing it with the 4th-order one. If they disagree by too much, it shrinks the step and tries again; if they agree very closely, it grows the step to save time. This adaptive step size is what makes ode45 both accurate and efficient without you having to choose h manually.
| Method | Slope samples per step | Step size | Typical error per step |
|---|---|---|---|
| Euler | 1 | fixed | O(h²) |
| RK4 | 4 | fixed | O(h⁵) |
| ode45 (RK4+5) | 6 | adaptive | O(h⁵), controlled |
The interactive experiment below uses RK4 with a fixed step size — straightforward to understand, and accurate enough for all the parameter values on the sliders.
Experiment
What You're Seeing
- Undamped (c = 0)
- Pure sinusoidal oscillation. Energy is conserved. The angular frequency is ω₀ = √(k/m).
- Underdamped (c² < 4mk)
- Oscillates with an exponentially decaying envelope. The characteristic roots are complex: r = −c/(2m) ± iωd where ωd = √(k/m − c²/(4m²)).
- Critically damped (c² = 4mk)
- Returns to rest as fast as possible without oscillating. Repeated real root r = −c/(2m).
- Overdamped (c² > 4mk)
- Returns to rest slowly, no oscillations. Two distinct real negative roots.
The curve labelled y(t) is the displacement; y′(t) is the velocity. Both are computed by the RK4 numerical solver described in the How the Solver Works section above.