---
title: Simulating motion with drag
heading: When there's no formula, integrate Newton's law numerically
eyebrow: B1 · Lesson 52 · Simulating motion with drag
xp: 50
---

::: hook
Every projectile formula in lesson 3 ignored air. With real air drag, which grows with the square of speed, there is no formula for the path at all: the horizontal and vertical motions are tied together through the speed. Engineers handle this the way circuit simulators handle nonlinear circuits: step Newton's law forward in small time steps on a computer. In this lab you'll write that simulation, check it against the drag-free formula, and use it to find the best launch angle when drag matters.
:::

::: play
- The solid curve is the drag-free parabola. The dashed curve is the path with drag proportional to velocity, $\mathbf F = -bm\mathbf v$, one of the few drag laws with an exact solution.
- Raise $b$: the path falls short and comes down more steeply than it went up.
- Real air drag goes as $v^2$, which has no closed form; the lab simulates it.
:::
:::

```sim plot
view: graph
x: x = 0..100 m
y: y = 0..30 m
slider: v0 = 10..31 step 0.5 = 30 m/s
slider: th = 10..80 step 1 = 45 °
slider: b = 0.05..1 step 0.05 = 0.2 /s
plot: step(v0^2*sin(2*th°)/9.81 - x)*(x*tan(th°) - 9.81*x^2/(2*v0^2*cos(th°)^2)) | label: no drag | samples: 800
plot: step(v0*cos(th°)/b - x - 0.001)*((v0*sin(th°) + 9.81/b)*x/(v0*cos(th°)) + 9.81/b^2*ln(abs(1 - b*x/(v0*cos(th°))) + 1e-12)) | label: linear drag | dashed | color: ch2 | samples: 800
```

::: predict
## With air drag, is the launch angle that throws farthest above or below 45°?

- [x] Below 45°
- [ ] Above 45°
- [ ] Exactly 45°
- [ ] Drag doesn't change the best angle

::: reveal
Below. Drag eats into a high, slow arc more than a low, fast one, because the projectile spends longer in the air losing speed. For a ball at 30 m/s with moderate drag the best angle is about 41°; for a golf ball or a bullet it's lower still. You'll find it with your own simulation in step 3.
:::
:::

::: nameit
## The equations

With quadratic drag $\mathbf F_d = -\frac12\rho C_dA|\mathbf v|\mathbf v$, Newton's law for a mass $m$ is

$$\frac{d\mathbf v}{dt} = -g\hat{\mathbf y} - k|\mathbf v|\mathbf v, \qquad k = \frac{\rho C_dA}{2m}$$

with $k$ in 1/m. The speed $|\mathbf v|$ couples the two directions, so they can't be solved separately. Along with $\frac{d\mathbf r}{dt} = \mathbf v$, that's four first-order equations for $x$, $y$, $v_x$ and $v_y$.

## Stepping in time

Starting from the launch state, advance by small steps $\Delta t$. The **RK4** method (A8.45) evaluates the acceleration four times per step and is accurate to order $\Delta t^4$; Euler's method, using only the start of the step, is accurate only to order $\Delta t$ and needs far smaller steps for the same accuracy.

## Finding the landing point

The projectile crosses $y = 0$ between two steps. Interpolate linearly between the last point above and the first below: if they're at heights $y_1 > 0$ and $y_2 < 0$, the crossing is a fraction $f = \frac{y_1}{y_1 - y_2}$ of the way through the step, which pins the range far more precisely than the step size.

## Trusting a simulation

1. **Check a case you can solve.** With $k = 0$ the range must match $\frac{v_0^2\sin 2\theta}{g}$.
2. **Halve the step.** If the answer barely changes, the step is small enough.
3. **Check the physics.** With drag the range must be shorter, and the falling half of the path steeper.
:::

::: example
## A ball leaves at 30 m/s and 45°. Without drag, how far does it go? With $k = 0.01$ /m, what does an accurate simulation give?

::: solution
1. **Without drag.**
   $$\begin{aligned} R &= \frac{30^2}{9.81} \\ &= 91.7\ \text{m} \end{aligned}$$
   in 4.32 s.
2. **With drag.** With $k = 0.01$ /m, RK4 with $\Delta t = 1$ ms gives 55.8 m in 3.72 s.
3. **Check the step.** Halving the step changes the range by less than a micrometer.
4. **Compare.** Drag cost 39% of the range.

**Answer:** 91.7 m without drag, and 55.8 m with it.
:::
:::

::: example
## What is the terminal speed for $k = 0.01$ /m?

