我在R中使用deSolve解决ODE系统。我打算计算一阶导数的时间平均值作为我的二阶导数。那就是:
dCt1 = (g/V - NCt1)dt
dCt2 = (1/t * Ct1)dt
以下是我的代码:
myfun <- function(tm, state, parms) {
with(as.list(c(tm, state, parms)),
{
dC1 <- (g/V) - (N*Ct1)
dCt2 <- Ct1 * 1/tm
res <- c(dCt1, dCt2)
return(list(res))
})
}
库( “deSolve”)
> duration <- seq(0, 4.5, by = 0.5);
> flow <- 0.5;
> source <- 128;
> Vol <- 168;
> state = c(Ct1 = 0, Ct2 = 0) # initial conditions
>
> para = list(V = Vol, g = source, N = flow)
>
> sol = ode(y = state, times = duration, parms = para, func = myfun,
> method = rkMethod("rk45ck"))
我收到错误消息:
警告消息:在rk(y,times,func,parms,method = method,...)中: 在t = 0时,时间步数1超过最大步数
然而,我能够使用另一种方法无误地解决:
myfun <- function(tm, state, parms) {
with(as.list(c(tm, state, parms)),
{
dCt1 <- (g/V) - (N*Ct1)
dCt2 <- Ct1
res <- c(dCt1, dCt2)
return(list(res, dC3 = dC2/tm))
})
}
> sol = ode(y = state, times = duration, parms = para, func = myfun,
> method = rkMethod("rk45ck"))
> sol[is.na(sol)] <- 0;
> head(sol ,10)
> #which gives the following
time C1 C2 C3
[1,] 0.0 0.0000000 0.00000000 0.0000000
[2,] 0.5 0.3370655 0.08777375 0.1755475
[3,] 1.0 0.5995724 0.32466476 0.3246648
[4,] 1.5 0.8040129 0.67768842 0.4517923
[5,] 2.0 0.9632314 1.12115626 0.5605781
[6,] 2.5 1.0872308 1.63506211 0.6540248
[7,] 3.0 1.1838017 2.20382513 0.7346084
[8,] 3.5 1.2590112 2.81531096 0.8043746
[9,] 4.0 1.3175844 3.46006934 0.8650173
[10,] 4.5 1.3632012 4.13074041 0.9179423
我担心的是我真的不需要Ct2并且不想输出它。我认为第一种计算方法dCt2 = (1/tm)*dCt1
应该有一种解决方法。所有必要的帮助将受到高度赞赏。
谢谢大家