Sub topic 15
Simulating a Trajectory Step by Step
This chapter assumes you know the ideal projectile equations from the earlier chapters: constant horizontal velocity, constant vertical acceleration of -g, and the range formula R = v0² sin(2θ) / g for level ground. You should also be able to read basic Python (loops, functions, math). Everything else is introduced here.
Why simulate at all
The ideal model has closed-form answers because acceleration is constant. Once air resistance is added, the acceleration depends on the current velocity, and the equations no longer have simple solutions. A numerical simulation does not need a formula for the whole path. It only needs the rule for acceleration at one instant.
The idea is to cut time into small steps of length dt. Within one step, pretend the acceleration and velocity stay fixed. Update the state, then repeat.
The Euler method
The state of the projectile is four numbers: x, y, vx and vy. From the definitions of velocity and acceleration, a small time step dt changes them by:
- x → x + vx · dt
- y → y + vy · dt
- vx → vx + ax · dt
- vy → vy + ay · dt
This is the explicit Euler method. For the ideal model, ax = 0 and ay = -g. For quadratic drag, the acceleration opposes the velocity and grows with speed:
- ax = -k · |v| · vx
- ay = -g - k · |v| · vy
Here |v| = √(vx² + vy²), and k is a drag coefficient per unit mass with units 1/m. Setting k = 0 recovers the ideal model, so one function can cover both cases.
The order of updates matters. In the code below, position is advanced with the old velocity, and only then is velocity updated. That is what makes it explicit Euler.
A first simulation
import math
def simulate_range(v0, angle_deg, k=0.0, dt=0.001, g=9.81):
"""Euler integration of a projectile launched from y = 0.
k is a quadratic drag coefficient per unit mass (1/m); k = 0 is the ideal case."""
theta = math.radians(angle_deg)
x, y = 0.0, 0.0
vx, vy = v0 * math.cos(theta), v0 * math.sin(theta)
while y >= 0.0:
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
return x
ideal = simulate_range(20.0, 30.0, k=0.0)
with_drag = simulate_range(20.0, 30.0, k=0.02)
print(f"ideal: {ideal:.2f} m, with drag: {with_drag:.2f} m")The analytic range for this launch is 20² · sin(60°) / 9.81 ≈ 35.31 m. The ideal run should print a value very close to that. The drag run prints a clearly shorter distance, because drag removes horizontal speed throughout the flight.
Two details of the loop are worth noting.
- The loop condition
y >= 0.0is true at the start because y begins at exactly 0. A launch at a positive angle rises immediately, so the loop continues. - The loop stops after the first step that ends below the ground. The returned x therefore belongs to a point slightly below y = 0, so it overshoots the true landing point by up to one step of horizontal travel.
How wrong is it?
Explicit Euler has a local error proportional to dt² per step and a global error proportional to dt. In practice, halving dt roughly halves the final error. You can see the source of the error in the ideal case. Euler uses the velocity at the start of each step, but vertical velocity is decreasing. The simulated height therefore ends up slightly too large, by about g · dt · t / 2 at time t. For dt = 0.001 s and t ≈ 2 s, that is about 1 cm.
To measure this instead of guessing, compare with the analytic result and change dt. The version below also removes the overshoot by interpolating between the last two points, so that the landing x is placed where the straight segment crosses y = 0.
import math
def analytic_range(v0, angle_deg, g=9.81):
return v0**2 * math.sin(2 * math.radians(angle_deg)) / g
def simulate_range_interp(v0, angle_deg, k=0.0, dt=0.001, g=9.81, interpolate=True):
"""Euler integration from y = 0. If interpolate is True, the landing x is
found by linear interpolation between the last two positions."""
theta = math.radians(angle_deg)
x, y = 0.0, 0.0
vx, vy = v0 * math.cos(theta), v0 * math.sin(theta)
while True:
speed = math.hypot(vx, vy)
ax = -k * speed * vx
ay = -g - k * speed * vy
x_new = x + vx * dt
y_new = y + vy * dt
vx += ax * dt
vy += ay * dt
if y_new < 0.0:
if not interpolate:
return x_new
fraction = y / (y - y_new) # how far along the step y reaches 0
return x + fraction * (x_new - x)
x, y = x_new, y_new
exact = analytic_range(20.0, 30.0)
for dt in (0.01, 0.001, 0.0001):
plain = simulate_range_interp(20.0, 30.0, dt=dt, interpolate=False)
interp = simulate_range_interp(20.0, 30.0, dt=dt, interpolate=True)
print(f"dt={dt:<7} plain error={plain - exact:+.4f} m "
f"interpolated error={interp - exact:+.4f} m")Expect the plain error to be positive and to shrink roughly tenfold each time dt shrinks tenfold. The interpolated error is smaller at each dt, but it still shrinks in proportion to dt. Interpolation fixes the stopping rule. It does not fix the Euler error in the path itself.
Choosing a method and a step size
Explicit Euler is the simplest option, and it is enough for this chapter. Other common choices differ in how they use the velocity within a step.
| Method | Update order | Typical behavior for projectiles |
|---|---|---|
| Explicit Euler | Position uses the old velocity, then velocity updates | First-order error; simulated height runs slightly high |
| Semi-implicit Euler | Velocity updates first, position uses the new velocity | First-order error with the opposite sign for height; behaves better for oscillating systems |
| Analytic solution | Closed-form formulas | Exact, but only available for constant acceleration |
For constant acceleration, a step that uses the average of the old and new velocity reproduces the exact position. For drag, no such shortcut exists. Higher-order methods such as Runge–Kutta 4 reach a given accuracy with much larger steps. They are worth learning once you need them, but they use the same loop structure.
To choose dt, run the simulation at dt and at dt / 2. If the result changes by less than the precision you need, dt is small enough. This convergence check works even when no analytic answer exists, which is the whole point of simulating drag.
Reading the drag result
Drag introduces behavior that the ideal model cannot produce.
- The range is shorter at every angle.
- The path is no longer symmetric. The descent is steeper than the ascent.
- The best launch angle for range drops below 45° on level ground.
- Speed cannot grow without limit in a long fall. For quadratic drag, vertical motion approaches a terminal speed of √(g / k). With k = 0.02 1/m, that is √(9.81 / 0.02) ≈ 22 m/s.
You can check the last point by dropping an object from rest in the simulation and watching vy level off.
Common errors
- Updating velocity before position in code you intended to be explicit Euler. This silently switches to semi-implicit Euler and changes the error.
- Reporting x from the last step without handling the overshoot, then comparing to the formula at a precision finer than dt · vx.
- Using
y > 0as the loop condition when launching from y = 0. The loop would end before it starts. - Forgetting that the drag acceleration uses the speed |v|, not just one component. Using
-k * vx**2for ax gives the wrong direction when vx changes sign and ignores the coupling between axes.
Exercise
A simulation with dt = 0.01 s gives a range error of +0.17 m against the analytic result. Roughly what error do you expect with dt = 0.0025 s, and what check could confirm it?
Show answer
Euler's global error is proportional to dt, so reducing dt by a factor of 4 should reduce the error by about 4, to roughly +0.04 m. You can confirm it by running both step sizes and comparing each to the analytic range. The error ratio should be close to 4.
Exercise
With k = 0.02 1/m, what vertical speed should a simulated object approach when dropped from rest, and which code change would let you test it?
Show answer
The terminal speed is √(g / k) = √(9.81 / 0.02) ≈ 22 m/s. To test it, start with vx = 0 and vy = 0, run the same update loop for a long time, such as 10 s, without the ground check, and print |vy|. It should level off near 22 m/s.