Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. from numpy import sqrt, array, cos, sin, pi, arange, array
  2. from pylab import plot, show, legend, xlabel, ylabel
  3. %matplotlib inline
  4.  
  5. G = 6.674e-11
  6. M = 5.972e24
  7.  
  8. def alpha(r):
  9.     radiusMagnitude = sqrt(x**2 + y**2)
  10.     return -G * M * r / (radiusMagnitude**3)
  11.  
  12. # This program solves dr/dt = f(r,t)
  13. def f(r,t):
  14.    
  15.     # unpack variables
  16.     x = r[0]
  17.     y = r[1]
  18.     vx = r[2]
  19.     vy = r[3]
  20.    
  21.     radiusMagnitude = sqrt(x**2 + y**2)
  22.    
  23.     fx = vx
  24.     fy = vy
  25.     fvx = -G * M * x / (radiusMagnitude**3)
  26.     fvy = -G * M * y / (radiusMagnitude**3)
  27.    
  28.     return array([fx, fy, fvx, fvy], float)
  29.  
  30. # initial conditions
  31. t0 = 0.0                          # initial time
  32. tf = 6.998e6 # 81 days
  33. h = 1 # time step of one hour
  34. tpoints = arange(t0, tf, h) # array of time values
  35.  
  36. x = -3.626e8                          # initial x in meters
  37. y = 0                          # intiial y
  38. vx = 0                # initial vx
  39. vy = 1.077e3       # initial vy m/s
  40. r = array([x, y, vx, vy], float) # initial r vector
  41. xpointsV = []
  42. ypointsV = []
  43. vxpointsV = []
  44. vypointsV = []
  45.  
  46. vxmid = vx + alpha(x) * 0.5 * h
  47. vymid = vy + alpha(y) * 0.5 * h
  48.  
  49. for t in tpoints:
  50.    
  51.     #verlet method
  52.     xpointsV.append(x)
  53.     ypointsV.append(y)
  54.     vxpointsV.append(vx)
  55.     vypointsV.append(vy)
  56.    
  57.     x += vxmid * h
  58.     y += vymid * h
  59.     vx = vxmid + alpha(x) * 0.5 * h
  60.     vy = vymid + alpha(y) * 0.5 * h
  61.     vxmid += alpha(x) * h
  62.     vymid += alpha(y) * h
  63.    
  64. plot (ypointsV, xpointsV, "b-", label="y vs x")
  65. xlabel("y pos")
  66. ylabel("x pos")
  67. show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement