[关闭]
@Xc-liu 2016-05-09T23:08:21.000000Z 字数 6653 阅读 767

homework_11 计算程序

计算程序1

  1. import math
  2. G=4*math.pi**2
  3. m1=1
  4. m2=100
  5. dt=0.001
  6. end_t=20
  7. t=[0]
  8. x_1=[10]
  9. v_x_1=[0]
  10. y_1=[0]
  11. v_y_1=[(4.0*math.pi**2*10/(10.1**2))**0.5]
  12. r_1=[]
  13. theta_1=[]
  14. x_2=[-0.1]
  15. v_x_2=[0]
  16. y_2=[0]
  17. v_y_2=[-(0.1*4*math.pi**2/(10.1**2))**0.5]
  18. r_2=[]
  19. theta_2=[]
  20. r=[]
  21. n_i=[]
  22. n_j=[]
  23. for i in range(int(end_t/dt)):
  24. r_1.append((x_1[i]**2+y_1[i]**2)**0.5)
  25. if y_1[i]<0:
  26. theta_1.append(2*math.pi-math.acos(x_1[i]/(x_1[i]**2+y_1[i]**2)**0.5))
  27. else:
  28. theta_1.append(math.acos(x_1[i]/(x_1[i]**2+y_1[i]**2)**0.5))
  29. r_2.append((x_2[i]**2+y_2[i]**2)**0.5)
  30. if y_2[i]<0:
  31. theta_2.append(2*math.pi-math.acos(x_2[i]/(x_2[i]**2+y_2[i]**2)**0.5))
  32. else:
  33. theta_2.append(math.acos(x_2[i]/(x_2[i]**2+y_2[i]**2)**0.5))
  34. #print theta_1[i]
  35. #print theta_2[i]
  36. r.append((r_1[i]**2+r_2[i]**2-2*math.cos(theta_1[i]-theta_2[i])*r_1[i]*r_2[i])**0.5)
  37. #print r[-1]
  38. n_i.append((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))/((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  39. n_j.append((r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))/((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  40. print n_j[i]
  41. v_x_1.append(v_x_1[i]+(G*m2/r[i]**2)*n_i[i]*dt)
  42. x_1.append(x_1[i]+v_x_1[i+1]*dt)
  43. v_y_1.append(v_y_1[i]+(G*m2/r[i]**2)*n_j[i]*dt)
  44. y_1.append(y_1[i]+v_y_1[i+1]*dt)
  45. v_x_2.append(v_x_2[i]-(G*m1/r[i]**2)*n_i[i]*dt)
  46. x_2.append(x_2[i]+v_x_2[i+1]*dt)
  47. v_y_2.append(v_y_2[i]-(G*m1/r[i]**2)*n_j[i]*dt)
  48. y_2.append(y_2[i]+v_y_2[i+1]*dt)
  49. t.append(t[i]+dt)
  50. import matplotlib.pyplot as plt
  51. import numpy as np
  52. plt.plot(x_1,y_1,'--' ,label='star_1,$r(2,0),v(0,4/3 \pi-0.1,m_1=1$')
  53. plt.plot(x_2,y_2,'--',label='star_2,$r(-1,0),v(0,-2/3 \pi+0.1,m_2=2$')
  54. plt.title(u'the orbit of star',fontsize=14)
  55. plt.xlabel(u'x/AU',fontsize=14)
  56. plt.ylabel(u'y/AU',fontsize=14)
  57. plt.legend(fontsize=12,loc='best')
  58. plt.show()

程序2

  1. import math
  2. G=4*math.pi**2
  3. m1=1
  4. m2=1
  5. m3=1
  6. dt=0.001
  7. end_t=12
  8. t=[0]
  9. x_1=[0]
  10. v_x_1=[-((4*3**0.5*math.pi**2)/3.0)**0.5]
  11. y_1=[1]
  12. v_y_1=[0]
  13. r_1=[]
  14. theta_1=[]
  15. x_2=[-3**0.5/2.0]
  16. v_x_2=[0.5*((4*3**0.5*math.pi**2)/3.0)**0.5]
  17. y_2=[-1/2.0]
  18. v_y_2=[-(3**0.5/2.0)*((4*3**0.5*math.pi**2)/3.0)**0.5]
  19. r_2=[]
  20. theta_2=[]
  21. x_3=[3.0**0.5/2]
  22. v_x_3=[0.5*((4*3**0.5*math.pi**2)/3.0)**0.5]
  23. y_3=[-1.0/2]
  24. v_y_3=[3**0.5/2*((4*3**0.5*math.pi**2)/3.0)**0.5]
  25. r_3=[]
  26. theta_3=[]
  27. r12=[]
  28. r13=[]
  29. r23=[]
  30. n12_i=[]
  31. n13_i=[]
  32. n23_i=[]
  33. n12_j=[]
  34. n13_j=[]
  35. n23_j=[]
  36. for i in range(int(end_t/dt)):
  37. '''
  38. r_1.append((x_1[i]**2+y_1[i]**2)**0.5)
  39. if y_1[i]<0:
  40. theta_1.append(2*math.pi-math.acos(x_1[i]/(x_1[i]**2+y_1[i]**2)**0.5))
  41. else:
  42. theta_1.append(math.acos(x_1[i]/(x_1[i]**2+y_1[i]**2)**0.5))
  43. r_2.append((x_2[i]**2+y_2[i]**2)**0.5)
  44. if y_2[i]<0:
  45. theta_2.append(2*math.pi-math.acos(x_2[i]/(x_2[i]**2+y_2[i]**2)**0.5))
  46. else:
  47. theta_2.append(math.acos(x_2[i]/(x_2[i]**2+y_2[i]**2)**0.5))
  48. r_3.append((x_3[i]**2+y_3[i]**2)**0.5)
  49. if y_1[i]<0:
  50. theta_3.append(2*math.pi-math.acos(x_3[i]/(x_3[i]**2+y_3[i]**2)**0.5))
  51. else:
  52. theta_3.append(math.acos(x_3[i]/(x_3[i]**2+y_3[i]**2)**0.5))
  53. #print theta_3[i]
  54. #print theta_2[i]
  55. r12.append((r_1[i]**2+r_2[i]**2-2*math.cos(theta_1[i]-theta_2[i])*r_1[i]*r_2[i])**0.5)
  56. r13.append((r_1[i]**2+r_3[i]**2-2*math.cos(theta_1[i]-theta_3[i])*r_1[i]*r_3[i])**0.5)
  57. r23.append((r_3[i]**2+r_2[i]**2-2*math.cos(theta_3[i]-theta_2[i])*r_3[i]*r_2[i])**0.5)
  58. print r_1[-1]
  59. n12_i.append((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))/((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  60. n13_i.append((r_3[i]*math.cos(theta_3[i])-r_1[i]*math.cos(theta_1[i]))/((r_3[i]*math.cos(theta_3[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_3[i]*math.sin(theta_3[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  61. n23_i.append((r_2[i]*math.cos(theta_2[i])-r_3[i]*math.cos(theta_3[i]))/((r_2[i]*math.cos(theta_2[i])-r_3[i]*math.cos(theta_3[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_3[i]*math.sin(theta_3[i]))**2)**0.5)
  62. n12_j.append((r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))/((r_2[i]*math.cos(theta_2[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  63. n13_j.append((r_3[i]*math.sin(theta_3[i])-r_1[i]*math.sin(theta_1[i]))/((r_3[i]*math.cos(theta_3[i])-r_1[i]*math.cos(theta_1[i]))**2+(r_3[i]*math.sin(theta_3[i])-r_1[i]*math.sin(theta_1[i]))**2)**0.5)
  64. n23_j.append((r_2[i]*math.sin(theta_2[i])-r_3[i]*math.sin(theta_3[i]))/((r_2[i]*math.cos(theta_2[i])-r_3[i]*math.cos(theta_3[i]))**2+(r_2[i]*math.sin(theta_2[i])-r_3[i]*math.sin(theta_3[i]))**2)**0.5)
  65. #print n_j[i]
  66. v_x_1.append(v_x_1[i]+(G*m2/r12[i]**2)*n12_i[i]*dt+(G*m3/r13[i]**2)*n13_i[i]*dt)
  67. x_1.append(x_1[i]+v_x_1[i+1]*dt)
  68. v_y_1.append(v_y_1[i]+(G*m2/r12[i]**2)*n12_j[i]*dt+(G*m3/r13[i]**2)*n13_j[i]*dt)
  69. y_1.append(y_1[i]+v_y_1[i+1]*dt)
  70. v_x_2.append(v_x_2[i]-(G*m1/r12[i]**2)*n12_i[i]*dt-(G*m3/r23[i]**2)*n23_i[i]*dt)
  71. x_2.append(x_2[i]+v_x_2[i+1]*dt)
  72. v_y_2.append(v_y_2[i]-(G*m1/r12[i]**2)*n12_j[i]*dt-(G*m3/r23[i]**2)*n23_j[i]*dt)
  73. y_2.append(y_2[i]+v_y_2[i+1]*dt)
  74. v_x_3.append(v_x_3[i]-(G*m1/r13[i]**2)*n13_i[i]*dt+(G*m2/r23[i]**2)*n23_i[i]*dt)
  75. x_3.append(x_3[i]+v_x_3[i+1]*dt)
  76. v_y_3.append(v_y_3[i]-(G*m1/r13[i]**2)*n13_j[i]*dt+(G*m2/r23[i]**2)*n23_j[i]*dt)
  77. y_3.append(y_3[i]+v_y_3[i+1]*dt)
  78. t.append(t[i]+dt)
  79. '''
  80. v_x_1.append(v_x_1[i]+(G*m2*(x_2[i]-x_1[i])/((x_1[i]-x_2[i])**2+(y_1[i]-y_2[i])**2)**(1.5)+G*m3*(x_3[i]-x_1[i])/((x_1[i]-x_3[i])**2+(y_1[i]-y_3[i])**2)**(1.5))*dt)
  81. v_x_2.append(v_x_2[i]+(G*m1*(x_1[i]-x_2[i])/((x_1[i]-x_2[i])**2+(y_1[i]-y_2[i])**2)**(1.5)+G*m3*(x_3[i]-x_2[i])/((x_2[i]-x_3[i])**2+(y_2[i]-y_3[i])**2)**(1.5))*dt)
  82. v_x_3.append(v_x_3[i]+(G*m1*(x_1[i]-x_3[i])/((x_1[i]-x_3[i])**2+(y_1[i]-y_3[i])**2)**(1.5)+G*m2*(x_2[i]-x_3[i])/((x_2[i]-x_3[i])**2+(y_2[i]-y_3[i])**2)**(1.5))*dt)
  83. v_y_1.append(v_y_1[i]+(G*m2*(y_2[i]-y_1[i])/((x_1[i]-x_2[i])**2+(y_1[i]-y_2[i])**2)**(1.5)+G*m3*(y_3[i]-y_1[i])/((x_1[i]-x_3[i])**2+(y_1[i]-y_3[i])**2)**(1.5))*dt)
  84. v_y_2.append(v_y_2[i]+(G*m1*(y_1[i]-y_2[i])/((x_1[i]-x_2[i])**2+(y_1[i]-y_2[i])**2)**(1.5)+G*m3*(y_3[i]-y_2[i])/((x_2[i]-x_3[i])**2+(y_2[i]-y_3[i])**2)**(1.5))*dt)
  85. v_y_3.append(v_y_3[i]+(G*m1*(y_1[i]-y_3[i])/((x_1[i]-x_3[i])**2+(y_1[i]-y_3[i])**2)**(1.5)+G*m2*(y_2[i]-y_3[i])/((x_2[i]-x_3[i])**2+(y_2[i]-y_3[i])**2)**(1.5))*dt)
  86. x_1.append(x_1[i]+v_x_1[i+1]*dt)
  87. x_2.append(x_2[i]+v_x_2[i+1]*dt)
  88. x_3.append(x_3[i]+v_x_3[i+1]*dt)
  89. y_1.append(y_1[i]+v_y_1[i+1]*dt)
  90. y_2.append(y_2[i]+v_y_2[i+1]*dt)
  91. y_3.append(y_3[i]+v_y_3[i+1]*dt)
  92. import matplotlib.pyplot as plt
  93. import numpy as np
  94. plt.plot(x_1,y_1,'--' ,label='star_1,$r_1=1,m_1=1$')
  95. plt.plot(x_2,y_2,'--',label='star_2,$r_2=1,m_2=1$')
  96. plt.plot(x_3,y_3,'--',label='star_2,$r_3=1,m_3=1$')
  97. plt.title(u'the orbit of star',fontsize=14)
  98. plt.xlabel(u'x/AU',fontsize=14)
  99. plt.ylabel(u'y/AU',fontsize=14)
  100. plt.legend(fontsize=12,loc='best')
  101. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注