Skip to content
Tactiverge

Simulating a spring–mass–damper system

Simulate the classic model of anything that vibrates, see how damping changes its behaviour, and connect it to how a controlled robot joint responds.

Lesson 4 of 6 · 35 min · Self-paced · Free

The most useful model in engineering

A car's suspension, a building in an earthquake, a guitar string, a robot gripper finger flexing as it closes, and a robot joint under a position controller all behave, at least approximately, like a mass on a spring with a damper. Learn this one system well and you have a model for a huge range of things that vibrate, overshoot or settle.

Picture a mass m sliding on a frictionless surface, attached to a wall by a spring and a damper side by side. x is how far the mass is from its resting position.

  • The spring pulls the mass back towards rest with force −k · x, where k is the stiffness in N/m. The further you stretch it, the harder it pulls (Hooke's law).
  • The damper, like a piston in oil, resists motion with force −c · v, where c is the damping coefficient in N·s/m. The faster the mass moves, the harder it resists.

Newton's second law gives the acceleration:

m · a = −k · x − c · v
a = (−k · x − c · v) / m

The state is position x and velocity v. The rates of change are v and a. That is everything a simulation needs.

Two numbers describe the behaviour

Without damping, the mass oscillates forever at the natural frequency

ω_n = √(k / m)     (radians per second)
period T = 2π / ω_n

A stiffer spring or a lighter mass oscillates faster.

How quickly the oscillation dies away depends on the damping ratio

ζ = c / (2 · √(k · m))

(ζ is the Greek letter zeta.) The quantity 2√(km) is called critical damping. Three regimes follow:

  • ζ < 1, underdamped: the mass overshoots and oscillates, with the swings shrinking each time.
  • ζ = 1, critically damped: the mass returns to rest as fast as possible without overshooting.
  • ζ > 1, overdamped: no overshoot, but the return is sluggish, as if moving through treacle.

For our example: m = 0.5 kg and k = 20 N/m, so ω_n = √40 ≈ 6.325 rad/s, the period is about 0.993 s, and critical damping is 2√10 ≈ 6.325 N·s/m.

The simulation

We pull the mass 0.10 m from rest, let go, and simulate three damping ratios:

import math

M = 0.5    # kg
K = 20.0   # N/m
C_CRIT = 2 * math.sqrt(K * M)   # critical damping, N·s/m

def simulate(c, x0=0.10, t_end=3.0, dt=0.0001, report_every=0.5):
    """Explicit Euler for m·x'' + c·x' + k·x = 0. Returns reported (t, x)."""
    x, v = x0, 0.0
    out = []
    steps = round(t_end / dt)
    every = round(report_every / dt)
    for step in range(1, steps + 1):
        a = (-K * x - c * v) / M
        x, v = x + v * dt, v + a * dt
        if step % every == 0:
            out.append((step * dt, x))
    return out

print(f"natural frequency: {math.sqrt(K / M):.3f} rad/s, "
      f"period {2 * math.pi / math.sqrt(K / M):.3f} s")
print(f"critical damping:  {C_CRIT:.3f} N·s/m")
for zeta in [0.1, 1.0, 2.0]:
    rows = simulate(zeta * C_CRIT)
    xs = "  ".join(f"{x * 1000:6.1f}" for _, x in rows)
    print(f"zeta = {zeta}: x (mm) at 0.5 s steps: {xs}")

Output:

natural frequency: 6.325 rad/s, period 0.993 s
critical damping:  6.325 N·s/m
zeta = 0.1: x (mm) at 0.5 s steps:  -73.0    53.3   -38.9    28.4   -20.7    15.1
zeta = 1.0: x (mm) at 0.5 s steps:   17.6     1.3     0.1     0.0     0.0     0.0
zeta = 2.0: x (mm) at 0.5 s steps:   46.2    19.8     8.5     3.6     1.6     0.7

The line x, v = x + v * dt, v + a * dt updates both variables at once from their old values, which is exactly Euler's method. We used a very small step, 0.0001 s, for a reason explained in the next lesson.

Reading the results

Each column is half a second apart, which is almost exactly half a period (0.993 s / 2).

  • ζ = 0.1: the sign flips every column, because each half period the mass swings to the other side. The swings shrink by a roughly constant factor (73.0 → 53.3 → 38.9 → …, about 0.73 each time). That constant ratio is the signature of an underdamped system. After 3 s the mass is still 15 mm from rest.
  • ζ = 1.0: the mass is at 17.6 mm after 0.5 s and effectively at rest by 1.5 s, with no overshoot. This is the fastest return without crossing zero.
  • ζ = 2.0: no overshoot, but after 3 s the mass is still 0.7 mm away. More damping is not always better.

Verification. This system has an exact solution too. For ζ = 0.1, the textbook formula gives −72.9, 53.2, −38.8, 28.3, −20.6 and 15.0 mm at the same times, and for ζ = 1 it gives 17.6, 1.3 and 0.1 mm. The simulation agrees within 0.1 mm. You can also check that the oscillation's period matches 2π / ω_n.

Why robot engineers care: a joint under PD control

In Robotics Foundations you met proportional control. Add a derivative term and the controller's torque on a joint is

torque = Kp · (target − angle) − Kd · angular_velocity

Compare this with the spring and damper: the proportional gain Kp acts like a spring pulling the joint towards the target, and the derivative gain Kd acts like a damper resisting fast motion. The joint's inertia plays the role of the mass. So a PD-controlled joint behaves like a spring–mass–damper, and everything above applies:

  • Too little Kd (low ζ): the arm overshoots the target and wobbles.
  • ζ near 1: fast, clean arrival with little or no overshoot.
  • Too much Kd (high ζ): sluggish movement.
  • Raising Kp makes the joint "stiffer" and faster (higher ω_n), but for the same damping ratio Kd must rise too.

This is why simulation is a standard first step in controller tuning: you can try gains safely in software before running them on a real arm, then validate on the hardware.

Recap

  • A spring–mass–damper obeys m · a = −k · x − c · v. Its state is position and velocity.
  • The natural frequency ω_n = √(k/m) sets how fast it oscillates.
  • The damping ratio ζ = c / (2√(km)) sets whether it is underdamped (ζ < 1), critically damped (ζ = 1) or overdamped (ζ > 1).
  • Verify the simulation against the exact solution or at least against the expected period.
  • A PD-controlled joint behaves like this system, with Kp as the spring and Kd as the damper.

Project

Find the fastest settling damping

Using the simulation from this lesson, investigate how damping affects settling time: the time after which the mass stays within 2% of its starting displacement (here, within ±2 mm).

  1. Write a function settling_time(c) that simulates the release from 0.10 m and returns the last time at which |x| exceeds 0.002 m.
  2. Compute settling times for ζ = 0.1, 0.2, 0.3, …, 2.0 and print them in a table.
  3. Which ζ settles fastest? (You may find it is a little below 1, because a small overshoot that stays inside the band can be acceptable.)
  4. Double k and repeat. What happens to the best ζ, and to the best settling time?
  5. Explain, in two or three sentences, what this suggests about tuning Kd for a PD-controlled joint.

Deliverables

  • A settling_time function and a table of settling time against ζ
  • The best ζ for both stiffness values
  • A short explanation linking the result to PD controller tuning

Knowledge check

Check your understanding

  1. 1. A mass of 2 kg hangs on a spring of stiffness 50 N/m. What is its natural frequency ω_n?
  2. 2. A system with ζ = 0.1 is released from rest away from its equilibrium. What will it do?
  3. 3. For m = 0.5 kg and k = 20 N/m, what damping coefficient c gives critical damping?
  4. 4. In a PD-controlled robot joint, which gain behaves most like the damper?
  5. 5. Why might ζ = 2 be a poor choice even though it never overshoots?

Go further

Simulating a spring–mass–damper system — Computational Engineering — Tactiverge