繁体   English   中英

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

[英]Fitting differential equations: how to fit a set of data to a differential equation in R

带有数据集:

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

我想将这些数据拟合到下面的微分方程中:

dC/dt= -kC

其中C是浓度,时间t是数据集中的时间。 这也将得出k的结果。 有人可以给我一个提示,以了解如何在R中执行此操作吗? 谢谢。

首先使用变量分离来求解微分方程。 这样得出log(C)=-k * t + C0。

绘制数据:

plot(log(concentration) ~ time,data=conc)

拟合线性模型:

fit <- lm(log(concentration) ~ time,data=conc)
summary(fit)

# Coefficients:
#               Estimate Std. Error t value Pr(>|t|)    
# (Intercept)   5.299355   0.009787   541.4 4.08e-13 ***
#   time       -0.992208   0.005426  -182.9 9.28e-11 ***
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
# 
# Residual standard error: 0.01388 on 5 degrees of freedom
# Multiple R-squared: 0.9999,  Adjusted R-squared: 0.9998 
# F-statistic: 3.344e+04 on 1 and 5 DF,  p-value: 9.281e-11 

绘制预测值:

lines(predict(fit)~conc$time)

提取k:

k <- -coef(fit)[2]
#0.9922081

在此处输入图片说明

这可能是一个解决方案:

    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似乎对于只用一个微分方程进行计算来说有点过头了……但是它肯定会优化您的k。 您可能要检查积分的C起始值,我在此处将其设置为170,您是否没有t = 0的值?

暂无
暂无

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

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