Hi Drew,
Since there is a closed-form solution to the logistic model, it's easy, you
can just code the solution into a function:
logistic <- function(times, r, K, Y0){
numer = K * Y0 * exp(r * times)
denom = K + Y0 * (exp(r * times) - 1)
return(numer / denom)
}
times = seq(from = 0, to = 100, by = 1)
r = 0.4
K = 1000
Y0 = 1
plot(times, logistic(times, r, K, Y0), type = "l")
If, however, you want them to do numerical simulation, then the deSolve
package (which you've been reading about) is the way to go:
install.packages('deSolve')
library(deSolve)
logistic_de <- function(times, y, parms){
dN = y * parms[1] * (1 - y / parms[2])
return(list(dN))
}
y0 = 1
parms = c(0.4, 1000)
times = seq(from = 0, to = 100, by = 1)
out = ode(y = y0, times = times, func = logistic_de, parms = parms)
plot(out)
Note that there are better (i.e. easier to read) ways to code the function
for the logistic for deSolve numerical solving, but this is the easiest.
Jeremy
On Thu, Feb 23, 2017 at 1:35 PM, Drew Tyre <atyre2 at unl.edu> wrote:
Hi all,
I'm teaching a population dynamics course for upper level undergraduates.
I am using R for the first time (first time using it in this class, not my
first time!). So I needed to make a small function to simulate a logistic
population model, and it occurred to me that other's may have already
solved this problem in a better way. I'm looking for pointers to other
implementations, and/or feedback on how I've set it up. A bit of
description of my goals and the function are at
http://atyre2.github.io/2017/02/22/whats-the-best-logistic-model.html
I have looked at Karline Soetaert and Peter Herman's book A practical
guide to ecological modelling, as well as Henry Steven's book A primer of
Ecology with R, and discuss their approaches at the above blog post.
Thank you for your time and attention!
--
Drew Tyre
School of Natural Resources
University of Nebraska-Lincoln
416 Hardin Hall, East Campus
3310 Holdrege Street
Lincoln, NE 68583-0974
phone: +1 402 472 4054
fax: +1 402 472 2946
email: atyre2 at unl.edu
http://snr.unl.edu/tyre
http://atyre2.github.io
ORCID: orcid.org/0000-0001-9736-641X