Astronomy and Space Exploration ES

Secondary Education / Introduction to calculus in astronomy

11.º · U2: Simulating orbits

Simulating an orbit with the Euler method

Newton's second law as a differential equation solved step by step: the Euler method, why it makes the orbit grow (the energy increases as h²) and how Euler–Cromer fixes it.

⏱ 165 min derivative as a rate of changedifferential equationsvectors in the planelinear approximationrecursively defined sequencesorder of a numerical methodenergy conservation revision

By the end you will be able to:
  • Write Newton's second law with gravitation as a vector differential equation for the position of a planet.
  • Apply three or four steps of the Euler method by hand to a circular orbit and organize them in a table.
  • Explain with the Pythagorean theorem why the Euler method makes the orbit grow and estimate the energy increase per step, h².
  • Compare Euler, Euler–Cromer and Kepler's exact solution using energy conservation as the criterion.
  • Program both methods in a spreadsheet and measure the error after one orbit as a function of the step h.

Kepler’s laws describe how the planets move. Newton explained why: a force produces an acceleration. But going from “I know the acceleration” to “I know the orbit” requires solving a differential equation, and for gravity there is no elementary formula. In this lesson we are going to do it the way computers do: in small steps. And we are going to discover that the most naive way of doing it has a trap that a physicist spotted, and fixed, in 1981.

1. Review: Newton’s law is a differential equation

In the previous lesson we saw that velocity is the derivative of position, v⃗=r⃗ ′\vec v = \vec r\,', and acceleration the derivative of velocity, a⃗=v⃗ ′\vec a = \vec v\,'. In the plane we use vectors: r⃗=(x,y)\vec r = (x, y), v⃗=(vx,vy)\vec v = (v_x, v_y), and each component is differentiated separately.

A planet of mass mm around a star of mass MM feels the force of gravity, directed toward the star and of magnitude GMm/r2GMm/r^2, where r=∣r⃗∣r = |\vec r| is the distance. With F⃗=ma⃗\vec F = m\vec a, the mass of the planet cancels:

a⃗=−GMr3 r⃗.\vec a = -\frac{GM}{r^3}\,\vec r .

(The factor r⃗/r\vec r/r is a vector of length 1 pointing from the star to the planet; dividing by r2r^2 as well and adding the minus sign turns it into the acceleration pointing toward the star.) Since a⃗\vec a is the second derivative of r⃗\vec r, this is a differential equation: it relates the unknown function r⃗(t)\vec r(t) to its derivatives.

To avoid carrying huge numbers we will use units in which GM=1GM = 1. Then a circular orbit of radius r=1r = 1 has velocity v=1v = 1 (because v2/r=GM/r2v^2/r = GM/r^2) and period T=2π≈6.28T = 2\pi \approx 6.28.

2. The Euler method: following the tangent

Illustration: The Euler method: following the tangent

If hh is small, the definition of the derivative tells us that

x(t+h)≈x(t)+h x′(t).x(t + h) \approx x(t) + h\,x'(t).

It means following the tangent line for a short time hh. Applied to position and velocity, we get the Euler method: from the state at step nn, we compute step n+1n+1 with

r⃗n+1=r⃗n+h v⃗n,v⃗n+1=v⃗n+h a⃗n,witha⃗n=−r⃗nrn3.\vec r_{n+1} = \vec r_n + h\,\vec v_n, \qquad \vec v_{n+1} = \vec v_n + h\,\vec a_n, \qquad\text{with}\quad \vec a_n = -\frac{\vec r_n}{r_n^3}.

It is a recursively defined sequence: each term is computed from the previous one.

Worked example: three steps by hand

We start on the circular orbit: r⃗0=(1,0)\vec r_0 = (1, 0), v⃗0=(0,1)\vec v_0 = (0, 1), and use h=0.1h = 0.1.

Step 1. r0=1r_0 = 1, so a⃗0=−(1,0)/13=(−1,0)\vec a_0 = -(1, 0)/1^3 = (-1, 0).

r⃗1=(1,0)+0.1 (0,1)=(1, 0.1),v⃗1=(0,1)+0.1 (−1,0)=(−0.1, 1).\vec r_1 = (1, 0) + 0.1\,(0, 1) = (1,\ 0.1), \qquad \vec v_1 = (0, 1) + 0.1\,(-1, 0) = (-0.1,\ 1).

Step 2. r1=12+0.12=1.01≈1.0050r_1 = \sqrt{1^2 + 0.1^2} = \sqrt{1.01} \approx 1.0050, so that a⃗1=−(1,0.1)/1.00503≈(−0.9852, −0.0985)\vec a_1 = -(1, 0.1)/1.0050^3 \approx (-0.9852,\ -0.0985).

