You are sitting in your chair and reading this because of:
I am writing this blog entry for my astronomy term project (not saying which one because the professor my be reading this, but after I finish the course I will remove this note and say a few good words).
Generations of mathematicians have been frustrated because they failed to find an analytic solution to the -body (
) problem represented by the above equation.
The -body problem is easy to solve by using numerical methods. In order to use numerical integration methods one has to convert the original problem to an Ordinary Differential Equation (ODE). Here is how Newton's law of gravity looks for
bodies as an ODE:
This is a second order ODE. To solve it with Python we first have to convert the second order ODE to a system of first-order ODEs. Let's substitute with
and
with
. We now get:
Let us first solve the two-body problem in two dimensions. This, of course, can be done analytically but we want to go through the steps of writing and evolving a Python program. So, here are the four equations:
And this is the Python code that can solve the equations:
#!/usr/bin/env python import numpy import scipy import sys import matplotlib.pyplot import scipy.integrate G = 6.674e-11 m1 = 1e2 m2 = 1e2 def func(Y, t): p1 = numpy.array([ Y[0], Y[1] ]) p2 = numpy.array([ Y[2], Y[3] ]) q1 = numpy.array([ Y[4], Y[5] ]) q2 = numpy.array([ Y[6], Y[7] ]) p1_prime = G * m2 * (q2 - q1) / \ numpy.sum((q2 - q1)**2) p2_prime = G * m1 * (q1 - q2) / \ numpy.sum((q1 - q2)**2) q1_prime = p1 q2_prime = p2 return [ p1_prime[0], p1_prime[1], p2_prime[0], p2_prime[1], q1_prime[0], q1_prime[1], q2_prime[0], q2_prime[1] ] def main(): t = numpy.arange(0, 20000.0, 1) init_p1 = [ 0, 1 ] init_p2 = [ 0, -1 ] init_q1 = [ 0, 0 ] init_q2 = [ 10, 0 ] iv = init_p1 + init_p2 + init_q1 + init_q2 result = scipy.integrate.odeint(func, iv, t) matplotlib.pyplot.scatter(result[:, 4], result[:, 5], s = 1, lw = 0, c = 'blue') matplotlib.pyplot.scatter(result[:, 6], result[:, 7], s = 1, lw = 0, c = 'red') matplotlib.pyplot.show() if __name__ == '__main__': main()
So what do we set for the masses of the objects ( and
) and for the initial position and velocity. Let's try with very small masses (
) and the two bodies going in different directions with the same speed. This is the result:

So, given the initial vector, the above plot looks correct. The two bodies have small mass and as they keep flying in the direction in which they were flying initially. The next check is to see if when we increase the mass of the two bodies they will start attracting each other:

Now we are going to change the initial velocity of one of the two bodies and this is the result:

Finally, we can play some more and get this really complicated looking motion:

Why is all this interesting? Simulation is a very important tool in astronomy and the -body problem served as a fundamental building block of today's understanding of the formation of the Solar system. Of course, Newton's formulas work only for small speeds and we have to add relativity into the picture if we want a more accurate prediction. But before updating our equations we want to generalize this introductory example first into 2-bodies in three dimensions and then to
bodies in three dimensions. This is going to happen in our next post.