简体   繁体   English

R:解溶剂包装中的初始条件错误

[英]R: initial condition error in deSolve-package

I just started working with deSolve. 我刚刚开始与deSolve合作。 For some reason a simple example code gives me this error message: 由于某种原因,一个简单的示例代码给了我这个错误消息:

Error in checkFunc(Func2, times, y, rho) : The number of derivatives returned by func() (1) must equal the length of the initial conditions vector (4) checkFunc(Func2,times,y,rho)中的错误:func()返回的导数的数量(1)必须等于初始条件向量的长度(4)

I use the following example code: 我使用以下示例代码:

library(deSolve)
model <- function (time, y, parms) {
  with(as.list(c(y, parms)), {
    dY1 <- -k1*y1*y2 + k2*y3
    dY2 <- k2 * y3 - k1*y1*y2
    dY3 <- -k2*y3-k3*y3+k1*y1*y2
    dY4 <- k3*y3
    list(dY1,dY2,dY3,dY4)
  })
}
yini <- c(y1 = 1,y2=1,y3=0,y4=0)
parms <- c(k1=0.1,k2=0.1,k3=0.1)
times <- seq(0, 100, 1)
out <- ode(y=yini, times=times, func=model, parms=parms)
plot(out)

As you can see I have exactly 4 derivatives and 4 initial conditions defined in yini. 如您所见,我在yini中定义了4个导数和4个初始条件。 Therefore, I cannot interpret this error. 因此,我无法解释此错误。

How can I solve this problem? 我怎么解决这个问题?

In your function model the last line has to be: 在您的函数model ,最后一行必须为:

list(c(dY1,dY2,dY3,dY4))

So the output has to be concatenated c() in a vector. 因此,输出必须在向量中串联c() This is required by the deSolve package. 这是deSolve软件包所必需的。

So the whole function looks like this: 因此,整个函数如下所示:

model <- function (time, y, parms) {
  with(as.list(c(y, parms)), {
    dY1 <- -k1*y1*y2 + k2*y3
    dY2 <- k2 * y3 - k1*y1*y2
    dY3 <- -k2*y3-k3*y3+k1*y1*y2
    dY4 <- k3*y3
    list(c(dY1,dY2,dY3,dY4))
  })
}

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

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