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/chap16.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 16
Copyright 2017 Allen Downey
Code from previous notebooks
Using root_bisect
As a simple example, let's find the roots of this function; that is, the values of x
that make the result 0.
modsim.py
provides root_bisect
, which searches for a root by bisection. The first argument is the function whose roots we want. The second argument is an interval that contains a root.
The result is an object that contains the root that was found and other information.
If we provide a different interval, we find a different root.
If the interval doesn't contain a root, the results explain the error.
We want to find the value of r
that makes the final temperature 70, so we define an "error function" that takes r
as a parameter and returns the difference between the final temperature and the goal.
With r=0.01
, we end up a little too warm.
With r=0.02
, we end up too cold.
The return value from root_bisect
is an array with a single element, the estimated value of r
.
If we run the simulation with the estimated value of r
, the final temperature is 70 C, as expected.
Exercise: When you call root_bisect
, it calls error_func1
several times. To see how this works, add a print statement to error_func1
and run root_bisect
again.
Exercise: Repeat this process to estimate r_milk
, given that it starts at 5 C and reaches 20 C after 15 minutes.
Before you use root_bisect
, you might want to try a few values for r_milk
and see how close you can get by trial and error. Here's an initial guess to get you started:
Mixing liquids
The following function takes System
objects that represent two liquids, computes the temperature of the mixture, and returns a new System
object that represents the mixture.
mix
requires the System
objects to have temp
as a system variable. make_system
initializes this variable; the following function makes sure it gets updated when we run a simulation.
Mixing immediately
Next here's what we get if we add the milk immediately.
Mixing at the end
First we'll see what happens if we add the milk at the end. We'll simulate the coffee and the milk separately.
Here's what the results look like.
Here's what happens when we mix them.
The following function takes t_add
, which is the time when the milk is added, and returns the final temperature.
We can try it out with a few values.
And then sweep a range of values for t_add
Here's what the result looks like.
Analysis
Now we can use the analytic result to compute temperature as a function of time. The following function is similar to run_simulation
.
Here's how we run it. From the analysis (see chap16sympy.ipynb
), we have the computed value of r_coffee2
And we can compare to the results from simulation.
They are identical except for a small roundoff error.
Exercises
Exercise: Suppose the coffee shop won't let me take milk in a separate container, but I keep a bottle of milk in the refrigerator at my office. In that case is it better to add the milk at the coffee shop, or wait until I get to the office?
Hint: Think about the simplest way to represent the behavior of a refrigerator in this model. The change you make to test this variation of the problem should be very small!