0

我想用 R 来解决 boxmodel 的两个 ODE。我想使用强制函数随时间 t 改变输入参数 cw。这在我只使用带有两个盒子的盒子模型时有效:

dCs<-function(t, y, parms){
  with(as.list(c(y, parms)),{
    dCs<-k3*cw(t)-k4*cs
    return(list(dCs, dCw=cw(t)))
  })
}

但是当我更改函数以添加另一个框时,如下所示,出现错误:

dCs<-function(t, y, parms){
  with(as.list(c(y, parms)),{
    dCp<-(k1*cw(t)-(k2+k3)*cp+k4*cs)
    dCs<-(k3*cp-k4*cs)
    list(dCw=cw(t), dCs, dCp)
  })
}

Fehler in checkFunc(Func2, times, y, rho) : 
  The number of derivatives returned by func() (1) must equal the length of the initial conditions vector (2) 

我对相同错误的以下答案进行了仔细检查,但它对我不起作用。据我所见,所有参数均已正确标记: Difficulty running an ODE model in R include a parameter that changed by time (forcing function)

#modelled time
times<-seq(0,56)

#input parameters
parms<-c(k1=0.1,
         k2=0.01,
         k3=0.1,
         k4=0.01)

#initial values
y0<-c(cs = 0,
      cp = 0)

#linear interpolation of concentration cw
flux<-data.frame(time = c(10,12, 17,1,2,14,3,21,4,28,5,35,6,42,7,49,8,56),
                 cw = c(48,61,62,32,65,71,95,71,65,67,48,66,81,71,64,91,87,67))
cw<-approxfun(x = flux[,1], y = flux[,2], method = "linear", rule = 2)

out1<-ode(times = times, func = dCs, y = y0 , parms = parms)

如何纠正初始条件?

4

1 回答 1

0

以下解决方案允许将两个自变量添加到 ode。输出仅包含时间作为因变量,但它对应于第二个自变量 cw。

#model
model<-function(time, y, parms, ...){
  with(as.list(c(parms, y)), {
    dCp<-(k1*cw(time)-k2*cp-k3*cp+k4*cs)
    dCs<-(k3*cp-k4*cs)
    list(c(dCp, dCs))
  })
}
#cost function
cost.model <- function(p) {
  out  <-  ode(y0, times, model, p, method="rk4")
  modCost(out, dat, y="value")
}

#independent variable times
times<-seq(0,56)

#independent variable cw
flux<-df.i[df.i$Samp=="CC",c(2,9)]
flux<-flux[!duplicated(flux$time),]
#interpolation of cw
cw<-approxfun(x = flux[,1], y = flux[,2], method = "linear", rule = 2)
#initial values
y0<-c(cp=0,
      cs=0)

#parameters
parms<-c(k1=0.1,
         k2=0.1,
         k3=0.1,
         k4=0.001)

#fit
dat<-df.i
fit<-modFit(f=cost.mod, p=parms)

从数据帧中cw提取数据df.i并进行插值以给出 的每个值的值timesdf.i被调制,因此它适合调用成本函数(未显示),然后执行。

于 2020-09-24T07:03:54.963 回答