0

I would like to terminate ODE solver when the dependent variable exceeded certain value. Consider a simple ODE model

library("deSolve")
dModel<- function(t, y, parms) {list(c(y))}
out<-ode(c(1),seq(0,100,1),dModel, parms=NULL)

I want to stop solver when y>100 and display output. I know there is an event of ODE but after reading documentations, I did not understand how to implement in my situation. Could you please provide solution to this problem?

bell
  • 191
  • 1
  • 1
  • 11

1 Answers1

1

I got an answer to my problem, here is a solution:

library("deSolve")
dModel<- function(t, y, parms) {list(c(y))}
rootfun <- function (t, y, parms) { return(y - 100) }
sol<-ode(c(1),seq(0,100,1),dModel, parms=NULL,rootfun = rootfun, method="lsodar")

Here is the output:

     time      value
1 0.000000   1.000000
2 1.000000   2.718290
3 2.000000   7.389087
4 3.000000  20.085627
5 4.000000  54.598440
6 4.605164 100.000000

I can say after t>4.605164, y would be greater than 100.

bell
  • 191
  • 1
  • 1
  • 11