Ordinary Differential Equations II
Eugeniy E. Mikhailov and Greg Bentsen
Logistics and Agenda
Homework 6 due Monday Nov 16 at 11:59pm
Agenda
- This week: Ordinary Differential Equations (ODEs)
- Next week: Fourier transforms
- Nov 23: Remote class (Nov 25 - 29: Thanksgiving)
- Nov 30 - Dec 4: Machine learning & review
Final exam
- Dec 9 or Dec 11
- Same 2-part structure as midterms (take-home, in-class)
Today
Ordinary differential equations (ODEs) & orbital motion
- Newton’s universal law of gravitation
- 2-body motion & Kepler orbits
- Energy conservation as a debugging tool
- 3-body motion & chaos
Predicting the motion of the heavens
ODEs allow us to predict the future!
Source: science.nasa.gov
Source: www.iflscience.com
- Newton’s universal law of gravitation predicts the motion of
planets, moons, comets, and spacecraft.
- Example: Halley’s comet returns once every ~76
years
- Understood by English astronomer Edmond Halley using Newton’s laws
in 1705
- Predicted return in 1758 and showed that sightings from 1531, 1607,
and 1682 were the same object
Today’s motivating question: If we know the masses, positions, and
velocities today, how accurately can we predict the motion
tomorrow—or in 100 years?
The answer is: it depends
Newton’s law of universal gravitation
Source: Wikipedia
- Two bodies: regular, analytically solvable
orbits.
- Three bodies: the same law can generate chaotic
motion.
Both motions are deterministic.
However, regular orbits are predictable, while
chaotic motion is not.
Two bodies: motion in the 2D plane
Source: Wikipedia
The motion of two bodies is relatively simple: Kepler orbits
Two simplifications:
- Only need the relative separation
(assume the center of mass is fixed).
- The angular momentum of the system is conserved: reduces motion to
the 2D plane, with
Thus, we only need to solve for
and
.
Equations of motion for two bodies
Newton’s second law
and the gravitational force law
combine to yield:
where
.
Or, equivalently (setting
):
Thus the motion is described by a pair of 2nd-order ODEs.
Reducing 2nd-order ODEs to 1st-order ODEs
Pair of 2nd order ODEs:
The essential trick: use the velocity
to express the second derivatives as
.
So we define the state vector
to obtain four 1st-order ODEs:
- We need four initial conditions:
.
- This first-order representation
is exactly what
solve_ivp expects.
Solving the ODEs with SciPy
import numpy as np
from scipy.integrate import solve_ivp
def func(t, state):
x, y, vx, vy = state
r = np.sqrt(x**2 + y**2)
return [vx, vy, -x/r**3, -y/r**3]
s0 = [1.0, 0.0, 0.0, 1.0] # Initial conditions
eval_times = np.linspace(0, 20, 1000)
sol = solve_ivp(fun=func, t_span=(0, 20), y0=s0,
t_eval=eval_times, rtol=1e-9, atol=1e-11)
func gives derivatives, not the next
discrete time step.
sol.y[0], sol.y[1] are
,
;
plot
versus
.
t_eval sets output times;
rtol and atol control solver accuracy.
Code demo I: four kinds of Kepler motion
DEMO: kepler_orbits.py
For
,
,
and tangential launch speed
Total energy:
|
Circular |
|
|
Elliptical |
|
|
Parabolic escape |
|
|
Hyperbolic escape |
|
Numerical sanity check: What is the escape
speed?
Validating numerical solutions
Question: When can we trust numerical solutions to
ODEs?
- Elliptical orbits should be stable: (no spiral inward nor
outward).
- Numerical solutions like Euler’s method can drift
badly.
- We can use conservation laws as a nontrivial sanity
check to validate our simulations.
Code demo 2: Test energy conservation
For
the total energy is
x, y, vx, vy = sol.y # Extract numerical solve_ivp solution
r = np.sqrt(x**2 + y**2)
energy = 0.5 * (vx**2 + vy**2) - 1 / r
energy_error = energy - energy[0]
DEMO: kepler_energy.py
- Compare Euler’s method against
solve_ivp from identical
initial conditions.
- Plot the orbit and energy drift
side by side.
- Decrease Euler’s time step: does the trajectory improve?
Takeaway: Physical conservation laws provide
numerical sanity checks—but conserved energy alone does not
prove an orbit is accurate.
The three-body problem
- Real examples: Sun–Earth–Moon, triple-star systems,
spacecraft in multi-body gravitational fields.
- Setting for Liu Cixin’s The Three-Body Problem.
- The force law hasn’t changed—we have added just one interacting
mass.
Question: Does a deterministic force law guarantee a
simple, predictable trajectory?
Source: Wikipedia
Three bodies = 12 first-order equations in a plane
For each body
:
As before, reduce from 2nd-order ODEs to 1st-order ODEs
Write the state as six positions followed by six
velocities:
- Three bodies
two coordinates
(position + velocity)
ODEs.
- Each acceleration depends on both other
bodies.
solve_ivp handles the same kind of first-order system
as before.
Python: just change the right-hand side
For
,
we have
def func_3body(t, state):
x = state[:6].reshape(3, 2) # positions
v = state[6:].reshape(3, 2) # velocities
a = np.zeros((3, 2)) # accelerations
for i in range(3):
for j in range(3):
if i != j:
dx = x[j] - x[i]
r = np.linalg.norm(dx)
a[i] += dx / r**3
return np.concatenate([v.reshape(6), a.reshape(6)])
- Same
solve_ivp interface; now the force is a
sum over pairs.
- Close approaches require extra numerical care because gravity is
singular at collision.
- Energy, total momentum, and angular momentum are still
conserved.
Code demo 3: chaos and the butterfly effect
DEMO: three_body_chaos.py (use
--animate for moving trajectories)
- Compare two systems with almost identical initial
positions.
- First they appear to agree; later their trajectories differ
enormously.
- This is sensitivity to initial conditions, i.e. the
butterfly effect
Key distinction: Deterministic
motion does not necessarily mean that motion is
predictable.
Recap and takeaways
- Orbital motion obeys Newton’s second law + universal law of
gravitation
- Convert
2nd-order ODEs into
1st-order ODEs
- Trick: introduce velocity
and use
.
- Standard form:
- Use
solve_ivp to solve ODEs in standard form. Need to
specify
- Energy conservation is a useful sanity check and debugging tool
- Ordinary differential equations are deterministic,
but do not always lead to predictable results due to
chaos and the butterfly effect.
Source: science.nasa.gov
