Sub topic 15
Simulating an Orbit Step by Step
This chapter assumes that you know vectors, Newton’s second law, circular motion, kinetic energy, and basic Python. You will model one small body orbiting a much larger body. The larger body stays fixed at the origin, so this is an approximation to the two-body problem.
From gravity to an update rule
For a body at position \((x,y)\), its distance from the origin is
\[ r=\sqrt{x^2+y^2}. \]
Newtonian gravity gives the acceleration
\[ \vec a=-\frac{GM}{r^3}(x,y), \]
where \(G\) is the gravitational constant and \(M\) is the central mass. The minus sign makes the acceleration point toward the origin.
A computer cannot update a continuous path all at once. It advances the body through small time steps:
- Use the current position to calculate acceleration.
- Update the velocity.
- Update the position.
- Recalculate acceleration.
- Repeat.
A simple Euler update is easy to write, but it usually causes the orbit to spiral inward or outward. The leapfrog method, also called velocity Verlet, gives much better long-term behavior because it treats the acceleration in two half-steps.
A working Python simulation
The code below starts a satellite in a circular orbit 400 km above Earth. It uses Earth’s gravitational parameter \(GM\), which combines two constants into one useful value.
import math
GM = 3.986004e14 # m^3/s^2
r0 = 6.771e6 # 400 km above Earth's surface
dt = 5.0 # time step in seconds
def acceleration(x, y):
r3 = (x * x + y * y) ** 1.5
return -GM * x / r3, -GM * y / r3
def specific_energy(x, y, vx, vy):
speed_squared = vx * vx + vy * vy
return 0.5 * speed_squared - GM / math.hypot(x, y)
x, y = r0, 0.0
vx, vy = 0.0, math.sqrt(GM / r0)
initial_energy = specific_energy(x, y, vx, vy)
ax, ay = acceleration(x, y)
for _ in range(1200):
vx += 0.5 * dt * ax
vy += 0.5 * dt * ay
x += dt * vx
y += dt * vy
ax, ay = acceleration(x, y)
vx += 0.5 * dt * ax
vy += 0.5 * dt * ay
final_radius = math.hypot(x, y)
final_energy = specific_energy(x, y, vx, vy)
print(f"radius change: {final_radius - r0:.1f} m")
print(f"relative energy drift: "
f"{abs((final_energy - initial_energy) / initial_energy):.2e}")The specific orbital energy is energy per unit mass:
\[ \epsilon=\frac{v^2}{2}-\frac{GM}{r}. \]
For an ideal circular orbit, the radius and \(\epsilon\) should remain constant. A small change is numerical error. If the change grows rapidly, reduce dt.
| Quantity | What to check | Expected result |
|---|---|---|
| Radius | `final_radius - r0` | Close to zero |
| Specific energy | Relative drift | Small |
| Path | Plot of \(x\) against \(y\) | Nearly circular |
Common errors
- Forgetting the factor \(r^3\) in the acceleration formula.
- Mixing kilometres with metres.
- Choosing a time step that is too large.
- Updating position before completing the first half velocity step.
- Checking only the picture instead of measuring energy and radius.
A visually reasonable orbit can still have serious numerical error. Conservation checks provide a stronger test.
Exercise
If the simulation’s radius steadily increases, what should you try first?
Show answer
Reduce the time step and check that the acceleration points toward the origin and uses r**3 in its denominator.