我是R的新手,我正在尝试用de deSolve软件包解决一个微分方程系统。
我总共有10000个时间步,每100个时间步我必须改变变量的值。我尝试的代码显示以下错误:
Error in checkFunc(Func2, times, y, rho) :
The number of derivatives returned by func() (2) must equal the length of the initial conditions vector (3) 我不太确定错误是从哪里来的。到目前为止,我的代码如下:
library(deSolve)
#--------variables and parameter--------------------------
parameters <- c(up = 0.0001, hm = 0.1, hp = 0,889, mm = 0.1 , nt = 100)
yini <- c(u = 1, m = 0.001, h = 0.001)
times <- seq(0,100, by=0.1)
out<- NULL
#--------functions---------------------------------------
DiffU <- function(t, yini, parameters){
with(as.list(c(yini, parameters)),{
#functions
du <- -(up*u) + hm*h + (1/2*nt)*h
dm <- hp*h - mm*m - (1/nt)*m
# return functions
list(c(du, dm))
})
}
#---------------------------------------------
repeat{
out<- rbind(out, ode(yini, times, DiffU, parameters))
yini<-c(u = u+0.5*h, m = 0.001, h = m+0.5*h)
x<- x+1
if (x==100){
break
}
}你有什么建议来消除这个错误或者改进代码吗?非常感谢!
发布于 2016-05-30 15:38:02
你的问题是,你有两个导数'du‘和'dm',但是yini有三个值('u','m’和'h')。这会导致错误。
一种解决方案是(1)添加三阶导数或(2)从yini中删除'h‘并将其添加到参数中。
https://stackoverflow.com/questions/37456997
复制相似问题