#########################################
## Example1: Pred-Prey Lotka-volterra model
#########################################
LVmod <- function(Time,State,Pars)
{
with(as.list(c(State,Pars)),
{
Ingestion <- rIng * Prey*Predator
GrowthPrey <- rGrow * Prey*(1-Prey/K)
MortPredator <- rMort * Predator
dPrey <- GrowthPrey - Ingestion
dPredator <- Ingestion*assEff -MortPredator
return(list(c( dPrey, dPredator)))
})
}
pars <- c(rIng =0.2, # /day, rate of ingestion
rGrow =1.0, # /day, growth rate of prey
rMort =0.2 , # /day, mortality rate of predator
assEff =0.5, # -, assimilation efficiency
K =10 ) # mmol/m3, carrying capacity
yini <- c(Prey=1,Predator=2)
times <- seq(0,200,by=1)
out <- as.data.frame(lsoda(func= LVmod, y=yini,
parms=pars, times=times))
matplot(out$time,out[,2:3],type="l",xlab="time",ylab="Conc",
main="Lotka-Volterra",lwd=2)
legend("topright",c("prey", "predator"),col=1:2, lty=1:2)
#########################################
## Example2: Resource-producer-consumer Lotka-volterra model
#########################################
## Note:
## 1. parameter and state variable names made
## accessible via "with" statement
## 2. function sigimp passed as an argument (input) to model
## (see also lsoda and rk examples)
lvmodel <- function(t, x, parms, input) {
with(as.list(c(parms,x)), {
import <- input(t)
dS <- import - b*S*P + g*K #substrate
dP <- c*S*P - d*K*P #producer
dK <- e*P*K - f*K #consumer
res<-c(dS, dP, dK)
list(res)
})
}
## The parameters
parms <- c(b=0.0, c=0.1, d=0.1, e=0.1, f=0.1, g=0.0)
## vector of timesteps
times <- seq(0, 100, length=101)
## external signal with rectangle impulse
signal <- as.data.frame(list(times = times,
import = rep(0,length(times))))
signal$import[signal$times >= 10 & signal$times <=11] <- 0.2
sigimp <- approxfun(signal$times, signal$import, rule=2)
## Start values for steady state
xstart <- c(S=1, P=1, K=1)
## Solve model
out <- as.data.frame(ode(y=xstart,times= times,
func=lvmodel, parms, input =sigimp))
plot(out$P,out$K,type="l",lwd=2,xlab="producer",ylab="consumer")
Run the code above in your browser using DataLab