N-Body Gravsim and Hohmann Transfer Orbit
In spring 2025 I wrote a classical n-body gravitational simulation in 3D, visualized though matplotlib. I chose to model the solar system, including Pluto, the Moon, Phobos, and Deimos. It can use either Euler’s Method or Runge-Kutta 4 for numerical integration. In the former case, I used the fact that the gravitational force between any two bodies is equal and opposite (Newton’s 3rd Law) to reduce the number of computations needed to find the net acceleration on each body. In practice this reduced from n² to n(n+1)/2 calculations.
The program also tracks the drift in total energy of the system over time. For timesteps smaller than 1 hour, the total energy remains within 10⁻¹³%. The total energy only seems to drift without bound if the timestep is greater than about 1 month. The simulation is accurate enough that the moon can be seen orbiting the earth with a timestep as large as 1 hour.
I also programmed a simple visualization of a 2-burn Hohmann transfer orbit between two given orbits, using the Earth as the central mass and assuming instantaneous burns. The program precomputes the necessary burn velocities and times the burns to when the probe is moving along the y-axis, creating a spiral-shaped orbit that is segmented based on the stage of the transfer. Here is the Python code.