Untitled
unknown
plain_text
2 years ago
974 B
4
Indexable
import math
import matplotlib.pyplot as plt
# Constants
v0 = 20  # initial velocity
theta = 30  # launch angle
h0 = 50  # initial height
g = 9.8  # acceleration due to gravity
# Calculate time of flight
t_max = 2 * v0 * math.sin(math.radians(theta)) / g
# Calculate horizontal and vertical distances
x_max = v0**2 * math.sin(math.radians(2*theta)) / g
y_max = h0 + v0**2 * math.sin(math.radians(theta))**2 / (2*g)
# Create arrays for time, horizontal and vertical positions
t = []
x = []
y = []
# Calculate position at each time interval
dt = 0.01  # time interval
for i in range(int(t_max/dt)):
    time = i * dt
    t.append(time)
    x.append(v0 * math.cos(math.radians(theta)) * time)
    y.append(h0 + v0 * math.sin(math.radians(theta)) * time - 0.5 * g * time**2)
# Plot the trajectory
plt.plot(x, y)
plt.title('Projectile Motion')
plt.xlabel('Horizontal Distance (m)')
plt.ylabel('Vertical Distance (m)')
plt.ylim(0, y_max*1.2)
plt.xlim(0, x_max*1.2)
plt.show()Editor is loading...