The first task was to generalize the 1-body problem, coded in the textbook, to the 2-body problem, where now we assume that both bodies have substantial mass relative to each other. Mathematically, the difference was easy to adapt: create 2 bodies, with positions \(x_{1}\) and \(x_{2}\), and velocities \(vx_{1}\) and \(vx_{2}\), respectively. Simply add 4 more differential equations to make a system of 8 ODEs, to be solved by the code. The extra ODEs added were:
\begin{align*} z_{5}&=x_{2}'=v_{x_{2}}\\ z_{6}&=\frac{m_1(x_1-x_2)}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}}\\ z_{7}&=y_{2}'=v_{y_{2}}\\ z_{8}&=\frac{m_1(y_1-y_2)}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} \end{align*}
Our code for part 1 can be viewed here. Initially, we kept the Euler scheme in our code. The output for the Euler two-body is shown here:
You can see how the body does not stay on a steady orbit, but slowly spirals away from the body. This is visible not because this is what the orbit actually looks like, but because Euler is merely a first-order method.
To amend this, we added a fourth-order Runge-Kutta scheme to our code. In the code, you can specify which approximation scheme to view by adjusting the last parameter in the input line. If the final variable is 1, it runs an Euler, and anything else will run RK4. The RK4 video can be viewed here: