我想用 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)
如何纠正初始条件?