[R-sig-dyn-mod] changing state variable in simulations

Andras Farkas motyocska at yahoo.com
Fri Sep 13 19:34:18 CEST 2013


 Dear All,
 
have this simple model:
 
require(deSolve)
plist <-matrix(c(0.08,0.07,0.09,15,20,25),ncol=2 )
colnames(plist)=c("K","V")
derivs <- function(t, state, pars) {
  with(as.list(pars), {
    intimes <- c(0,0.5,12)
    input   <- c(1200,0,0)
    forc <- approxfun(intimes, input, method="constant",rule=2)
    inp <- forc(t)
    dy1 <- inp/V - K * state[1] 
    return(list(c(dy1)))
  })

}
model <- function(pars, times=seq(0, 13, by = 0.05)) {
  state <- c(a = 5 )
  return(ode(y = state, times = times, func = derivs, parms = plist[i,],method="lsoda",rtol = 1e-8,atol =1e-8,hmax=0.05))
}
modelout <- list()
for (i in 1:nrow(plist))
  modelout[[i]] <- model(plist)

out <-data.frame(modelout)
time <-out$time
out <-out[, grepl("^a", colnames(out))]
plot(time,out[,3],type="l")

 
Please provide some insights for the following: I would like to change the initial state variable during simulations for each time we move down one row in plist, ie: just like the parameters K and V change from row to row, I would also like the initial state variable to change by "putting" the initial state variables into plist like this:
 
 plist <-matrix(c(0.08,0.07,0.09,15,20,25,3,5,6),ncol=3 )
colnames(plist)=c("K","V","state")

I would greatly appreciate your thoughts on how to incorporate this into the above model,
 
thanks,
 
Andras 



More information about the R-sig-dynamic-models mailing list