Manojit Roy wrote:> Hello,
>
> I am an R newbie, trying to use lsoda to solve standard Lotka-Volterra
> competition equations. My question is: how do I pass a parameter that
> varies with time, like say, phix <- 0.7 + runif(tmax) in the example
below.
Hello,
the simplest and most pragmatic way to implement such non-autonomous
systems is to define a global variable, e.g. phix.t and to refer to it
in the model function via its index (see below) or via approx. If you
want a more sophisticated solution, don't hesitate to ask me again.
Hope it helps
Thomas P.
lotvol <- function(t,n,p){
x <- n[1]; y <- n[2]
rx <- p["rx"]; ry <- p["ry"]
Kx <- p["Kx"]; Ky <- p["Ky"]
phix <- phix.t[floor(t+1)]
phiy <- p["phiy"]
dx.dt <- rx*x*(1 - x/Kx) - phix*x*y
dy.dt <- ry*y*(1 - y/Ky) - phiy*x*y
list(c(dx.dt, dy.dt))
}
# running lsoda
nstart <- c(x=0.5, y=0.5)
parms <- c(rx=1, ry=1, Kx=1, Ky=1, phix=1.2, phiy=0.8)
tmax <- 100
times <- seq(0, tmax)
phix.t <- 0.7 + runif(tmax)
require(odesolve)
out <- as.data.frame(lsoda(nstart, times, lotvol, parms))