2016-07-04 52 views
-2

这是我写在VPython中的代码,到目前为止,我是Vpython的新手,我不是那种在python中经验丰富的,所以请给我一些松懈,我得到的错误是:在Visual Python中模拟轨道时遇到了问题

Traceback (most recent call last): File "C:\Users\Makka Pakka\Documents\Planetary Orbits.py", line 28 
    SUVAT(EarthInitialV,Distance) File "C:\Users\Makka Pakka\Documents\Planetary Orbits.py", line 4, in SUVAT 
    EarthFinalV.x = sqrt((A.x) + 2*Acceleration*(B.x)) TypeError: unsupported operand type(s) for +: 'float' and 'vector' 
from visual import * 

def SUVAT(A,B): 
     EarthFinalV.x = sqrt((A.x) + 2*Acceleration*(B.x)) 
     EarthFinalV.y = sqrt((A.y) + 2*Acceleration*(B.y)) 
     EarthFinalV.z = sqrt((A.z) + 2*Acceleration*(B.z)) 

GravitationalConstant = 10 

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(5,5,5) 
EarthFinalV = vector(0,0,0) 

while 1 != 2: 
    Distance = Earth.pos - Sun.pos 

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

    SUVAT(EarthInitialV,Distance) 

    Earth.x.pos = Earth.x.pos + EarthFinalV.x 
    Earth.y.pos = Earth.y.pos + EarthFinalV.y 
    Earth.z.pos = Earth.z.pos + EarthFinalV.z 
+3

您碰到什么问题?你用上面的代码观察什么?在你的问题中回答这些问题将有助于获得更多答复。 – nbryans

+0

哦对不起,我必须包括,现在编辑它 –

回答

0

除了不是正确的物理方程, 误差通过加入数(分量)和载体(3个分量)引起的。为了使它工作,你需要SUVAT后重写功能

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

然后在while环(为什么不说while True:),与

Earth.pos = Earth.pos + EarthFinalV 

球有属性Earth.pos更换3个语句(矢量)或Earth.pos.x(一个组件),但不是Earth.x.pos

无论如何,你的代码应该运行,但物理需要更正。

+0

非常感谢你帮助我,代码确实工作 –

+0

接受答案,然后:) – DeltaG