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/chap20.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 20
Copyright 2017 Allen Downey
Dropping pennies
I'll start by getting the units we need from Pint.
And defining the initial state.
Acceleration due to gravity is about 9.8 m / s.
I'll start with a duration of 10 seconds and step size 0.1 second.
Now we make a System
object.
And define the slope function.
It's always a good idea to test the slope function with the initial conditions.
Now we're ready to call run_ode_solver
Here are the results:
And here's position as a function of time:
Onto the sidewalk
To figure out when the penny hit the sidewalk, we can use crossings
, which finds the times where a Series
passes through a given value.
For this example there should be just one crossing, the time when the penny hits the sidewalk.
We can compare that to the exact result. Without air resistance, we have
and
Setting and solving for yields
The estimate is accurate to about 9 decimal places.
Events
Instead of running the simulation until the penny goes through the sidewalk, it would be better to detect the point where the penny hits the sidewalk and stop. run_ode_solver
provides exactly the tool we need, event functions.
Here's an event function that returns the height of the penny above the sidewalk:
And here's how we pass it to run_ode_solver
. The solver should run until the event function returns 0, and then terminate.
The message from the solver indicates the solver stopped because the event we wanted to detect happened.
Here are the results:
With the events
option, the solver returns the actual time steps it computed, which are not necessarily equally spaced.
The last time step is when the event occurred:
The result is accurate to about 4 decimal places.
We can also check the velocity of the penny when it hits the sidewalk:
And convert to kilometers per hour.
If there were no air resistance, the penny would hit the sidewalk (or someone's head) at more than 300 km/h.
So it's a good thing there is air resistance.
Under the hood
Here is the source code for crossings
so you can see what's happening under the hood:
Exercises
Exercise: Here's a question from the web site Ask an Astronomer:
"If the Earth suddenly stopped orbiting the Sun, I know eventually it would be pulled in by the Sun's gravity and hit it. How long would it take the Earth to hit the Sun? I imagine it would go slowly at first and then pick up speed."
Use run_ode_solver
to answer this question.
Here are some suggestions about how to proceed:
Look up the Law of Universal Gravitation and any constants you need. I suggest you work entirely in SI units: meters, kilograms, and Newtons.
When the distance between the Earth and the Sun gets small, this system behaves badly, so you should use an event function to stop when the surface of Earth reaches the surface of the Sun.
Express your answer in days, and plot the results as millions of kilometers versus days.
If you read the reply by Dave Rothstein, you will see other ways to solve the problem, and a good discussion of the modeling decisions behind them.
You might also be interested to know that it's actually not that easy to get to the Sun.