我出于说明目的从文档中提取了此示例。在以下示例中,当y的值达到0.1时,将添加一个随机值。如果y值大于0.8,我想终止求解器。
一种可能的解决方案是在eventfun中生成随机值,以使y始终小于0.8。还有其他解决方案可以终止求解器?这对我的复杂模型很有帮助。
## =======================================================================
## Example 3:
## using lsodar to trigger an event
## =======================================================================
## a state variable is decaying at a first-order rate.
## when it reaches the value 0.1, a random amount is added.
library("deSolve")
derivfun <- function (t,y,parms)
list (-0.05 * y)
rootfun <- function (t,y,parms)
return(y - 0.1)
eventfun <- function(t,y,parms)
return(y + runif(1))
yini <- 0.5
times <- 0:400
out <- lsodar(func=derivfun, y = yini, times=times,
rootfunc = rootfun, events = list(func=eventfun, root = TRUE))
plot(out, type = "l", lwd = 2, main = "lsodar with event")
# }