two_body_ode, a Python code which defines ordinary differential equations (ODE) which simulate the behavior of two bodies, constrained to lie in a plane, moving under the influence of gravity, with one body much more massive than the other.

Two bodies, regarded as point masses, are constrained to lie in a plane. The masses of each body are given, as are the positions and velocities at a starting time T = 0. The bodies move in accordance with the gravitational force between them. One body is assume to be much more massive than the other. Therefore, the common motion of the two bodies about their center of mass can be approximated by assuming that the large body remains fixed.

Under these assumptions, Newton's equations for (x(t),y(t)), the positition of the lighter body with respect to the heavy body at (0,0), can be written as:

        r(t) = sqrt ( x(t)^2 + y(t)^2 )
        x''(t) = - x(t) / r^3
        y''(t) = - y(t) / r^3

These two second order equations can be rewritten as a system of four first order equations using the variable u = [ x(t), x'(t), y(t), y'(t) ], resulting in the equations:

        r = sqrt ( u(1)^2 + u(3)^2 )
        u'(1) =   u(2)
        u'(2) = - u(1) / r^3
        u'(3) =   u(4)
        u'(4) = - u(3) / r^3

By specifying some initial condition for u, the system can then be integrated in time using a standard ODE solver.


The computer code and data files described and made available on this web page are distributed under the MIT license


two_body_ode is available in a MATLAB version and an Octave version and a Python version.

Related Data and codes:

python_ode, Python codes which sets up various systems of ordinary differential equations (ODE).


  1. Charles VanLoan,
    Introduction to Scientific Computing,
    Prentice Hall, 1997,
    ISBN: 0-13-125444-8,
    LC: QA76.9.M35V375.

Source Code:

Last modified on 11 November 2020.