R ODE 中的强制函数给出误差(导数数)



我想用R求解一个盒子模型的两个常微分方程。我想使用强制函数随时间 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) 

我仔细检查了以下对同一错误的答案,但它对我不起作用。据我所知,所有参数都正确标记: 难以在 R 中运行 ODE 模型,包括随时间变化的参数(强制函数)

#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)

如何纠正初始条件?

以下解决方案允许向颂歌中添加两个自变量。输出仅包含时间作为因变量,但它对应于第二个自变量 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并进行插值,以给出每个值times的值。df.i被调制,使其适合成本函数(未显示)的调用,然后执行该调用。

最新更新