python,matplotlib.animation:我的行星'不旋转?

时间:2017-12-05 07:58:33

标签: python animation matplotlib

我的星球在我的阴谋中没有旋转,我不完全确定为什么?

作为旁注,有没有办法相对于太阳正确地缩放行星半径。初始位置(x,y)可能是距离太阳的远日点距离吗?但不用担心,没有必要回答,只是形成一些洞察力。谢谢。

from pylab import* 
from matplotlib.animation import *

earth_radius = 6.3781e6#meters earth radius
suns_radius = 696e6#meters suns radius 
mercury_radius = 2439.5e3# meters mercury radius 
venus_radius = 6052e3 #meters venus radius

SunEarth_dist = 152e9 #distance from sun to earth approx. 1Au = 152e6km
SunMercury_dist = 69.8e9 #meters
SunVenus_dist = 108.9e9#meters


sun_mass = 1.988e30 #m1 : kg
mercury_mass = .330e24#m2: kg 
venus_mass = 4.87e24 #m3 : kg 
earth_mass = 5.97e24#m4 : kg


#radius of the planets scaled from the sun
r1 = suns_radius
r2 = mercury_radius
r3 = venus_radius
r4 = earth_radius
n = 10000#number of steps
dt = 10000#step size 
G = 6.67384*10**(-11)#gravitational constant


