logisticGrowth <- function(n, r, k) {
# n+(n*r*(1-(n / k)))
n * exp(r * (1 - (n / k)))
}
logisticStochasticGrowth <- function(n, r, k) {
n <- sum(rbinom(n, 1, 0.9))
n * exp(r * (1 - (n / k)))
}
logisticDynamics <- function(n, r, k, steps = 100, stochastic = FALSE) {
ret <- c()
ret[1] <- n
if (length(r) == 1) {
r <- rep(r, steps)
}
for (i in 1:(steps - 1)) {
if (stochastic) {
ret[i + 1] <- logisticStochasticGrowth(ret[i], r[i], k)
} else{
ret[i + 1] <- logisticGrowth(ret[i], r[i], k)
}
}
return(ret)
}
stps <- 100
plot(
x = 1:stps,
y = logisticDynamics(n = 30, r = 0.25, k = 100, steps = stps),
type = 'n',
las = 1,
ylim = c(0, 100),
xlab = 'Time',
ylab = 'Population size',
col = 1
)
x = sapply(rep(30, 30), function(x) {
lines(logisticDynamics(n = x, r = 0.25, k = 100,
steps = stps, stochastic = TRUE),
col = 'firebrick')
})
x = sapply(rep(5, 30), function(x) {
lines(logisticDynamics(n = x, r = 0.25, k = 100,
steps = stps, stochastic = TRUE),
col = 'blue')
})