Home Subscribe

4. Simulating a Bouncing Ball

bouncing ball

To model a bouncing tennis ball, let’s ignore air friction and assume a partially inelastic collision of the ball with the ground.

Assume the ball is at height, \(h\), and time, \(t\), after the last bounce at time, \(t_0\). If the velocity after this bounce was \(v_0\), the changes in height and velocity will be governed by Equation 1.

\[\begin{align}\tag{1} h(t+t_0) = v_0t - \frac{1}{2}gt^2 \end{align}\]

When the ball hits the ground, it deforms depending on its material and that of the ground. Let us ignore these details and assume a damped simple harmonic motion during the impact to the ground, and that the impact time is independent of the impact velocity.

Let us assume the coefficient of restitution of a tennis ball, \(\rho = 0.70\). The time the ball spends in the air before hitting the ground for the next bounce is given by Equation 2.

\[\begin{align*} \rho = \frac{v \text{ (after the bounce)}}{v \text{ (before the bounce)}} \end{align*}\]
\[\begin{align}\tag{2} t_1 &= 2\frac{v_1}{g} &= 2\rho \frac{v_0}{g} \end{align}\]

Import the required libraries.

1import numpy as np
2import matplotlib.pyplot as plt
3from matplotlib.animation import FuncAnimation

Part 1: Solve the equations involved in your idea.

 4h0 = 5         # m/s
 5v = 0          # m/s, current velocity
 6g = 9.81       # m/s/s
 7t = 0          # starting time
 8dt = 0.03      # time step
 9rho = 0.70     # coefficient of restitution
10tau = 0        # contact time for bounce
11hmax = h0      # keep track of the maximum height
12h = h0
13hstop = 0.01   # stop when bounce is less than 1 cm
14freefall = True# state: freefall or in contact
15t_last = -np.sqrt(2*h0/g) # time we would have launched to get to h0 at t=0
16vmax = np.sqrt(2 * hmax * g)
17H = []         # Prepare array for height data
18T = []         # Prepare array for time data
19
20while(hmax > hstop):
21  if(freefall):
22    hnew = h + v*dt - 0.5*g*dt*dt
23    if(hnew<0):
24      t = t_last + 2*np.sqrt(2*hmax/g)
25      freefall = False
26      t_last = t + tau
27      h = 0
28    else:
29      t = t + dt
30      v = v - g*dt
31      h = hnew
32  else:
33    t = t + tau
34    vmax = vmax * rho
35    v = vmax
36    freefall = True
37    h = 0
38  hmax = 0.5*vmax*vmax/g
39  H.append(h)   # Add data to H array
40  T.append(t)

Part 2: Use the output data to visualize the idea.

Prepare the space for plotting.

41fig, ax = plt.subplots()
42ax.set_xlim(0, np.max(T))
43ax.set_ylim(0, np.max(H))
44ax.set_xlabel('Time')
45ax.set_ylabel('Height')
46ax.set_aspect('equal')
47graph = plt.plot([], [], ls='--', color='grey')[0]
48
49ball = plt.Circle((0, np.max(H)), 0.08, color='red')
50ax.add_patch(ball)
51
52height_text = ax.text(np.max(T)*0.7, np.max(H)*0.9, \
53              f'Height: {np.max(H):.1f} m', color='blue', \
54              bbox=dict(facecolor='none', edgecolor='blue', pad=10.0))

Create the animation function.

56def animate(i):
57    graph.set_data(T[:i],H[:i])
58    ball.set_center((T[i], H[i]))
59    height_text.set_text(f'Height: {H[i]:.1f} m')
60    return graph, ball, height_text, 

Animate the data.

61anim = FuncAnimation(fig, 
62                    animate, 
63                    frames=len(T), 
64                    interval=20, 
65                    blit=True,
66                    )
67#anim.save(r'bouncing_ball.mp4')
bouncing ball


Add Comment

* Required information
1000
Drag & drop images (max 3)
What is the opposite word of small?

Comments

No comments yet. Be the first!