本文介绍了拟合微分方程:如何将一组数据拟合到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))

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

and I would like to fit this data to the differential equation below:

dC/dt= -kC

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

where C would be the concentration, and time t in the data set. This would also give a result of k. Could anybody give me a clue how to do this in R? Thanks.

推荐答案

这可能是一种解决方案:

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?

这篇关于拟合微分方程:如何将一组数据拟合到R中的微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 22:51