concept launch angle in category python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.
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).
Figure 12.3: Looking at a plot of range vs. launch angle, we can see the approximate value of the launch angle that produces the longest range.
![]()
The second way we can find the optimal launch angle is to set our simulator aside and find a formula for the range r(𝜃) of the projectile as a function of the launch angle 𝜃. This should produce identical results as the simulation, but because it is a mathematical formula, we can take its derivative using the rules from chapter 10. The derivative of the landing position with respect to the launch angle tells us how much increase in the range we’ll get for small increases in the launch angle. At some angle, we can see that we get diminishing returns—increasing the launch angle causes the range to decrease and we’ll have passed our optimal value. Before this, the derivative of r(𝜃) will instantaneously be zero, and the value of 𝜃 where the derivative is zero happens to be the maximum value.
The trajectory function takes a launch angle and produces the full time and position data for the cannonball over its flight. A function like landing_position takes this data and produces a single number. Composing these two together (figure 12.7), we get a function for landing position in terms of the launch angle, where all other properties of the simulation are assumed constant.
Figure 12.7: Landing position as a function of the launch angle
![]()
One way to test the effect of the launch angle on a landing position is to make a plot of the resulting landing position for several different values of the launch angle (figure 12.8). To do this, we need to calculate the result of the composition landing_position(trajectory(theta)) for several different values of theta and pass these to Matplotlib’s scatter function. Here, for example, I use range(0,95,5) as the launch angles. This is every angle from zero to 90 in increments of 5:
import matplotlib.pyplot as plt angles = range(0,90,5) landing_positions = [landing_position(trajectory(theta)) for theta in angles] plt.scatter(angles,landing_positions)Figure 12.8: A plot of the landing position vs. the launch angle for the cannon for several different values of the launch angle
![]()
From this plot, we can guess what the optimal value is. At a launch angle of 45°, the landing position is maximized at little over 40 meters from the launch position. In this case, 45° turns out to be the exact value of the angle that maximizes the landing position. In the next section, we’ll use calculus to confirm this maximum value without having to do any simulation.
As your simulator becomes more complicated, there can be multiple parameters governing its behavior. For our original cannon, the launch angle 𝜃 was the only parameter we were playing with. To optimize the range of the cannon, we worked with a function of one variable: r(𝜃). In this section, we’ll make our cannon fire in 3D, meaning that we need to vary two launch angles as parameters to optimize the range of the cannonball.