简体   繁体   English

R中的deSolve的lsoda函数出错

[英]Error with lsoda function of deSolve in R

I'm trying to simulate the logistic population growth model (dp/dt = k p (1-P/K)) using the lsoda function of deSolve library. 我正在尝试使用deSolve库的lsoda函数模拟逻辑人口增长模型(dp / dt = k p (1-P / K))。 However, I keep getting an error on the defined parameters: 但是,我一直在定义的参数上出现错误:

tiempo <- seq(0,10,0.5) #define time interval
ic2 <- 1 #define initial population density
parms <- c(K=100, k=2) #define the parameters of the model

log.gr <- function(t,x,k,K){ #define function.

x1 <- k * x[1] * (1-(x[1]/K))
list(c(x1))
}

log.gr.out <- lsoda(ic2, tiempo,log.gr,parms)

Error: Error in func(time, state, parms, ...) : argument "K" is missing, with no default 错误:函数(时间,状态,参数,...)中的错误:缺少参数“ K”,没有默认值

I already defined parameter K in the parms vector, so I don't know where the error is comming from. 我已经在parms向量中定义了参数K,所以我不知道错误从何而来。 This is my first time using deSolve. 这是我第一次使用deSolve。 I tried to look for a similar answer in the forums, but I wasn't successful. 我试图在论坛中寻找类似的答案,但没有成功。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

It looks like the parameter variable ( parms ) is accessible via with() (see here ). 看起来参数变量( parms )可通过with()访问(请参阅此处 )。 Try this: 尝试这个:

tiempo <- seq(0, 10, 0.5) #define time interval
ic2 <- 1 #define initial population density
parms <- c(K = 100, k=2) #define the parameters of the model
log.gr <- function(t, x, parms) {
        with(as.list(c(parms, x)), {
                x1 <- k * x[1] * (1-(x[1]/K))
                list(c(x1))
        })}
log.gr.out <- lsoda(ic2, tiempo,log.gr,parms)
log.gr.out # output
   time         1
1   0.0  1.000000
2   0.5  2.672371
3   1.0  6.945310
4   1.5 16.866424
5   2.0 35.546072
6   2.5 59.985918
7   3.0 80.295546
8   3.5 91.719949
9   4.0 96.785724
10  4.5 98.793065
11  5.0 99.552603
12  5.5 99.834928
13  6.0 99.939218
14  6.5 99.977638
15  7.0 99.991767
16  7.5 99.996957
17  8.0 99.998889
18  8.5 99.999599
19  9.0 99.999844
20  9.5 99.999940
21 10.0 99.999977

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

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