By Harald Lykke Joakimsen (harald.l.joakimsen@uit.no)
-
Clone the repository:
git clone <this repo url> -
Make sure you have a Python environment with the required packages installed. You can use the provided
pyproject.tomlfile with a tool like uv or install the packages manually using pip:pip install numpy scipy matplotlib sympy. -
change directory to
src/(for relative referance to cache and figures to work):cd src -
Run code:
- With uv:
uv run trebuchet.pyetc. - Or run with python directly:
python3 trebuchet.py
- The simulation results will be saved in the
figuresfolder astrebuchet_simulation.gifand the expressions for the double derivatives of the two angles should be printed to the console.
This is a simple trebuchet simulator written in Python. The steps involved are:
- Draw figure to define variables and parameters.
- Use Lagrangian mechanics to derive equations of motion symbolically using SymPy.
- Numerically solve the equations of motion using SciPy.
- Visualize the results with Matplotlib.
Figure made with Excalidraw.
We assume all motion is in a plane, and that the arms are rigid bodies with uniform mass distribution. We also assume no friction or air resistance, and that the pivot points are fixed. The projectile and counter weight are treated as point masses.
The equations of motion are derived using Lagrangian mechanics. The Lagrangian is given by
$$
L = T - V
$$
where
where trebuchet/src/trebuchet.py and look for the printed output.
The simulations are performed using the solve_ivp function from SciPy. The equations of motion are converted to a system of first-order ODEs and solved with a method from the Runge-Kutta family. The results are then visualized using Matplotlib.
The following is the result of 5 simulations with slightly different initial conditions overlaid:

Notice how the simulations look very similar to begin with, but diverge after a while. This is a hallmark of chaotic systems, where small differences in initial conditions can lead to vastly different outcomes. In this case, the simulations have slightly different initial angles for the first arm, which leads to different trajectories for the projectile. For a simple double pendulum, there is a great video about the chaotic and non-chaotic regimes here.
The single and double pendulums at the top correspond to the other two files in the src/ folder.


