This repository contains C programs that simulate a damped harmonic oscillator, a fundamental concept in physics and engineering. The simulations model the motion of a mass-spring-damper system using two different numerical integration methods to solve the second-order ordinary differential equation (ODE) of motion.
To run these simulations, you will need a C compiler (like GCC) installed on your system.
Compiling and Running
-
Clone the repository
-
Compile the
Euler.c
program:gcc Euler.c -o euler
-
Compile the
Euler-Cromer.c
program:gcc Euler-Cromer.c -o euler-cromer
-
Run the Euler simulation This will generate a file named
damped_oscillation_Euler.txt
with the simulation data. -
Run the Euler-Cromer simulation This will generate a file named
damped_oscillation_Euler_Cromer.txt
with its data.
-
Euler.c
: Implements the simulation using the Explicit Euler method. -
Euler-Cromer.c
: Implements the simulation using the Euler-Cromer method. This method is a more stable and energy-conserving alternative, especially for oscillatory systems. -
LICENSE
: The project's license (MIT)
Programs solve the differential equation for a damped harmonic oscillator, but they use different numerical methods to approximate the solution over time.
This is a simple, first-order method. It calculates the new position based on the old velocity and the new velocity based on the old acceleration. While easy to implement, it can introduce numerical errors that cause the simulated energy to increase or decrease over time, making it less stable for long-term oscillatory simulations.
The Euler-Cromer method is a slight but crucial modification. It first calculates the new velocity and then uses this updated velocity to calculate the new position. This simple change significantly improves the method's stability and accuracy, especially for systems that conserve energy (like an undamped oscillator), making it a popular choice in physics simulations.
The output files (damped_oscillation_Euler.txt
and damped_oscillation_Euler_Cromer.txt
) are simple text files with two columns: Time (s) and Position (m). You can easily plot this data to compare the two methods.
A common and straightforward way to visualize this data is using a Python script with the popular matplotlib
library.
Install the necessary library:
pip install matplotlib numpy
Create a Python script:
(e.g., plot.py
) with the following code. This script will read both data files, plot the position over time for each method, and save a comparative graph.
import matplotlib.pyplot as plt
import numpy as np
# Load data from the simulation files
data_euler = np.loadtxt('damped_oscillation_Euler.txt', skiprows=1)
data_euler_cromer = np.loadtxt('damped_oscillation_Euler_Cromer.txt', skiprows=1)
# Extract time and position for each method
time_euler, pos_euler = data_euler[:, 0], data_euler[:, 1]
time_euler_cromer, pos_euler_cromer = data_euler_cromer[:, 0], data_euler_cromer[:, 1]
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(time_euler, pos_euler, label='Explicit Euler Method', linestyle='--', color='red')
plt.plot(time_euler_cromer, pos_euler_cromer, label='Euler-Cromer Method', color='blue')
# Add labels and title
plt.title('Comparison of Damped Oscillator Simulation Methods')
plt.xlabel('Time [s]')
plt.ylabel('Position [m]')
plt.legend()
plt.grid(True)
plt.savefig('comparison_plot.png')
plt.show()
print("Plot saved as 'comparison_plot.png'.")
Run the script:
python plot.py
After running the script, you'll get a file named comparison_plot.png, which visually demonstrates the differences between the two numerical methods.
This project is licensed under the MIT License. For more details, see the LICENSE
file.