[R] deSolve question

Andras Farkas motyocska at yahoo.com
Wed Jun 19 01:46:25 CEST 2013



Dear All

wonder if you could provide some insights on the following: currently I have this code which produces the expected results:

require(deSolve)
pars <- list(k = 0.08,v=15)
intimes <- c(0,0.5,12)
input   <- c(800,0,0)
forc <- approxfun(intimes, input, method="constant", rule=2)

derivs <- function(t, state, pars) {
  inp <- forc(t)
  dy1 <- - pars$k * state[1] + inp/pars$v
  return(list(c(dy1)))
}

model <- function(pars, times=seq(0, 13, by = 0.01)) {
  state <- c(y = 0)
  return(ode(y = state, times = times, func = derivs, parms = pars,
             method="lsoda"))
}

plot(model(pars))

intuitively it would make sense to me that if I change the derivs as:

derivs <- function(t, state, pars) {
  inp <- forc(t)
  #note the added ()
  dy1 <- (- pars$k * state[1] + inp)/pars$v
  return(list(c(dy1)))
}

than I would get the same results, but that is not the case. I need to "relocate" pars$v to another position within derivs so that I could implement it in a forcing function to be able to change its value during derivation. Appreciate your help,

Andras



More information about the R-help mailing list