Skip to content
Tutorials Center
My learning

Topics

Lesson 60 of 68 ยท Projects

Project: Projectile Motion with Air Drag

Model a projectile with quadratic air drag as a system of ODEs, solve it with solve_ivp, find the optimal launch angle and compare it with the no-drag formula.

Every physics course derives the range of a projectile: R = v0**2 sin(2 theta) / g, maximal at 45 degrees. It is a beautiful result, and it is wrong for any real ball, arrow or shell, because it ignores air. With drag, no tidy formula exists. The equations of motion cannot be integrated by hand, and the optimal angle depends on the speed and on how strong the drag is. This is exactly the situation where numerical ODE solving shines.

In this project you will turn the physics into a first-order system, solve it with scipy.integrate.solve_ivp, validate the solver against the drag-free formula, find the optimal launch angle numerically, and explore how it changes. It uses ideas from Second-Order Linear ODEs, Solving ODEs Numerically and Applications of Derivatives.

Step 1: The model

Let the projectile have mass m, position (x, y) and velocity (vx, vy). Gravity pulls down with force m g. At the speeds of a thrown or hit ball, air resistance is well approximated by quadratic drag: a force opposite to the velocity, with magnitude k v**2, where v is the speed. As a vector, that force is -k |v| (vx, vy). The constant is k = (1/2) rho Cd A, using air density rho, drag coefficient Cd and frontal area A.

Newton's second law then gives

  • x' = vx
  • y' = vy
  • vx' = -(k/m) |v| vx
  • vy' = -g - (k/m) |v| vy

with |v| = sqrt(vx**2 + vy**2). This is a system of four first-order equations, the form solve_ivp expects. The drag couples the two directions: a fast vertical motion also slows the horizontal one.

flowchart LR
    A["Newton's law:<br/>m a = gravity + drag"] --> B["State s = (x, y, vx, vy)"]
    B --> C["s' = rhs(t, s)"]
    C --> D["solve_ivp with a<br/>ground-hit event"]
    D --> E["Range, flight time,<br/>trajectory"]
    E --> F["Sweep the angle,<br/>find the best one"]

From physics to a simulation, and then to an optimization over the launch angle.

We use a baseball-sized ball: m = 0.145 kg, Cd = 0.4, A = 0.0043 m**2, in air with rho = 1.2 kg/m**3, launched at 40 m/s. Quadratic drag has a terminal speed where drag balances weight, sqrt(m g / k), which is about 37 m/s here. So at 40 m/s, drag is certainly not a small correction.

Step 2: Solve and validate

A good habit: before trusting the simulation with drag, switch drag off and compare with the exact formula. The code stops the integration when the ball returns to the ground using an event: a function whose sign change solve_ivp detects precisely (with terminal = True it ends the run there, and direction = -1 accepts only downward crossings).

Python The model, the ground event, and validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numerical ranges match the formula to six decimals, which confirms the equations, the event logic and the tolerances. Notice also the symmetry in the drag-free case: 30 and 60 degrees give the same range. With drag, the ball launched at 45 degrees lands at about 91 m instead of 163 m, a loss of more than 40%. The model has just earned its keep.

Step 3: Trajectories and the best angle

The range is now a function R(theta) that you can only evaluate by running a simulation. The optimal angle is where R'(theta) = 0 (see Applications of Derivatives). You do not need the derivative: scipy.optimize.minimize_scalar finds the maximum of a one-variable function by bracketing, here applied to the negative of the range.

The plot compares trajectories at several angles with the drag-free parabola for 45 degrees. Drag makes the descent steeper than the climb, so the path is no longer symmetric.

Python Sweep the launch angle and plot the trajectories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With drag the range peaks at about 91.5 m, at a launch angle near 40.5 degrees, not 45. The range curve is flat near its top: the ranges at 35, 40 and 45 degrees are about 90.1, 91.5 and 90.6 m, so a few degrees of error cost very little, a general property of a smooth maximum. In the plot, the dashed no-drag parabola towers over the real trajectories, and every real path falls more steeply than it rises.

dense_output=True gives a continuous function sol.sol(t) for the solution, so you can evaluate it at any times for plotting instead of only at the solver's steps.

Step 4: How does drag change the answer?

Two knobs matter: the strength of the drag k, and the launch speed. A cleaner way to think about it uses the ratio of drag to weight at typical speeds, k v0**2 / (m g). When it is small the 45-degree rule is nearly right; when it is large, the best angle drops well below 45 degrees. Let us scan both.

Python Optimal angle versus drag strength and launch speed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first table shows the best angle sliding from 45 degrees (no drag) down to about 34.7 degrees when the drag is five times larger, while the range drops from 163 m to about 39 m. The second table is just as telling: at 10 m/s drag is mild and the best angle is about 44.6 degrees with a range only 5% short of the formula, whereas at 80 m/s the best angle falls to about 35.6 degrees and the real range, about 181 m, is a fraction of the drag-free 652 m. The bigger the speed, the less the textbook formula can be trusted. This is why artillery tables, golf ball designs and sports analytics all rely on numerical simulation.

What you built, and ideas to extend it

You built a validated simulator that turns a physical law into a system of ODEs, handles a stopping condition with events, and combines the solver with an optimizer to answer a design question. The same recipe works for rockets, falling bodies and vehicles: write the forces, define the state, solve, validate in a limit you know, then explore.

Ideas to continue:

  • Add wind by replacing the velocity in the drag with the velocity relative to the air.
  • Make the air density fall with height: rho(y) = rho0 exp(-y / 8500).
  • Launch from a height of 2 m and see how the optimal angle changes.
  • Add a spinning ball with a lift force perpendicular to the velocity.
  • Estimate how sensitive the range is to Cd by running the model at Cd +/- 10%, and use the error-propagation ideas from Statistics, Curve Fitting and Uncertainty.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the drag-free range formula used to validate the simulation?

    Setting k = 0 turns the model into the textbook case, so the numeric and exact ranges must agree; a mismatch would reveal a bug.
  2. 2. What does an event with terminal = True and direction = -1 do in solve_ivp?

    The event function (here the height y) is monitored for sign changes; with terminal=True the solver stops there, and direction=-1 selects only downward crossings.
  3. 3. With quadratic drag, what happens to the optimal launch angle compared with 45 degrees?

    Drag penalizes long flight time at high altitude, so a flatter trajectory is better; in the runs the best angle falls from 45 degrees to about 35 degrees as drag grows.
  4. 4. Why do we convert the second-order equations of motion to four first-order equations?

    Introducing velocities as state variables gives x' = vx, y' = vy and equations for vx' and vy', the first-order form numerical solvers use.

Summary

  • Quadratic drag adds a force -k |v| v that couples the horizontal and vertical motion, so there is no simple closed form.
  • Write the state as (x, y, vx, vy), define s' = rhs(t, s), and let solve_ivp integrate it with tight tolerances.
  • Use an event to stop exactly when the projectile lands; use dense_output to plot smooth paths.
  • Validate by switching drag off and comparing with v0**2 sin(2 theta) / g.
  • Combine the solver with minimize_scalar to optimize: with drag the best angle is below 45 degrees and drops as drag or speed grows.

Next in the series: Project: Simulating an RLC Circuit.