[关闭]
@LiuYongJie 2016-11-20T13:02:39.000000Z 字数 8449 阅读 329

Ex_09:Chapter 3 problem3.31:Billiard ball on diverse table

Author:刘永杰

Student number:2014301020094

Abstract

The billiard system can also be a chaotic system. In this problem, I will try to solve the trajectories of different kinds of tables and show the phase diagram to see whether the system is chaotic or not.

background

In this problem, I will not consider the effect of friction, which means the billiard ball will move without friction on a perfect billiard table.
Except for the collisions with the walls, the motion of the billiard is quite simple. Between collisions the velocity is constant so we have:


where and change only through collisions with the walls. These equations can be solved using our Euler algorithm.
For a circle or an ellipse, the direction of the normal vector is , which means (x,y) for circle and () for ellipse.
It is then useful to calculate the components of parallel and perpendicular to the wall. These are just:

Hence,after reflection from the wall, the velocity becomes:

content

根据课本p82,可知球的运动方程为:



得到递推关系:


根据以上递推公式,再加上边界条件即可编写程序。

code

代码1:只有方形边界条件(-1≤x≤1, -1≤x≤1),内部没有圆形边界条件。

  1. import matplotlib.pylab as plt
  2. import time
  3. from math import *
  4. start_time = time.time()
  5. print 'Exercise 3.31 V1.0'
  6. print 'Designed by roach'
  7. dt=0.001
  8. def calculate():
  9. c_x.append(initial_x)
  10. c_y.append(initial_y)
  11. c_vx.append(initial_vx)
  12. c_vy.append(initial_vy)
  13. c_t.append(0)
  14. for i in range(10000):
  15. c_x.append(c_x[i]+c_vx[i]*dt)
  16. c_y.append(c_y[i]+c_vy[i]*dt)
  17. c_vx.append(c_vx[i])
  18. c_vy.append(c_vy[i])
  19. c_t.append(c_t[i]+dt)
  20. if c_x[-1]>1.0 or c_x[-1]<-1.0:
  21. c_vx[-1]=-c_vx[-1]
  22. if c_y[-1]>1.0 or c_y[-1]<-1.0:
  23. c_vy[-1]=-c_vy[-1]
  24. return 0
  25. c_x=[]
  26. c_y=[]
  27. c_vx=[]
  28. c_vy=[]
  29. c_t=[]
  30. initial_x=0.2
  31. initial_y=0.0
  32. initial_vx=1.0
  33. initial_vy=1.2
  34. calculate()
  35. print 'Total time',c_t[-1]
  36. print 'Time used',time.time() - start_time,'s'
  37. plt.figure(figsize=(20,20))
  38. plt.scatter(c_x,c_y,s=1)
  39. plt.title('Exercise 3.31')
  40. plt.xlabel("x")
  41. plt.ylabel("y")
  42. plt.xlim(-1,1)
  43. plt.ylim(-1,1)
  44. plt.legend()
  45. plt.show()

运行结果

代码2:在中心加入圆形边界(-1≤x≤1, -1≤x≤1,)

  1. import matplotlib.pylab as plt
  2. import time
  3. from math import *
  4. start_time = time.time()
  5. print 'Exercise 3.31 V1.0'
  6. print 'Designed by roach'
  7. dt=0.001
  8. def calculate():
  9. c_x.append(initial_x)
  10. c_y.append(initial_y)
  11. c_vx.append(initial_vx)
  12. c_vy.append(initial_vy)
  13. c_t.append(0)
  14. n=0
  15. for i in range(1000000):
  16. c_x.append(c_x[i]+c_vx[i]*dt)
  17. c_y.append(c_y[i]+c_vy[i]*dt)
  18. c_vx.append(c_vx[i])
  19. c_vy.append(c_vy[i])
  20. c_t.append(c_t[i]+dt)
  21. if c_x[-1]>1.0 or c_x[-1]<-1.0:
  22. c_vx[-1]=-c_vx[-1]
  23. if c_y[-1]>1.0 or c_y[-1]<-1.0:
  24. c_vy[-1]=-c_vy[-1]
  25. if c_x[-1]**2+c_y[-1]**2<0.5**2 and n>10:
  26. v_vertical=c_vx[-1]*c_x[-1]/0.5+c_vy[-1]*c_y[-1]/0.5
  27. v_verticalx=v_vertical*c_x[-1]/0.5
  28. v_verticaly=v_vertical*c_y[-1]/0.5
  29. v_parallelx=c_vx[-1]-v_verticalx
  30. v_parallely=c_vy[-1]-v_verticaly
  31. v_verticalx=-v_verticalx
  32. v_verticaly=-v_verticaly
  33. c_vx[-1]=v_verticalx+v_parallelx
  34. c_vy[-1]=v_verticaly+v_parallely
  35. n=0
  36. n=n+1
  37. return 0
  38. c_x=[]
  39. c_y=[]
  40. c_vx=[]
  41. c_vy=[]
  42. c_t=[]
  43. initial_x=0.6
  44. initial_y=0.0
  45. initial_vx=1.0
  46. initial_vy=1.2
  47. calculate()
  48. print 'Total time',c_t[-1]
  49. print 'Time used',time.time() - start_time,'s'
  50. plt.figure(figsize=(20,20))
  51. plt.scatter(c_x,c_y,s=1)
  52. plt.title('Exercise 3.31')
  53. plt.xlabel('x')
  54. plt.ylabel('y')
  55. plt.xlim(-1.2,1.2)
  56. plt.ylim(-1.2,1.2)
  57. plt.legend()
  58. plt.show()

