Damped Harmonic Oscillator

JJ Ben-Joseph headshot JJ Ben-Joseph

Adjust the system parameters, then play the animation to watch how energy shifts between the spring, mass, and damper.

Enter parameters and press Play.
Simulation summary will appear here.

Kinetic energy

Spring potential energy

Dissipated energy

Resonance Arena Mini-Game

React to changing drive frequencies by dialing in damping fast enough to keep the steady-state amplitude below the damage threshold. Each round translates the response curve into an arcade-style targeting challenge.

Keep amplitude inside safe band

Click to Play, tune damping in time, and survive all 80 seconds.

Best run: 0

1. Real‑world motivation

From automobile suspensions to seismically isolated buildings, countless machines rely on the balance between inertia, spring stiffness, and damping. A traditional damped harmonic oscillator calculator reports only a displacement value, concealing how energy sloshes between kinetic and elastic stores while friction steals a portion every cycle. This interactive version keeps the original purpose of computing motion but augments it with a canvas that shows a block bobbing on a spring. As you adjust parameters like mass or forcing amplitude, the animation and energy bars update instantly, clarifying why an overly soft shock absorber leads to bouncy rides or why a lightly damped bridge oscillates for minutes after a gust of wind.

2. Variables and assumptions

The model treats a single degree‑of‑freedom system consisting of mass m, spring constant k, and viscous damping coefficient c. Position x and velocity v are measured along the spring’s axis with the equilibrium at x=0. Optional sinusoidal forcing F(t)=F_0sin(ω_dt) can drive the oscillator. Initial conditions x_0 and v_0 set the starting state. Air resistance and structural damping are modeled with the single coefficient c; nonlinear effects, large deflections, and temperature‑dependent stiffness are ignored. All quantities use SI units: kilograms, newtons, meters, and seconds. Input validation rejects non‑finite or negative mass and spring values, and the time step Δt is clamped between 0.001 and 0.1 s to maintain numerical stability.

3. Governing equations

The dynamics follow Newton’s second law applied to the mass: mdvdt=F(t)-cv-kx. Introducing dxdt=v yields a first‑order system:

dxdt=v, dvdt=F(t)-cv-kxm. Kinetic and potential energies are E_k=12mv2 and E_p=12kx2, while the power dissipated by damping is P_d=cv2. The work performed by the driving force accumulates as W=F(t)vdt, allowing an energy balance check E_k+E_p+E_d-(E_0+W).

4. Numerical scheme

The simulator advances the state with a classical fourth‑order Runge–Kutta method. For each step of size Δt, four derivative estimates sample the forces at the beginning, two midpoints, and the end. The weighted average s(t+Δt)=s(t)+Δt6(k_1+2k_2+2k_3+k_4) updates position and velocity simultaneously. RK4 offers a good compromise between speed and accuracy; the local error scales with Δt5, so halving the step roughly reduces the global error by a factor of sixteen. Nevertheless, very stiff systems with large k or c may require smaller steps to avoid overshoot. The code clamps the user‑selected Δt and recomputes the trajectory whenever inputs change, debounced by 400 ms to prevent excessive re‑simulation during typing.

5. Worked example

Consider a 1 kg mass attached to a spring with k=20 N/m and damping c=0.5 kg/s. Starting from x_0=0.1 m and v_0=0, with no external forcing, the simulator predicts oscillations that decay with a time constant τ=2c ≈ 4 s. Running the animation with Δt=0.02 s shows the block initially move toward the origin, overshoot, and settle after a few cycles. The energy bars reveal kinetic energy peaking as the block passes equilibrium and shrinking as damping converts it to heat. Downloading the CSV confirms the analytic period of about 1.4 s derived from ω_d=km-c24m2.

6. Comparison table

The table contrasts the baseline oscillator above with two variations: increased damping and a driven case. Values are extracted from the simulation after five seconds.

Scenario c (kg/s) F₀ (N) Max |x| (m) Energy drift (J)
Baseline 0.5 0 0.10 ≈0
Heavier damping 2.0 0 0.04 ≈0
Driven 0.5 1.0 0.12 <0.001

Doubling the damping coefficient reduces the peak displacement to 4 cm, while a modest sinusoidal drive at the natural frequency slightly increases the amplitude without introducing significant numerical energy error.

7. How to read the animation

The canvas displays a horizontal spring anchored to the left wall with a colored block on the right. The block’s position corresponds to the instantaneous displacement, scaled to fit the viewport. A blue trail traces its recent path. Beneath the canvas, striped orange, blue, and gray bars show kinetic energy, spring potential energy, and cumulative dissipated energy, respectively, with text labels so color perception is unnecessary. The caption narrates time, displacement, velocity, and the small energy‑balance error. Focus the canvas with the keyboard and press the space bar to toggle play and pause. The hidden “Simulation summary” div mirrors these numbers for screen readers.

8. Limitations

Several idealizations keep the model tractable. Damping is purely viscous and linear, whereas real shock absorbers can exhibit velocity‑squared or hysteretic behavior. The spring obeys Hooke’s law without yielding or coil bind. Forcing is limited to a single sinusoid; arbitrary waveforms or impacts are not included. The system moves only in one dimension, ignoring torsional or lateral modes. Numerical integration assumes the parameters remain constant and may lose accuracy for extremely stiff or poorly scaled inputs. Despite these simplifications, the simulator captures the essential trade‑offs that govern many engineering designs.

9. Suggested extensions

Ambitious users could extend the code to include nonlinear damping, piecewise spring behavior, or multiple degrees of freedom coupled in series. Plotting velocity versus displacement would generate a phase‑space portrait that highlights spiraling toward rest. Another extension is to add a second mass and spring, exploring resonance and modal beating. Because the project uses only vanilla JavaScript, editing the source to experiment with control strategies or feedback loops is straightforward. For related explorations, see the Mass‑Spring Oscillation Calculator, compare electrical analogs with the RLC Circuit Damping Calculator, or examine motion without springs in the Projectile Motion Calculator.

10. References

Standard treatments of damped oscillations appear in H. Goldstein, C. Poole, and J. Safko, Classical Mechanics, and R. C. Hibbeler, Engineering Mechanics: Dynamics. These texts discuss linear systems, energy methods, and the effect of damping on resonance. The numerical technique follows the RK4 algorithm described in any introductory computational physics resource.

Embed this calculator

Copy and paste the HTML below to add the Damped Harmonic Oscillator – Interactive Simulation to your website.