Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matyushkin/2bbf77d1541c09f17d48 to your computer and use it in GitHub Desktop.
Save matyushkin/2bbf77d1541c09f17d48 to your computer and use it in GitHub Desktop.
The orbit of the Earth around the Sun
from numpy import sqrt, arange
import matplotlib.pyplot as plt
G = 6.67384e-11 # Gravitational constant, m3 kg-1 s-2
M = 1.989e30 # Mass of Sun, kg
x = 1.475e11 # Perihelion Sun-Earth distance
y = 0
r = 0
vx = 0
vy = 3.01e4 # Velocity of Earth in perihelion
eps = 8.64573e4 # Seconds in a day
x_list = []
y_list = []
ax = 0
ay = 0
for t in arange(0, 365*eps, eps):
x = x + vx*eps
y = y + vy*eps
x_list.append(x)
y_list.append(y)
r = sqrt(x**2 + y**2)
ax = -G*M*x/r**3
ay = -G*M*y/r**3
vx = vx + eps*ax
vy = vy + eps*ay
x_mi = 1.1*min(x_list)
y_mi = 1.1*min(y_list)
x_ma = 1.1*max(x_list)
y_ma = 1.1*max(y_list)
plt.axis([x_mi, x_ma, y_mi, y_ma])
plt.xlabel('x, meters')
plt.ylabel('y, meters')
plt.title('The orbit of the Earth around the Sun')
plt.plot(0, 0, 'yo', label = 'Sun')
plt.plot(x_list, y_list, 'c.', label = 'Earth')
plt.legend()
plt.grid(True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment