Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Modelling the Projectile Motion using Python
Projectile motion is the motion of an object thrown into the air under the influence of gravity. Python provides excellent tools for modeling and visualizing this physics concept using mathematical equations and plotting libraries.
The projectile motion is studied in three main categories:
Horizontally projected from height
Inclined projection on level ground
Inclined projection with different landing levels
Case 1: Horizontally Projected
When an object is launched horizontally from height h with initial velocity u, it follows these equations:
$$\mathrm{h=\frac{1}{2}gt^{2}}$$
$$\mathrm{R=ut}$$
Example: Horizontal Projectile Motion
Let's calculate where a food packet dropped from an airplane will land ?
import math
# Input parameters
h = 900 # height in meters
u = 140 # horizontal velocity in m/s
g = 9.81 # acceleration due to gravity
# Calculate time of flight
t = math.sqrt(2 * h / g)
# Calculate horizontal range
R = u * t
# Display results
print(f"Time of flight = {round(t, 3)} s")
print(f"Range = {round(R, 3)} m")
Time of flight = 13.546 s Range = 1896.4 m
Case 2: Inclined Projection on Level Ground
For projectiles launched at angle ? from level ground, we use these formulas:
$$\mathrm{h_{max}=\frac{u^{2}\sin^{2}(\alpha)}{2g}}$$
$$\mathrm{R=\frac{u^{2}\sin(2\alpha)}{g}}$$
$$\mathrm{T=\frac{2u\sin(\alpha)}{g}}$$
The trajectory equation is:
$$\mathrm{y=x\tan(\alpha)-\frac{1}{2}\frac{gx^{2}}{u^{2}\cos^{2}(\alpha)}}$$
Example: Plotting Projectile Trajectory
import numpy as np
import matplotlib.pyplot as plt
# Input parameters
u = 50 # initial velocity in m/s
alpha_deg = 45 # angle in degrees
alpha = np.radians(alpha_deg) # convert to radians
g = 9.81 # acceleration due to gravity
# Calculate range and maximum height
R = u**2 * np.sin(2 * alpha) / g
h_max = u**2 * (np.sin(alpha))**2 / (2 * g)
# Create x-coordinate array
x = np.linspace(0, R, 50)
# Calculate y-coordinates using trajectory equation
y = x * np.tan(alpha) - (g * x**2) / (2 * u**2 * (np.cos(alpha))**2)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label=f'? = {alpha_deg}°')
plt.xlabel('Horizontal Distance (m)')
plt.ylabel('Height (m)')
plt.title('Projectile Motion Trajectory')
plt.grid(True, alpha=0.3)
plt.legend()
plt.ylim(0, h_max + 5)
print(f"Range = {round(R, 2)} m")
print(f"Maximum Height = {round(h_max, 2)} m")
plt.show()
Range = 254.84 m Maximum Height = 63.71 m
Comparing Multiple Launch Angles
import numpy as np
import matplotlib.pyplot as plt
def plot_trajectory(u, alpha_deg):
"""Plot projectile trajectory for given velocity and angle"""
alpha = np.radians(alpha_deg)
g = 9.81
# Calculate range
R = u**2 * np.sin(2 * alpha) / g
# Create coordinate arrays
x = np.linspace(0, R, 50)
y = x * np.tan(alpha) - (g * x**2) / (2 * u**2 * (np.cos(alpha))**2)
return x, y, R
# Plot trajectories for different angles
plt.figure(figsize=(12, 8))
u = 40 # m/s
angles = [15, 30, 45, 60, 75]
for angle in angles:
x, y, R = plot_trajectory(u, angle)
plt.plot(x, y, linewidth=2, label=f'{angle}°')
plt.xlabel('Horizontal Distance (m)')
plt.ylabel('Height (m)')
plt.title('Projectile Trajectories at Different Launch Angles')
plt.grid(True, alpha=0.3)
plt.legend()
plt.ylim(0, 85)
plt.show()
Case 3: Different Launch and Landing Heights
When the projectile lands at a different height than launch, we solve the quadratic equation:
$$\mathrm{gt^{2}-2u\sin(\alpha)t-2y_{0}=0}$$
Example: Projectile with Height Difference
import numpy as np
# Parameters
u = 100 # initial velocity (m/s)
alpha = np.radians(45) # launch angle
y0 = 100 # initial height above landing point (m)
g = 9.81 # acceleration due to gravity
# Solve quadratic equation for time of flight
# gt² - 2u*sin(?)*t - 2*y0 = 0
a = g
b = -2 * u * np.sin(alpha)
c = -2 * y0
# Coefficients array for numpy.roots
coefficients = np.array([a, b, c])
t1, t2 = np.roots(coefficients)
print(f"Time solutions: t1 = {round(t1, 3)} s, t2 = {round(t2, 3)} s")
# Take positive time value
t_flight = max(t1, t2)
# Calculate range
R = u * np.cos(alpha) * t_flight
# Calculate maximum height
h_relative = u**2 * (np.sin(alpha))**2 / (2 * g) # height above launch point
h_max = h_relative + y0 # total height above landing
print(f"Flight time = {round(t_flight, 3)} s")
print(f"Range = {round(R, 3)} m")
print(f"Maximum height = {round(h_max, 3)} m")
Time solutions: t1 = 15.713 s, t2 = -1.297 s Flight time = 15.713 s Range = 1111.111 m Maximum height = 354.842 m
Complete Trajectory Visualization
import numpy as np
import matplotlib.pyplot as plt
# Parameters from previous example
u = 100
alpha = np.radians(45)
y0 = 100
g = 9.81
R = 1111.111 # calculated range
# Create trajectory
x = np.linspace(0, R, 100)
y = x * np.tan(alpha) - (g * x**2) / (2 * u**2 * (np.cos(alpha))**2)
# Plot setup
plt.figure(figsize=(12, 8))
# Plot ground levels
plt.plot([0, R], [0, 0], 'k-', linewidth=2, label='Landing level')
plt.plot([0, R], [y0, -y0], 'brown', linewidth=3, label='Terrain')
# Plot trajectory
plt.plot(x, y + y0, 'r-', linewidth=2, label='Projectile path')
# Mark launch and landing points
plt.plot(0, y0, 'go', markersize=8, label='Launch point')
plt.plot(R, 0, 'ro', markersize=8, label='Landing point')
plt.xlabel('Horizontal Distance (m)')
plt.ylabel('Height (m)')
plt.title('Projectile Motion with Height Difference')
plt.grid(True, alpha=0.3)
plt.legend()
plt.axis('equal')
plt.show()
Summary of Key Equations
| Case | Time of Flight | Range | Max Height |
|---|---|---|---|
| Horizontal | ?(2h/g) | u×t | h |
| Level Ground | 2u sin(?)/g | u² sin(2?)/g | u² sin²(?)/(2g) |
| Height Difference | Quadratic solution | u cos(?)×t | u² sin²(?)/(2g) + y? |
Conclusion
Python's NumPy and Matplotlib libraries make projectile motion modeling straightforward. Use the trajectory equation for visualization and solve quadratic equations for complex scenarios with height differences.
