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/rabbits3.ipynb
Views: 531
Modeling and Simulation in Python
Rabbit example
Copyright 2017 Allen Downey
Rabbit is Rich
This notebook starts with a version of the rabbit population growth model. You will modify it using some of the tools in Chapter 5. Before you attempt this diagnostic, you should have a good understanding of State objects, as presented in Section 5.4. And you should understand the version of run_simulation
in Section 5.7.
Separating the State
from the System
Here's the System
object from the previous diagnostic. Notice that it includes system parameters, which don't change while the simulation is running, and population variables, which do. We're going to improve that by pulling the population variables into a State
object.
In the following cells, define a State
object named init
that contains two state variables, juveniles
and adults
, with initial values 0
and 10
. Make a version of the System
object that does NOT contain juvenile_pop0
and adult_pop0
, but DOES contain init
.
Updating run_simulation
Here's the version of run_simulation
from last time:
In the cell below, write a version of run_simulation
that works with the new System
object (the one that contains a State
object named init
).
Hint: you only have to change two lines.
Test your changes in run_simulation
:
Plotting the results
Here's a version of plot_results
that plots both the adult and juvenile TimeSeries
.
If your changes in the previous section were successful, you should be able to run this new version of plot_results
.
That's the end of the diagnostic. If you were able to get it done quickly, and you would like a challenge, here are two bonus questions:
Bonus question #1
Write a version of run_simulation
that puts the results into a single TimeFrame
named results
, rather than two TimeSeries
objects.
Write a version of plot_results
that can plot the results in this form.
WARNING: This question is substantially harder, and requires you to have a good understanding of everything in Chapter 5. We don't expect most people to be able to do this exercise at this point.
Bonus question #2
Factor out the update function.
Write a function called
update
that takes aState
object and aSystem
object and returns a newState
object that represents the state of the system after one time step.Write a version of
run_simulation
that takes an update function as a parameter and uses it to compute the update.Run your new version of
run_simulation
and plot the results.
WARNING: This question is substantially harder, and requires you to have a good understanding of everything in Chapter 5. We don't expect most people to be able to do this exercise at this point.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-5d0666d579e1> in <module>()
----> 1 run_simulation(system, update)
NameError: name 'update' is not defined