Damped spring oscillator analytical & numerical solutions in Python
This post demonstrates two approaches to simulating a damped spring oscillator: analytical and numerical solutions.
Simulation Methods
Analytical Solution
The damped harmonic oscillator follows the differential equation:
$$\frac{d^2x}{dt^2} + 2\zeta\omega_0\frac{dx}{dt} + \omega_0^2x = 0$$For an underdamped system ($\zeta < 1$), the analytical solution is:
$$x(t) = e^{-\zeta\omega_0 t}\left[x_0\cos(\omega_d t) + \frac{v_0 + \zeta\omega_0 x_0}{\omega_d}\sin(\omega_d t)\right]$$Where:
- $\zeta$ is the damping ratio
- $\omega_0$ is the natural angular frequency $(2\pi f_0)$
- $\omega_d = \omega_0\sqrt{1-\zeta^2}$ is the damped frequency
- $x_0$ and $v_0$ are initial position and velocity
Numerical Solution
$$\frac{dx}{dt} = v$$$$\frac{dv}{dt} = -\omega_0^2x - 2\zeta\omega_0v$$This method provides high accuracy and allows comparison with the analytical solution to verify correctness.
Source code
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: CC0 1.0 Universal
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
# Simulation parameters
fs = 1000 # Sample rate (Hz)
t_duration = 5 # Duration (seconds)
f_resonant = 4 # Resonant frequency (Hz)
x0 = 1.0 # Initial displacement
v0 = 0.0 # Initial velocity
# Derived parameters
omega0 = 2 * np.pi * f_resonant # Natural angular frequency
dt = 1.0 / fs # Time step
t = np.arange(0, t_duration, dt) # Time array
n_samples = len(t)
# Damping coefficient (you can adjust this to control damping)
# zeta < 1: underdamped, zeta = 1: critically damped, zeta > 1: overdamped
zeta = 0.03 # Light damping for oscillatory motion
gamma = 2 * zeta * omega0 # Damping coefficient
# Analytical solution for underdamped oscillator
omega_d = omega0 * np.sqrt(1 - zeta**2) # Damped frequency
# For initial conditions x(0) = x0, v(0) = v0
# Solution: x(t) = e^(-ζω₀t)[x0*cos(ωd*t) + ((v0 + ζω₀x0)/ωd)*sin(ωd*t)]
x_analytical = np.exp(-zeta * omega0 * t) * (
x0 * np.cos(omega_d * t) +
((v0 + zeta * omega0 * x0) / omega_d) * np.sin(omega_d * t)
)
print(f"Natural frequency: {f_resonant:.2f} Hz")
print(f"Damped frequency: {omega_d / (2 * np.pi):.2f} Hz")
print(f"Damping ratio: {zeta:.2f}")
print(f"Damping coefficient gamma: {gamma:.3f}")
print(f"Natural angular frequency ω₀: {omega0:.3f} rad/s")
# Numerical simulation using Runge-Kutta method
def spring_ode(state, t):
"""
Differential equation for dampened harmonic oscillator
d²x/dt² + 2ζω₀(dx/dt) + ω₀²x = 0
state = [position, velocity]
returns [velocity, acceleration]
"""
x, v = state
# acceleration = -ω₀²x - 2ζω₀v
acceleration = -omega0**2 * x - 2 * zeta * omega0 * v
return np.array([v, acceleration])
# Initialize arrays for numerical solution
x_numerical = np.zeros(n_samples)
v_numerical = np.zeros(n_samples)
# Initial conditions
x_numerical[0] = x0
v_numerical[0] = v0
# Runge-Kutta 4th order integration
for i in range(n_samples - 1):
state = np.array([x_numerical[i], v_numerical[i]])
k1 = dt * spring_ode(state, t[i])
k2 = dt * spring_ode(state + k1/2, t[i] + dt/2)
k3 = dt * spring_ode(state + k2/2, t[i] + dt/2)
k4 = dt * spring_ode(state + k3, t[i] + dt)
next_state = state + (k1 + 2*k2 + 2*k3 + k4) / 6
x_numerical[i+1] = next_state[0]
v_numerical[i+1] = next_state[1]
# Plot the results
plt.figure(figsize=(12, 8))
# Position plot
plt.subplot(2, 1, 1)
plt.plot(t, x_analytical, 'b-', label='Analytical Solution', linewidth=2)
plt.plot(t[::10], x_numerical[::10], 'ro', markersize=2, label='Numerical Solution', alpha=0.7)
plt.xlabel('Time (s)')
plt.ylabel('Position (m)')
plt.title(f'Dampened Spring Oscillator (f₀={f_resonant}Hz, ζ={zeta}, fs={fs}Hz)')
plt.legend()
plt.grid(True, alpha=0.3)
# Analytical velocity (derivative of position)
v_analytical = np.exp(-zeta * omega0 * t) * (
(-zeta * omega0 * x0 + (v0 + zeta * omega0 * x0) * omega_d / omega_d) * np.cos(omega_d * t) +
(-omega_d * x0 - zeta * omega0 * (v0 + zeta * omega0 * x0) / omega_d) * np.sin(omega_d * t)
)
# Velocity plot
plt.subplot(2, 1, 2)
plt.plot(t, v_analytical, 'g-', label='Analytical Velocity', linewidth=2)
plt.plot(t[::10], v_numerical[::10], 'mo', markersize=2, label='Numerical Velocity', alpha=0.7)
plt.xlabel('Time (s)')
plt.ylabel('Velocity (m/s)')
plt.title('Velocity of the Spring Mass')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# Calculate error between analytical and numerical solutions
error = np.abs(x_analytical - x_numerical)
max_error = np.max(error)
rms_error = np.sqrt(np.mean(error**2))
print(f"\nNumerical accuracy:")
print(f"Maximum error: {max_error:.6f} m")
print(f"RMS error: {rms_error:.6f} m")
print(f"Relative RMS error: {rms_error/np.max(np.abs(x_analytical))*100:.4f}%")
plt.savefig("dampened_spring_oscillator.svg", dpi=300, bbox_inches='tight')
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow