Ordinary Differential Equations II

Eugeniy E. Mikhailov and Greg Bentsen

Logistics and Agenda

Homework 6 due Monday Nov 16 at 11:59pm


Agenda


Final exam

Today

Ordinary differential equations (ODEs) & orbital motion

Predicting the motion of the heavens

ODEs allow us to predict the future!

Source: science.nasa.gov
Source: science.nasa.gov
Source: www.iflscience.com
Source: www.iflscience.com

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
Source: Wikipedia

𝐅=−Gm1m2r3𝐫𝐫=𝐱1−𝐱2 \boxed{\mathbf F=-\frac{G m_1 m_2}{r^3} \mathbf r} \quad \quad \quad \quad \mathbf r = \mathbf x_1 - \mathbf x_2

Both motions are deterministic.

However, regular orbits are predictable, while chaotic motion is not.

Two bodies: motion in the 2D plane

Source: Wikipedia
Source: Wikipedia

The motion of two bodies is relatively simple: Kepler orbits

Two simplifications:

  1. Only need the relative separation 𝐫=𝐫1−𝐫2\mathbf r=\mathbf r_1-\mathbf r_2 (assume the center of mass is fixed).
  2. The angular momentum of the system is conserved: reduces motion to the 2D plane, with

𝐫(t)=(x(t),y(t))\mathbf{r}(t) = (x(t), y(t))

Thus, we only need to solve for x(t)x(t) and y(t)y(t).

Equations of motion for two bodies

Newton’s second law 𝐅=m𝐚\mathbf{F} = m \mathbf{a} and the gravitational force law 𝐅=−Gm1m2r3𝐫\mathbf F=-\frac{G m_1 m_2}{r^3}\mathbf r combine to yield:

d2𝐫dt2=𝐫̈=−μ|𝐫|3𝐫 \boxed{\frac{d^2 \mathbf r}{dt^2} = \ddot{\mathbf r}=-\frac{\mu}{|\mathbf r|^3}\mathbf r}

where μ=G(m1+m2)\mu=G(m_1+m_2). Or, equivalently (setting μ=1\mu = 1):

ẍ=−x(x2+y2)3/2,ÿ=−y(x2+y2)3/2. \ddot x=-\frac{x}{(x^2+y^2)^{3/2}},\qquad \ddot y=-\frac{y}{(x^2+y^2)^{3/2}}.

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:

ẍ=−x(x2+y2)3/2,ÿ=−y(x2+y2)3/2. \ddot x=-\frac{x}{(x^2+y^2)^{3/2}},\qquad \ddot y=-\frac{y}{(x^2+y^2)^{3/2}}.

The essential trick: use the velocity vx=ẋ=dx/dtv_x = \dot x = dx/dt to express the second derivatives as ẍ=v̇x\ddot x = \dot v_x.

So we define the state vector 𝐬=(x,y,vx,vy)\mathbf s=(x,y,v_x,v_y) to obtain four 1st-order ODEs:

d𝐬dt=(vxvy−x/r3−y/r3)=f(t,𝐬),r=x2+y2. \boxed{\frac{d\mathbf s}{dt}= \begin{pmatrix} v_x\\v_y\\-x/r^3\\-y/r^3 \end{pmatrix}=f(t,\mathbf s)},\qquad r=\sqrt{x^2+y^2}.

Solving the ODEs with SciPy

d𝐬dt=(vx,vy,−x/r3,−y/r3)=f(t,𝐬) \frac{d\mathbf s}{dt}= ( v_x, v_y, -x/r^3, -y/r^3 )=f(t,\mathbf s)

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)

Code demo I: four kinds of Kepler motion

DEMO: kepler_orbits.py

For r0=1r_0=1, μ=1\mu=1, and tangential launch speed v0v_0

Total energy: E=12v02−1/r0E = \frac{1}{2} v_0^2-1/r_0

v0v_0 Orbit Energy
11 Circular −1/2-1/2
0.70.7 Elliptical <0<0
2\sqrt2 Parabolic escape 00
1.61.6 Hyperbolic escape >0>0

Numerical sanity check: What is the escape speed?

Validating numerical solutions

Question: When can we trust numerical solutions to ODEs?

Conservation laws: a physicist’s debugging tools

An isolated gravitational system conserves energy, momentum, and angular momentum:

E=∑i12mi|𝐯i|2−G∑i<jmimj|𝐫i−𝐫j| \boxed{E=\sum_i\frac12 m_i |\mathbf v_i|^2 -G\sum_{i<j}\frac{m_i m_j}{|\mathbf r_i-\mathbf r_j|}}

𝐏=∑imi𝐯i, \mathbf P=\sum_i m_i\mathbf v_i,\qquad

𝐋=∑imi𝐫i×𝐯i \mathbf L=\sum_i m_i\mathbf r_i\times\mathbf v_i

Code demo 2: Test energy conservation

For m1=m2=μ=1m_1 = m_2 = \mu = 1 the total energy is E=12v2−1/rE = \frac{1}{2} v^2-1/r

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

  1. Compare Euler’s method against solve_ivp from identical initial conditions.
  2. Plot the orbit and energy drift side by side.
  3. 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
Source: Wikipedia

Three bodies = 12 first-order equations in a plane

For each body i=1,2,3i=1,2,3:

𝐫̈i=G∑j≠imj𝐫j−𝐫i|𝐫j−𝐫i|3. \boxed{\ddot{\mathbf r}_i= G\sum_{j\ne i}m_j \frac{\mathbf r_j-\mathbf r_i}{|\mathbf r_j-\mathbf r_i|^3}}.

As before, reduce from 2nd-order ODEs to 1st-order ODEs

Write the state as six positions followed by six velocities:

𝐬=(x1,y1,x2,y2,x3,y3,vx1,vy1,vx2,vy2,vx3,vy3). \mathbf s=(x_1,y_1,x_2,y_2,x_3,y_3, v_{x1},v_{y1},v_{x2},v_{y2},v_{x3},v_{y3}).

Python: just change the right-hand side

For mi=G=1m_i = G = 1, we have 𝐫̈i=∑j≠i𝐫j−𝐫i|𝐫j−𝐫i|3.\boxed{\ddot{\mathbf r}_i=\sum_{j\ne i} \frac{\mathbf r_j-\mathbf r_i}{|\mathbf r_j-\mathbf r_i|^3}}.

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)])

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

𝐅=m𝐚=−GmMr3𝐫 \boxed{\mathbf F=m \mathbf a = -\frac{G m M}{r^3} \mathbf r}

  • Convert nn 2nd-order ODEs into 2n2n 1st-order ODEs
    • Trick: introduce velocity v=ẋv = \dot x and use v̇=ẍ\dot v = \ddot x.
    • Standard form:

𝐬̇=f(t,𝐬) \dot{\mathbf s}=f(t,\mathbf s)

  • Use solve_ivp to solve ODEs in standard form. Need to specify f(t,𝐬)f(t,\mathbf s)
  • 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
Source: science.nasa.gov