The Two-Body Problem in Python

Visualising the motion of two bodies due solely to their mutual gravitational attractionTwo masses represented in inertial space experience a mutual force of gravitational attraction.The objective of the two-body problem is to determine how a pair of o…


This content originally appeared on Level Up Coding - Medium and was authored by Andrew Joseph Davies

Visualising the motion of two bodies due solely to their mutual gravitational attraction

Two masses represented in inertial space experience a mutual force of gravitational attraction.

The objective of the two-body problem is to determine how a pair of objects move relative to one another under the influence of gravity.

Review Example 2.2 in the latest Orbital Mechanics for Engineering Students textbook to understand the theory in depth.

Photo by NASA on Unsplash

Understanding the System

Figure 1 shows the two-body problem, a diagram of two masses m₁ and m₂ in an inertial reference frame. An inertial reference frame is essentially not accelerating. The system is free of external forces; only gravity acts between the two objects.

Figure 1 — Two Masses Located in Inertial Space

The critical parameters of interest from the diagram are:

  • R₁: the position vector from the inertial frame origin to m1 as shown in Equation 1
Equation 1 — Body 1 Position Vector in Inertial Space
  • R₂: the position vector from the inertial frame origin to m2, given by Equation 2
Equation 2 — Equation 1 — Body 2 Position Vector in Inertial Space
  • r: the position vector from m₁ to m₂, found using Equation 3
Equation 3 — Position Vector from Body 1 to Body 2
  • G: the centre of mass of the system. If m₁ > m₂, the COM will lie closer to m₁.
  • RG: the position vector from the inertial frame origin to the system’s centre of mass (COM) and is determined using Equation 4.
Equation 4 — Position Vector of the Centre of Mass

Deriving the Equations of Motion

Applying Newton’s Law of Universal Gravitation and Newton’s Second Law gives the equations of motion (EOM). Knowing the EOM and the initial position and velocity vectors makes it possible to propagate the system state through time.

Equation 5 is Newton’s Law of Universal Gravitation representing the force, F, of attraction between two particles of matter.

Equation 5 — Newton’s Law of Universal Gravitation

Equation 6 shows Newton’s second law stating the relationship between the acceleration of an object of matter and the sum of the forces acting it.

Equation 6 — Newton’s Second Law

In the two-body system, the only force considered is gravity. Therefore, the force F₁₂ directed from m₁ to m₂ results from combining the above equation into the two-body EOM shown in Equation 7.

Equation 7 — Two-Body Equation of Motion

Newton’s third law indicates that the force directed from m₂ towards m₁ is equal and opposite to F₁₂. Equation 8 expresses the unit vector from object 1 to object 2.

Equation 8 — Unit Vector from Object 1 –> 2

Substituting the unit vector expression above into Equation 7 gives the final form of the equation of motion.

Equations 9 and 10 — Equations of Motion Describing the Inertial Motion of Body 1 and Body 2 Respectively

Equation 7 is a vector expression for acceleration with inertial components in the X, Y and Z directions. Thus, breaking Equations 9 and 10 into their constituent parts results in Equations 11— 16.

Equations 11–16 — Equations of Motion in Inertial Components

Numerical Integration

Numerically integrating the equations of motion gives the position and velocity of the masses over time. odeint from the scipy Python library is an adequate integral solver. The solver, such as Runge-Kutta, requires first-order ordinary differential equations (ODEs). Thus, several auxiliary variables facilitate the system modification, as shown in Equation 17–29.

Equations 17–29 — Auxiliary Variables Introduced for Numerical Method

The second-order system of six equations is cast to twelve first-order ODEs using the variables defined above, represented in Equations 30–41 below.

Equations 30–41 — Twelve First-Order Differential Equations

How is Numerical Integration Done Using Python

Initial conditions specify the positions and velocities of both objects at simulation commencement. Equations 42–45 show the conditions provided in the orbital mechanics textbook used in the simulation.

Equations 42–45 — Initial Conditions for Position and Velocity for both Bodies

Python Implementation

Figure 2 shows the Python code defining the simulation harness parameters. These parameters include the mass of bodies 1 and 2 and the initial position and velocities in the state vector y₀.

Figure 2 — Simulation Harness Parameters

The two-body system of first-order ODEs is a Numpy array in Python, indicated by Figure 3. Verify that these equations match Equations 11 through 16.

Propagating the initial conditions through time to solve the two-body problem is performed using a single line of Python, as exhibited below.

# solve two-body problem
y = odeint(two_body_eqm, y0, time, args=(G, m1, m2))
Figure 3 — Two-Body Equations of Motion as Numpy Array

y contains the history of the state vectors, position and velocities of m₁ and m₂, from time t₀ = 0 to t = 480 seconds.

  • y[0:3]: the X, Y and Z components of R₁ at time tᵢ
  • y[3:6]: the position of m₂ relative to the inertial frame

Figure 4 shows the Python code used to determine other position vectors relative to crucial points of interest, allowing motion visualisation from multiple perspectives, e.g. from the centre of mass or one of the objects.

Figure 4 — Python Code to find Position Vectors Relative to Key Points

Simulation Results

Figure 5(a) is the simulation animation from two perspectives.

  1. The trajectories of m₂ and the centre of mass as seen from body 1. Both the COM and body two appear to move in the shape of an ellipse.
  2. The orbits of both bodies that an observer would see positioned at the COM. Both orbits are elliptical.
Figure 5(a) — Relative Motion Animation (m₁ = cyan, m₂ = yellow, com = magenta)

Figure 5b shows the motion of the two bodies relative to inertial space.

The two-body system establishes a periodic spiral motion around the straight-line trajectory of the entre of mass through space relative to the inertial frame.¹
Figure 5(b) — Two-Body Problem Simulation

In the inertial frame, the centre of mass moves in a straight line with a constant velocity, proving that the system is free of external forces. This motion continues indefinitely.

This article demonstrates a method to find and then numerically integrate the equations of motion describing two bodies moving solely under the influence of the gravitational force acting between them.

The method presented to solve the two-body problem can simplify into a single body problem. See the references listed below for this analysis.

Similar Articles

3D Kinematics Visualisation with Python Libraries SymPy and NumPy

Find the Python code for solving the two-body equations of motion below.

References

[1] Orbital Mechanics for Engineering Students


The Two-Body Problem in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Andrew Joseph Davies


Print Share Comment Cite Upload Translate Updates
APA

Andrew Joseph Davies | Sciencx (2022-02-07T00:29:59+00:00) The Two-Body Problem in Python. Retrieved from https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/

MLA
" » The Two-Body Problem in Python." Andrew Joseph Davies | Sciencx - Monday February 7, 2022, https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/
HARVARD
Andrew Joseph Davies | Sciencx Monday February 7, 2022 » The Two-Body Problem in Python., viewed ,<https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/>
VANCOUVER
Andrew Joseph Davies | Sciencx - » The Two-Body Problem in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/
CHICAGO
" » The Two-Body Problem in Python." Andrew Joseph Davies | Sciencx - Accessed . https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/
IEEE
" » The Two-Body Problem in Python." Andrew Joseph Davies | Sciencx [Online]. Available: https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/. [Accessed: ]
rf:citation
» The Two-Body Problem in Python | Andrew Joseph Davies | Sciencx | https://www.scien.cx/2022/02/07/the-two-body-problem-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.