我的圈子与我的行星轨道不匹配

时间:2017-12-08 05:28:33

标签: python matplotlib

我的行星轨道与我的圆形路径不匹配我正在绘制轨道,我在我的for循环中匹配每个行星的距离,它们距离中心的距离。(图像已附加)     来自pylab import *     来自matplotlib.animation import * 这是我在定义圆圈的地方,我将在“def circle ...”中显示轨道的路径。

def circle(x0,y0,R,N) :
    ''' create circle, given center x0,y0, input radius R,
        and number of points N, then 
        output arrays of x and y coordinates '''

    theta = linspace(0.0,2.0*pi,N)
    x = R * cos(theta) + x0
    y = R * sin(theta) + y0
    return x,y  


a= array([.39,.72,1,1.52,5.20,9.54,19.2,30.1]) #astronomical units of #the planets in order of the planets(i.e mercury, venus, earth,mars...) 
period = a**(3.0/2.0) 

我正在考虑行星的干扰并将它们输入数组中,以便能够使用for循环来绘制圆圈图。

#distance of the planets 
d= array([390,790,980, 1520,5200,9540,19200,30100])#same order of the #planets as well 

#attributes of the sun
x0 =0 
y0 = 0
r_s=70*1.2#actual earth radius is 695e6 

#radius of the planets 
r_Ear = 63.781#e in m {for all the planets}
r_Merc= 24
r_Ven = 60 
r_Mars= 33.9
r_Jup = 700
r_Sat = 582 
r_Ura = 253
r_Nep = 246

行星的实际距离

#Distance of the planets
d_Ear = 1000#152
d_Merc= 390#70
d_Ven = 790#109
d_Mars= 1520#249
d_Jup = 5200#816
d_Sat = 9540#1514
d_Ura = 19200#3003
d_Nep = 30100#4545

fig = plt.figure()
ax = plt.axes(xlim=(-1e4-5000, 1e4+5000), ylim=(-1e4-5000, 1e4+5000), aspect=True)

这是我循环的地方,绘制8个圆圈。这里有一个链接,可以看到我生成的情节。

for i in range(8) :
    x, y = circle(0.0,0.0,d[i],10000) # orbit
    plot(x,y,':k')
[enter image description here][1] 

其余的是行星的补丁和FuncAnimation。

Sun = plt.Circle((x0, y0), radius=r_s, ec='yellow', fc='yellow', lw=3)
Mercury = plt.Circle((0, 0), radius=r_Merc, ec='brown', fc='brown', lw=3)
Venus = plt.Circle((0, 0), radius=r_Ven, ec='brown', fc='brown', lw=3)
Earth = plt.Circle((0, 0), radius=r_Ear, ec='black', fc='black', lw=3)
Mars = plt.Circle((0, 0), radius=r_Mars, ec='brown', fc='brown', lw=3)
Jupiter = plt.Circle((0, 0), radius=r_Jup, ec='green', fc='green', lw=3)
Saturn = plt.Circle((0, 0), radius=r_Sat, ec='green', fc='green', lw=3)
Uranus = plt.Circle((0, 0), radius=r_Ura, ec='green', fc='green', lw=3)
Neptune = plt.Circle((0, 0), radius=r_Nep, ec='green', fc='green', lw=3)

ax.add_patch(Sun)

def init():

    ax.add_patch(Earth)
    ax.add_patch(Mercury)
    ax.add_patch(Venus) 
    ax.add_patch(Mars) 
    ax.add_patch(Jupiter)
    ax.add_patch(Saturn)
    ax.add_patch(Uranus)
    ax.add_patch(Neptune)

    return Mercury, Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,
def animate(i):


    theta = radians(i)
    mx = d_Merc*np.cos(theta/period[0]) - d_Merc*np.sin(theta/period[0])
    my = d_Merc*np.sin(theta/period[0]) + d_Merc*np.cos(theta/period[0])

    vx = d_Ven *np.cos(theta/period[1]) - d_Ven*np.sin(theta/period[1]) 
    vy = d_Ven *np.cos(theta/period[1]) + d_Ven*np.sin(theta/period[1])

    ex = d_Ear*np.cos(theta/period[2]) - d_Ear*np.sin(theta/period[2])
    ey = d_Ear*np.sin(theta/period[2]) + d_Ear*np.cos(theta/period[2])

    Mx = d_Mars*np.cos(theta/period[3]) - d_Mars*np.sin(theta/period[3])
    My = d_Mars*np.sin(theta/period[3]) + d_Mars*np.cos(theta/period[3])

    Jx = d_Jup*np.cos(theta/period[4]) - d_Jup*np.sin(theta/period[4])
    Jy = d_Jup*np.sin(theta/period[4]) + d_Jup*np.cos(theta/period[4])

    Sx = d_Sat*np.cos(theta/period[5]) - d_Sat*np.sin(theta/period[5])
    Sy = d_Sat*np.sin(theta/period[5]) + d_Sat*np.cos(theta/period[5])

    Ux = d_Ura*np.cos(theta/period[6]) - d_Ura*np.sin(theta/period[6])
    Uy = d_Ura*np.sin(theta/period[6]) + d_Ura*np.cos(theta/period[6])

    Nx = d_Nep*np.cos(theta/period[7]) - d_Nep*np.sin(theta/period[7])
    Ny = d_Nep*np.sin(theta/period[7]) + d_Nep*np.cos(theta/period[7])

    Mercury.center = (mx, my)
    Mercury._angle = i
    Venus.center = (vx, vy)
    Venus._angle = i
    Earth.center = (ex, ey)
    Earth._angle = i
    Mars.center  = (Mx, My)
    Mars.angle =i 
    Jupiter.center = (Jx, Jy)
    Jupiter._angle = i
    Saturn.center = (Sx, Sy)
    Saturn._angle = i
    Uranus.center = (Ux, Uy)
    Uranus.angle = i 
    Neptune.center = (Nx, Ny)
    Neptune._angle = i





    return  Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus,          Neptune,

anim = FuncAnimation(fig, animate, init_func=init, frames=1080, 
                               interval=25, blit=True)


plt.show()

1 个答案:

答案 0 :(得分:1)

我没有关注你的数学:

mx = d_Merc*np.cos(theta/period[0]) - d_Merc*np.sin(theta/period[0])
my = d_Merc*np.sin(theta/period[0]) + d_Merc*np.cos(theta/period[0])

如果你改变它,你会更正确:

mx = d_Merc*np.cos(theta/period[0])
my = d_Merc*np.sin(theta/period[0])

如果考虑到地球的大小,那就更正确了

mx = (d_Merc+(r_Merc/2))*np.cos(theta/period[0])
my = (d_Merc+(r_Merc/2))*np.sin(theta/period[0])

这将解决我认为的基本问题。

除此之外:

  • d_Ear与d数组中的地球值不同。 980对1000。
  • vy = d_Ven *np.cos(theta/period[1])不正确。那需要 是vy = d_Ven *np.sin(theta/period[1])
  • 就代码而言,您可以考虑使用一些词典 避免重复,例如使用d数组。