r⃗2=(1,0.1)+0.1 (−0.1,1)=(0.99, 0.2),v⃗2≈(−0.1,1)+0.1 (−0.9852,−0.0985)=(−0.1985, 0.9901).\vec r_2 = (1, 0.1) + 0.1\,(-0.1, 1) = (0.99,\ 0.2), \qquad \vec v_2 \approx (-0.1, 1) + 0.1\,(-0.9852, -0.0985) = (-0.1985,\ 0.9901).

Step 3. Repeat the procedure yourself. Summary (rounded to 4 decimal places):

nnxnx_nyny_nvx,nv_{x,n}vy,nv_{y,n}rnr_nEnE_n
01.00000.00000.00001.00001.0000−0.5000
11.00000.1000−0.10001.00001.0050−0.4900
20.99000.2000−0.19850.99011.0100−0.4802
30.97010.2990−0.29460.97071.0152−0.4705
40.94070.3961−0.38730.94221.0207−0.4609

The last column is the energy per unit mass,

E=v22−GMr,E = \frac{v^2}{2} - \frac{GM}{r},

which in the real orbit is conserved (it equals −1/2-1/2 all the time). But in the table the radius grows and the energy rises at every step. After one full orbit (63 steps with h=0.1h = 0.1), the simulated planet is already at r≈2.1r \approx 2.1: the orbit opened up into a spiral!

3. Why does Euler make the orbit grow?

Illustration: Why does Euler make the orbit grow?

Look at the first step. In a circular orbit the velocity is perpendicular to the radius. Euler moves the planet in a straight line along the tangent, so the new radius is the hypotenuse of a right triangle with legs 11 and hh:

r12=1+h2>1.r_1^2 = 1 + h^2 > 1 .

And the acceleration is perpendicular to the velocity, so also by Pythagoras v12=1+h2v_1^2 = 1 + h^2. Then

E1=1+h22−11+h2≈−12+h2,E_1 = \frac{1 + h^2}{2} - \frac{1}{\sqrt{1 + h^2}} \approx -\frac{1}{2} + h^2 ,

using 1/1+u≈1−u/21/\sqrt{1 + u} \approx 1 - u/2 for small uu. Every step adds energy (of order h2h^2). A tangent always lies outside a circle, and Euler always follows tangents.

What if we make hh smaller?

Each step adds ∼h2\sim h^2, but one orbit needs 2π/h2\pi/h steps: the error per orbit is ∼h2×2π/h=2πh\sim h^2 \times 2\pi/h = 2\pi h, proportional to hh. Euler is said to be a method of order 1: dividing hh by 10 only divides the error by 10.

hhSteps per orbitrr after one orbit (Euler)Error
0.1632.09109%
0.016281.12112%
0.00162831.01251.25%

To bring the error down to one part in a million would take a billion steps per orbit. What is needed is a better idea, not a faster computer.

4. A small correction: Euler–Cromer

Illustration: A small correction: Euler–Cromer

Alan Cromer (1981) noticed that it is enough to swap the order: first update the velocity and then use the new velocity to move the planet:

v⃗n+1=v⃗n+h a⃗n,r⃗n+1=r⃗n+h v⃗n+1.\vec v_{n+1} = \vec v_n + h\,\vec a_n, \qquad \vec r_{n+1} = \vec r_n + h\,\vec v_{n+1}.

The first step gives v⃗1=(−0.1, 1)\vec v_1 = (-0.1,\ 1) and r⃗1=(0.99, 0.1)\vec r_1 = (0.99,\ 0.1): the planet already “turns inward” a little.

nnxnx_nyny_nvx,nv_{x,n}vy,nv_{y,n}rnr_nEnE_n
01.00000.00000.00001.00001.0000−0.5000
10.99000.1000−0.10001.00000.9950−0.5000
20.97000.1990−0.20050.98980.9902−0.4999
30.93990.2959−0.30040.96940.9854−0.4999
40.90000.3898−0.39860.93840.9808−0.4998

The energy wobbles a little but does not run away: after one orbit with h=0.1h = 0.1, the radius is 1.00061.0006 (an error of 0.06%, versus Euler’s 109% at the same cost); after ten orbits, 1.0061.006; and with h=0.01h = 0.01 the error after one orbit is 2 parts in 100 000. Euler–Cromer belongs to a special family, the symplectic integrators, which respect the geometry of the motion: their energy error stays bounded instead of accumulating. At university the reason is studied (the metodos-numericos module takes it up again with the leapfrog method, which is a “symmetrized” Euler–Cromer).

