简体   繁体   English

解散微分方程不起作用

[英]desolve differential equations not working

I've written some code that has a logistic growth component (ie as N approaches the 'carrying capacity' it grows at a slower rate, until when it reaches the 'carrying capacity' it stops growing). 我编写了一些具有逻辑增长成分的代码(即,当N接近“承载能力”时,它以较慢的速度增长,直到达到“承载能力”时它才停止增长)。 However, when I run it in R it doesn't seem to be working. 但是,当我在R中运行它似乎无法正常工作。 Some populations end up being larger than the carrying capacity. 一些人口最终超出了承载能力。 I've looked at the maths and its all OK. 我看过数学,一切都很好。 So I think that the problem is that dN/dt is only being calculated once for each population. 因此,我认为问题在于,每个人口的dN / dt仅计算一次。 Does anyone know how to fix this problem? 有谁知道如何解决这个问题?

Any help would be greatly appreciated! 任何帮助将不胜感激!

Example code below: 下面的示例代码:

library('optimbase')
library('deSolve')
library('tidyverse')
K = 150000                 #carrying capacity         
deaths = 0.2567534        #death rate        
treesize = 0.23523        #resource size        
K_mat = K*ones(10, 1)  #matrix of Ks         
death_mat = deaths*ones(10, 1) #matrix of deathrates
tree_mat = treesize*ones(11, 1) #matrix of resources 
for_mat <- matrix(rbinom(11 * 10, 1, 0.2), ncol = 11, nrow = 10) #connection 
 #matrix of foraging
parameters <- c(for_mat, tree_mat, death_mat, K_mat) #outline parameters
N <- runif(10,0,K) 
state <- N #starting state

nestchange <- function(t, state, parameters){
      with(as.list(c(state, parameters)),{
    r = for_mat %*% tree_mat - death_mat 
    dNdt = r*N  - r*N*(N/K_mat)
    list(c(dNdt))
  })
}
times <- seq(0,100)

out <- ode (y  = state, 
            times = times, 
            func = nestchange, 
            parms = parameters)

results <- as.data.frame(out)
results <- gather(results, 'nest', 'N', 2:11) 

ggplot(data=results,
       aes(x=time, y=N, colour=nest)) +
  geom_line() +
  theme_bw()

Should your function actually be, 如果您的职能实际上是

nestchange <- function(t, state, parameters){
      with(as.list(c(state, parameters)),{
    r <- for_mat %*% tree_mat - death_mat 
    dNdt <- r*state  - r*state*(state/K_mat)
    list(c(dNdt))
  })
}

as the ODE solver is passing state to the function at each time step, yet the function is using the variable N for the calculations instead which isn't updated by the ODE solver. 因为ODE求解器在每个时间步都将state传递给函数,但是函数使用变量N进行计算,而不是由ODE求解器更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM