如何模拟轨道

时间:2016-07-06 15:36:27

标签: python vpython

当我运行我的代码时,地球似乎倾向于限制而不是围绕太阳旋转,我是否缺少方程式或我的代码是否有问题?

这是我得到的错误:

Warning (from warnings module):
  File "C:\Python32\lib\site-packages\visual\visual_all.py", line 52
    return numpy(x)
RuntimeWarning: invalid value encountered in sqrt

这是我写的代码:

from visual import *

def SUVAT(A,B):
        global EarthFinalV
        global Acceleration
        EarthFinalV = sqrt((A) + 2*Acceleration*(B))

GravitationalConstant = 1

Sun = sphere(pos=(0,0,0), radius=10, color=color.red,
             make_trail=True)

Earth = sphere(pos=(50,0,0), radius=5, color=color.yellow,
               make_trail=True)

Sun.mass = 50
Earth.mass = 10

EarthInitialV = vector(0,1000,0)
EarthFinalV = vector(0,0,0)

while True:
    rate(1)
    Distance = Earth.pos - Sun.pos

    GravitationalEquation = (GravitationalConstant*Sun.mass*Earth.mass) / mag(Distance)**2
    Acceleration = GravitationalEquation/Earth.mass

    SUVAT(EarthInitialV,Distance)

    Earth.pos = Earth.pos - EarthFinalV

1 个答案:

答案 0 :(得分:0)

我注意到你的引力方程是错误的。你应该除以半径的平方,而不是半径的两倍。而不是const,请尝试mag(Distance)*2Distance * Distance