[R] using and event in deSolve

Thomas Petzoldt thpe at simecol.de
Sat Feb 23 19:50:09 CET 2013


Hi Jannetta,

as far as I can see, your implementation was almost ;-) correct, except 
that:

init[2] <- init[1] + d

should be:

init[2] <- init[2] + d



The root function with:

return(init[1]-30)

is correct, because this triggers an event when init[1]-30 crosses the 
zero line, i.e. when init[1] == 30. This can also be seen at the figure 
if we decrease the external step size, see the corrected example below.

Note also, that ";" at the end of line is not required (and should be 
avoided), because R is not C.

You can also send such questions to the special interest group:

r-sig-dynamic-models at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-dynamic-models


Thanks for using deSolve

Thomas



-------------- next part --------------
library(deSolve)

Izhikevich <- function(time, init, parms) {
  with(as.list(c(init, parms)), {
    dv <- 0.04 * v^2 + 5 * v + 140 - u + I
    du <- a * (b * v - u)
    list(c(dv, du))
  })
}

parms <- c(a = 0.02, b = 0.2, c = -65, d = 2, I = 10)
#times <- seq(from = 1, to = 1000, by = 0.1)
times <- seq(from = 1, to = 100, by = 0.005)
init  <- c(v = -65, u = 0.2)

root <- function(time, init, parms) {
  return(v = init[1] - 30)
}

event <- function(time, init, parms) {
  with(as.list(parms), {
    init[2] <- init[2] + d
    init[1] <- c
    return(init)
  })
}

out <- ode(y = init, times = times,
          func = Izhikevich, parms = parms,
          events = list(func = event, root = TRUE),
          rootfun = root)

plot(out)


More information about the R-help mailing list