Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/chap23.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 23
Copyright 2017 Allen Downey
Code from the previous chapter
Optimal launch angle
To find the launch angle that maximizes distance from home plate, we need a function that takes launch angle and returns range.
Let's test range_func
.
And sweep through a range of angles.
Plotting the Sweep
object, it looks like the peak is between 40 and 45 degrees.
We can use maximize
to search for the peak efficiently.
res
is an ModSimSeries
object with detailed results:
x
is the optimal angle and fun
the optional range.
Under the hood
Read the source code for maximize
and minimize_scalar
, below.
Add a print statement to range_func
that prints angle
. Then run maximize
again so you can see how many times it calls range_func
and what the arguments are.
The Manny Ramirez problem
Finally, let's solve the Manny Ramirez problem:
What is the minimum effort required to hit a home run in Fenway Park?
Fenway Park is a baseball stadium in Boston, Massachusetts. One of its most famous features is the "Green Monster", which is a wall in left field that is unusually close to home plate, only 310 feet along the left field line. To compensate for the short distance, the wall is unusually high, at 37 feet.
Although the problem asks for a minimum, it is not an optimization problem. Rather, we want to solve for the initial velocity that just barely gets the ball to the top of the wall, given that it is launched at the optimal angle.
And we have to be careful about what we mean by "optimal". For this problem, we don't want the longest range, we want the maximum height at the point where it reaches the wall.
If you are ready to solve the problem on your own, go ahead. Otherwise I will walk you through the process with an outline and some starter code.
As a first step, write a function called height_func
that takes a launch angle and a params as parameters, simulates the flights of a baseball, and returns the height of the baseball when it reaches a point 94.5 meters (310 feet) from home plate.
Always test the slope function with the initial conditions.
Test your function with a launch angle of 45 degrees:
Now use maximize
to find the optimal angle. Is it higher or lower than the angle that maximizes range?
With initial velocity 40 m/s and an optimal launch angle, the ball clears the Green Monster with a little room to spare.
Which means we can get over the wall with a lower initial velocity.
Finding the minimum velocity
Even though we are finding the "minimum" velocity, we are not really solving a minimization problem. Rather, we want to find the velocity that makes the height at the wall exactly 11 m, given given that it's launched at the optimal angle. And that's a job for root_bisect
.
Write an error function that takes a velocity and a Params
object as parameters. It should use maximize
to find the highest possible height of the ball at the wall, for the given velocity. Then it should return the difference between that optimal height and 11 meters.
Test your error function before you call root_bisect
.
Then use root_bisect
to find the answer to the problem, the minimum velocity that gets the ball out of the park.
And just to check, run error_func
with the value you found.