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/chap07.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 7
Copyright 2017 Allen Downey
Code from the previous chapter
Quadratic growth
Here's the implementation of the quadratic growth model.
Here's a System
object with the parameters alpha
and beta
:
And here are the results.
Exercise: Can you find values for the parameters that make the model fit better?
Equilibrium
To understand the quadratic model better, let's plot net growth as a function of population.
Here's what it looks like.
Here's what it looks like. Remember that the x axis is population now, not time.
It looks like the growth rate passes through 0 when the population is a little less than 14 billion.
In the book we found that the net growth is 0 when the population is :
This is the equilibrium the population tends toward.
sns
is a library called Seaborn which provides functions that control the appearance of plots. In this case I want a grid to make it easier to estimate the population where the growth rate crosses through 0.
Dysfunctions
When people first learn about functions, there are a few things they often find confusing. In this section I present and explain some common problems with functions.
As an example, suppose you want a function that takes a System
object, with variables alpha
and beta
, as a parameter and computes the carrying capacity, -alpha/beta
. Here's a good solution:
Now let's see all the ways that can go wrong.
Dysfunction #1: Not using parameters. In the following version, the function doesn't take any parameters; when sys1
appears inside the function, it refers to the object we created outside the function.
This version actually works, but it is not as versatile as it could be. If there are several System
objects, this function can only work with one of them, and only if it is named system
.
Dysfunction #2: Clobbering the parameters. When people first learn about parameters, they often write functions like this:
In this example, we have a System
object named sys1
that gets passed as an argument to carrying_capacity
. But when the function runs, it ignores the argument and immediately replaces it with a new System
object. As a result, this function always returns the same value, no matter what argument is passed.
When you write a function, you generally don't know what the values of the parameters will be. Your job is to write a function that works for any valid values. If you assign your own values to the parameters, you defeat the whole purpose of functions.
Dysfunction #3: No return value. Here's a version that computes the value of K
but doesn't return it.
A function that doesn't have a return statement always returns a special value called None
, so in this example the value of pop
is None
. If you are debugging a program and find that the value of a variable is None
when it shouldn't be, a function without a return statement is a likely cause.
Dysfunction #4: Ignoring the return value. Finally, here's a version where the function is correct, but the way it's used is not.
In this example, carrying_capacity
runs and returns K
, but the return value is dropped.
When you call a function that returns a value, you should do something with the result. Often you assign it to a variable, as in the previous examples, but you can also use it as part of an expression.
For example, you could eliminate the temporary variable pop
like this:
Or if you had more than one system, you could compute the total carrying capacity like this:
Exercises
Exercise: In the book, I present a different way to parameterize the quadratic model:
where and . Write a version of update_func
that implements this version of the model. Test it by computing the values of r
and K
that correspond to alpha=0.025, beta=-0.0018
, and confirm that you get the same results.