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/chap21.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 21
Copyright 2017 Allen Downey
With air resistance
Next we'll add air resistance using the drag equation
I'll start by getting the units we'll need from Pint.
Now I'll create a Params
object to contain the quantities we need. Using a Params object is convenient for grouping the system parameters in a way that's easy to read (and double-check).
Now we can pass the Params
object make_system
which computes some additional parameters and defines init
.
make_system
uses the given radius to compute area
and the given v_term
to compute the drag coefficient C_d
.
Let's make a System
Here's the slope function, including acceleration due to gravity and drag.
As always, let's test the slope function with the initial conditions.
We can use the same event function as in the previous chapter.
And then run the simulation.
Here are the results.
The final height is close to 0, as expected.
Interestingly, the final velocity is not exactly terminal velocity, which suggests that there are some numerical errors.
We can get the flight time from results
.
Here's the plot of position as a function of time.
And velocity as a function of time:
From an initial velocity of 0, the penny accelerates downward until it reaches terminal velocity; after that, velocity is constant.
Exercise: Run the simulation with an initial velocity, downward, that exceeds the penny's terminal velocity. Hint: You can create a new Params
object based on an existing one, like this:
params2 = Params(params, v_init=-30 * m/s)
What do you expect to happen? Plot velocity and position as a function of time, and see if they are consistent with your prediction.
Exercise: Suppose we drop a quarter from the Empire State Building and find that its flight time is 19.1 seconds. Use this measurement to estimate the terminal velocity.
You can get the relevant dimensions of a quarter from https://en.wikipedia.org/wiki/Quarter_(United_States_coin).
Create a
Params
object with the system parameters. We don't knowv_term
, so we'll start with the inital guessv_term = 18 * m / s
.Use
make_system
to create aSystem
object.Call
run_ode_solver
to simulate the system. How does the flight time of the simulation compare to the measurement?Try a few different values of
t_term
and see if you can get the simulated flight time close to 19.1 seconds.Optionally, write an error function and use
root_scalar
to improve your estimate.Use your best estimate of
v_term
to computeC_d
.
Note: I fabricated the observed flight time, so don't take the results of this exercise too seriously.