且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拟合微分方程:如何将一组数据拟合到R中的微分方程

更新时间:2022-02-09 00:14:58

这可能是一种解决方案:

This could be a solution:

    require('deSolve')
conc <- data.frame(time <- c(0.16, 0.5, 1.0, 1.5, 2.0, 2.5, 3), concentration <- c(170, 122, 74, 45, 28, 17, 10))

##"Model" with differential equation
model <- function(t, C, k){
  list(-k * C)
}

##Cost function with sum of squared residuals:
cost <- function(k){
  c.start <- 170
  out <- lsoda(c(C=c.start), conc$time, model, k)
  c <- sum( (conc$concentration - out[,"C"])^2)       
  c
}

##Initial value for k
k <- 3
##  Use some optimization procedure
opt <- optim(k, cost, method="Brent", lower=0, upper=10)

k.fitted <- opt$par

也许天真一点,因为使用lsoda似乎仅用一个计算就有点矫kill过正强>微分方程...但是它肯定是最优的缩小您的k。
您可能要检查积分的C起始值,我在此处将其设置为170,难道您没有t = 0的值吗?

Maybe it is a bit naive since using lsoda seems to be a bit overkill for calculating with only one differential equation... But it certainly optimizes your k. You might want to check the start value for C for the integration, I set it to 170 here, don't you have a value for t=0?