← All Base Form Math projects
BM-20NOT STARTEDBASE FORM · CALCULUSMATHPYTHON

Solar-system simulator

Simulate the planets with Euler and RK4, watch energy drift, and see which integrator keeps Earth on its orbit for 100 years.

EST.8 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
energy drift over 100 years
GOAL · RK4 drift ≤ solve_ivp's
scipy.integrate.solve_ivp—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

Newton's law of gravity gives the acceleration of every planet from where all the others are:

r¨i=∑j≠iGmj rj−ri∥rj−ri∥3\ddot r_i = \sum_{j \ne i} G m_j \, \frac{r_j - r_i}{\|r_j - r_i\|^3}

That is an ordinary differential equation (ODE): the rate of change of the state, here all positions and velocities, is a function of the state. With more than two bodies there is no formula for the answer, so you step forward in time.

Euler's method is the simplest step, yn+1=yn+h f(yn)y_{n+1} = y_n + h\,f(y_n). It follows the current velocity in a straight line for a time hh. On a curved orbit, a straight line always ends up a little too far out, so Earth spirals away and its energy drifts. RK4 samples the slope four times per step (at the start, twice at the midpoint, and at the end) and combines them: yn+1=yn+h6(k1+2k2+2k3+k4)y_{n+1} = y_n + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4).

Stepping an ODE forward is a kind of integration. If the slope depends only on time, Euler becomes the left-rectangle rule and RK4 becomes Simpson's rule. That's why the project starts with quadrature: the error orders you measure there, hh, h2h^2 and h4h^4, are the same ones you'll see again in the orbits.

VISUALFig. 1
3 YEARS, STEP h = 0.01 YEAR RK4: stays on the circleenergy drift ≈ 5e-07 Euler: spirals outwardenergy drift ≈ 52% Euler follows the currentvelocity in a straight line.On a curved orbit, thatalways lands a bit too far out.
Earth for 3 years with a step of 0.01 year. RK4 stays on the circle; Euler spirals outward and gains energy. Why it's here: It shows what energy drift looks like before you measure it in objective 3.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 5
Stuck? Hints stay hidden until you ask.

DATA · planet positions from public ephemeris tables

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Over 100 years, your RK4 energy drift is no larger than that of `scipy.integrate.solve_ivp` on the same system, with its settings stated. Record both with `python tools/mlarena.py record BM-20 "<RK4 drift> vs <solve_ivp drift>"`.
  2. A log-log plot shows Euler's drift shrinking like h and RK4's like h⁴.
  3. The README has a plot or animation of the eight orbits over 100 years.

Side quests

Add velocity Verlet (leapfrog). It is only second order, but its energy error stays bounded instead of growing. Run it for 10,000 years and compare with RK4.
Show that RK4 applied to y′ = f(t), with no y on the right, is exactly Simpson's rule.
Add the Moon, or send a probe from Earth to Mars on a Hohmann transfer orbit.

Terms in plain words

ODE (ordinary differential equation)
A rule that gives the rate of change of a state from the state itself, like y′ = f(t, y). Simulating it means stepping forward in time.
Euler's method
y_next = y + h·f(t, y): follow the current slope for one step. Its error over a fixed time span shrinks like h.
RK4
The classic Runge–Kutta method. It samples the slope four times per step and averages them with weights 1, 2, 2, 1. Its error shrinks like h⁴.
Quadrature
Numerical integration of a function, for example with the trapezoid rule or Simpson's rule.
Energy drift
How far the total energy (kinetic plus potential) wanders from its starting value. Real gravity conserves energy, so drift measures the integrator's error.