Skip to content
Back to formatted view

Raw Message

Message-ID: <1371599185.84369.YahooMailClassic@web140401.mail.bf1.yahoo.com>
Date: 2013-06-18T23:46:25Z
From: Andras Farkas
Subject: [R-sig-dyn-mod] deSolve question

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