TExcelTAD
Home › Calculus in Depth › Applications of Second-Order Equations

Applications of Second-Order Equations

Springs, electric circuits and smooth-follow cameras obey the same equation, mx′′+cx′+kx=F(t)mx'' + cx' + kx = F(t). 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 mm on a spring is pulled back by Hooke's force −kx-kx (xx = displacement from equilibrium, kk = stiffness) and slowed by a damping force −cx′-cx' proportional to velocity. Newton's law mx′′=sum of forcesmx'' = \text{sum of forces} gives

mx′′+cx′+kx=F(t)characteristic equation mr2+cr+k=0r=−c±c2−4mk2m\begin{aligned} &mx'' + cx' + kx = F(t) && \text{characteristic equation } mr^2 + cr + k = 0 \\ &r = \frac{-c \pm \sqrt{c^2 - 4mk}}{2m} \end{aligned}
Case Condition Roots Motion
Undamped c=0c = 0 ±ω0i\pm\omega_0 i, ω0=k/m\omega_0 = \sqrt{k/m} x=Acos⁡(ω0t−φ)x = A\cos(\omega_0 t - \varphi), period 2π/ω02\pi/\omega_0
Underdamped c2<4mkc^2 < 4mk −c/(2m)±ωdi-c/(2m) \pm \omega_d i decaying oscillation, ωd=k/m−c2/(4m2)\omega_d = \sqrt{k/m - c^2/(4m^2)}
Critically damped c2=4mkc^2 = 4mk −c/(2m)-c/(2m) (double) fastest return without crossing zero more than once
Overdamped c2>4mkc^2 > 4mk two negative reals slow creep back, no oscillation

The critical damping constant is ccrit=2mkc_{\text{crit}} = 2\sqrt{mk}. The ratio ζ=c/ccrit\zeta = c / c_{\text{crit}} (the damping ratio) is how engineers and animation libraries describe a spring: ζ<1\zeta < 1 bounces, ζ=1\zeta = 1 is critical, ζ>1\zeta > 1 is sluggish.

python · try it yourself
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 F(t)=F0cos⁡ωtF(t) = F_0\cos\omega t. With damping, the complementary solution dies out and the steady state is the particular solution xp=A(ω)cos⁡(ωt−δ)x_p = A(\omega)\cos(\omega t - \delta). Undetermined coefficients with the trial acos⁡ωt+bsin⁡ωta\cos\omega t + b\sin\omega t gives

A(ω)=F0(k−mω2)2+(cω)2A(\omega) = \frac{F_0}{\sqrt{(k - m\omega^2)^2 + (c\omega)^2}}

With little damping the amplitude peaks sharply near ω0=k/m\omega_0 = \sqrt{k/m}: 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.

python · try it yourself
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 LL (henries), resistance RR (ohms), capacitance CC (farads) and source voltage E(t)E(t), Kirchhoff's voltage law for the charge Q(t)Q(t) on the capacitor reads

LQ′′+RQ′+1CQ=E(t),current I=Q′LQ'' + RQ' + \frac{1}{C}Q = E(t), \qquad \text{current } I = Q'
Spring Circuit
mass mm inductance LL
damping cc resistance RR
stiffness kk 1/C1/C
force F(t)F(t) voltage E(t)E(t)
displacement xx charge QQ

Example: L=0.5 HL = 0.5\ \text{H}, R=10 ΩR = 10\ \Omega, C=0.002 FC = 0.002\ \text{F}, a 12 V battery switched on at t=0t = 0 with Q(0)=0Q(0) = 0, I(0)=0I(0) = 0. Dividing by LL gives Q′′+20Q′+1000Q=24Q'' + 20Q' + 1000Q = 24, roots −10±30i-10 \pm 30i, and a steady charge Q=C⋅E=0.024 CQ = C \cdot E = 0.024\ \text{C}.

python · try it yourself
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 Q(t)=0.024−e−10t(0.024cos⁡30t+0.008sin⁡30t)Q(t) = 0.024 - e^{-10t}(0.024\cos 30t + 0.008\sin 30t): the charge overshoots 0.024 C, rings at 30 rad/s and settles, and the current I=0.8e−10tsin⁡30tI = 0.8e^{-10t}\sin 30t 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 e=position−targete = \text{position} - \text{target} as a critically damped spring with angular frequency ω\omega (higher ω\omega = snappier):

e′′+2ωe′+ω2e=0→e(t)=(e0+(v0+ωe0) t) e−ωtv(t)=(v0−ω(v0+ωe0) t) e−ωt\begin{aligned} e'' + 2\omega e' + \omega^2 e = 0 \quad \to \quad e(t) &= (e_0 + (v_0 + \omega e_0)\,t)\,e^{-\omega t} \\ v(t) &= (v_0 - \omega(v_0 + \omega e_0)\,t)\,e^{-\omega t} \end{aligned}

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.

python · try it yourself
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 e(0.5)=10−10⋅(1+4)⋅e−4≈9.08422e(0.5) = 10 - 10 \cdot (1 + 4) \cdot e^{-4} \approx 9.08422); 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 kk and cc.

Exercises

Exercise 1: Natural frequency and critical damping

