Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: A Projectile with Air Drag

Combine ODE solvers, events, root finding and optimization to simulate a cannonball with air drag and find the launch angle that gives the longest range.

In this project you will model something with no tidy formula: a ball thrown through the air with quadratic air drag. Without drag the path is a parabola and the best angle for distance is 45 degrees. With drag the equations of motion can only be solved numerically, and the answer changes. Along the way you will use almost every skill from the course: writing the model as a system of ODEs (systems and higher-order ODEs), choosing an integrator, detecting the landing, validating against a known case, and optimizing the launch angle (one-dimensional optimization).

Step 1: the model

State: position (x, y) and velocity (vx, vy). Gravity pulls down at g = 9.81 m/s^2. Quadratic drag opposes the velocity with a force proportional to speed squared; per unit mass it is -k |v| v, where k = (1/2) rho Cd A / m. For a smooth ball, k is about 0.005 per meter. The equations:

x' = vx, y' = vy vx' = -k |v| vx vy' = -g - k |v| vy, with |v| = sqrt(vx^2 + vy^2).

flowchart LR
  A["Launch speed + angle"] --> B["Integrate the 4 ODEs"]
  B --> C["Event: y crosses 0<br/>(landing)"]
  C --> D["Range = x at landing"]
  D --> E["Optimize the angle"]

Step 2: validate with k = 0

Always test a numerical model against a case you can solve exactly. With k = 0 the range is v0^2 sin(2 theta) / g.

Python Simulate and validate against the exact parabola

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 and exact ranges agree to many digits. That builds trust in the right-hand side, the event handling and the tolerances.

Step 3: turn drag on

Python Drag shortens the flight

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.

Drag lowers both the range and the peak height. Notice also that with drag the path is no longer symmetric: it falls more steeply than it rises.

Step 4: find the best launch angle

Range is a function of one variable, the angle. Maximize it with a bounded scalar minimizer on the negative range. (Each function evaluation is a full ODE solve, which is exactly when derivative-free 1D methods shine.)

Python Optimal angle with and without drag

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.

Without drag the answer is 45 degrees (as theory says); drag moves the best angle lower, and the stronger the drag, the lower it goes. Your numerical experiment found something not obvious from the formulas.

Step 5: hit a target

Suppose a target stands 60 meters away. For a fixed speed, which angle lands there? This is root finding: solve range(angle) - 60 = 0. Because the range rises and then falls, there are generally two angles (a low and a high arc), so bracket each side of the optimum separately.

Python Aim at a target with brentq

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.

Extensions to try

  • Add wind: subtract a wind velocity before computing the relative speed in the drag term.
  • Make drag depend on altitude (air gets thinner with height): k(y) = k0 * exp(-y / 8000).
  • Compare RK45 with a hand-written RK4 (Runge-Kutta methods) and plot the error versus step size.
  • Add a Magnus (spin) force and see how a curveball differs.
  • Find the angle that maximizes the speed at impact instead of the range.

What you practiced

Modelling with systems of ODEs, validation against an exact case, event detection, error control with tolerances, optimization built on simulation, and root finding to solve an inverse problem.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why simulate with k = 0 first?

    Testing against a known analytical answer is the best way to catch errors in the model, event and tolerance.
  2. 2. How is the landing detected in the simulation?

    A terminal event with direction -1 stops the integration exactly when the height crosses zero going down.
  3. 3. How does quadratic drag change the optimal launch angle for range?

    Drag penalizes long flight times at high arcs, so flatter launches do better.
  4. 4. Why are there usually two angles that hit a target within the maximum range?

    Range as a function of angle is unimodal, so each reachable distance below the maximum is reached twice.

Summary

  • Turn the physics into a first-order system, integrate with an adaptive solver and stop with an event.
  • Validate against a case with a known answer before trusting results.
  • Wrap the simulation in an optimizer or root finder to answer design questions.

Next: Project: a sensor data pipeline