[R-sig-dyn-mod] deSolve events - different event functions at different times

Faelens Ruben ruben.faelens at altran.com
Fri Jan 25 16:27:54 CET 2013


Dear Tom,

We are using the root finding function of deSolve to fire multiple events at specific times.
Simply add an extra ODE that starts at t0 and has a derivative value of '1'.
You can then use a root function that returns 't - c(1, 2)' to fire an event at t=1 and t=2.

Your event function will look like this:
eventfun <- function(t, x, parms) {
    if(abs(t-1) < absolutePrecision) eventfun1(t, x, parms)
    if(abs(t-2) < absolutePrecision) eventfun2(t, x, parms)
}

It's ugly, but it works! The advantage is that you can add new event times while the solver is running (i.e. from within an event).

@List: I am very open to alternative suggestions that depend less on the precision of the solver though...

Ruben FAELENS 
Consultant
Altran Belgium

-----Original Message-----
From: r-sig-dynamic-models-bounces at r-project.org [mailto:r-sig-dynamic-models-bounces at r-project.org] On Behalf Of Tom.Sumner at lshtm.ac.uk
Sent: vrijdag 25 januari 2013 16:06
To: r-sig-dynamic-models at r-project.org
Subject: [R-sig-dyn-mod] deSolve events - different event functions at different times

Dear list, 

I am using deSolve to solve a set of ODEs and am trying to make use of the events mechanism. Is there a way to apply two different events (defined as functions) at different times?

Using an example from the inside-R documentation to illustrate:

# Derivative function
derive <- function(t, var, parms){
                  list(c(0,-0.5 *var[2])) }

# events
event1 <- function(t,y,parms){
with (as.list(y),{
v1 <- v1 + 1
v2 <- 5 * runif(1)
})
}

event2 <- function(t,y,parms){
with (as.list(y),{
v1 <- v1 + 0.5*y2
v2 <- 5 * runif(1)
})
}

yini <- c(v1  = 1, v2 = 2)
times <- seq(0, 10, by 0.1)

out <- ode(func = derivs, y =yini, times = times, parms = NULL,
  events = list(func = event1, time = 1)

The above would call event1 at time=1. Is there a way to also call event2 at a different time?

I am relatively new to R so apologies if I am missing something obvious.

Many thanks, 

Tom



   

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



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