concept cannonball in category python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.
To learn about optimization, we’ll focus on the simple example of a projectile; namely, a cannonball being fired from a cannon. Assuming the cannonball comes out of the barrel at the same speed every time, the launch angle will decide the trajectory (figure 12.1).
Figure 12.1: Trajectories for a cannonball fired at four different launch angles
![]()
As you can see in figure 12.1, four different launch angles produce four different trajectories. Among these, 45° is the launch angle that sends the cannonball the furthest, while 80° is the angle that sends it the highest. These are only a few angles of all of the possible values between 0 and 90°, so we can’t be sure they are the best. Our goal is to systematically explore the range of possible launch angles to be sure we’ve found the one that optimizes the range of the cannon.
To do this, we first build a simulator for the cannonball. This simulator will be a Python function that takes a launch angle as input, runs Euler’s method (as we did in chapter 9) to simulate the moment-by-moment motion of the cannonball until it hits the ground, and outputs a list of positions of the cannonball over time. From the result, we’ll extract the final horizontal position of the cannonball, which will be the landing position or range. Putting these steps together, we implement a function that takes a launch angle and returns the range of the cannonball at that angle (figure 12.2).
Figure 12.2: Computing the range of a projectile using a simulator
![]()
Once we have encapsulated all of this logic in a single Python function called landing_position, which computes the range of a cannonball as a function of its launch angle, we can think about the problem of finding the launch angle that maximizes the range. We can do this in two ways: first, we make a graph of the range versus the launch angle and look for the largest value (figure 12.3).
It’s useful to keep the raw output of the trajectory in case we want to plot it, but sometimes we’ll want to focus on one number that matters most. For instance, the range of the projectile is the last x-coordinate of the trajectory, which is the last x position before the cannonball hits the ground. Here’s a function that takes the result of the trajectory function (parallel lists with time and the x and z positions), and extracts the range or landing position. For the input trajectory, traj, traj[1] lists the x-coordinates, and traj[1][-1] is the last entry in the list:
def landing_position(traj): return traj[1][-1]This is the main metric of a projectile’s trajectory that interests us, but we can also measure some other ones. For instance, we might want to know the hang time (or how long the cannonball stays in the air) or its maximum height. We can easily create other Python functions that measure these properties from simulated trajectories; for example,
When we use the + operator, we find t = 0, confirming that the cannonball is at ground level at the moment it is launched. The interesting solution is obtained using the − operator, which is the time the projectile lands. This time is t = (-b − b)/2a = -b/a. Plugging in the expressions for a and b, we get an expression for landing time in terms of quantities we know how to calculate: