Home Subscribe

6. Simulating a Projectile Motion

projectile

Assume a ball shot from a point (0,0) in space on earth. simulate the path it is likely to follow on its way to the ground.

We have to come up with equations describing the instantaneous position, velocity, and acceleration of the ball.

projectile

Let acceleration due to gravity \(g = 9.81 m/s^2\)
Gravitational acceleration \(a_y = -g\)
Velocity \(= v\), time \(=t\), and angle \(\theta\)
At the final time, \(t_{final}\), the vertical position, \(y_{final} = 0\)

\[\begin{align*} v_x &= v_0 \times cos(\theta)\\ x &= v_0 \times cos(\theta) \times t\\ v_y &= v_0 \times sin(\theta)- gt\\ y &= v_0 \times sin(\theta) \times t - \frac{1}{2}gt^2\\ y_{final} &= v_0 \times sin(\theta) \times t_{final} - \frac{1}{2}gt_{final}^2 \\ t_{final} &= 2v_0 \times \frac{sin(\theta)}{g} \end{align*}\]

Here is the projectile motion simulation. Import the required libraries.

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

Initialize variables.

4g = 9.81                # m/s/s
5v0 = 2                  # m/s
6angle0 = np.deg2rad(60) # anitial angle

Solve equations involved and store the data in an array.

 7t_final = 2*v0*np.sin(angle0)/g
 8t = np.linspace(0, t_final, 200)
 9x = v0*np.cos(angle0)*t
10y = v0*np.sin(angle0)*t - 0.5*g*t**2

Prepare an empty plot.

11fig, ax = plt.subplots()
12ax.set_xlim(np.min(x), np.max(x))
13ax.set_ylim(np.min(y), np.max(y)*1.07)
14ax.set_xlabel('X')
15ax.set_ylabel('Y')
16ax.set_aspect('equal')
17trajectory = ax.plot(x, y, ls='--', c='grey')[0]
18ball = plt.Circle((np.min(x), np.min(y)), radius=0.007, color='red')
19ax.add_patch(ball)

Make a function to update the plot.

21def animation(frame_i):
22    trajectory.set_data(x[:frame_i], y[:frame_i])
23    ball.center = x[frame_i],y[frame_i]
24    return trajectory,ball

Animate your plot.

25anim = FuncAnimation(
26                    fig, 
27                    animation,
28                    frames = len(x), 
29                    interval=25, 
30                    blit=True)
31plt.show()
projectile


Add Comment

* Required information
1000
Drag & drop images (max 3)
Is it true or false that green is a number?

Comments

No comments yet. Be the first!