2016-07-04 58 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