A Python simulation of a damped harmonic oscillator with forced vibration analysis using FFT.
- Description
- Mathematical Model
- Dependencies
- Installation
- Usage
- Parameters
- Output
- Customization
- Theory
- License
This program simulates the behavior of a damped harmonic oscillator under forced vibration, then analyzes the displacement using Fast Fourier Transform (FFT). The simulation includes:
- Time-domain analysis of displacement and force
- Frequency-domain analysis via FFT
- Visualization of both time and frequency responses
The system is modeled using the second-order differential equation: mẍ + cẋ + kx = F₀cos(ωt)
text
Converted to state-space representation: Aẋ + Bx = F
text where:
A = [[m, 0], [0, 1]]
B = [[c, k], [-1, 0]]
F = [F₀cos(ωt), 0]
- Python 3.x
- NumPy (>=1.20)
- SciPy (>=1.7)
- Matplotlib (>=3.4)
- Clone the repository:
git clone https://github.com/yourusername/vibration-simulation.git
cd vibration-simulation
Install required packages:
pip install numpy scipy matplotlib
Run the simulation:
python vibration_simulation.py
Parameter | Description | Default Value |
---|---|---|
m |
Mass (kg) | 2.0 |
k |
Spring constant (N/m) | 2.0 |
c |
Damping coefficient (Ns/m) | 0.2 |
omega |
Forcing frequency (rad/s) | 1.0 |
F0 |
Force amplitude (N) | 1.0 |
delta_t |
Time step (s) | 0.001 |
time |
Simulation duration (s) | 100 |
The program generates two plots:
Time Domain:
- Displacement (x) vs. Time
- Force (F) vs. Time
Frequency Domain:
- FFT magnitude of the displacement
- Shows dominant frequency components
To modify the simulation:
Change system parameters at the top of the script:
m = 2.0 # Mass
k = 2.0 # Spring constant
c = 0.2 # Damping
Adjust forcing function:
if t <= 15: # Force duration
F[0] = F0 * np.cos(omega*t)
else:
F[0] = 0.0 # Force removal
Change simulation duration and time step:
delta_t = 0.001 # Time step
time = np.arange(0.0, 100.0, delta_t) # Simulation time
The state-space equations are solved using forward Euler integration:
x = x + delta_t * inv(A).dot(F - B.dot(x))
The displacement data is analyzed using SciPy's FFT:
Y = fft(X)
Free for academic, personal, and research use.