::: solution
1. **Drag balances gravity.** Falling straight down at terminal speed,
   $$kv^2 = g$$
2. **Terminal speed.**
   $$\begin{aligned} v_t &= \sqrt{\frac{9.81}{0.01}} \\ &= 31.3\ \text{m/s} \end{aligned}$$
3. **What it means.** The ball launched at 30 m/s is already near its terminal speed, which is why drag affects it so strongly.

**Answer:** the terminal speed is 31.3 m/s.
:::
:::

::: check
kind: python

## Step 1. Write `accel(v, k)` returning the acceleration as a NumPy array `[ax, ay]` for velocity `v` (an array `[vx, vy]`), with gravity 9.81 m/s² and quadratic drag $-k|\mathbf v|\mathbf v$.

```python starter
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g])

print(accel(np.array([30.0, 0.0]), 0.01))
```

```python solution
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g]) - k * np.linalg.norm(v) * v

print(accel(np.array([30.0, 0.0]), 0.01))
```

```python tests
import numpy as np
a = accel(np.array([30.0, 0.0]), 0.01)
assert np.allclose(a, [-9.0, -9.81]), f"Moving at 30 m/s horizontally with k = 0.01, expect (-9, -9.81); got {a}."
a = accel(np.array([3.0, 4.0]), 0.02)
assert np.allclose(a, [-0.3, -9.81 - 0.4]), f"For v = (3, 4) and k = 0.02, drag is 0.02 × 5 × v; got {a}."
assert np.allclose(accel(np.array([5.0, -2.0]), 0.0), [0, -9.81]), "With k = 0 only gravity acts."
```

::: right
Right. The drag term points opposite the velocity and grows with the square of the speed.
:::
:::

::: check
kind: python

## Step 2. Write `simulate(v0, theta, k, dt)` that launches from the origin at speed `v0` and angle `theta` (degrees), steps with RK4, and returns `(range, flight_time)`, interpolating the landing between steps. `accel` is included.

```python starter
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g]) - k * np.linalg.norm(v) * v

def simulate(v0, theta, k, dt=1e-3):
    return 0.0, 0.0

print(simulate(30, 45, 0.0), simulate(30, 45, 0.01))
```

```python solution
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g]) - k * np.linalg.norm(v) * v

def simulate(v0, theta, k, dt=1e-3):
    th = np.radians(theta)
    r = np.array([0.0, 0.0])
    v = v0 * np.array([np.cos(th), np.sin(th)])
    t = 0.0
    while True:
        k1v = accel(v, k); k1r = v
        k2v = accel(v + dt / 2 * k1v, k); k2r = v + dt / 2 * k1v
        k3v = accel(v + dt / 2 * k2v, k); k3r = v + dt / 2 * k2v
        k4v = accel(v + dt * k3v, k); k4r = v + dt * k3v
        rn = r + dt / 6 * (k1r + 2 * k2r + 2 * k3r + k4r)
        vn = v + dt / 6 * (k1v + 2 * k2v + 2 * k3v + k4v)
        if rn[1] < 0 and t > 0:
            f = r[1] / (r[1] - rn[1])
            return r[0] + f * (rn[0] - r[0]), t + f * dt
        r, v, t = rn, vn, t + dt

print(simulate(30, 45, 0.0), simulate(30, 45, 0.01))
```

```python tests
R0, T0 = simulate(30, 45, 0.0, 2e-3)
assert abs(R0 - 900 / 9.81) / (900 / 9.81) < 1e-3, f"With no drag the range should be v²/g = 91.74 m; got {R0:.3f}."
assert abs(T0 - 4.3248) < 0.005, f"With no drag the flight time should be 4.325 s; got {T0:.4f}."
R1, T1 = simulate(30, 45, 0.01, 2e-3)
assert abs(R1 - 55.782) < 0.1, f"With k = 0.01 the range should be about 55.78 m; got {R1:.3f}."
assert abs(T1 - 3.7232) < 0.01, f"With k = 0.01 the flight time should be about 3.723 s; got {T1:.4f}."
```

::: right
Right. Your simulation reproduces the formula without drag and gives 55.8 m with it.
:::
:::

::: check
kind: python

## Step 3. Write `best_angle(v0, k)` that tries every whole-degree angle from 30° to 50° with `simulate` (use `dt=4e-3`) and returns the angle with the longest range. `simulate` is included.

