r - R中deSolve的lsoda函数出错
问题描述
我正在尝试使用 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)
错误:func(time, state, 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
推荐阅读
- javascript - 将图像作为数据标签包含在压缩气泡图 highcharts 中
- javascript - 我需要从函数中返回变量的值以用于货币转换
- python - 用熊猫或其他东西从excel打印整行?
- r - 如何将特定日期格式转换为 POSXct
- javascript - 出于某种原因不让我在 Ember js 中导入模块
- html - 位置设置为绝对的导航栏在滚动时仍在移动?
- android - 为什么我不能在类转换异常后检查实例类型?
- c++ - 如何在 Oracle Linux 上的 Eclipse 中编译和链接 boost 库
- java - Android Studio XML centercrop 不将图像居中
- c# - 如何修复 snmp v3 错误“请求已达到最大重试次数。”?