<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 9.00.8112.16457"></HEAD>
<BODY style="MARGIN: 4px 4px 1px; FONT: 10pt Segoe UI">
<DIV>Thanks both for the suggestions. The root based method does exactly what I need.</DIV>
<DIV> </DIV>
<DIV>Thanks again</DIV>
<DIV> </DIV>
<DIV>Tom<BR><BR>>>> Thomas Petzoldt <Thomas.Petzoldt@tu-dresden.de> 25/01/2013 17:33 >>><BR>On 25.01.2013 16:27, Faelens Ruben wrote:<BR><BR>> @List: I am very open to alternative suggestions that depend less on<BR>> the precision of the solver though...<BR><BR>Hi,<BR><BR>what about creating a dispatch function that decides between the two (or<BR>more) events?<BR><BR>Have fun!<BR><BR>Thomas P.<BR><BR>##-- two events ---------------------------------------<BR><BR>library("deSolve")<BR><BR>## Derivative function<BR>derivs <- function(t, v, parms) list(c(0, -0.5 * v[2]))<BR><BR>## events<BR>event1 <- function(t, y, parms){<BR>   cat("event1 at t=", t, "\n")<BR>   with (as.list(y), {<BR>     v1 <- v1 + 1<BR>     v2 <- 5 * runif(1)<BR>     return(c(v1, v2))<BR>   })<BR>}<BR><BR>event2 <- function(t, y, parms){<BR>   cat("event2 at t=", t, "\n")<BR>   with (as.list(y), {<BR>     v1 <- v1 + 0.5 * v2<BR>     v2 <- 5 * runif(1)<BR>     return(c(v1, v2))<BR>   })<BR>}<BR><BR>etimes1 <- c(1, 3, 4, 5)<BR>etimes2 <- c(2, 4, 6)<BR>allevents <- sort(unique(c(etimes1, etimes2)))<BR><BR>dispatch <- function(t, y, parms) {<BR>   ## important! conflict resolution<BR>   ##   what if 2 events occur at the same time?<BR>   ## here: use 2nd event<BR>   ##   alternatives: add, average, randomize, ...<BR>   ret <- y<BR>   if (t %in% etimes1) ret <- event1(t, y, parms)<BR>   if (t %in% etimes2) ret <- event2(t, y, parms)<BR>   return(ret)<BR>}<BR><BR>out <- ode(func = derivs,<BR>   y = c(v1  = 1, v2 = 2),<BR>   times = seq(0, 10, by=0.1),<BR>   parms = NULL,<BR>   events = list(func = dispatch, time = allevents))<BR><BR>plot(out)<BR>abline(v=etimes1, col="blue", lty="dashed")<BR>abline(v=etimes2, col="red", lty="dotted", lwd=2)<BR><BR>_______________________________________________<BR>R-sig-dynamic-models mailing list<BR>R-sig-dynamic-models@r-project.org<BR><A href="https://stat.ethz.ch/mailman/listinfo/r-sig-dynamic-models">https://stat.ethz.ch/mailman/listinfo/r-sig-dynamic-models</A><BR></DIV></BODY></HTML>