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/spiderman.ipynb
Views: 531
Modeling and Simulation in Python
Case study: Spider-Man
Copyright 2017 Allen Downey
I'll start by getting the units we'll need from Pint.
Spider-Man
In this case study we'll develop a model of Spider-Man swinging from a springy cable of webbing attached to the top of the Empire State Building. Initially, Spider-Man is at the top of a nearby building, as shown in this diagram.
The origin, O⃗
, is at the base of the Empire State Building. The vector H⃗
represents the position where the webbing is attached to the building, relative to O⃗
. The vector P⃗
is the position of Spider-Man relative to O⃗
. And L⃗
is the vector from the attachment point to Spider-Man.
By following the arrows from O⃗
, along H⃗
, and along L⃗
, we can see that
H⃗ + L⃗ = P⃗
So we can compute L⃗
like this:
L⃗ = P⃗ - H⃗
The goals of this case study are:
Implement a model of this scenario to predict Spider-Man's trajectory.
Choose the right time for Spider-Man to let go of the webbing in order to maximize the distance he travels before landing.
Choose the best angle for Spider-Man to jump off the building, and let go of the webbing, to maximize range.
I'll create a Params
object to contain the quantities we'll need:
According to the Spider-Man Wiki, Spider-Man weighs 76 kg.
Let's assume his terminal velocity is 60 m/s.
The length of the web is 100 m.
The initial angle of the web is 45 degrees to the left of straight down.
The spring constant of the web is 40 N / m when the cord is stretched, and 0 when it's compressed.
Here's a Params
object.
Compute the initial position
Now here's a version of make_system
that takes a Params
object as a parameter.
make_system
uses the given value of v_term
to compute the drag coefficient C_d
.
Let's make a System
Drag and spring forces
Here's drag force, as we saw in Chapter 22.
And here's the 2-D version of spring force. We saw the 1-D version in Chapter 21.
Here's the slope function, including acceleration due to gravity, drag, and the spring force of the webbing.
As always, let's test the slope function with the initial conditions.
And then run the simulation.
Visualizing the results
We can extract the x and y components as Series
objects.
The simplest way to visualize the results is to plot x and y as functions of time.
We can plot the velocities the same way.
Another way to visualize the results is to plot y versus x. The result is the trajectory through the plane of motion.
Letting go
Now let's find the optimal time for Spider-Man to let go. We have to run the simulation in two phases because the spring force changes abruptly when Spider-Man lets go, so we can't integrate through it.
Here are the parameters for Phase 1, running for 9 seconds.
The final conditions from Phase 1 are the initial conditions for Phase 2.
Here's the position Vector.
And the velocity Vector.
Here is the System
for Phase 2. We can turn off the spring force by setting k=0
, so we don't have to write a new slope function.
Here's an event function that stops the simulation when Spider-Man reaches the ground.
Run Phase 2.
Plot the results.
Now we can gather all that into a function that takes t_release
and V_0
, runs both phases, and returns the results.
And here's a test run.
Animation
Here's a draw function we can use to animate the results.
Maximizing range
To find the best value of t_release
, we need a function that takes possible values, runs the simulation, and returns the range.
We can test it.
And run it for a few values.
Now we can use maximize_scalar
to find the optimum.
Finally, we can run the simulation with the optimal value.