-2

This is the code i have written in VPython so Far, I'm new to Vpython and i'm not that experienced in python so cut me some slack please, The error I get is:

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 Answers1

0

Besides the physics equations not being correct, the error is caused by adding a number (a component) and a vector (3 components). To make it work, you need to rewrite the function as

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

Then in the while loop (why not say while True:) after SUVAT, replace the 3 statements with

Earth.pos = Earth.pos + EarthFinalV

Spheres have attributes Earth.pos (vector), or Earth.pos.x (a component), but not Earth.x.pos.

Anyway, your code should run, but the physics needs correction.

com-py
  • 1