Skip to content

Instantly share code, notes, and snippets.

@tmastny
Created August 20, 2020 01:42
Show Gist options
  • Save tmastny/8507052d17ec41d78b0abfc698536f1f to your computer and use it in GitHub Desktop.
Save tmastny/8507052d17ec41d78b0abfc698536f1f to your computer and use it in GitHub Desktop.
# source: https://www.r-bloggers.com/lotka-volterra-model%C2%A0%C2%A0intro/
library(deSolve)
LotVmod <- function (Time, State, Pars) {
with(as.list(c(State, Pars)), {
dx = x * (alpha - beta * y)
dy = -y * (gamma - delta * x)
return(list(c(dx, dy)))
})
}
Pars <- c(alpha = 2, beta = .5, gamma = .2, delta = .6)
State <- c(x = 10, y = 10)
Time <- seq(0, 100, by = 1)
out <- as.data.frame(ode(func = LotVmod, y = State, parms = Pars, times = Time))
matplot(out[,-1], type = "l", xlab = "time", ylab = "population")
legend(
"topright",
c("Cute bunnies", "Rabid foxes"),
lty = c(1,2), col = c(1,2), box.lwd = 0
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment