简体   繁体   English

如何在R中求解具有时间相关参数的常微分方程?

[英]How to solve ordinary differential equations with time dependent parameters in R?

I am trying to replicate an existing mathematical model in order to get "control" data for an independent research project.我正在尝试复制现有的数学模型,以便为独立研究项目获得“控制”数据。 Link to article. 链接到文章。

I want to solve a system of five ordinary differential equations in R, using the deSolve package.我想使用 deSolve 包在 R 中求解由五个常微分方程组成的系统。 However, most of the parameters are seasonal;然而,大多数参数是季节性的; the previous researchers used the 'pchip' function from the pracma package in order to create functions for the parameters:以前的研究人员使用 pracma 包中的“pchip”函数来为参数创建函数:

pchip is a `shape-preserving' piecewise cubic Hermite polynomial approach that apptempts to determine slopes such that function values do not overshoot data values. pchip 是一种“形状保持”分段三次 Hermite 多项式方法,它试图确定斜率,使函数值不会超过数据值。 pchipfun is a wrapper around pchip and returns a function. pchipfun 是 pchip 的包装器并返回一个函数。 Both pchip and the function returned by pchipfun are vectorized. pchip 和 pchipfun 返回的函数都是矢量化的。

xi and yi must be vectors of the same length greater or equal 3 (for cubic interpolation to be possible), and xi must be sorted. xi 和 yi 必须是大于或等于 3 的相同长度的向量(为了可以进行三次插值),并且必须对 xi 进行排序。 pchip can be applied to points outside [min(xi), max(xi)], but the result does not make much sense outside this interval. pchip 可以应用于 [min(xi), max(xi)] 之外的点,但结果在此区间之外没有多大意义。

graphed, 'beta' parameter over five years五年内绘制的“beta”参数

This is the code I have (just trying to get outputs for one year):这是我的代码(只是想获得一年的输出):

x <- c(0, 1, 2, 3, 4)
ybeta <- c(500, 1500, 500, 0, 500)
yk <- c(8000, 12, 0, 8000, 8000)
yrec <- c(0.25, 0.25, 0.25, 0, 0.25)
yfb <- c(1.5, 1.5, 1.5, 1.5, 1.5)
yno <- c(0, 0, 0, 0.00649, 0)
yni <- c(0, 0, 0, 0.00649, 0)
ypo <- c(0.08511, 0.08511, 0.08511, 0, 0.08511)
ypi <- c(0.16936, 0.16936, 0.16936, 0, 0.16936)
yalpha <- c(0.55, 0.12, 0.24, 0, 0.55)
yW <- 0.1
ydep <- c(0.2061 * yW, 0.2835 * yW, 0.2527 * yW, yW, 0.2061 * yW)
ydec <- c(0.006470, 0.023300, 0.015683, 0, 0.006470)
yup <- c(0.15, 0.15, 0.15, 0.15, 0.15)

xs <- seq(0, 1, len = 365)


nosema <- function(time, state, parameters) {

    with(as.list(c(state, parameters)), {

        H <- Ho + Hi
        Fr <- Fo + Fi
        Z <- H + Fr
        dHo <- pchip(x, ybeta, xs)* (Z^n) / (pchip(x, yk, xs)^n + Z^n) - pchip(x, yrec, xs) * Ho + pchip(x, yfb, xs) * Fr/Z * Fo - pchip(x, yno, xs) * Ho - pchip(x, yalpha, xs) * Ho * E/(sr + E)
        dHi <- -pchip(x, yrec, xs) * Hi + pchip(x, yfb, xs) * Fr/Z * Fi - pchip(x, yni, xs) * Hi + pchip(x, yalpha, xs) * Ho * E/(sr + E)
        dFo <- pchip(x, yrec, xs) * Ho - pchip(x, yfb, xs) * Fr/Z * Fo - pchip(x, ypo, xs) * Fo
        dFi <- pchip(x, yrec, xs) * Hi - pchip(x, yfb, xs) * Fr/Z * Fi - pchip(x, ypi, xs) * Fi
        dE <- pchip(x, ydep, xs) * Hi - pchip(x, ydec, xs) * E - pchip(x, yup, xs) * Ho * E / (sr + E)

        return(list(c(dHo, dHi, dFo, dFi, dE)))
    })
}


init <- c(Ho = 10^4, Hi = 0, Fo = 10000, Fi = 0, E = 0)

parameters <- c(n = 2, 
        sr = 10000)

out <- ode(y = init, times = seq(0, 365, len = 365), func = nosema, parms = parameters)

out <- as.data.frame(out)

out$time <- NULL

head(out)

However, when I run the code above...但是,当我运行上面的代码时...

The number of derivatives returned by func() (1825) must equal the length of the initial conditions vector (5) func() (1825) 返回的导数数量必须等于初始条件向量的长度 (5)

I think I have an inkling of why this isn't working, but I have no clue how to go about fixing it.我想我知道为什么这不起作用,但我不知道如何解决它。

Does anyone have any advice on how to proceed?有没有人对如何进行有任何建议?

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

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