这是示例R代码:
library(deSolve)
d = c(5.29,4.16,2.49,1.53,0.7,0.41,0.21)*10^-4
rho = rep(1.27,7)
dg = d * sqrt(1/rho)
r0 = dg/2
Fr = c(0.188,0.297,0.274,0.181,0.032,0.013,0.015)
X0 = Fr*200*10^-6
N0 = X0*(3/(4*3.14*r0^3*rho))
state <- c(X1=X0[1],X2=X0[2],X3=X0[3],X4=X0[4],X5=X0[5],
X6=X0[6],X7=X0[7],S=0)
parameters <- c(D=6.19*60*10^-6,rho=rho,N=N0,Cs=17*10^-6,V=1000)
times <- seq(0,16,by=0.0005)
func2 <- function(t,state,parameters){
n <- length(state)
v <- 1:(n-1)
grad <- rep(NA,n)
tmp1 <- (4*3.14*rho[v]*N0[v])
tmp2 <- 3*state[v]/tmp1
tmp3 <- tmp2^(1/3)
grad[v] <- with(as.list(c(state,parameters)),{
-D*(N0[v]*4*3.14*tmp3)*(Cs-S/V)
})
grad[n] <- -sum(grad[v], na.rm = T)
return(list(grad))
}
sfunc <- function(t,state,parameters){
with(as.list(c(state,parameters)),{
S = S - (S/1000)*10
res <- c(X1,X2,X3,X4,X5,X6,X7,S)
return(res)
})
}
spt <- c(0,10/60,20/60,30/60,45/60,1,1.5,2,3,4,6,8,12,24)
out <- ode(y = state, times = times,func = func2,
parms = parameters, method = 'rk4',
events=list(func=sfunc,time=spt))
plot(out)
我遇到以下错误:
rk中的错误(y,times,func,parms,method =“ rk4”,...): 'rho'不能为C NULL:在C级eval中检测到
和以下警告消息:
此外:警告消息:
1:在checkevents(事件,时间,Ynames,dllname)中: 并非所有事件时间“ events $ time”都在输出“ times”中,因此它们会自动包含在内。
2:在checkevents(事件,时间,Ynames,dllname)中: 有些时间步伐非常接近事件-在这种情况下,仅使用事件时间。
如果删除事件,它将运行正常。