```python starter
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g]) - k * np.linalg.norm(v) * v

def simulate(v0, theta, k, dt=1e-3):
    th = np.radians(theta)
    r = np.array([0.0, 0.0]); v = v0 * np.array([np.cos(th), np.sin(th)]); t = 0.0
    while True:
        k1v = accel(v, k); k2v = accel(v + dt / 2 * k1v, k)
        k3v = accel(v + dt / 2 * k2v, k); k4v = accel(v + dt * k3v, k)
        rn = r + dt / 6 * (v + 2 * (v + dt / 2 * k1v) + 2 * (v + dt / 2 * k2v) + (v + dt * k3v))
        vn = v + dt / 6 * (k1v + 2 * k2v + 2 * k3v + k4v)
        if rn[1] < 0 and t > 0:
            f = r[1] / (r[1] - rn[1])
            return r[0] + f * (rn[0] - r[0]), t + f * dt
        r, v, t = rn, vn, t + dt

def best_angle(v0, k):
    return 45

print(best_angle(30, 0.01))
```

```python solution
import numpy as np
g = 9.81

def accel(v, k):
    return np.array([0.0, -g]) - k * np.linalg.norm(v) * v

def simulate(v0, theta, k, dt=1e-3):
    th = np.radians(theta)
    r = np.array([0.0, 0.0]); v = v0 * np.array([np.cos(th), np.sin(th)]); t = 0.0
    while True:
        k1v = accel(v, k); k2v = accel(v + dt / 2 * k1v, k)
        k3v = accel(v + dt / 2 * k2v, k); k4v = accel(v + dt * k3v, k)
        rn = r + dt / 6 * (v + 2 * (v + dt / 2 * k1v) + 2 * (v + dt / 2 * k2v) + (v + dt * k3v))
        vn = v + dt / 6 * (k1v + 2 * k2v + 2 * k3v + k4v)
        if rn[1] < 0 and t > 0:
            f = r[1] / (r[1] - rn[1])
            return r[0] + f * (rn[0] - r[0]), t + f * dt
        r, v, t = rn, vn, t + dt

def best_angle(v0, k):
    return max(range(30, 51), key=lambda a: simulate(v0, a, k, 4e-3)[0])

print(best_angle(30, 0.01))
```

```python tests
a = best_angle(30, 0.01)
assert a == 41, f"With k = 0.01 and 30 m/s the best whole-degree angle is 41°; got {a}."
```

::: right
Right. Drag pulls the best angle down to 41°, and the range there is 56.2 m, only slightly more than the 55.8 m at 45°.
:::
:::

::: check
given: k = 0.005 | 0.01 | 0.02
answer: sqrt(9.81/k)
unit: m/s
tolerance: 0.5%

## Step 4. What terminal speed does a falling object reach with quadratic drag coefficient $k$ = {k} /m?

::: right
Right. $kv^2 = g$: {answer}.
:::

::: misses
- [9.81/k] Drag goes as $v^2$: take the square root.
- [any] Not yet. Drag balances gravity.
:::
:::

::: explain
## Why check the simulation against the drag-free case before trusting it with drag?

::: model
A simulation can be wrong in many quiet ways: a sign error, a missing factor, a step too big, a landing detected one step late. With drag turned off there's an exact answer, so any of those mistakes shows up as a mismatch. If the code reproduces $v_0^2\sin 2\theta/g$ to high precision, the integrator, the units and the landing detection are all working, and the only new ingredient with drag on is one extra term in the acceleration, which is easy to check by hand.
:::
:::

::: practice

::: check
given: rho = 1.2; Cd = 0.47 | 0.5; d = 4 | 22; m = 0.045 | 0.6
answer: 1.2*Cd*pi*(d/200)^2/(2*m)
unit: /m
tolerance: 0.5%

## A ball of diameter {d} cm and mass {m} kg has $C_d$ = {Cd}, in air of 1.2 kg/m³. What is its drag coefficient $k = \frac{\rho C_dA}{2m}$?

::: misses
- [any] $A = \pi(d/2)^2$.
:::
:::

::: check
given: y1 = 0.3 | 0.05; y2 = -0.1 | -0.15
answer: y1/(y1 - y2)
tolerance: 0.5%

## A simulated projectile is at height {y1} m at one step and {y2} m at the next. What fraction of the step had passed when it reached the ground?

::: misses
- [any] $\frac{y_1}{y_1 - y_2}$.
:::
:::

::: check
given: k = 2..10 int
answer: k^4
tolerance: 0.5%

## RK4's error goes as $\Delta t^4$. By what factor does the error shrink when the step is made {k} times smaller?

::: misses
- [k] That's Euler's method.
- [any] $k^4$.
:::
:::
:::
