simulate the orbit of a satellite in the earth moon system in python

To simulate the orbit of a satellite in the Earth-Moon system, we can use the following steps:

  1. Define the parameters of the system, such as the masses of the Earth and the Moon, the distance between them, and the initial position and velocity of the satellite.

  2. Use the gravitational force formula to calculate the acceleration of the satellite due to the gravitational attraction of the Earth and the Moon.

  3. Update the position and velocity of the satellite using numerical integration techniques, such as Euler's method, Runge-Kutta method, or Verlet integration.

  4. Repeat steps 2 and 3 for a certain number of time steps to simulate the orbit of the satellite.

Here's an example implementation of a simulation using Euler's method in Python:

main.py
import numpy as np
import matplotlib.pyplot as plt

# Parameters of the system
G = 6.67e-11  # gravitational constant
M_e = 5.97e24  # mass of Earth
M_m = 7.35e22  # mass of Moon
R_em = 3.84e8  # distance between Earth and Moon
v_s = 1e3  # initial velocity of satellite
h = 1  # time step of simulation
t_max = 3600*24*30  # duration of simulation

# Initial position of satellite (at apogee)
r_s = np.array([R_em, 0, 0])
v_s = np.array([0, v_s, 0])

# Simulation loop
t = 0
while t <= t_max:
    # Distance between satellite and Earth/Moon
    r_e = -r_s
    r_m = np.array([R_em*np.cos(2*np.pi*t/(3600*24*27)), R_em*np.sin(2*np.pi*t/(3600*24*27)), 0])
    d_e = np.linalg.norm(r_e)
    d_m = np.linalg.norm(r_m)

    # Acceleration of satellite due to gravitational force
    F_e = G*M_e/d_e**2 * r_e/d_e
    F_m = G*M_m/d_m**2 * r_m/d_m
    a_s = (F_e + F_m)/M_s

    # Update position and velocity of satellite
    r_s += v_s*h
    v_s += a_s*h

    t += h

# Plot the orbit of the satellite
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(r_s[:,0], r_s[:,1], r_s[:,2])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
1165 chars
45 lines

This code simulates the orbit of a satellite in the Earth-Moon system using Euler's method. It calculates the acceleration of the satellite due to the gravitational attraction of the Earth and the Moon, and updates its position and velocity using small time steps. The resulting trajectory of the satellite is plotted in a 3D graph using Matplotlib.

gistlibby LogSnag