在Visual Python中模拟轨道时遇到问题

时间:2016-07-04 15:48:19

标签: python vpython

这是我在VPython中写的代码到目前为止,我是Vpython的新手,我不是那种经验丰富的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

1 个答案:

答案 0 :(得分:0)

除了物理方程式不正确之外, 错误是由添加数字(组件)和向量(3个组件)引起的。要使其工作,您需要将该函数重写为

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

然后在while之后while True:循环(为什么不说SUVAT),用

替换3个语句
Earth.pos = Earth.pos + EarthFinalV

球体具有属性Earth.pos(向量)或Earth.pos.x(组件),但不具有Earth.x.pos

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