[关闭]
@ibilis 2016-11-27T06:45:12.000000Z 字数 2190 阅读 446

code

未分类


  1. from matplotlib.pyplot import *
  2. import numpy as np
  3. from visual import *
  4. class Planet:
  5. def __init__(self,_x=0,_vx=0,_y=0,_vy=0,_t=0,_dt=0.01,_beta=2):
  6. self.x=[]
  7. self.x.append(_x)
  8. self.t=[]
  9. self.t.append(_t)
  10. self.vx=[]
  11. self.vx.append(_vx)
  12. self.y=[]
  13. self.y.append(_y)
  14. self.vy=[]
  15. self.vy.append(_vy)
  16. self.dt=_dt
  17. self.beta=_beta
  18. #print self.theta[-1],self.t[-1],self.w[-1]
  19. return
  20. def calculate(self):
  21. while self.t[-1]<7:
  22. r=(self.x[-1]**2+self.y[-1]**2)**0.5
  23. self.vx.append(self.vx[-1]-4*np.pi**2*self.x[-1]/r**(self.beta+1)*self.dt)
  24. self.x.append(self.x[-1]+self.vx[-1]*self.dt)
  25. self.vy.append(self.vy[-1]-4*np.pi**2*self.y[-1]/r**(self.beta+1)*self.dt)
  26. self.y.append(self.y[-1]+self.vy[-1]*self.dt)
  27. self.t.append(self.t[-1]+self.dt)
  28. return
  29. def plot_2d(self):
  30. global x,y
  31. x=self.x
  32. y=self.y
  33. plot(self.x,self.y,label=r'$\beta=%.2f$'%self.beta)
  34. xlabel('x/AU')
  35. ylabel('y/AU')
  36. grid(True)
  37. axis('equal')
  38. scatter([0],[0],s=500,color='red')
  39. legend(loc='upper right',frameon=False)
  40. title("the orbit of the planet")
  41. show()
  42. return
  43. a=Planet(1.5,0.0,0.0,2*np.pi,_beta=2)
  44. a.calculate()
  45. a.plot_2d()
  46. '''scene.title='planet'
  47. scene.forward=vector(0,0.0,-0.6)
  48. b1=Planet(1.2,0.0,0.0,2*np.pi,_beta=2.0)
  49. b1.calculate()
  50. b1.plot_2d()
  51. c1=Planet(1.0,0.0,0.0,2*np.pi,_beta=2.0)
  52. c1.calculate()
  53. c1.plot_2d()
  54. d1=Planet(1.1,0.0,0.0,2*np.pi,_beta=2.0)
  55. d1.calculate()
  56. d1.plot_2d()
  57. e1=Planet(1.3,0.0,0.0,2*np.pi,_beta=2.0)
  58. e1.calculate()
  59. e1.plot_2d()
  60. #print b1.xi
  61. b=sphere(radius=0.1,color=color.green)
  62. b.trail=curve(color=b.color)
  63. c=sphere(radius=0.05,color=color.blue)
  64. c.trail=curve(color=c.color)
  65. d=sphere(radius=0.08,color=color.yellow)
  66. d.trail=curve(color=d.color)
  67. e=sphere(radius=0.2,color=color.cyan)
  68. e.trail=curve(color=e.color)
  69. a=sphere(pos=(0,0,0),radius=0.3,color=color.red)
  70. for i in range(len(b1.x)):
  71. rate(50)
  72. #a.pos=(0,0,0)
  73. b.pos=(b1.x[i],b1.y[i],0)
  74. b.trail.append(pos=b.pos)
  75. c.pos=(c1.x[i],c1.y[i],0)
  76. c.trail.append(pos=c.pos)
  77. d.pos=(d1.x[i],d1.y[i],0)
  78. d.trail.append(pos=d.pos)
  79. e.pos=(e1.x[i],e1.y[i],0)
  80. e.trail.append(pos=e.pos)
  81. subplot(221)
  82. a=Planet(1.1,0.0,0.0,2*np.pi,_beta=2)
  83. a.calculate()
  84. a.plot_2d()'''
  85. b=Planet(1.2,0.0,0.0,2*np.pi,_beta=2)
  86. b.calculate()
  87. b.plot_2d()
  88. c=Planet(1,0.0,0.0,2*np.pi,_beta=2)
  89. c.calculate()
  90. c.plot_2d()
  91. '''subplot(224)
  92. a=Planet(1.1,0.0,0.0,2*np.pi,_beta=2.01)
  93. a.calculate()
  94. a.plot_2d()'''
  95. show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注