5. Compare with the exact solution

Illustration: Compare with the exact solution

The following simulator does not use steps: it solves Kepler’s equation, which is the exact solution of the differential equation for two bodies. Set it to e=0e = 0 (circular orbit) and compare: the planet always returns to the same point and rr does not change. That is the behavior a good numerical method must reproduce. With e=0.9e = 0.9, notice how fast the velocity changes near perihelion: there any fixed-step method suffers.

Period T = 1.000 years · t = 0.000 years · r = 0.000 au · v = 0.00 km/s

For the curious: a simulator with two integrators

This second simulator (from the higher-education level) integrates numerically, as you did in the table, and plots on a logarithmic scale the energy error. Choose the scenario “2 bodies, e = 0.8”, the integrator “Both overlaid” and look at the graph: the error of the symplectic method (leapfrog) oscillates without growing, while that of RK4, a more accurate but non-symplectic method, grows little by little. Then raise the step hh and watch how the errors get bigger.

three equal masses, T ≈ 6.3259 · t = 0.00

Activity: the orbit in a spreadsheet

Materials: a computer or phone with a spreadsheet (LibreOffice, Excel, Google Sheets).

  1. Create the columns t, x, y, vx, vy, r, E. In row 2 enter the initial state: 0, 1, 0, 0, 1, with r = =SQRT(B2^2+C2^2) and E = =(D2^2+E2^2)/2-1/F2.
  2. In row 3 write the Euler formulas with h=0.1h = 0.1: x = =B2+0.1*D2, y = =C2+0.1*E2, vx = =D2+0.1*(-B2/F2^3), vy = =E2+0.1*(-C2/F2^3), and copy r and E. Drag down 200 rows.
  3. Make a scatter plot of y against x: you will see the spiral. Also plot E against t.
  4. Copy the sheet and change only the position formulas so that they use the velocity from the same row (x = =B2+0.1*D3, y = =C2+0.1*E3): that is Euler–Cromer. Compare the two plots.
  5. Change hh to 0.05 and to 0.01 (you will need more rows) and write down rr after one orbit (t≈6.28t \approx 6.28) in each case.

Final question: how does Euler’s error change when hh is divided by 10? And Euler–Cromer’s? Which of the two would you use to simulate the Solar System for a thousand years?

Exercises

Basic

  1. With GM=1GM = 1, what is the acceleration of a planet at r⃗=(2,0)\vec r = (2, 0)? And at r⃗=(0,−0.5)\vec r = (0, -0.5)?
Show solution

a⃗=−(2,0)/8=(−0.25,0)\vec a = -(2, 0)/8 = (-0.25, 0); a⃗=−(0,−0.5)/0.125=(0,4)\vec a = -(0, -0.5)/0.125 = (0, 4). It always points toward the star and is 4 times larger if the distance is halved.

  1. Verify step 1 of the Euler table and calculate E1E_1.
Show solution

r⃗1=(1,0.1)\vec r_1 = (1, 0.1), v⃗1=(−0.1,1)\vec v_1 = (-0.1, 1); E1=1.01/2−1/1.01=0.505−0.99504≈−0.4900E_1 = 1.01/2 - 1/\sqrt{1.01} = 0.505 - 0.99504 \approx -0.4900.

  1. Do the first step of Euler–Cromer with h=0.1h = 0.1 and check that E1≈−0.5E_1 \approx -0.5.
Show solution

v⃗1=(−0.1,1)\vec v_1 = (-0.1, 1), r⃗1=(0.99,0.1)\vec r_1 = (0.99, 0.1), r1=0.9901≈0.99504r_1 = \sqrt{0.9901} \approx 0.99504; E1=0.505−1/0.99504=0.505−1.00498≈−0.49998E_1 = 0.505 - 1/0.99504 = 0.505 - 1.00498 \approx -0.49998.

Intermediate

  1. Complete step 3 of Euler by hand (starting from row 2) and compare it with the table.
Show solution

r2≈1.0100r_2 \approx 1.0100, a⃗2≈−(0.99,0.2)/1.01003≈(−0.9609,−0.1941)\vec a_2 \approx -(0.99, 0.2)/1.0100^3 \approx (-0.9609, -0.1941); r⃗3=(0.99−0.01985, 0.2+0.09901)≈(0.9701,0.2990)\vec r_3 = (0.99 - 0.01985,\ 0.2 + 0.09901) \approx (0.9701, 0.2990); v⃗3≈(−0.1985−0.0961, 0.9901−0.0194)=(−0.2946,0.9707)\vec v_3 \approx (-0.1985 - 0.0961,\ 0.9901 - 0.0194) = (-0.2946, 0.9707). ✓

  1. Do the first Euler step with h=0.05h = 0.05. What is r1r_1? By how much did the energy increase? Compare with the prediction ΔE≈h2\Delta E \approx h^2.
