## A Predator-Prey model with 4 species in matrix formulation
LVmatrix <- function(t, n, parms) {
with(parms, {
dn <- r * n + n * (A %*% n)
return(list(c(dn)))
})
}
parms <- list(
r = c(r1 = 0.1, r2 = 0.1, r3 = -0.1, r4 = -0.1),
A = matrix(c(0.0, 0.0, -0.2, 0.01, # prey 1
0.0, 0.0, 0.02, -0.1, # prey 2
0.2, 0.02, 0.0, 0.0, # predator 1; prefers prey 1
0.01, 0.1, 0.0, 0.0), # predator 2; prefers prey 2
nrow = 4, ncol = 4, byrow=TRUE)
)
times <- seq(from = 0, to = 500, by = 0.1)
y <- c(prey1 = 1, prey2 = 1, pred1 = 2, pred2 = 2)
out <- ode(y, times, LVmatrix, parms)
## Basic line plot
plot(out, type = "l")
## User-specified axis labels
plot(out, type = "l", ylab = c("Prey 1", "Prey 2", "Pred 1", "Pred 2"),
xlab = "Time (d)", main = "Time Series")
hist(out, col="darkblue", breaks = 50)
Run the code above in your browser using DataLab