【问题标题】:Solving ODE with deSolve in R- number of derivatives error用 deSolve 在 R- 导数误差中求解 ODE
【发布时间】:2016-10-13 15:27:42
【问题描述】:

我正在尝试将 deSolve 包用于一组以方程作为辅助变量的 ODE。我不断收到错误,其中导数的数量与初始条件向量的长度不同。我应该改变什么?

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

【问题讨论】:

    标签: r ode


    【解决方案1】:

    错误来自您定义的函数中的return: 您的输入参数y 的长度为 3,但您只返回 2 个值,这就是错误。你可以解决你的问题

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

    另一种可能性是将K2 带入参数,那么您的旧return 是对的。您必须决定K2 是变量还是参数。

    顺便说一句:为什么要使用 for 循环?我认为这没有必要,因为 ODE 在您提交给 odefunction 的时间间隔内解决。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 2019-07-05
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多