Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. from vpython import *
  2. import numpy as np
  3.  
  4. #Earth-Moon gravity EMmulation
  5.  
  6. #IMPORTANT CONSTANTS
  7.  
  8. G = 6.67e-11    #Newton's Constant
  9.  
  10. EM = 6e24       #Earth's Mass
  11. MM = 7e22       #Moon's Mass
  12.  
  13. ER = 6.3e6      #Earth's Radius
  14. MR = 1.7e6      #Moon's Radius
  15.  
  16. Distance = 3.8e8
  17.  
  18.  
  19. Earth = sphere(pos = vector(0,0,0), radius = ER, color = color.blue)
  20. Earth.m = EM
  21. Earth.v = vector(0,0,0) #Velocity
  22. Earth.p = Earth.m*Earth.v #Momentum
  23.  
  24. Moon = sphere(pos = vector(Distance,0,0), radius = MR, color = vector(0.5,0.5,0.5), make_trail=True)
  25.  
  26. Moon.m = MM
  27. Moon.v = vector(0,np.sqrt(G*Earth.m/Distance),0)
  28. Moon.p = Moon.m*Moon.v
  29.  
  30. dt = 0.01
  31.  
  32. while True:
  33.     Vec = Earth.pos + Moon.pos
  34.     Fg = -G*Earth.m*Moon.m/mag(Vec)**3*Vec #Vector form of the Gravity Equation
  35.  
  36.     # 1. We will update the momentum according to Pf = FΔt + Pi
  37.  
  38.     Earth.p = -Fg*dt + Earth.p
  39.     Moon.p = Fg*dt+Moon.p #-Fg becDistancese forces are equal and opposite
  40.  
  41.     #2. Update the velocites of each object, p = mv, so v = m/p
  42.  
  43.     Earth.v = Earth.p/Earth.m
  44.     Moon.v = Moon.p/Moon.m
  45.  
  46.     #3. Update the positions
  47.     Earth.pos = Earth.pos+Earth.v
  48.     Moon.pos = Moon.pos+Moon.v
  49.     print("Vector Velocity: ",Moon.v,"Magnitutde: ",mag(Moon.v))
  50.     rate(10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement