求解R中的微分方程组

col*_*lin 7 optimization r calculus differential-equations maximization

我在R中有一个简单的通量模型.它归结为两个微分方程,模拟模型中的两个状态变量,我们称之为AB.它们被计算为四分量通量flux1-flux4,5个参数p1-p5和第六个参数的简单差分方程of_interest,其可以取0-1之间的值.

parameters<- c(p1=0.028, p2=0.3, p3=0.5, p4=0.0002, p5=0.001, of_interest=0.1) 
state     <- c(A=28, B=1.4)

model<-function(t,state,parameters){
  with(as.list(c(state,parameters)),{
  #fluxes
  flux1  = (1-of_interest) * p1*(B / (p2 + B))*p3
  flux2  = p4* A          #microbial death
  flux3  = of_interest * p1*(B / (p2 + B))*p3 
  flux4  = p5* B      

  #differential equations of component fluxes
  dAdt<- flux1 - flux2
  dBdt<- flux3 - flux4
  list(c(dAdt,dBdt))
  })
Run Code Online (Sandbox Code Playgroud)

我想编写一个函数来取的导数dAdt相对于of_interest,派生方程设置为0,然后重新排列并求解的值of_interest.这将是of_interest最大化函数的参数的值dAdt.

到目前为止,我已经能够在稳定状态下解决模型,跨越可能的值of_interest来证明应该有一个最大值.

require(rootSolve)
range<- seq(0,1,by=0.01)
for(i in range){
of_interest=i
parameters<- c(p1=0.028, p2=0.3, p3=0.5, p4=0.0002, p5=0.001, of_interest=of_interest) 
state     <- c(A=28, B=1.4)
ST<- stode(y=y,func=model,parms=parameters,pos=T)
out<- c(out,ST$y[1])
Run Code Online (Sandbox Code Playgroud)

然后绘图:

plot(out~range, pch=16,col='purple')
lines(smooth.spline(out~range,spar=0.35), lwd=3,lty=1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我怎样才能在分析上解决R of_interest中最大化的价值dAdt?如果无法获得分析解决方案,我怎么知道,如何以数字方式解决这个问题呢?

更新:我认为这个问题可以通过R中的deSolve包解决,链接在这里,但是我在使用我的特定示例实现它时遇到了麻烦.

Mic*_*ico 5

你的方程B(t)几乎是可分的,因为你可以除掉B(t),从中你可以得到

B(t) = C * exp{-p5 * t} * (p2 + B(t)) ^ {of_interest * p1 * p3}
Run Code Online (Sandbox Code Playgroud)

这是B(t)我们将逐点解决的隐式解决方案。

您可以求解给C定您的初始值B。我想t = 0最初?在这种情况下

C = B_0 / (p2 + B_0) ^ {of_interest * p1 * p3}
Run Code Online (Sandbox Code Playgroud)

这也为 提供了一个更好看的表达式A(t)

dA(t) / dt = B_0 / (p2 + B_0) * p1 * p3 * (1 - of_interest) *
   exp{-p5 * t} * ((p2 + B(t) / (p2 + B_0)) ^ 
   {of_interest * p1 * p3 - 1} - p4 * A(t)
Run Code Online (Sandbox Code Playgroud)

这可以通过积分因子 (= exp{p4 * t})来解决,通过对涉及 的项进行数值积分B(t)。我们将积分的下限指定为 0,这样我们就不必在 range 之外计算 B [0, t],这意味着积分常数很简单A_0,因此:

A(t) = (A_0 + integral_0^t { f(tau; parameters) d tau}) * exp{-p4 * t}
Run Code Online (Sandbox Code Playgroud)

基本要点是B(t)驱动这个系统中的一切——方法是:解决 的行为B(t),然后用它来弄清楚 发生了什么A(t),然后最大化。

一、“外”参数;我们还需要nleqslv得到B

library(nleqslv)

t_min <- 0
t_max <- 10000
t_N <- 10

#we'll only solve the behavior of A & B over t_rng
t_rng <- seq(t_min, t_max, length.out = t_N)

#I'm calling of_interest ttheta
ttheta_min <- 0
ttheta_max <- 1
ttheta_N <- 5

tthetas <- seq(ttheta_min, ttheta_max, length.out = ttheta_N)

B_0 <- 1.4
A_0 <- 28

#No sense storing this as a vector when we'll only ever use it as a list
parameters <- list(p1 = 0.028, p2 = 0.3, p3 = 0.5, 
                   p4 = 0.0002, p5 = 0.001)
Run Code Online (Sandbox Code Playgroud)

从这里开始,基本轮廓是:

  1. 给定的参数值(尤其是ttheta),求解BBt_rng通过非线性方程组求解
  2. 鉴于BB和参数值,解决AAt_rng通过数值积分
  3. 给定AA和你的 dAdt 表达式,即插即用。

derivs <- sapply(ttthetas, function(th){ #append current ttheta params <- c(parameters, ttheta = th)

#declare a function we'll use to solve for B (see above)
b_slv <- function(b, t) 
  with(params, b - B_0 * ((p2 + b)/(p2 + B_0)) ^ 
         (ttheta * p1 * p3) * exp(-p5 * t))

#solving point-wise (this is pretty fast)
#  **See below for a note**
BB <- sapply(t_rng, function(t) nleqslv(B_0, function(b) b_slv(b, t))$x)

#this is f(tau; params) that I mentioned above;
#  we have to do linear interpolation since the
#  numerical integrator isn't constrained to the grid.
#  **See below for note**
a_int <- function(t){
  #approximate t to the grid (t_rng)
  #  (assumes B is monotonic, which seems to be true)
  #  (also, if t ends up negative, just assign t_rng[1])
  t_n <- max(1L, which.max(t_rng - t >= 0) - 1L)
  idx <- t_n:(t_n+1)
  ts <- t_rng[idx]

  #distance-weighted average of the local B values
  B_app <- sum((-1) ^ (0:1) * (t - ts) / diff(ts) * BB[idx])
  #finally, f(tau; params)
  with(params, (1 - ttheta) * p1 * p3 * B_0 / (p2 + B_0) *
         ((p2 + B_app)/(p2 + B_0)) ^ (ttheta * p1 * p3 - 1) *
         exp((p4 - p5) * t))
}

#a_int only works on scalars; the numeric integrator
#  requires a version that works on vectors
a_int_v <- function(t) sapply(t, a_int)

AA <- exp(-params$p4 * t_rng) * 
  sapply(t_rng, function(tt)
    #I found the subdivisions constraint binding in some cases
    #  at the default value; no trouble at 1000.
    A_0 + integrate(a_int_v, 0, tt, subdivisions = 1000L)$value)

#using the explicit version of dAdt given as flux1 - flux2
max(with(params, (1 - ttheta) * p1 * p3 * BB / (p2 + BB) - p4 * AA))})

Finally, simply run `tthetas[which.max(derivs)]` to get the maximizer.
Run Code Online (Sandbox Code Playgroud)

笔记:

此代码未针对效率进行优化。有几个地方可能有一些潜在的加速:

  • 递归地运行方程求解器可能会更快,因为它会随着更好的初始猜测收敛得更快——使用前一个值而不是初始值肯定更好
  • 简单地使用黎曼和进行积分会更快;权衡是准确性,但如果您有足够密集的网格,应该没问题。Riemann 的一个优点是您根本不必进行插值,并且在数值上它们是简单的线性代数。我用t_N == ttheta_N == 1000L它运行了它,它在几分钟内运行。
  • 可能可以a_int直接矢量化而不是仅仅对其进行矢量化sapply,这会通过更直接地诉诸 BLAS 来加速。
  • 大量其他小东西。预先计算,ttheta * p1 * p3因为它被重复使用了这么多,等等。

不过,我没有费心包括任何这些东西,因为老实说,您最好将它移植到更快的语言中——Julia 是我自己的最爱,但当然 R 与 C++、C、Fortran 等都说得很好.