(Personal weekend project from 2024, README written and project published to GitHub 2025, Updated on 05/2025 for code efficiency)
During the summer, every normal university student starts to miss their classes (or... maybe not). In my case, I started to miss my mechanics classes and thought to myself:
"How can I satisfy my need for Mechanics?"
The answer was obviously to dive into the chaotic beauty of physics with a classic problem — the 3-Body Problem — and bring it to life using a bit of Python.
-
Newton’s Second Law:
$$\vec{F} = m \vec{a}$$
Used to relate force and acceleration for a given mass. -
The
$1/r^n$ - acceleration field (with$b=2$ and$a=G$ this is Newton's law of gravitation):
$$\vec{F} = a\frac{mM}{r^b} \hat{r}$$ -
Discrete approximation of acceleration:
$$a = \frac{\Delta v}{\Delta t} \quad \Leftrightarrow \quad \Delta v= a\Delta t$$
A simple numerical estimate that gets better as$\Delta t$ decreases.
The
You can't solve it analytically.
It’s chaotic. It’s sensitive.
Change the starting conditions just a little, and you'll end up with a totally different outcome.
That’s what makes it fascinating. It sits right at the edge of classical mechanics and chaos theory. A seriously beautiful thing.
Changed 07/2025:
The simulation now mostly uses vectorized operations to speed up the simulation:
First, we initialize five tensors/arrays: position, velocity, acceleration, time, and mass. Initially, the first four consist of zeros which we update with a for-loop. The mass array consists of the masses of the
- Update elapsed time to the time array.
- Update the acceleration to the acceleration array.
- Using the saved acceleration and velocity array update position to the position array.
Finally, we plot all data to visualize the orbits.
In all the figures, the initial positions are marked with small dots, and the most recent with large ones. Here are some special cases:
In this case, the motion seems completely chaotic with no clear pattern. However, so far, the bodies have remained relatively close to each other.
This shows what happens when the 3-body problem momentarily reduces to the two-body problem: the blue and yellow masses form a close bond and orbit each other. Their combined center of mass then orbits the green mass as if it were just a single object.
In these last cases, the 3-body problem fully reduces to a two-body scenario due to perfect balancing of forces from very specific initial conditions.
I might revisit the simulation to improve and expand it. Exploring Lyapunov exponents could be an interesting next step or perhaps animate the simulation.



