Applications of Second-Order Equations
Springs, electric circuits and smooth-follow cameras obey the same equation, . This lesson builds the spring model from Newton's second law, classifies the three kinds of damping, derives the resonance curve of a forced oscillator, maps the model onto an RLC circuit, and ends with the critically damped spring that game and UI programmers use to make a camera follow a target without overshooting.
The methods come from Second-Order Linear Equations and Nonhomogeneous Linear Equations. For a survey-level view of oscillators see Second-order equations.
The mass–spring–damper
A mass on a spring is pulled back by Hooke's force ( = displacement from equilibrium, = stiffness) and slowed by a damping force proportional to velocity. Newton's law gives
| Case | Condition | Roots | Motion |
|---|---|---|---|
| Undamped | , | , period | |
| Underdamped | decaying oscillation, | ||
| Critically damped | (double) | fastest return without crossing zero more than once | |
| Overdamped | two negative reals | slow creep back, no oscillation |
The critical damping constant is . The ratio (the damping ratio) is how engineers and animation libraries describe a spring: bounces, is critical, is sluggish.
import numpy as np
import matplotlib.pyplot as plt
m, k = 2.0, 50.0 # c_crit = 2*sqrt(100) = 20
t = np.linspace(0, 3, 400)
plt.figure(figsize=(6, 4))
for c in [4.0, 20.0, 60.0]:
r = np.roots([m, c, k])
print(f"c = {c:4.0f} zeta = {c / (2*np.sqrt(m*k)):.1f} roots = {np.round(r, 3)}")
# solve numerically from x(0) = 1, x'(0) = 0 with small RK4 steps
x, v, xs = 1.0, 0.0, []
h = t[1] - t[0]
f = lambda x, v: (v, -(c*v + k*x) / m)
for _ in t:
xs.append(x)
k1 = f(x, v); k2 = f(x + h/2*k1[0], v + h/2*k1[1])
k3 = f(x + h/2*k2[0], v + h/2*k2[1]); k4 = f(x + h*k3[0], v + h*k3[1])
x += h/6*(k1[0] + 2*k2[0] + 2*k3[0] + k4[0]); v += h/6*(k1[1] + 2*k2[1] + 2*k3[1] + k4[1])
plt.plot(t, xs, label=f"c = {c:.0f}")
plt.axhline(0, color="gray", lw=0.5); plt.xlabel("t (s)"); plt.ylabel("x (m)"); plt.legend()c = 4 zeta = 0.2 roots = [-1.+4.899j -1.-4.899j]
c = 20 zeta = 1.0 roots = [-5. -5.]
c = 60 zeta = 3.0 roots = [-29.142 -0.858]
Forced oscillation and resonance
Drive the spring with . With damping, the complementary solution dies out and the steady state is the particular solution . Undetermined coefficients with the trial gives
With little damping the amplitude peaks sharply near : resonance, the reason soldiers break step on bridges and why washing machines shake at one spin speed. The program compares the formula with a long simulation.
import numpy as np
from scipy.integrate import solve_ivp
m, c, k, F0 = 1.0, 1.0, 25.0, 1.0 # omega_0 = 5
A = lambda w: F0 / np.sqrt((k - m*w**2)**2 + (c*w)**2)
for w in [1.0, 4.0, 5.0, 6.0, 10.0]:
rhs = lambda t, u: [u[1], (F0*np.cos(w*t) - c*u[1] - k*u[0]) / m]
T = 40.0 # transients decay like e^(-t/2)
sol = solve_ivp(rhs, (0, T), [0, 0], max_step=0.01, rtol=1e-9, atol=1e-12)
tail = sol.y[0][sol.t > T - 2*np.pi/w] # the last full period
print(f"w = {w:4.1f} formula A = {A(w):.4f} simulated = {float(tail.max()):.4f}")w = 1.0 formula A = 0.0416 simulated = 0.0416
w = 4.0 formula A = 0.1015 simulated = 0.1015
w = 5.0 formula A = 0.2000 simulated = 0.2000
w = 6.0 formula A = 0.0798 simulated = 0.0798
w = 10.0 formula A = 0.0132 simulated = 0.0132
RLC circuits: the same equation
In a series circuit with inductance (henries), resistance (ohms), capacitance (farads) and source voltage , Kirchhoff's voltage law for the charge on the capacitor reads
| Spring | Circuit |
|---|---|
| mass | inductance |
| damping | resistance |
| stiffness | |
| force | voltage |
| displacement | charge |
Example: , , , a 12 V battery switched on at with , . Dividing by gives , roots , and a steady charge .
import sympy as sp
t = sp.symbols('t', nonnegative=True)
Q = sp.Function('Q')
L, R, C, E = sp.Rational(1, 2), 10, sp.Rational(1, 500), 12
ode = sp.Eq(L*Q(t).diff(t, 2) + R*Q(t).diff(t) + Q(t)/C, E)
sol = sp.dsolve(ode, ics={Q(0): 0, Q(t).diff(t).subs(t, 0): 0}).rhs
print("Q(t) =", sp.simplify(sol))
print("I(t) =", sp.simplify(sol.diff(t)))
print("Q(0.1) =", round(float(sol.subs(t, sp.Rational(1, 10))), 6), " limit =", sp.limit(sol, t, sp.oo))Q(t) = (3*exp(10*t) - sin(30*t) - 3*cos(30*t))*exp(-10*t)/125
I(t) = 4*exp(-10*t)*sin(30*t)/5
Q(0.1) = 0.032325 limit = 3/125
So : the charge overshoots 0.024 C, rings at 30 rad/s and settles, and the current dies away.
In software: the critically damped camera spring
A third-person camera should follow the player smoothly, never overshoot, and settle quickly. Model the offset as a critically damped spring with angular frequency (higher = snappier):
Using the exact solution for each frame step, rather than an Euler step, makes the motion frame-rate independent: 30 fps and 144 fps produce the same curve. This is the idea behind "smooth damp" functions in game engines.
import math
def spring_step(pos, vel, target, omega, dt):
e, v = pos - target, vel
decay = math.exp(-omega * dt)
tmp = (v + omega * e) * dt
return target + (e + tmp) * decay, (v - omega * tmp) * decay
def euler_step(pos, vel, target, omega, dt):
acc = -2*omega*vel - omega**2 * (pos - target)
vel += acc * dt # semi-implicit Euler
return pos + vel * dt, vel
for name, step in [("exact", spring_step), ("Euler", euler_step)]:
for fps in [30, 144]:
pos, vel = 0.0, 0.0
for _ in range(fps // 2): # half a second toward target 10
pos, vel = step(pos, vel, 10.0, 8.0, 1 / fps)
print(f"{name:5} {fps:3d} fps position after 0.5 s = {pos:.5f}")exact 30 fps position after 0.5 s = 9.08422
exact 144 fps position after 0.5 s = 9.08422
Euler 30 fps position after 0.5 s = 9.04042
Euler 144 fps position after 0.5 s = 9.07679
The exact step agrees at both frame rates (and with ); Euler's answer depends on the frame rate. Other uses of the same model: UI spring animations (iOS and CSS spring curves are parameterised by stiffness and damping), vehicle suspension in racing games, and PD controllers in robotics, where the gains play the role of and .
Exercises
Exercise 1: Natural frequency and critical damping
A 4 kg mass hangs on a spring with . Find , the period of undamped oscillation and the critical damping constant.
Answer
, period , .
Solution
Exercise 2: Finding k from a stretch
A 2 kg mass stretches a spring 0.49 m (). It is pulled 0.1 m below equilibrium and released at rest, with no damping. Find .
Answer
, with .
Solution
Exercise 3: An RLC circuit
A series circuit has , , . Classify its free response and find the resistance that would make it critically damped.
Answer
Underdamped, roots (oscillation at 4 rad/s); critical resistance .
Solution
Exercise 4: The resonance peak (code)
For , , , , find the forcing frequency with the largest steady-state amplitude, numerically on a fine grid, and compare it with the formula .
Answer
, amplitude .
Solution
Minimise over : . Then .
import numpy as np
m, c, k = 1.0, 2.0, 16.0
w = np.linspace(0.01, 8, 800_001)
A = 1 / np.sqrt((k - m*w**2)**2 + (c*w)**2)
i = int(np.argmax(A))
print(round(float(w[i]), 4), round(float(A[i]), 4), round(float(np.sqrt(k/m - c**2/(2*m**2))), 4))3.7417 0.1291 3.7417
Exercise 5: How snappy is the camera? (code)
For the critically damped camera spring with and starting at rest, the remaining fraction of the offset is . Find the time at which only 1 % remains.
Answer
.
Solution
Solve . The left side decreases for , so there is one root; bisection or brentq finds it.
import math
from scipy.optimize import brentq
f = lambda t: (1 + 8*t) * math.exp(-8*t) - 0.01
print(round(brentq(f, 0.01, 5), 4))0.8298
Test yourself
A spring has and . Which damping constant makes it critically damped?
In the spring–circuit analogy, which circuit quantity plays the role of the spring stiffness ?
Why does a camera spring that uses the exact solution per frame behave the same at 30 and 144 fps?
- Because it ignores dt
- Because composing exact steps of length gives exactly the exact solution at the total time
- Because Euler's method is used
- Because the target never moves