Show solution

r1=1.0025≈1.00125r_1 = \sqrt{1.0025} \approx 1.00125; E1=0.50125−0.99875≈−0.4975E_1 = 0.50125 - 0.99875 \approx -0.4975: it increased by ≈0.0025=0.052\approx 0.0025 = 0.05^2. ✓

  1. Use the table in section 3 to estimate Euler’s error after one orbit with h=0.0001h = 0.0001. How many steps would that be? And how many would Euler–Cromer need with h=0.1h = 0.1 for the same error, if its error oscillates without growing?
Show solution

Order 1: dividing hh by 10 divides the error by 10: ≈0.125 %\approx 0.125\,\%, with 62 832 steps. Euler–Cromer with h=0.1h = 0.1 already has an error of 0.06% with 63 steps, a thousand times less work.

Challenge

  1. Why is it not enough to make hh ten times smaller for Euler to work well over 1000 orbits? Think about how many steps there are and how much energy each one adds.
Show solution

With hh ten times smaller, each step adds about 100 times less energy, but there are 10 times more steps: the error per orbit drops only about 10 times (order 1). Over 1000 orbits the accumulated error keeps growing without bound; a better method is needed, not just a smaller step.

  1. A student simulates the Earth with Euler for 100 years and concludes that “the Earth is moving away from the Sun.” What would you tell them? How could they check whether the result is physical or numerical?
Show solution

That it may be a numerical error: Euler always adds energy. They can repeat with a smaller hh (if the result changes, it is numerical), monitor the energy (it must be conserved) or use Euler–Cromer. A result that depends on the step is not physics.

  1. In the Euler method, what role does the approximation x(t+h)≈x(t)+h x′(t)x(t + h) \approx x(t) + h\,x'(t) play? Relate it to the tangent line and explain in which cases the error is large.
Show solution

It means following the tangent line for a time hh. The error is large if hh is large compared with the time over which the velocity changes: for example, near the Sun in a highly eccentric orbit, where the curve bends quickly (see it in the simulator with e=0.9e = 0.9).

✦ Mini-quiz

  1. 1. In units with GM = 1, the acceleration of a planet at position r is…
  2. 2. The Euler method computes r(t+h) as…
  3. 3. When simulating a circular orbit with Euler, the energy…
  4. 4. Euler is of order 1. If I divide h by 10, the error after one orbit…
  5. 5. The only difference between Euler and Euler–Cromer is that…
Answer all the questions to check.

Key takeaways

  • Newton: a⃗=−GM r⃗/r3\vec a = -GM\,\vec r/r^3 is a differential equation; with GM=1GM = 1, the circular orbit has r=v=1r = v = 1 and T=2πT = 2\pi.
  • Euler: r⃗n+1=r⃗n+hv⃗n\vec r_{n+1} = \vec r_n + h\vec v_n, v⃗n+1=v⃗n+ha⃗n\vec v_{n+1} = \vec v_n + h\vec a_n. It follows tangents, so the orbit grows: ΔE≈h2\Delta E \approx h^2 per step.
  • It is of order 1: the error per orbit is proportional to hh.
  • Euler–Cromer: first v⃗\vec v, then r⃗\vec r with the new v⃗\vec v. Same cost, bounded energy.
  • Energy conservation is the thermometer of a simulation: if EE drifts, the result is numerical, not physical.
🍎 Teacher guide
Time
3 sessions of 55 min
Colombian national standard (MEN)
Mathematics, Grades 10 to 11, variational thinking: “I use approximation techniques in infinite numerical processes” and “I interpret the notion of derivative as a rate of change and as the value of the slope of the tangent to a curve, and I develop methods to find the derivatives of some basic functions in mathematical and non-mathematical contexts”; metric thinking: “I justify results obtained through processes of successive approximation, ranges of variation and limits in measurement situations”. Natural Sciences, Grades 10 to 11: “I model mathematically the motion of everyday objects from the forces acting on them”, “I establish relations between the different forces acting on bodies at rest or in uniform rectilinear motion and I establish conditions for conserving mechanical energy”, “I propose models to predict the results of my experiments and simulations” and “I relate the information gathered to the data from my experiments and simulations”.
Evidence of learning
Carries out by hand three steps of Euler and one of Euler–Cromer with their energies, explains with Pythagoras why Euler adds ≈ h² of energy per step, programs both methods in a spreadsheet and measures how the error after one orbit changes when h is divided by 10.