运行结果

代码3:偏离中心的圆形边界条件如下:

ball condition(0.8,0.0) vx = 1.73205080757 vy = 1.0
circle condition (-0.1,0.3) r = 0.3

  1. import matplotlib.pylab as plt
  2. import time
  3. from math import *
  4. start_time = time.time()
  5. ##circle not in the middle
  6. print 'Exercise 3.31 V2.0'
  7. print 'Designed by roach'
  8. #calculate circle condition
  9. def calculate():
  10. c_x.append(initial_x)
  11. c_y.append(initial_y)
  12. c_vx.append(initial_vx)
  13. c_vy.append(initial_vy)
  14. c_t.append(0)
  15. n=0
  16. for i in range(100000):
  17. c_x.append(c_x[i]+c_vx[i]*dt)
  18. c_y.append(c_y[i]+c_vy[i]*dt)
  19. c_vx.append(c_vx[i])
  20. c_vy.append(c_vy[i])
  21. c_t.append((i+1)*dt)
  22. #range square
  23. if c_x[-1]>1.0 or c_x[-1]<-1.0:
  24. c_vx[-1]=-c_vx[-1]
  25. if c_y[-1]>1.0 or c_y[-1]<-1.0:
  26. c_vy[-1]=-c_vy[-1]
  27. #range circle
  28. if (c_x[-1]-circle_cx)**2+(c_y[-1]-circle_cy)**2<circle_r**2 and n>5:
  29. v_vertical=c_vx[-1]*(c_x[-1]-circle_cx)/circle_r+c_vy[-1]*(c_y[-1]-circle_cy)/circle_r
  30. v_verticalx=v_vertical*(c_x[-1]-circle_cx)/circle_r
  31. v_verticaly=v_vertical*(c_y[-1]-circle_cy)/circle_r
  32. v_parallelx=c_vx[-1]-v_verticalx
  33. v_parallely=c_vy[-1]-v_verticaly
  34. v_verticalx=-v_verticalx
  35. v_verticaly=-v_verticaly
  36. c_vx[-1]=v_verticalx+v_parallelx
  37. c_vy[-1]=v_verticaly+v_parallely
  38. n=0
  39. n=n+1
  40. return 0
  41. #calculate ellipse condition
  42. def calculate2():
  43. c_x.append(initial_x)
  44. c_y.append(initial_y)
  45. c_vx.append(initial_vx)
  46. c_vy.append(initial_vy)
  47. c_t.append(0)
  48. n0=0
  49. for i in range(100000):
  50. c_x.append(c_x[i]+c_vx[i]*dt)
  51. c_y.append(c_y[i]+c_vy[i]*dt)
  52. c_vx.append(c_vx[i])
  53. c_vy.append(c_vy[i])
  54. c_t.append((i+1)*dt)
  55. #range square
  56. if c_x[-1]>1.0 or c_x[-1]<-1.0:
  57. c_vx[-1]=-c_vx[-1]
  58. if c_y[-1]>1.0 or c_y[-1]<-1.0:
  59. c_vy[-1]=-c_vy[-1]
  60. #range ellipse
  61. if ((c_x[-1]-circle_cx)/circle_a)**2+((c_y[-1]-circle_cy)/circle_b)**2<1.0 and n0>5:
  62. theta1=acos((c_x[-1]-circle_cx)/circle_a)
  63. theta2=asin((c_y[-1]-circle_cy)/circle_b)
  64. kx=circle_a*sin(theta1)
  65. ky=circle_b*cos(theta2)
  66. nx=abs(ky/(kx**2+ky**2)**0.5)
  67. ny=abs(kx/(kx**2+ky**2)**0.5)
  68. if c_x[-1]-circle_cx<0 and c_y[-1]-circle_cy<0:
  69. nx=-nx
  70. ny=-ny
  71. elif c_x[-1]-circle_cx>0 and c_y[-1]-circle_cy<0:
  72. ny=-ny
  73. elif c_x[-1]-circle_cx<0 and c_y[-1]-circle_cy>0:
  74. nx=-nx
  75. # plt.plot([c_x[-1]-nx*0.1,c_x[-1]+nx*0.1],[c_y[-1]-ny*0.1,c_y[-1]+ny*0.1])
  76. v_vertical=c_vx[-1]*nx+c_vy[-1]*ny
  77. v_verticalx=v_vertical*nx
  78. v_verticaly=v_vertical*ny
  79. v_parallelx=c_vx[-1]-v_verticalx
  80. v_parallely=c_vy[-1]-v_verticaly
  81. v_verticalx=-v_verticalx
  82. v_verticaly=-v_verticaly
  83. c_vx[-1]=v_verticalx+v_parallelx
  84. c_vy[-1]=v_verticaly+v_parallely
  85. n0=0
  86. # plt.plot([c_x[-1]-nx*0.1,c_x[-1]+nx*0.1],[c_y[-1]-ny*0.1,c_y[-1]+ny*0.1])
  87. n0=n0+1
  88. return 0
  89. c_x=[]
  90. c_y=[]
  91. c_vx=[]
  92. c_vy=[]
  93. c_t=[]
  94. #Initial conditions
  95. dt=0.001
  96. initial_x=0.8
  97. initial_y=0.0
  98. initial_vx=1.0*cos(45*pi/180)
  99. initial_vy=1.0*sin(45*pi/180)
  100. circle_cx=-0.1
  101. circle_cy=0.3
  102. circle_r=0.0 #No use for circle r
  103. #ellipse condition
  104. circle_a=0.5
  105. circle_b=0.3
  106. print 'ball condition x =',initial_x,' y =',initial_y
  107. print ' vx =',initial_vx,' vy =',initial_vy
  108. #if it is a circle
  109. if circle_a == circle_b:
  110. circle_r = circle_a
  111. print 'circle condition x =',circle_cx,' y =',circle_cy
  112. print ' r =',circle_r
  113. calculate()
  114. plt.figure(figsize=(20,20))
  115. plt.scatter(initial_x,initial_y,color='blue',label='initial position')
  116. plt.scatter(c_x,c_y,s=1,label='trajectory',color ='red')
  117. plt.title('Exercise 3.31 circle x='+str(circle_cx)+' y='+str(circle_cy)+' r='+str(circle_r))
  118. plt.xlabel('x(length unit)')
  119. plt.ylabel('y(length unit)')
  120. plt.xlim(-1.2,1.2)
  121. plt.ylim(-1.2,1.2)
  122. #draw a square
  123. plt.plot([1,-1,-1,1,1],[1,1,-1,-1,1],color='black',label='range')
  124. #draw a circle
  125. circle_x=[]
  126. circle_y=[]
  127. for i in range(510):
  128. circle_x.append(circle_r*cos(i*pi/250.0)+circle_cx)
  129. circle_y.append(circle_r*sin(i*pi/250.0)+circle_cy)
  130. plt.plot(circle_x,circle_y,color='black',label=None)
  131. plt.legend()
  132. #total time used
  133. print 'Total time',c_t[-1],'time unit'
  134. print 'Time used',time.time() - start_time,'s'
  135. plt.show()
  136. #if it is a ellipse
  137. else:
  138. #swap a and b
  139. if circle_a<circle_b:
  140. conditionb = circle_a
  141. conditiona = circle_b
  142. conditione = (1-(conditionb/conditiona)**2)**0.5
  143. print 'ellipse condition x =',circle_cx,' y =',circle_cy
  144. print ' a =',conditiona,' b =',conditionb
  145. print ' e =',conditione
  146. print ' a parallel y axis'
  147. else:
  148. conditiona = circle_a
  149. conditionb = circle_b
  150. conditione = (1-(conditionb/conditiona)**2)**0.5
  151. print 'ellipse condition x =',circle_cx,' y =',circle_cy
  152. print ' a =',circle_a,' b =',circle_b
  153. print ' e =',(conditiona**2-conditionb**2)**0.5/conditiona
  154. print ' a parallel x axis'
  155. calculate2()
  156. plt.figure(figsize=(20,20))
  157. plt.scatter(initial_x,initial_y,color='blue',label='initial position')
  158. plt.scatter(c_x,c_y,s=1,label='trajectory',color ='red')
  159. plt.title('Exercise 3.31 ellipse x='+str(circle_cx)+' y='+str(circle_cy)+' x_='+str(circle_a)+' y_='+str(circle_b)+' e='+str(conditione))
  160. plt.xlabel('x(length unit)')
  161. plt.ylabel('y(length unit)')
  162. plt.xlim(-1.2,1.2)
  163. plt.ylim(-1.2,1.2)
  164. #draw a square
  165. plt.plot([1,-1,-1,1,1],[1,1,-1,-1,1],color='black',label='range')
  166. #draw a circle
  167. ellipse_x=[]
  168. ellipse_y=[]
  169. for i in range(510):
  170. ellipse_x.append(circle_a*cos(i*pi/250.0)+circle_cx)
  171. ellipse_y.append(circle_b*sin(i*pi/250.0)+circle_cy)
  172. plt.plot(ellipse_x,ellipse_y,color='black',label=None)
  173. plt.legend()
  174. #total time used
  175. print 'Total time',c_t[-1],'time unit'
  176. print 'Time used',time.time() - start_time,'s'
  177. plt.show()

运行结果

Conclusion

Ths chaotic system will present when the boundary is composed by some shapes (different or same). If the boundary is a simple regular shape, the chaotic situation cannot be observed.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注