This repository contains Python implementations of the Three-Body Problem, a classical problem in celestial mechanics that involves predicting the motion of three bodies under mutual gravitational attraction. The simulations are available in both 2D and 3D versions.
- 2D Simulation: Visualizes the orbits of three bodies in a 2D plane.
- 3D Simulation: Adds depth and realism by simulating the bodies in three-dimensional space.
- Custom Initial Conditions: Modify mass, velocity, and starting positions for different scenarios.
- Animated Trajectories: Real-time visualization using
matplotlib.animation
.
- Clone this repository:
git clone https://github.com/yourusername/three-body-simulation.git cd three-body-simulation
- Install the required dependencies:
pip install numpy matplotlib scipy
- Run the simulation:
a. 2D Version:
b. 3D Version:
python three_body_2d.py
python three_body_3d.py
The system is governed by Newton's law of universal gravitation:
For each body, acceleration is computed as:
Where G is the gravitational constant, 𝑚 is mass, and 𝑟 is the distance between bodies.
The code uses scipy.integrate.solve_ivp with the RK45 method to numerically solve the system of differential equations.
You can modify the starting positions and velocities of the bodies inside the Python files:
initial_conditions = [
-1, 0, 0, 0, -0.5, 0, # Body 1
1, 0, 0, 0, 0.5, 0, # Body 2
0, 1, 0, 0.5, -0.5, 0 # Body 3
]
masses = [1, 1.5, 2] # Different masses
Adjusting these values can create different orbital behaviors.
- Add energy conservation checks.
- Implement a GUI for user-friendly interaction.
- Optimize performance for longer simulations.
- Newtonian Mechanics & Three-Body Problem: Wikipedia
- matplotlib animations: Matplotlib Do
This project is open-source and licensed under the MIT License. 📩 Feel free to contribute and improve this simulation!