1

When I run my code the earth seems to tend towards a limit instead of orbiting round the sun, am I missing equations or is there something wrong with my code?

Here is the error I get:

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

Here is the code I have written:

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
  • Is that the full trace on the warning? Also, just as a nit-pick. You should consider not using glob imports (i.e. `from visual import *`) it can cause issues from time to time. Do you know which part of the code is causing the issue? – Tom Myddeltyn Jul 06 '16 at 15:38
  • 1
    I don't know how SUVAT is getting `Acceleration` since it should be out of scope. Can you post what you think your equations should be in a less programmatic, more mathematical sense? – Tom Myddeltyn Jul 06 '16 at 15:52

1 Answers1

0

I notice your gravity equation is wrong. You are supposed to divide by the radius squared, not twice the radius. Instead of mag(Distance)*2, try Distance * Distance, or Distance**2

PfunnyGuy
  • 750
  • 9
  • 22
  • I got an error for that because its a 3D vector. If i don't have the magnitude of the vector it doesn't run the code. – Benjamin Anderson Jul 06 '16 at 18:53
  • Right, Mag(vector) = vector length. That makes sense, it's been a while since physics class. BUT: F=g * m1 * m2 / (r * r). So make the denominator Mag(Distance) ** 2, not Mag(Distance)*2. The extra asterisk will cause the radius to be squared instead of doubled. – PfunnyGuy Jul 06 '16 at 19:36
  • I've edited it now so that the distance is squared but now it tends to a limit with no error, it still doesn't orbit around the sun. What do you think causes that? – Benjamin Anderson Jul 06 '16 at 19:53
  • Have you looked at @busfault comment above? You've defined `SUVAT()` to use the parameter `Acceleration`, but it is undefined. I believe the default value for `Acceleration` will be 'None'. So try passing in `Acceleration`. That, or make it global as you did for `EarthFinalV` – PfunnyGuy Jul 06 '16 at 20:43
  • Another thing, if you are developing on linux, run pychecker on the code. `pychecker module.py`. It's an adequate static analysis tool. – PfunnyGuy Jul 06 '16 at 20:48
  • I just tried running the code and it didn't change anything. Also i'm running windows 10, is there another way to do pychecker or is it exclusive to Linux. – Benjamin Anderson Jul 07 '16 at 13:34
  • I don't know of any, be feel free to review all listed in this [stackoverflow](http://stackoverflow.com/questions/100298/how-can-i-analyze-python-code-to-identify-problematic-areas) posting. – PfunnyGuy Jul 07 '16 at 17:47