Processing math: 100%

Simulation of Bouncing Balls | #3: Bouncing on Explicit Functions (1) [gnuplot]

Saturday, March 23, 2019

gnuplot Mechanics Simulation of Bouncing Balls YouTube

t f B! P L

YouTube

Simulation [gnuplot]

Model of a Bouncing Ball


Impulse-momentum theorem \begin{eqnarray} \label{eq:im3} m\boldsymbol{v}'=m\boldsymbol{v}+\boldsymbol{I}=m\boldsymbol{v}+I\boldsymbol{n} \end{eqnarray} Collision \begin{eqnarray} \left|\boldsymbol{v}'\cdot\boldsymbol{n}\right|=e\left|\boldsymbol{v}\cdot\boldsymbol{n}\right| \end{eqnarray} Calculating I \begin{eqnarray} I &=& \left| \left(m\boldsymbol{v}'\right) \cdot \boldsymbol{n} \right| + \left| \left(m\boldsymbol{v}\right) \cdot \boldsymbol{n} \right| \nonumber\\ &=& m\left(\left|\boldsymbol{v}' \cdot \boldsymbol{n} \right| + \left| \boldsymbol{v} \cdot \boldsymbol{n} \right|\right) \nonumber\\ &=& m\left(e\left|\boldsymbol{v}\cdot\boldsymbol{n}\right|+\left|\boldsymbol{v}\cdot\boldsymbol{n}\right|\right) \nonumber\\ &=& -m\left(e+1\right) \left( \boldsymbol{v} \cdot \boldsymbol{n} \right) \label{eq:I} \end{eqnarray} Velocity after the collision (inserting (\ref{eq:I}) into (\ref{eq:im})) \begin{eqnarray} \boldsymbol{v}'=\boldsymbol{v}-(e+1)\left(\boldsymbol{v}\cdot\boldsymbol{n}\right)\boldsymbol{n} \end{eqnarray}

Wall function \begin{equation} W(x, y)=y-f(x) \end{equation} Inequality constraint \begin{equation} W(x, y)\geq 0 \end{equation} Unit normal vector \begin{equation} \boldsymbol{n}=\frac{\nabla W}{\left|\nabla W\right|} \end{equation} Collision detection \begin{equation} \boldsymbol{v}'=\boldsymbol{v}-\left(e+1\right)\left(\boldsymbol{v}\cdot \frac{\nabla W}{\left|\nabla W\right|}\right)\frac{\nabla W}{\left|\nabla W\right|} \end{equation}

Case 1: f(x)=\left(\frac{x}{7}\right)^2-13

