Exercise 3
Forward integration
In class01, we wrote a julia program orbit.jl that simulates a
two-dimensional gravitating system. It describes a test mass
\(\mathsf{m}\) in orbit around a heavy mass \(\mathsf{M}\),
following the usual \(\mathsf{1/r^2}\) law and also subject
to a velocity-dependent drag force:
Here, \(\mathsf{\vec{r}(t)} = \mathsf{x\hat{x} + y\hat{y}}\)
is the position vector, and \(\mathsf{\vec{v}(t)} =
\mathsf{\dot{\vec{r}}} = \mathsf{\dot{x}\hat{x} + \dot{y}\hat{y}}
= \mathsf{v_x\hat{x} + v_y\hat{y}}\) is the vector velocity. In units
where GM = 1 and C_0 = 0.01, the initial conditions are
\(\mathsf{\vec{r}(0)} = \mathsf{\hat{x}}\) and
\(\mathsf{\vec{v}(0)} = \mathsf{\hat{y}}\)
(corresponding to the planet starting at the “eastern” edge of the orbit
and moving “north”). These are chosen to produce a near-circular orbit
that slowly spirals in toward the central mass \(\mathsf{M}\).
In the code, we don’t use vectors. Rather, we define individual
floating-point variables defined for the \(\mathsf{x}\) and
\(\mathsf{y}\) components of the position (x and y), velocity (vx and \mathsf{vy}), and acceleration
(ax and ay). Breaking the
vector differential equation into components gives
Integrating over small finite steps of duration \(\mathsf{\Delta t}\) produces
where the final equality is guaranteed to hold (by a theorem of calculus, very easy to justify geometrically) for some instant in time satisfying \(\mathsf{t < t^* < t + \Delta t}\).
Verlet algorithm
A quick and dirty (but surprisingly effective) approximation is to put \(\mathsf{t^* = t + \Delta t/2}\) at the interval midpoint and to update the position in two half-steps; this leads to the famous Verlet algorithm, viz.,
Some things to try
Try generalizing the program from two to three dimensions, and give the test mass a small out-of-plane initial velocity, \(\mathsf{z(0) = 0.1}\). Let’s call that code
orbit3D.jl.Try converting your julia program to python. Confirm that
orbit3D.jland the neworbit3D.pyboth produce the same trajectory.$ julia orbit3D.jl > traj1.dat $ python3 orbit3D.py > traj2.dat $ gnuplot > plot for [n=1:2] "traj".n.".dat" using 1:2 with lines
Notice that the stopping condition (
r < R0) terminates the program when the test mass hits the surface of the “planet” (we model the large mass \(\mathsf{M}\) as a sphere or radius \(\mathsf{R_0}\)). What if that stopping condition is removed? In physical terms, what if \(\mathsf{M}\) is a point mass with no spatial extent? See what the outcome is and try to relate it to your understanding of the floating-point representation.