用lsoda、R语言定义结果阈值
我对R中deSolve包中的lsoda有一个问题(它可能也适用于ode函数)。我正在用一组ODE来模拟食物网的动态,计算两个相同食物网中5个物种的丰度,这两个食物网通过扩散连接在一起。 丰度以2000个时间步长计算,不应为负值或小于1e-6。在这种情况下,结果应更改为0。我找不到lsoda将负结果变为零的任何参数。我在ODE函数中尝试了以下技巧:用lsoda、R语言定义结果阈值,r,ode,differential-equations,R,Ode,Differential Equations,我对R中deSolve包中的lsoda有一个问题(它可能也适用于ode函数)。我正在用一组ODE来模拟食物网的动态,计算两个相同食物网中5个物种的丰度,这两个食物网通过扩散连接在一起。 丰度以2000个时间步长计算,不应为负值或小于1e-6。在这种情况下,结果应更改为0。我找不到lsoda将负结果变为零的任何参数。我在ODE函数中尝试了以下技巧: 你的全部代码是什么?我刚刚编辑了这个问题。添加了ODE函数和ODE公式的代码。谢谢你的全部代码是什么?我刚刚编辑了这个问题。添加了ODE函数和ODE公
你的全部代码是什么?我刚刚编辑了这个问题。添加了ODE函数和ODE公式的代码。谢谢你的全部代码是什么?我刚刚编辑了这个问题。添加了ODE函数和ODE公式的代码。谢谢
solve.model <- function(t,y, parms){
y <- ifelse(y<1e-6, 0, y)
#ODE functions here
#...
#...
return(list(dy))
}
solve.model <- function(t, y, parms){
y <- ifelse(y<1e-6, 0, y)
with(parms,{
# return from vector form into matrix form for calculations
(R <- as.matrix(y[(max(no.species)*length(no.species)+1):length(y)]))
(N <- matrix(y[1:(max(no.species)*length(no.species))], ncol=length(no.species)))
dy1 <- matrix(nrow=max(no.species), ncol=length(no.species))
dy2 <- matrix(nrow=length(no.species), ncol=1)
for (i in 1:no.webs){
species <- no.species[i]
(abundance <- N[1:species,i])
adj <- as.matrix(webs[[i]])
a.temp <- a[1:species, 1:species]*adj
b.temp <- b[1:species, 1:species]*adj
h.temp <- h[1:species, 1:species]*adj
#Calculating sigmas in denominator of Holing type II functional response
(sum.over.preys <- abundance%*%(a.temp*h.temp))
(sum.over.predators <- (a.temp*h.temp)%*%abundance)
#Calculating growth of basal
(basal.growth <- basals[,i]*N[,i]*(mu*R[i]/(K+R[i])-m))
# Calculating growth for non-basal species
no.basal <- rep(1,len=species)-basals[1:species]
predator.growth<- rep(0, max(no.species))
(predator.growth[1:species] <- ((abundance%*%(a.temp*b.temp))/(1+sum.over.preys)-m*no.basal)*abundance)
predation <- rep(0, max(no.species))
(predation[1:species] <- (((a.temp*b.temp)%*%abundance)/t(1+sum.over.preys))*abundance)
(pop <- basal.growth + predator.growth - predation)
dy1[,i] <- pop
dy2[i] <- 0.0005 #Change in the resource
}
#Calculating dispersals .they can be easily replaced
# by adjacency maps of connections between food webs arbitrarily!
# added to solve the problem of negative abundances
deltas <- append(c(dy1), dy2)
return(list(append(c(dy1),dy2)))
})
}
temp.abund[[j]] <- lsoda(y=initials, func=solve.model, times=0:max.time, parms=parms)