Sub topic 15
Simulating a Trajectory Step by Step
Assume you know basic algebra, trigonometry, one-dimensional motion, and Python syntax. This chapter uses the ideal projectile equations from earlier chapters and turns them into a numerical simulation.
A numerical simulation advances a system through many small time steps. Instead of solving directly for the entire path, it repeatedly updates position and velocity. For a projectile launched from level ground,
- \(x\) is horizontal position.
- \(y\) is vertical position.
- \(vx\) and \(vy\) are velocity components.
- \(ax=0\) and \(ay=-g\) in the ideal model.
For a small time step \(\Delta t\), the Euler method uses
\[ x{\text{new}}=x+vx\Delta t \]
\[ y{\text{new}}=y+vy\Delta t \]
\[ v{x,\text{new}}=vx+a_x\Delta t \]
\[ v{y,\text{new}}=vy+a_y\Delta t. \]
The method approximates the motion. A smaller \(\Delta t\) usually gives a more accurate result, but requires more calculations.
The simulation loop
The loop performs these actions:
- Store the current position and velocity.
- Calculate acceleration.
- Advance position and velocity by one time step.
- Stop when the projectile reaches the landing level.
- Record the horizontal position at landing.
The code below also includes a simple quadratic drag model. Here, k is a drag coefficient per unit mass. Set k=0 for the ideal model.
import math
def simulate_range(v0, angle_deg, k=0.0, dt=0.001, g=9.81):
"""Return landing time and horizontal range.
The projectile starts at x = 0, y = 0.
k = 0 uses the ideal model.
"""
theta = math.radians(angle_deg)
x, y = 0.0, 0.0
vx = v0 * math.cos(theta)
vy = v0 * math.sin(theta)
t = 0.0
while y >= 0.0:
old_x, old_y = x, y
speed = math.hypot(vx, vy)
ax = -k * speed * vx
ay = -g - k * speed * vy
x += vx * dt
y += vy * dt
vx += ax * dt
vy += ay * dt
t += dt
# Estimate the exact crossing between the final two points.
fraction = old_y / (old_y - y)
landing_x = old_x + fraction * (x - old_x)
landing_t = t - dt + fraction * dt
return landing_t, landing_x
v0 = 20.0
angle = 30.0
g = 9.81
theta = math.radians(angle)
vx0 = v0 * math.cos(theta)
vy0 = v0 * math.sin(theta)
analytic_time = 2 * vy0 / g
analytic_range = vx0 * analytic_time
ideal_time, ideal_range = simulate_range(v0, angle, k=0.0)
drag_time, drag_range = simulate_range(v0, angle, k=0.02)
print(f"Analytic ideal result: T = {analytic_time:.3f} s, R = {analytic_range:.3f} m")
print(f"Simulated ideal result: T = {ideal_time:.3f} s, R = {ideal_range:.3f} m")
print(f"Simulated with drag: T = {drag_time:.3f} s, R = {drag_range:.3f} m")The simulated ideal result should be close to the analytic result. It may differ slightly because the simulation uses discrete steps. Drag makes both velocity components change continuously. The projectile usually travels a shorter distance and follows a nonsymmetrical path.
Exercise
Exercise
Why does the simulation with k=0.02 usually have a shorter range than the ideal simulation?
Show answer
Drag creates acceleration opposite to the velocity, reducing the projectile's horizontal and vertical speeds and therefore shortening its flight and range.