使用python制作行星的轨道

时间:2019-05-26 11:26:30

标签: python physics

我已经为'r'(半径)值编写了一般轨道方程的代码。并尝试绘制x = r cos(theta)和y = r sin(theta)的图。 但是我的代码给了我相同的x值,不同的r值。所以我以{而不是椭圆形}结束enter code here

from numpy import *
import matplotlib.pyplot as plt
import pprint

L = 9.11*10**38     #L = angular momentum
m = 3.28*10**23     #m = mass of mercury
M = 1.99*10**30     #M = mass of sun
a = 5.8*10**7       #a = semi-major axis
G = 6.674*10**-11   #G = Gravitationl constant
k = G*M*m        
E = -k/(2*a)        #E = energy
p = L**2/(m*k)   
c = 1 + (2*E*L**2)/m*k**2
e = sqrt(-c)        #e = eccentricity

def fx(x):
    r = p/(1 + e*cos(x))
    return r

n = 1000
phi =linspace(0,2*pi,n)
radius = zeros([n])
theta = zeros([n])
x = zeros([n])
y = zeros([n])

for i in range(0,n):
    radius[i] = fx(phi[i])
    theta[i] = 180*phi[i]/pi

for i in range(0,n):
    x[i] = radius[i]*cos(phi[i])

for i in range(0,n):
    y[i] = radius[i]*sin(phi[i])

print('r =',radius)
print('x =',x)
print('y =',y)
plt.plot(x,y)
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:2)

您的代码至少有两个问题:

1)a = 5.8*10**7应该为a = 5.8*10**10,因为(根据您的G),您需要的距离以米为单位,而不是公里。

2)正如@JohnO所指出的那样,您需要1 + (2*E*L**2)/(m*k**2)而不是1 + (2*E*L**2)/m*k**2

如果您进行了这两项更改,您将得到一个合理的椭圆:

enter image description here