PLT file ①

  1. # Setting --------------------
  2. reset
  3. set term gif animate delay 5 size 1280, 720
  4. set output"ball_fx.gif"
  5.  
  6. set nokey
  7. set sample 10000
  8. set xr[-35:35]
  9. set yr[-15:15]
  10. set xl "{/TimesNewRoman:Italic=24 x}"
  11. set yl "{/TimesNewRoman:Italic=24 y}"
  12. set tics font 'Times New Roman,20'
  13. set xtics 5
  14. set ytics 5
  15.  
  16. set size ratio -1 # ratio = yrange / xrange
  17. set grid
  18.  
  19. N = 5
  20. array x[N]
  21. array y[N]
  22. array vx[N]
  23. array vy[N]
  24. array c[N] = ["royalblue", "red", "orange", "green", "black"]
  25.  
  26. # Parameter --------------------
  27. r = 0.3 # Radius of the ball
  28. g = 9.8 # Gravitational acceleration
  29. dis = 500 # Start to disappear
  30. e = 0.95 # Elasticity coefficient
  31. dt = 0.001 # Time step
  32. dh = dt/6
  33. cut = 80 # Decimation
  34. ep = 1e-4 # for collision detection
  35. time1 = 2 # Stop time4
  36. lim1 = time1/dt/cut
  37.  
  38. time2 = 40 # Time limit40
  39. lim2 = time2/dt
  40.  
  41. # Functions --------------------
  42. # Wall
  43. g(x) = x**2
  44. f(x) = g(x/7.)-13.
  45. W(x, y) = y-f(x)
  46. # Partial derivative of f(x)
  47. Wx(x) = -(f(x-2*dt)-8*f(x-dt)+8*f(x+dt)-f(x+2*dt)) / (12*dt) # 4th order central difference formula
  48. Wy(y) = 1
  49. # n = (fx, fy) / sqrt(fx^2+fy^2)
  50. d(x, y) = sqrt(x**2+y**2)
  51. nx(x, y) = Wx(x)/d(Wx(x), Wy(y))
  52. ny(x, y) = Wy(y)/d(Wx(x), Wy(y))
  53.  
  54. # Inner product of v and n
  55. vn(x,y,vx,vy) = vx*nx(x,y)+vy*ny(x,y)
  56. I(x,y,vx,vy) = -(1+e)*vn(x,y,vx,vy)
  57.  
  58. # Equations of Motion
  59. rk1(x,y,vx,vy) = vx # dx/dt
  60. rk2(x,y,vx,vy) = vy # dy/dt
  61. rk3(x,y,vx,vy) = 0 # dvx/dt
  62. rk4(x,y,vx,vy) = -g # dvy/dt
  63.  
  64. # 4th order Runge-Kutta (Define RK_i(x, y, vx, vy))
  65. do for[i=1:4]{
  66. RKi = "RK"
  67. rki = "rk".sprintf("%d", i)
  68. RKi = RKi.sprintf("%d(x, y, vx, vy) = (\
  69. k1 = %s(x, y, vx, vy),\
  70. k2 = %s(x + dt*k1/2., y + dt*k1/2., vx + dt*k1/2., vy + dt*k1/2.),\
  71. k3 = %s(x + dt*k2/2., y + dt*k2/2., vx + dt*k2/2., vy + dt*k2/2.),\
  72. k4 = %s(x + dt*k3, y + dt*k3, vx + dt*k3, vy + dt*k3),\
  73. dh * (k1 + 2*k2 + 2*k3 + k4))", i, rki, rki, rki, rki)
  74. eval RKi
  75. }
  76.  
  77. # Label of displaying parameters
  78. # Elastic
  79. e(e) = sprintf("{/TimesNewRoman:Italic e} = %.2f", e)
  80. # Time
  81. Time(t) = sprintf("{/TimesNewRoman:Italic t} = %.1f s", t)
  82.  
  83.  
  84. # Plot --------------------
  85. # Initiate value
  86. t = 0.0 # Time
  87.  
  88. do for[j=1:N]{
  89. x[j] = 13.*(j-3)-2 # Position
  90. y[j] = 10.
  91. vx[j] = 0. # Velocity
  92. vy[j] = 0.
  93. }
  94. # Draw initial state for lim1 steps
  95. do for[i=1:lim1]{
  96. # Time
  97. set label 1 Time(t) font 'Times New Roman, 24' at graph 0.03, 0.03
  98. set label 2 e(e) font 'Times New Roman, 24' at graph 0.023, 0.08
  99. # Ball
  100. do for[j=1:N]{
  101. set object j+1 circle at x[j], y[j] size r fc rgb c[j] fs solid front
  102. }
  103. plot f(x) lc rgb 'black'
  104. }
  105.  
  106.  
  107.  
  108. # Update
  109. do for[i=1:lim2]{
  110. # Time
  111. t = t + dt
  112. set label 1 Time(t)
  113. # Balls
  114. do for[j=1:N]{
  115. # 4th order Runge-Kutta
  116. tmp_x = x[j] + RK1(x[j], y[j], vx[j], vy[j])
  117. tmp_y = y[j] + RK2(x[j], y[j], vx[j], vy[j])
  118. tmp_vx = vx[j] + RK3(x[j], y[j], vx[j], vy[j])
  119. tmp_vy = vy[j] + RK4(x[j], y[j], vx[j], vy[j])
  120. # Collision detection
  121. if(W(x[j], y[j]) >= 0 && W(tmp_x, tmp_y) < 0){
  122. # vec = x_[i+1] - x_[i]
  123. vec_x = tmp_x - x[j]
  124. vec_y = tmp_y - y[j]
  125. nor_v = d(vec_x, vec_y)
  126. # Find collision point
  127. while(W(x[j], y[j]) >= 0){
  128. x[j] = x[j] + vec_x / nor_v * ep
  129. y[j] = y[j] + vec_y / nor_v * ep
  130. }
  131. x[j] = x[j] - vec_x / nor_v * ep
  132. y[j] = y[j] - vec_y / nor_v * ep
  133. # Calculate bounce direction
  134. size = d(tmp_x-x[j], tmp_y-y[j])
  135. tmp_x = x[j] - (1+e) * size * vn(x[j],y[j],vec_x,vec_y) / nor_v * nx(x[j], y[j])
  136. tmp_y = y[j] - (1+e) * size * vn(x[j],y[j],vec_x,vec_y) / nor_v * ny(x[j], y[j])
  137. # Collision
  138. tmp_vx = vx[j] + I(x[j], y[j], vx[j], vy[j]) * nx(x[j], y[j])
  139. tmp_vy = vy[j] + I(x[j], y[j], vx[j], vy[j]) * ny(x[j], y[j])
  140. }
  141. # Update position and velocity of a ball
  142. x[j] = tmp_x
  143. y[j] = tmp_y
  144. vx[j] = tmp_vx
  145. vy[j] = tmp_vy
  146. # Draw a ball
  147. set object N*i+j circ at x[j], y[j] size r fc rgb c[j] fs solid front
  148. set object N*(i-1)+j circ size r*0.01 # Old ball turns smaller
  149.  
  150. # Start to disappear
  151. if(i>=dis){
  152. unset object N*(i-dis)+j
  153. }
  154. }
  155. # Decimate and draw
  156. if(i%cut==0){
  157. replot
  158. }
  159. }
  160.  
  161. set out

GIF file ①

e=0.95

Case 2: f(x)=\left|x \right|-5

GIF file ②

e=1.00

Case 3: f(x) = \frac{x^2}{40}+\cos x-10

GIF file ③

e=0.95

Case 4: f(x)= \frac{3}{4}x\sin\left(\frac{x^2}{10}+\frac{2}{5}\pi\right)

GIF file ④

e=0.95

Extra

Fall from a non-differentiable point (f(x)=\left|x \right|-5)

e=0.50


e=0.80

Search This Blog

Translate

QooQ