我在我的微分方程求解器中包含了 'time' 参数,它以某种方式搞砸了

I include the 'time' paramter in my differential equation solver and it messes it up somehow

library(deSolve)
require(deSolve)

delta_conc <- function(time, current_state, params) {

  with(as.list(c(current_state, params)),{

    dX <- Y
    dY <- X - X^3 - 0.25*Y + A * sin(t)

    return(list(c(dX, dY)))
  })
}

params <- c(
  A <- 0.2645
)

initial_state <- c(
  X <- 0.9,
  Y <- 0.4
)

times <- 1:10

model <- ode(initial_state, times, delta_conc, params)

summary(model)

matplot(model, type="l",lty=1, main="Enzyme model", xlab="Time")

我尝试 运行 时收到此错误消息:

checkFunc(Func2, times, y, rho) 错误: func() 返回的导数数 (21) 必须等于初始条件向量的长度 (2)

当我排除 'sin(t)' 部分时它起作用了,所以问题出在那个部分,但我是一个初学者所以我不知道如何解决这个问题

您应该始终使用 einer ttime 作为实际时间步长。在您的情况下 t 未定义为变量,因此 t 被解释为转置函数。

以下应该有效:

require(deSolve)

delta_conc <- function(time, current_state, params) {
  with(as.list(c(current_state, params)),{
    dX <- Y
    dY <- X - X^3 - 0.25*Y + A * sin(time)
    return(list(c(dX, dY)))
  })
}

params <- c(
  A = 0.2645
)

initial_state <- c(
  X = 0.9,
  Y = 0.4
)

times <- 1:10
model <- ode(initial_state, times, delta_conc, params)
summary(model)

matplot.0D(model, type="l",lty=1, main="Enzyme model", xlab="Time")

此外,代码还存在一些其他问题:

  • 使用 requirelibrary 而不是两者都使用
  • c() 内使用 =。是参数匹配不是赋值

另外两条建议:

  • 您可以使用 deSolve 内置的绘图函数 matplot.0D
  • 我建议使用 times <- seq(0, 10, length.out = 100) 而不是 1:10。这样情节就会变得顺利。以 1(或其他值)开始时间可能没问题,但以零开始时间通常更方便。