Materials

  • Scientific calculator
  • One computer per pair with a spreadsheet
  • Photocopy of the two step tables with blank cells to fill in
  • Computer or tablet with the simulators

Suggested sequence

Session 1. The equation and the steps (55 min). 10 min review (previous lesson). Derivation of a⃗=−r⃗/r3\vec a = -\vec r/r^3 and units GM=1GM = 1 (10 min). The three steps by hand shared among groups (each group does one step and passes it to the next): it becomes visible that each step depends on the previous one (25 min). Closing: exercises 1 and 2.

Session 2. The trap and the fix (55 min). Pythagoras and ΔE≈h2\Delta E \approx h^2 (15 min); order-1 table (10 min); Euler–Cromer with the first step by hand and exercise 3 (15 min); comparison with the exact solution and, if there is time, the two-integrator simulator (15 min).

Session 3. Spreadsheet (55 min). Activity in pairs (40 min): Euler’s spiral, Euler–Cromer and measurement of the error with three values of hh. Mini-quiz (10 min). Challenges 7–9 as homework. Connects with the grade project: programming the Earth’s orbit with Euler.

Common mistakes

  • Using r instead of r³ in the denominator (forgetting that r/r is already a unit vector). Check with exercise 1.
  • In Euler–Cromer, using the old velocity when updating the position. In the spreadsheet, point out the reference D3 versus D2.
  • Interpreting the spiral as a physical effect (“the planets move away”). Exercise 8: energy must be conserved.
  • Believing that a step 10 times smaller fixes everything. Table in section 3: order 1.
  • Confusing the exact solution of the Kepler simulator with “another step-by-step simulation”.
Key terms in this lesson (12)

Glossary terms that appear in the text. Each one links to its full entry.

  • Euler's method The simplest way to simulate a motion: advance in small time steps assuming the velocity does not change during each step. With small steps it works, but the error builds up.
  • Orbit The path one body follows around another because of gravity: the Moon around Earth, Earth around the Sun. Almost all are ellipses, slightly flattened ovals.
  • Differential equation An equation that does not say where something is, but how it changes: "the acceleration of Earth points toward the Sun and has this value". Solving it step by step is what an orbit simulator does.
  • Symplectic integrator A numerical method designed so that, when simulating orbits over millions of years, energy neither leaks away nor builds up little by little. That is why it is used to study the stability of the Solar System.
  • Derivative How fast something changes at an instant: the derivative of position is velocity, and the derivative of velocity is acceleration. It is the slope of the graph at each point.
  • Gravity The force with which a planet pulls everything toward its center. On Earth it makes things fall 9.8 m/s faster every second; on Mars, with less mass and a smaller size, it is only 38 % of Earth's.
  • Leapfrog A simulation method that alternates a "kick" (changing the velocity) and a "drift" (moving the position), like a frog jumping. It is simple, time-reversible and conserves energy very well.
  • Kepler's laws Three rules about how the planets move: (1) they follow ellipses with the Sun at one focus, (2) they move faster when they are closer to the Sun and (3) distant planets take much longer to go around.
  • Apparent magnitude A number that says how bright a star looks from Earth. The smaller it is, the brighter the star: Sirius has −1.5, and the faintest stars you can see with the naked eye, 6.
  • Kepler's equation The equation that says where on its ellipse a planet is on a given date. It cannot be solved with algebra: it has to be solved by successive approximations, as astronomy programs do.
  • Perihelion The point of the orbit closest to the Sun; there the planet moves fastest. Earth passes through it in early January. For the Moon around Earth it is called perigee.
  • Runge–Kutta method An improvement on Euler's method that "samples" the slope at several intermediate points of each step and averages them. The fourth-order version (RK4) is thousands of times more accurate for the same step size.

See the whole glossary →

References

  • Cromer, A. (1981). Stable solutions using the Euler approximation. American Journal of Physics 49, 455–459.
  • Feynman, R. P., Leighton, R. B. & Sands, M. (1963). The Feynman Lectures on Physics, vol. I, ch. 9 (numerical calculation of an orbit).
  • Hairer, E., Lubich, C. & Wanner, G. (2006). Geometric Numerical Integration, 2nd ed., Springer (ch. I).
  • Tables and errors after one orbit computed with the same methods in units GM = 1 (verifiable with the spreadsheet of the activity).
  • Ministerio de Educación Nacional (2006). Estándares Básicos de Competencias en Lenguaje, Matemáticas, Ciencias y Ciudadanas [Basic Competency Standards in Language, Mathematics, Science and Citizenship]. Bogotá, MEN.

Simulators for this lesson: Keplerian orbitN bodies: integrators and chaos