A 4 kg mass hangs on a spring with k=100 N/mk = 100\ \text{N/m}. Find ω0\omega_0, the period of undamped oscillation and the critical damping constant.

Answer

ω0=5 rad/s\omega_0 = 5\ \text{rad/s}, period 2π/5≈1.2566 s2\pi/5 \approx 1.2566\ \text{s}, ccrit=40 N⋅s/mc_{\text{crit}} = 40\ \text{N} \cdot \text{s/m}.

Solution
ω0=k/m=25=5 rad/s,T=2π/ω0≈1.2566 sccrit=2mk=2400=40\begin{aligned} &\omega_0 = \sqrt{k/m} = \sqrt{25} = 5\ \text{rad/s}, \qquad T = 2\pi/\omega_0 \approx 1.2566\ \text{s} \\ &c_{\text{crit}} = 2\sqrt{mk} = 2\sqrt{400} = 40 \end{aligned}

Exercise 2: Finding k from a stretch

A 2 kg mass stretches a spring 0.49 m (g=9.8 m/s2g = 9.8\ \text{m/s}^2). It is pulled 0.1 m below equilibrium and released at rest, with no damping. Find x(t)x(t).

Answer

x(t)=0.1cos⁡(20 t)x(t) = 0.1\cos(\sqrt{20}\,t), with 20≈4.4721 rad/s\sqrt{20} \approx 4.4721\ \text{rad/s}.

Solution
equilibrium: k⋅0.49=mg=19.6  →  k=40 N/m2x′′+40x=0  →  x′′+20x=0  →  x=c1cos⁡20 t+c2sin⁡20 tx(0)=0.1→c1=0.1;x′(0)=0→c2=0\begin{aligned} &\text{equilibrium: } k \cdot 0.49 = mg = 19.6 \;\to\; k = 40\ \text{N/m} \\ &2x'' + 40x = 0 \;\to\; x'' + 20x = 0 \;\to\; x = c_1\cos\sqrt{20}\,t + c_2\sin\sqrt{20}\,t \\ &x(0) = 0.1 \to c_1 = 0.1; \quad x'(0) = 0 \to c_2 = 0 \end{aligned}

Exercise 3: An RLC circuit

A series circuit has L=1 HL = 1\ \text{H}, R=4 ΩR = 4\ \Omega, C=0.05 FC = 0.05\ \text{F}. Classify its free response and find the resistance that would make it critically damped.

Answer

Underdamped, roots −2±4i-2 \pm 4i (oscillation at 4 rad/s); critical resistance R=80≈8.944 ΩR = \sqrt{80} \approx 8.944\ \Omega.

Solution
Q′′+4Q′+20Q=0  →  r=−4±16−802=−2±4icritical: R2=4L/C=4⋅1/0.05=80  →  R=80≈8.944 Ω\begin{aligned} &Q'' + 4Q' + 20Q = 0 \;\to\; r = \frac{-4 \pm \sqrt{16 - 80}}{2} = -2 \pm 4i \\ &\text{critical: } R^2 = 4L/C = 4 \cdot 1 / 0.05 = 80 \;\to\; R = \sqrt{80} \approx 8.944\ \Omega \end{aligned}

Exercise 4: The resonance peak (code)

For m=1m = 1, c=2c = 2, k=16k = 16, F0=1F_0 = 1, find the forcing frequency with the largest steady-state amplitude, numerically on a fine grid, and compare it with the formula ωpeak=k/m−c2/(2m2)\omega_{\text{peak}} = \sqrt{k/m - c^2/(2m^2)}.

Answer

ωpeak=14≈3.7417 rad/s\omega_{\text{peak}} = \sqrt{14} \approx 3.7417\ \text{rad/s}, amplitude 1/60≈0.12911/\sqrt{60} \approx 0.1291.

Solution

Minimise D(ω2)=(16−ω2)2+4ω2D(\omega^2) = (16 - \omega^2)^2 + 4\omega^2 over s=ω2s = \omega^2: dD/ds=−2(16−s)+4=0→s=14dD/ds = -2(16 - s) + 4 = 0 \to s = 14. Then D=4+56=60D = 4 + 56 = 60.

python · try it yourself
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 ω=8\omega = 8 and starting at rest, the remaining fraction of the offset is (1+ωt)e−ωt(1 + \omega t)e^{-\omega t}. Find the time at which only 1 % remains.

Answer

t≈0.8298 st \approx 0.8298\ \text{s}.

Solution

Solve (1+8t)e−8t=0.01(1 + 8t)e^{-8t} = 0.01. The left side decreases for t>0t > 0, so there is one root; bisection or brentq finds it.

python · try it yourself
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 m=1m = 1 and k=9k = 9. Which damping constant makes it critically damped?

  1. c=3c = 3
  2. c=9c = 9
  3. c=6c = 6
  4. c=18c = 18

In the spring–circuit analogy, which circuit quantity plays the role of the spring stiffness kk?

  1. 1/C1/C
  2. RR
  3. LL
  4. E(t)E(t)

Why does a camera spring that uses the exact solution per frame behave the same at 30 and 144 fps?

  1. Because it ignores dt
  2. Because composing exact steps of length dtdt gives exactly the exact solution at the total time
  3. Because Euler's method is used
  4. Because the target never moves