def planets():
    tmax = dt* n 
    t = 0
    x = 0
    #inital position of the planets
    x1 = 3844e8*0.8*0
    y1 = 3844e8*0.8*0
    x2 = 3844e8*0.8
    y2 = -3844e8*0.8*0
    x3 = -3844e8*0.8
    y3 = 3844e8*0.8*0
    x4 = -3844e8*0.8*0
    y4 = -3844e8*0.8
    #intial velocity of each planet

    Velocity_xS = 0
    Velocity_yS = 0
    Velocity_xM = 800
    Velocity_yM= 1700
    Velocity_xV = 0
    Velocity_yV = -1500
    Velocity_xE = 2000
    Velocity_yE = 0

    #distance between the planets
    d12 = sqrt((x1-x2)**2+(y1-y2)**2)
    d23 = sqrt((x2-x3)**2+(y2-y3)**2)
    d13 = sqrt((x1-x3)**2+(y1-y3)**2)
    d14 = sqrt((x1-x4)**2+(y1-y4)**2)
    d24 = sqrt((x2-x4)**2+(y2-y4)**2)
    d34 = sqrt((x3-x4)**2+(y3-y4)**2)
    while t < tmax:
        Fg12 = (G*sun_mass*mercury_mass)/d12**2
        Fgx12 = -Fg12*((x1-x2))/d12
        Fgy12 = -Fg12*((y1-y2))/d12

        Fgx21 = -Fg12*((x2-x1))/d12
        Fgy21 = -Fg12*((y2-y1))/d12

        Fg13 = (G*sun_mass*venus_mass)/d13**2
        Fgx13 = -Fg13*((x1-x3))/d13
        Fgy13 = -Fg13*((y1-y3))/d13

        Fgx31 = -Fg13*((x3-x1))/d13
        Fgy31 = -Fg13*((y3-y1))/d13

        Fg23 = (G*venus_mass*mercury_mass)/d23**2
        Fgx23 = -Fg23*((x2-x3))/d23
        Fgy23 = -Fg23*((y2-y3))/d23

        Fgx32 = -Fg23*((x3-x2))/d23
        Fgy32 = -Fg23*((y3-y2))/d23

        Fg14 = (G*sun_mass*earth_mass)/d14**2
        Fgx14 = -Fg14*((x1-x4))/d14
        Fgy14 = -Fg14*((y1-y4))/d14

        Fgx41 = -Fg14*((x4-x1))/d14
        Fgy41 = -Fg14*((y4-y1))/d14

        Fg24 = (G*sun_mass*earth_mass)/d24**2
        Fgx24 = -Fg24*((x2-x4))/d24
        Fgy24 = -Fg24*((y2-y4))/d24

        Fgx42 = -Fg24*((x4-x2))/d24
        Fgy42 = -Fg24*((x4-x2))/d24

        Fg34 = (G*sun_mass*earth_mass)/d34**2
        Fgx34 = -Fg34*((x3-x4))/d34
        Fgy34 = -Fg34*((y3-y4))/d34

        Fgx43 = -Fg34*((x4-x3))/d34
        Fgy43 = -Fg34*((y4-y3))/d34

        Acceleration_xS = Fgx12/sun_mass + Fgx13/sun_mass + Fgx14/sun_mass
        Acceleration_yS = Fgy12/sun_mass + Fgy13/sun_mass + Fgy14/sun_mass

        Acceleration_xM = Fgx21/mercury_mass + Fgx23/mercury_mass + Fgx24/mercury_mass
        Acceleration_yM = Fgy21/mercury_mass + Fgy23/mercury_mass + Fgy24/mercury_mass

        Acceleration_xV = Fgx32/venus_mass + Fgx31/venus_mass + Fgx34/venus_mass
        Acceleration_yV = Fgy32/venus_mass + Fgy31/venus_mass + Fgy34/venus_mass

        Acceleration_xE = Fgx41/earth_mass + Fgx42/earth_mass+ Fgx43/earth_mass
        Acceleration_yE = Fgy41/earth_mass + Fgy42/earth_mass + Fgx43/earth_mass

        Velocity_xS = Velocity_xS +Acceleration_xS*dt
        Velocity_yS = Velocity_yS +Acceleration_yS*dt

        Velocity_xM = Velocity_xM +Acceleration_xM*dt
        Velocity_yM = Velocity_yM +Acceleration_yM*dt

        Velocity_xV = Velocity_xV +Acceleration_xV*dt
        Velocity_yV = Velocity_yV +Acceleration_yV*dt

        Velocity_xE = Velocity_xE +Acceleration_xE*dt
        Velocity_yE = Velocity_yE +Acceleration_yE*dt

        #update the position of the planets 
        x1 = x1 + Velocity_xS*dt
        y1 = y1 + Velocity_yS*dt


        x2 = x2 + Velocity_xM*dt
        y2 = y2 + Velocity_yM*dt

        x3 = x3 + Velocity_xV*dt
        y3 = y3 + Velocity_yV*dt

        x4 = x4 + Velocity_xE*dt
        y4 = y4 + Velocity_yE*dt

        Sun.center = x1,y1
        Mercury.center = x2,y2
        Venus.center = x3,y3
        Earth.center = x4,y4

        d12 = sqrt((x1-x2)**2+(y1-y2)**2)
        d23 = sqrt((x2-x3)**2+(y2-y3)**2)
        d13 = sqrt((x1-x3)**2+(y1-y3)**2)
        d14 = sqrt((x1-x4)**2+(y1-y4)**2)
        d24 = sqrt((x2-x4)**2+(y2-y4)**2)
        d34 = sqrt((x3-x4)**2+(y3-y4)**2)
        t = t+dt

        return x, t

def initial_points(planets):
    x, t = planets[0], planets[1]
    line.set_data(t, x)
    ctr = Sun.center
    ax.set_xlim(ctr[0]-5e12, ctr[0]+5e12)
    ax.set_ylim(ctr[1]-5e12, ctr[1]+5e12)
    return line

fig = plt.figure()
ax = plt.axes(xlim=(-5e12, 5e12), ylim=(-5e12, 5e12))
ax.set_aspect("equal")
line, = ax.plot([], [], '', ms=10)



Sun = Circle((0,0), r1, fc='yellow')
ax.add_artist(Sun)

Mercury = Circle((0 ,0), r2, fc='brown')
ax.add_artist(Mercury)

Venus = Circle(( 0,0), r3, fc='green')
ax.add_artist(Venus)

Earth = Circle((0,0), r4, fc='red')
ax.add_artist(Earth)


ani = FuncAnimation(fig, initial_points, planets, blit=False,\
     interval=10, repeat=True)

plt.show()

0 个答案:

没有答案