Skip to content

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

k c m y f = −ky f = −cy′
The spring (top) and damper (bottom) are connected in parallel between the fixed wall and the mass. When the mass moves right by y, the spring pulls it back with force −ky; the damper resists its velocity with force −cy′.

Applying Newton's second law (F = ma) gives:

m y″ + c y′ + k y = 0

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:

g = y′

Then g′ = y″, and substituting into the ODE gives us two coupled first-order equations:

y′ = g
g′ = −(c g + k y) / m

In vector form with u = [y, g]T:

u′ = [ 0 −k/m 1 −c/m ] u

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

What is ode45?

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.

t t + h one step of size h known: y, y′ predicted: y, y′ t
Starting from a known state at time t, the solver estimates the state at t + h, then repeats.

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:

y(t + h) ≈ y(t) + h · y′(t)
y′(t + h) ≈ y′(t) + h · y″(t)

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:

k₁ t k₂ t + h/2 k₃ k₄ t + h
RK4 evaluates the slope at four points and combines them: y(t+h) ≈ y(t) + (h/6)(k₁ + 2k₂ + 2k₃ + k₄). The midpoint slopes k₂ and k₃ are weighted more heavily.

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.

Comparing the methods
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

Parameters
Initial Conditions
Presets:
eq m
y(t) — displacement
y′(t) — velocity
Current ODE:

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.