Second-Order Linear Equations
Second-order linear equations with constant coefficients, , describe anything that bounces back toward a rest position: springs, pendulums at small angles, circuits, and the spring-based cameras and UI animations in games. A guess of the form reduces them to a quadratic equation, and the three kinds of roots give the three kinds of motion.
The characteristic equation
Try . Then and , and the equation becomes
This characteristic equation is an ordinary quadratic (see polynomials and roots). Its discriminant picks the shape of the general solution:
| Roots | General solution | Behaviour |
|---|---|---|
| : two real | sums of exponentials, no oscillation | |
| : one repeated | the fastest non-oscillating return | |
| : | oscillation inside an envelope |
The complex case uses Euler's formula (see complex numbers). Two constants means two starting values, usually and .
import sympy as sp
t = sp.symbols("t")
y = sp.Function("y")
for b, c in [(5, 6), (4, 4), (2, 5)]:
r = sp.symbols("r")
roots = sp.roots(r**2 + b * r + c, r)
sol = sp.dsolve(y(t).diff(t, 2) + b * y(t).diff(t) + c * y(t))
print(f"y'' + {b}y' + {c}y = 0 roots {roots}")
print(" ", sol.rhs)y'' + 5y' + 6y = 0 roots {-2: 1, -3: 1}
(C1 + C2*exp(-t))*exp(-2*t)
y'' + 4y' + 4y = 0 roots {-2: 2}
(C1 + C2*t)*exp(-2*t)
y'' + 2y' + 5y = 0 roots {-1 - 2*I: 1, -1 + 2*I: 1}
(C1*sin(2*t) + C2*cos(2*t))*exp(-t)
SymPy writes the roots as a dictionary {root: multiplicity}. The three lines show the three rows of the table: −2 and −3; −2 twice; . SymPy likes to factor: is just .
The damped spring
A mass on a spring of stiffness , with a damper (friction) of strength , obeys Newton's second law:
Two numbers describe the motion:
| Name | What you see | |
|---|---|---|
| 0 | undamped | oscillates forever, |
| underdamped | overshoots, rings, settles | |
| 1 | critically damped | fastest return without overshoot |
| overdamped | slow creep back, no overshoot |
Let's plot all three damped cases for , (so ) starting at at rest:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
m, k = 1.0, 4.0
ts = np.linspace(0, 6, 601)
plt.figure(figsize=(6, 4))
for c in [1.0, 4.0, 8.0]:
zeta = c / (2 * np.sqrt(m * k))
sol = solve_ivp(lambda t, s: [s[1], (-k * s[0] - c * s[1]) / m],
(0, 6), [1.0, 0.0], t_eval=ts, rtol=1e-9, atol=1e-12)
x = sol.y[0]
plt.plot(ts, x, label=f"c = {c:g}, ζ = {zeta:g}")
print(f"ζ = {zeta:4.2f}: lowest x = {x.min():7.4f}, x(3) = {x[300]:7.4f}")
plt.axhline(0, color="0.7", lw=0.8)
plt.xlabel("t (s)"); plt.ylabel("x"); plt.legend(); plt.title("Damped spring, m = 1, k = 4")ζ = 0.25: lowest x = -0.4443, x(3) = 0.1723
ζ = 1.00: lowest x = 0.0001, x(3) = 0.0174
ζ = 2.00: lowest x = 0.0432, x(3) = 0.2158
The underdamped spring () swings well past 0 to the negative side. The critically damped one never crosses zero and is the closest to rest at . The overdamped one is "safe" but sluggish.
Game camera springs
A follow camera that snaps instantly to the player feels harsh, and one that glides with plain damping feels floaty. Game code often treats the camera as a mass on a spring attached to the target: . Here sets the speed of the response and gives a quick, overshoot-free follow (Unity's SmoothDamp is built on the same critically damped idea).
def follow(zeta, omega=8.0, dt=1 / 60, seconds=1.5):
x, v, target = 0.0, 0.0, 10.0 # the player teleports to 10
peak, log = 0.0, {}
for frame in range(1, int(seconds / dt) + 1):
a = omega**2 * (target - x) - 2 * zeta * omega * v
v += a * dt # semi-implicit Euler:
x += v * dt # update v first, then x with the new v
peak = max(peak, x)
if frame in (15, 30, 60, 90):
log[frame] = x
return peak, log
for zeta in [0.3, 1.0]:
peak, log = follow(zeta)
marks = " ".join(f"f{f}: {x:6.3f}" for f, x in log.items())
print(f"ζ = {zeta}: max x = {peak:6.3f} {marks}")ζ = 0.3: max x = 13.648 f15: 10.709 f30: 12.585 f60: 9.736 f90: 9.899
ζ = 1.0: max x = 9.997 f15: 6.229 f30: 9.065 f60: 9.949 f90: 9.997
With the camera overshoots to about 13.6, more than a third of the jump past the target, and swings back below 10; with it arrives smoothly and stays within a hair of 10. How we step the equation (semi-implicit Euler at a fixed s) is the subject of physics integration in game loops.
Forced motion, in one paragraph
With a driving force, , the general solution is "any one particular solution" plus the homogeneous solution above. For example has the particular solution , so . When the force oscillates at the system's own frequency, as in , the response grows like : resonance, the reason bridges and washing machines need damping.
In software
- Animation springs: spring-based UI libraries (React Spring, SwiftUI's
.spring, Android'sSpringAnimation) expose stiffness and damping, or directly a damping ratio and a response time. - Game feel: camera follow, weapon sway, procedural recoil and jiggle bones are tuned with and .
- Control systems: a PD controller () is a damped spring; robotics and drone code tune it to be near critical damping.
- Optimisation: gradient descent with momentum on a quadratic loss behaves like a damped spring, which explains its overshoot and oscillation (gradient descent).
Exercises
Exercise 1: Real roots
Solve with and .
Answer
.
Solution
Exercise 2: Classify the damping
A 0.5 kg mass hangs on a spring with N/m. Find and the damper strength that makes it critically damped. What is if ?
Answer
rad/s; critical damping at N·s/m; with , (underdamped).
Solution
Exercise 3: Complex roots
Solve with , , and check it with SymPy.
Answer
.
Solution
import sympy as sp
t = sp.symbols("t")
y = sp.Function("y")
ode = y(t).diff(t, 2) + 6 * y(t).diff(t) + 13 * y(t)
print(sp.dsolve(ode, ics={y(0): 2, y(t).diff(t).subs(t, 0): 0}))Eq(y(t), (3*sin(2*t) + 2*cos(2*t))*exp(-3*t))
Exercise 4: Tune a camera
A designer wants the camera spring from this lesson to be critically damped with . Write the acceleration formula with numbers, and say what happens if they double .
Answer
. With () the camera is overdamped: no overshoot, but slower to arrive.
Solution
The characteristic roots for are , i.e. −1.34 and −18.66. The slow root −1.34 dominates, so the gap shrinks like instead of the critical .
Test yourself
The characteristic equation of has a double root . What is the general solution?
What does a damping ratio give a spring camera?
- Endless oscillation
- Overshoot then ringing
- The fastest return to the target without overshoot
- No movement at all
Which roots does have?
- −2 and −5