简体   繁体   English

用deSolve解决ODE的R-导数错误

[英]Solving ODE with deSolve in R- number of derivatives error

I am trying to use the deSolve package for a set of ODE's with equations as auxiliary variables. 我正在尝试将deSolve软件包用于带有方程作为辅助变量的一组ODE。 I keep getting the error where the number of derivatives isn't the same length as the initial conditions vector. 我不断收到错误,其中导数的数量与初始条件向量的长度不同。 What should I change? 我应该改变什么?

# rm(list=ls())  
library(deSolve)

exponential=function(t,state,parameters){ with(as.list( c(state,parameters)), {

   #Aux. Var.
   fX2 = pmax(0,1-(1-(d2/r12)*(X2/K2)))
   fX1 = X1/(X1+k1); 

   # equations (ODE)
   dX1 = C-((d1)*(X1))-(r12)*(X2)*fX2*fX1 # differential equaion
   dX2 = r12*(X2)*fX2*fX1-((d2)*(X2))

   return(list(c(dX1, dX2)))
   })
 }

# -- RUN INFORMATION

# Set Initial Values and Simulation Time
state = c(X1=2,X2=0.01,K2= 10)
times=0:100

# Assign Parameter Values
parameters = c(d1=0.001, d2=0.008, r12=0.3,C=0.5,k1= 0.001)

for (i in 1:length(times)){
  out= ode(y=state,times=times,func=exponential,parms=parameters)
  }

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

The error comes from the return in your defined function: Your input parameter y has length 3, but you only return 2 values back, that's the error. 错误来自定义函数的return :输入参数y长度为3,但是仅返回2个值,这就是错误。 You can solve your problem with 您可以解决您的问题

return(list(c(X1, X2, K2)))

Another possibility is to take K2 to the parameters, then your old return was right. 另一种可能性是将K2参数,那么您的旧return是正确的。 You have to decide if K2 is a variable or a parameter. 您必须确定K2是变量还是参数。

And BTW: Why a for loop with the time? 顺便说一句:为什么要用时间进行循环? In my opinion that is not necessary, because the ODEs are solved in the timeinterval you submitted to the ode function. 我认为这是没有必要的,因为ODE是在您提交给ode函数的timeinterval中解决的。

out= ode(y=state,times=times,func=exponential,parms=parameters)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM