[关闭]
@Xc-liu 2016-05-03T20:38:40.000000Z 字数 5325 阅读 859

homework_10程序

  • 程序一
  1. x_0=0
  2. y_0=0
  3. v_x_0=0.1
  4. v_y_0=0.1
  5. dt=0.01
  6. end_t=100
  7. x=[]
  8. y=[]
  9. v_x=[]
  10. v_y=[]
  11. t=[]
  12. x.append(x_0)
  13. y.append(y_0)
  14. v_x.append(v_x_0)
  15. v_y.append(v_y_0)
  16. t.append(0)
  17. for i in range(int(end_t/dt)):
  18. x.append(x[i]+v_x[i]*dt)
  19. y.append(y[i]+v_y[i]*dt)
  20. v_x.append(v_x[i])
  21. v_y.append(v_y[i])
  22. t.append(t[i]+dt)
  23. #print abs(x[i+1])+abs(y[i+1])
  24. if abs(x[i+1])+abs(y[i+1])>1:
  25. while abs(abs(x[i+1])+abs(y[i+1])-1)>0.00001:
  26. X=(x[i]+x[i+1])/2
  27. Y=(y[i]+y[i+1])/2
  28. a=(2**0.5/2.0)*abs(X)/X
  29. b=(2**0.5/2.0)*abs(Y)/Y
  30. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  31. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  32. #v_y[i+1]=-((2.0*a**3-2.0*a)/b)*v_x[i+1]-(2*a**2-1)*v_y[i+1]
  33. if abs(X)+abs(Y)>1:
  34. x[i+1]=X
  35. y[i+1]=Y
  36. continue
  37. else:
  38. x[i]=X
  39. y[i]=Y
  40. continue
  41. #if abs(abs(X)+abs(Y)-1)<0.1:
  42. # break
  43. x_1=[-1]
  44. y_1=[0]
  45. x_2=[-1]
  46. y_2=[0]
  47. dx=0.1
  48. for j in range(20):
  49. x_1.append(x_1[j]+dx)
  50. y_1.append(1-abs(x_1[j+1]))
  51. x_2.append(x_1[j]+dx)
  52. y_2.append(-(1-abs(x_1[j+1])))
  53. import matplotlib.pyplot as plt
  54. import numpy as np
  55. import math
  56. plt.plot(x,y,'--' ,label='aa')
  57. plt.plot(x_1,y_1,'--',label='bianary')
  58. plt.plot(x_2,y_2,'--',label='bianary')
  59. plt.title(u'',fontsize=14)
  60. plt.xlabel(u'x',fontsize=14)
  61. plt.ylabel(u'y',fontsize=14)
  62. plt.legend(fontsize=12,loc='best')
  63. plt.show()
  • 程序二
  1. import math
  2. x_0=0
  3. y_0=0
  4. v_x_0=1
  5. v_y_0=0.002
  6. dt=0.01
  7. end_t=7000
  8. x=[]
  9. y=[]
  10. v_x=[]
  11. v_y=[]
  12. t=[]
  13. x.append(x_0)
  14. y.append(y_0)
  15. v_x.append(v_x_0)
  16. v_y.append(v_y_0)
  17. t.append(0)
  18. alpha=0.001
  19. v_x_1=[]
  20. x_1_1=[]
  21. for i in range(int(end_t/dt)):
  22. x.append(x[i]+v_x[i]*dt)
  23. y.append(y[i]+v_y[i]*dt)
  24. v_x.append(v_x[i])
  25. v_y.append(v_y[i])
  26. t.append(t[i]+dt)
  27. if abs(x[i+1]-1)<0.0001:
  28. print x[i+1]
  29. a=-x[i+1]
  30. b=0
  31. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  32. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  33. else:
  34. if y[i+1]<0:
  35. if x[i+1]**2.0+(y[i+1]+alpha/2.0)**2.0>1:
  36. while abs(x[i+1]**2+(y[i+1]+alpha/2.0)**2-1)>0.0001:
  37. #print abs(x[i+1]**2+y[i+1]**2-1)
  38. X=(x[i]+x[i+1])/2
  39. Y=(y[i]+y[i+1])/2
  40. a=X/((X**2+(Y+alpha/2.0)**2)**0.5)
  41. b=(Y+alpha/2.0)/((X**2+(Y+alpha/2.0)**2)**0.5)
  42. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  43. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  44. #v_y[i+1]=-((2.0*a**3-2.0*a)/b)*v_x[i+1]-(2*a**2-1)*v_y[i+1]
  45. if X**2+(Y+alpha/2.0)**2>1:
  46. x[i+1]=X
  47. y[i+1]=Y
  48. continue
  49. else:
  50. x[i]=X
  51. y[i]=Y
  52. continue
  53. else:
  54. if x[i+1]**2.0+(y[i+1]-alpha/2.0)**2.0>1:
  55. while abs(x[i+1]**2+(y[i+1]-alpha/2.0)**2-1)>0.0001:
  56. #print abs(x[i+1]**2+y[i+1]**2-1)
  57. X=(x[i]+x[i+1])/2
  58. Y=(y[i]+y[i+1])/2
  59. a=X/((X**2+(Y-alpha/2.0)**2)**0.5)
  60. b=(Y-alpha/2.0)/((X**2+(Y-alpha/2.0)**2)**0.5)
  61. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  62. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  63. #v_y[i+1]=-((2.0*a**3-2.0*a)/b)*v_x[i+1]-(2*a**2-1)*v_y[i+1]
  64. if X**2+(Y-alpha/2.0)**2>1:
  65. x[i+1]=X
  66. y[i+1]=Y
  67. continue
  68. else:
  69. x[i]=X
  70. y[i]=Y
  71. continue
  72. if abs(y[i]-0)<0.0001:
  73. p=x[i]
  74. q=v_x[i]
  75. v_x_1.append(q)
  76. x_1_1.append(p)
  77. print v_x[30]
  78. x_1=[-1]
  79. y_1=[0]
  80. x_2=[-1]
  81. y_2=[0]
  82. dx=0.01
  83. for j in range(199):
  84. x_1.append(x_1[j]+dx)
  85. y_1.append(alpha/2.0+(1-x_1[j+1]**2)**0.5)
  86. x_2.append(x_2[j]+dx)
  87. y_2.append(-alpha/2.0-(1-x_2[j+1]**2)**0.5)
  88. import matplotlib.pyplot as plt
  89. import numpy as np
  90. import math
  91. fig= plt.figure(figsize=(7,6))
  92. #fig= plt.figure(figsize=(7,6))
  93. plt.scatter(x_1_1,v_x_1,marker='+',color='b',label='phase space plot')
  94. #plt.plot(x,y,'--' ,label='orbit')
  95. #plt.plot(x_1,y_1,'--',label='bianary')
  96. #plt.plot(x_2,y_2,'--',label='bianary')
  97. plt.title(u'$v_x/v_y=1/0.002,y_0=0,x_0=0,a=0.001,scan-point-y=0$',fontsize=16)
  98. plt.xlabel(u'x',fontsize=14)
  99. plt.ylabel(u'y',fontsize=14)
  100. plt.legend(fontsize=12,loc='best')
  101. plt.show(fig)
  • 程序三
  1. import math
  2. x_0=1.2
  3. y_0=0
  4. v_x_0=1
  5. v_y_0=2
  6. dt=0.01
  7. end_t=25
  8. x=[]
  9. y=[]
  10. v_x=[]
  11. v_y=[]
  12. t=[]
  13. x.append(x_0)
  14. y.append(y_0)
  15. v_x.append(v_x_0)
  16. v_y.append(v_y_0)
  17. t.append(0)
  18. #v_x_1=[]
  19. #x_1_1=[]
  20. q=1.6*10**-19
  21. m=1*10**-19
  22. b=0.9
  23. for i in range(int(end_t/dt)):
  24. x.append(x[i]+v_x[i]*dt)
  25. y.append(y[i]+v_y[i]*dt)
  26. #v_x.append(v_x[i])
  27. #v_y.append(v_y[i])
  28. v_x.append(v_x[i]+q*b*v_y[i]*dt/m)
  29. v_y.append(v_y[i]-q*b*v_x[i]*dt/m)
  30. t.append(t[i]+dt)
  31. #print x[i]**2+y[i]**2
  32. if x[i+1]**2+y[i+1]**2>4:
  33. print x[i+1]**2+y[i+1]**2
  34. while abs(x[i+1]**2+y[i+1]**2-4)>0.001:
  35. #print abs(x[i+1]**2+y[i+1]**2-4)
  36. X=(x[i]+x[i+1])/2
  37. Y=(y[i]+y[i+1])/2
  38. a=X/(X**2+Y**2)**0.5
  39. b=Y/(X**2+Y**2)**0.5
  40. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  41. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  42. #v_y[i+1]=-((2.0*a**3-2.0*a)/b)*v_x[i+1]-(2*a**2-1)*v_y[i+1]
  43. if X**2+Y**2>4:
  44. x[i+1]=X
  45. y[i+1]=Y
  46. continue
  47. else:
  48. x[i]=X
  49. y[i]=Y
  50. continue
  51. #if abs(abs(X)+abs(Y)-1)<0.1:
  52. # break
  53. if x[i+1]**2+y[i+1]**2<1:
  54. while abs(x[i+1]**2+y[i+1]**2-1)>0.001:
  55. X=(x[i]+x[i+1])/2
  56. Y=(y[i]+y[i+1])/2
  57. a=X/(X**2+Y**2)**0.5
  58. b=Y/(X**2+Y**2)**0.5
  59. v_x[i+1]=(1-2*a**2)*v_x[i]-2*a*b*v_y[i]
  60. v_y[i+1]=(1-2*b**2)*v_y[i]-2*a*b*v_x[i]
  61. #v_y[i+1]=-((2.0*a**3-2.0*a)/b)*v_x[i+1]-(2*a**2-1)*v_y[i+1]
  62. if X**2+Y**2<1:
  63. x[i+1]=X
  64. y[i+1]=Y
  65. continue
  66. else:
  67. x[i]=X
  68. y[i]=Y
  69. continue
  70. #if abs(y[i])<0.0001:
  71. # p=x[i]
  72. # q=v_x[i]
  73. # v_x_1.append(q)
  74. # x_1_1.append(p)
  75. x_1=[-1]
  76. y_1=[0]
  77. x_2=[-1]
  78. y_2=[0]
  79. x_3=[-2]
  80. y_3=[0]
  81. x_4=[-2]
  82. y_4=[0]
  83. dx=0.0001
  84. for j in range(20000):
  85. x_1.append(x_1[j]+dx)
  86. y_1.append((1-x_1[j+1]**2)**0.5)
  87. x_2.append(x_1[j]+dx)
  88. y_2.append(-(1-x_1[j+1]**2)**0.5)
  89. for k in range(40000):
  90. x_3.append(x_3[k]+dx)
  91. y_3.append((4-x_3[k+1]**2)**0.5)
  92. x_4.append(x_4[k]+dx)
  93. y_4.append(-(4-x_4[k+1]**2)**0.5)
  94. #x_4=x_4[1:39998]
  95. #y_4=y_4[1:39998]
  96. import matplotlib.pyplot as plt
  97. import numpy as np
  98. import math
  99. fig= plt.figure(figsize=(6,6))
  100. #plt.scatter(x_1_1,v_x_1,marker='+',color='b',label='phase space plot')
  101. plt.plot(x,y,'--',label='orbit')
  102. plt.plot(x_1,y_1,'--',label='binary')
  103. plt.plot(x_2,y_2,'--',label='binary')
  104. plt.plot(x_3,y_3,'--',label='binary')
  105. plt.plot(x_4,y_4,'--',label='binary')
  106. plt.title(u'$v_x/v_y=1/2,x_0=1.2,y_0=0,magnitude=0.9T$',fontsize=14)
  107. plt.xlabel(u'x',fontsize=14)
  108. plt.ylabel(u'y',fontsize=14)
  109. plt.legend(fontsize=12,loc='best')
  110. plt.show(fig)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注