public

N-Body Gravity Simulation

read-only

AI authored to showcase ironpad capabilities.

N-Body Gravity Simulation 🪐

The N-body problem asks: given N massive bodies interacting through gravity, how do they move over time?

The Physics

Newton's law of gravitation gives the force between two bodies:

F = \frac{G \, m_1 \, m_2}{r^2}

For N bodies, each feels the combined gravitational pull of every other body, requiring \binom{N}{2} = N(N\!-\!1)/2 force pair calculations per timestep.

Our Approach

We simulate a central star (mass = 200) with four orbiting planets using Velocity Verlet integration, a symplectic integrator that conserves energy far better than naive Euler methods. A softening parameter \varepsilon prevents numerical singularities at close approach.

Run the cells below to see orbital mechanics emerge from simple force laws!

[1]N-Body Simulation
1 panel
Saved output from the author's last run. Press Run to execute live in your browser.
Simulated 5 bodies for 2000 steps (dt=0.01). 201 points per body.
[2]Orbital Trail Visualization
1 panel
Saved output from the author's last run. Press Run to execute live in your browser.
N-Body Orbital Trails -15.0 -10.0 -5.0 0.0 5.0 10.0 15.0 -15.0 -10.0 -5.0 0.0 5.0 10.0 15.0 Star Inner Second Third Outer

Understanding the Results

What You're Seeing

Each colored trail traces a planet's orbit around the central star. The inner planets complete more orbits because they move faster (Kepler's third law: T^2 \propto r^3).

Velocity Verlet Integration

Unlike naive Euler integration (which systematically gains or loses energy), Velocity Verlet is symplectic: it preserves the geometric structure of the phase space. This means:

  • Orbits remain stable over long simulations
  • Energy oscillates around the true value rather than drifting
  • Closed orbits stay closed

Sensitivity to Initial Conditions

While our 5-body system appears stable, N-body systems are fundamentally chaotic: tiny perturbations in initial positions grow exponentially over time. This is why long-term weather prediction is impossible and why predicting the solar system beyond ~100 million years requires extreme precision.

"The three-body problem is one of the most famous unsolved problems in mathematical physics."

Try modifying the initial conditions or adding more bodies to see how the dynamics change!