Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. %matplotlib inline
  2. import math
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. class Planet:
  7.  
  8. def __init__(self,x,y,vx,vy):
  9. self.x = []
  10. self.y = []
  11. self.vx = []
  12. self.vy = []
  13. self.vx.append(vx)
  14. self.vy.append(vy)
  15. self.x.append(x)
  16. self.y.append(y)
  17.  
  18. def doTimeStep(self,dt):
  19. k = 4*math.pi**2
  20. n = 0
  21. initialAngle = np.sign(self.y[n]) * np.arccos(self.x[n] / np.sqrt(self.x[n] ** 2 + self.y[n] ** 2))
  22. okToExitLoop = False
  23.  
  24. for n in range(1,1000000):
  25. self.vx.append(self.vx[n-1]+(-k)*self.x[n-1]/((self.x[n-1]**2+self.y[n-1]**2)**(3/2))*dt)
  26. self.vy.append(self.vy[n-1]+(-k)*self.y[n-1]/((self.x[n-1]**2+self.y[n-1]**2)**(3/2))*dt)
  27. self.x.append(self.x[n-1]+self.vx[n]*dt)
  28. self.y.append(self.y[n-1]+self.vy[n]*dt)
  29.  
  30. if np.sign(self.y[n]) * np.arccos(self.x[n] / np.sqrt(self.x[n] ** 2 + self.y[n] ** 2)) < initialAngle:
  31. okToExitLoop = True
  32. if np.sign(self.y[n]) * np.arccos(self.x[n] / np.sqrt(self.x[n] ** 2 + self.y[n] ** 2)) > initialAngle and okToExitLoop:
  33. break
  34.  
  35. #if self.x[n]>self.x[0] and self.x[n-1]<self.x[0]:
  36. #break
  37.  
  38. def getPeriodAndAxis(self,dt):
  39. self.a = 0
  40. b = 0
  41. self.T = dt*len(self.x)
  42. for n in range(len(self.x)):
  43. b = np.sqrt(self.x[n]**2+self.y[n]**2)
  44. if b > self.a:
  45. self.a = b
  46. self.ratio = (self.T**2)/self.a**3
  47.  
  48. def PlotPlanet(x,y,vx,vy,dt):
  49. Planet1 = Planet(x,y,vx,vy)
  50. Planet1.doTimeStep(dt)
  51. plt.plot(Planet1.x,Planet1.y)
  52. Planet1.getPeriodAndAxis(dt)
  53. print('T^2/a^3 =')
  54. print(Planet1.ratio)
  55.  
  56. PlotPlanet(1,0,0,1,0.00001)
  57. PlotPlanet(1,0,1,1,0.00001)
  58. PlotPlanet(1,0,2,1,0.00001)
  59. PlotPlanet(1,0,3,1,0.00001)
  60. PlotPlanet(1,0,1,2,0.00001)
  61. PlotPlanet(1,0,1,3,0.00001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement