Lab 10 – Logistics of Zombies

advertisement
Lab 10 – Logistics of Zombies
Date: November 1, 2011
Assignment Due Date: November 8, 2011
Goal: Today we will explore the logistic equation and consider how derivatives relate to discrete time
dynamical systems. You will begin with the skeleton code given in this lab and fill in the empty boxes
with appropriate code to do what is indicated.
Logistic Dynamical System
As you now know the logistic dynamical system can be written as follows.
In this case the carrying capacity has been scaled to 1, meaning that m has to be between 0 and 1. As
you learned in class, the system changes depending on the value of r. You also learned that we can take
derivatives of the updating function to determine the stability of the equilibrium points. Before we
begin coding we need to do a couple calculations by hand. Write down the answers to the following
questions before continuing on with the lab. YOU WILL NEED THEM!
What is the updating function?
What are the equilibrium points?
What is the derivative of the updating function?
Logistic.R
Begin a new script and save it as Logistic.R. You can copy and paste the parts that I have written for you
but remember that you may have to retype the quotation marks or you will get errors.
#Logistic.R
#This code explores the logistic dynamical system and its relationship
#to derivatives. The user must specify r, the number of time steps to
#simulate, and the initial condition. This code will produce a figure
#with three plots: the cobweb plot, the solution plot, and a plot of
#the derivative.
#Define r, the number of time steps, and the initial condition
r = 3.5
t = 100
m0 = .01
#Define the updating function for the logistic.
update =
#Calculate the values of the solution vector for each time step.
#should remember this from labs in Chapter 1.
m = m0
for (i in 1:t){
m[i+1] = update(m[i])
}
You
#Create a vector for the appropriate number of time steps.
time = 0:t
OUT = cbind(time,m)
#cbind makes a matrix with the first column having time steps and the
#second column with the output values
print(OUT) #prints the output values to the console
#Create a figure with three plots side by side
#Cobweb magic... Take a few minutes to think about what this does.
x = NULL
for (j in 1:t){
x[(2*j-1):(2*j)]=m[j]
}
y = NULL
for (k in 1:t-1){
y[(2*k):(2*k+1)]=m[k+1]
}
y[1] = m[1]
y[2*t] = m[t+1]
#Plot the cobweb plot
#plot the cobweb lines in orange
plot(x,y,type='o',ann=FALSE,pch = 20,col="orange")
#plot the one to one line in blue
lines(c(min(m),max(m)),c(min(m),max(m)),col="blue")
#add a title to the first plot
title(main = "Cobweb",xlab="m_t",ylab="m_t+1")
#add a green dot to the starting point of the cobweb
points(m0,m0,col = "green",pch=19,cex=1.5)
#add a red dot to the end point of the cobweb
points(x[2*t],y[2*t], col="red",pch=19,cex=1.5)
#Plot the solution in the second plot
plot(time,m,main="Solution",pch = 20, type="o")
#Calculate the derivative of the updating function
#Define a new input vector for out calculation of the derivative which
#begins at 0 and goes to 1 since m must be between 0 and 1 as
#specified before.
#Define function for the exact derivative of the updating function.
#Plot the derivative from 0 to 1 and add a title to the plot,
#“Derivative”
#Print the values of the equilibrium points along with the value of
#the derivative at these points to the console.
#Add purple dots to your plot which represent the values of the
derivative at the equilibrium points.
points(
),col="purple",pch=19,cex=1.5)
#Add horizontal lines at 1 and -1, remember the trick of defining a
#function in R to plot horizontal lines.
Using Logistic.R
Once your code is running properly we can now see what happens for different values of r. Imagining
that m represents the fraction of zombies in our population and that r is the growth rate of the zombie
population on earth let’s see the outcomes for different values of r. Set r to .5 and see what happens
when you run your code.
How many equilibrium points are there? What are they?
Are they stable or unstable? Are there oscillations in the population of zombies? Use the derivative plot
to answer.
What is the final outcome for the fraction of zombies which populate the earth?
Compare this behavior to the system when r = 3.5 and answer the same questions. What happens when
r = 3.7? This is an example of chaos, and not just because zombies are attacking!
Related documents
Download