在lsoda,R语言中定义结果的阈值

时间:2015-08-28 20:02:31

标签: r ode differential-equations

我在R中的deSolve包中遇到lsoda问题(它也可能适用于ode函数)。我使用一组ODE计算食物网的动态,计算两个相同食物网中5种物种的丰度,这些食物网通过扩散连接。 丰度是按2000个时间步长计算的,它们不应该是负数或小于1e-6。在这种情况下,结果应该更改为0.我找不到任何lsoda参数将负结果变为零。我在ODE函数中尝试了以下技巧:     solve.model< - function(t,y,parms){

solve.model <- function(t,y, parms){

    y <- ifelse(y<1e-6, 0, y)
    #ODE functions here
    #...
    #...
    return(list(dy))
}

但它似乎无法正常工作。以下是网中物种丰富度的样本。 abundances of the 5 species in 2000 time steps

我将非常感谢您的帮助,并希望示例代码可以提供有关我的问题的足够信息。 巴巴克

P.S。我正在为物种丰度(前两个方程)和资源变化(第三个方程)解决以下ODE集 enter image description here

该功能的相应代码如下

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)))    
    })
}

lsoda通过以下调用使用此函数:

temp.abund[[j]] <- lsoda(y=initials, func=solve.model, times=0:max.time, parms=parms)

0 个答案:

没有答案