This repository contains a simulator for the spring-pendulum system with two degrees of freedom (
To find the position of the mass we use the following second-order ordinary differential equations:
scipy.integrate.solve_ivp
along with the starting conditions to solve the overall state over any time span.
The only python requirements are matplotlib
for visualisation, scipy
for numeric integration, and numpy
for general computations. All can be installed as follows:
pip install -r requirements.txt
The program also requires ffmpeg
if you plan to create an animation, to turn the image frames into a gif or webp or mp4.
# MacOS
brew install ffmpeg
# Arch
sudo pacman -S ffmpeg
# ... Figure it out if your OS is not listed
The software has two primary modes, plot_stats
and animate
. plot_stats
will show a graph resembling the following:
animate
will render frames to frames/%04d.png
and then render them to an output file (out.gif
by default) using ffmpeg
. The gif will resemble the following:
To switch between these modes, simply scroll to the bottom of simulator.py
and comment or uncomment either line.
The starting conditions of the system can be modified by editing l0, k, m, g,
and s0
in simulator.py
. Additionally, some parameters regarding the simulation and rendering can be edited: tmax, dt, fps
.
There is an additional mode time_for_n_swings
, which will compute how many seconds the pendulum takes to complete
The above formulas can be derived using the Euler-Lagrange Equation. Firstly, the system and variables must be defined:
Firstly, consider the Lagrangian function
The gravitational potential is given as
Now, we can apply the Euler-Lagrange function:
The same equation can be used with
Additionally, notice that if we force