簡體   English   中英

R中的deSolve的lsoda函數出錯

[英]Error with lsoda function of deSolve in R

我正在嘗試使用deSolve庫的lsoda函數模擬邏輯人口增長模型(dp / dt = k p (1-P / K))。 但是,我一直在定義的參數上出現錯誤:

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)

錯誤:函數(時間,狀態,參數,...)中的錯誤:缺少參數“ K”,沒有默認值

我已經在parms向量中定義了參數K,所以我不知道錯誤從何而來。 這是我第一次使用deSolve。 我試圖在論壇中尋找類似的答案,但沒有成功。 任何幫助是極大的贊賞。

看起來參數變量( parms )可通過with()訪問(請參閱此處 )。 嘗試這個:

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