R中的模型复方程

时间:2018-05-04 20:51:55

标签: r math equation differential-equations equation-solving

我有以下型号:

$y(t) = C + A_{0}\exp^{(-\sigma t)}\cos(\omega_dt+\phi)$

我在R中编码为:

function(t,C,Ao,s,wd,ph) C + Ao * exp(-s*t) * cos(wd*t + ph)

我想用这个等式来形成一个预测模型。

但是,我无法弄清楚如何成功运行或绘制这个等式。

  • 我尝试nls但遇到了各种错误(包括那些警告我singular gradient的错误。

我看了here但我不知道在哪里可以看到我的模型似乎不起作用。

如何在R中对此功能进行建模和绘图?

我尝试了什么:

LTI.func <- function(t,C,Ao,s,wd,ph) C + Ao * exp(-s*t) * cos(wd * t + ph)
mod <- nls(Y ~ LTI.func(t = I(scale(t)), C, Ao, s, wd, ph), 
           data = dat, 
           start = list(C = 1, Ao = 1, s = 1, w = 1, ph = 1))

我不知道选择starts是什么,所以我尝试了一堆随机的,导致错误。即使我在y(t)〜t趋势的引导下选择了开始,我总是会遇到某种错误:

Error in nlsModel(formula, mf, start, wts) : 
  singular gradient matrix at initial parameter estimates

Error in nls(Y ~ LTI.func(I(scale(t)), C, Ao, s, wd, ph), data = dat  : 
  singular gradient

更新:

以下是一组示例数据:

dat <- data.frame(t = c(72, 25, 10, 88, 67, 63, 34, 41, 75, 13, 59, 8, 30, 52, 21),
                  Y = c(108.7, 157.5, 17.7, 175, 246.8, 233.5, 208.6, 246.5, 126.5, 
                        45.5, 214.1, 4.9, 184, 239.2, 113.3))

1 个答案:

答案 0 :(得分:1)

这显示在关闭队列中,但在我看来,如果检查参数名称的一致性很差,那么我的形成相当合理。这是我的解决方案。我已经尝试首先将函数的参数更改为x,当不起作用时尝试调整起始值。最终,我决定扩展数据参数:

LTI.func <- function(x,C,Ao,s,wd,ph) {C + Ao * exp(-s*x) * cos(wd * x + ph)}
 mod <- nls(Y ~ LTI.func(x=t , C, Ao, s, wd, ph), data=data.frame(scale(dat)), 
                     start=list(C = 0,Ao = -1,s = 1,wd = 1,ph = 0))
 mod
#-------------
Nonlinear regression model
  model: Y ~ LTI.func(x = t, C, Ao, s, wd, ph)
   data: data.frame(scale(dat))
        C        Ao         s        wd        ph 
 0.288729 -0.986426  0.517128  2.002040  2.756004 
 residual sum-of-squares: 1.53608

Number of iterations to convergence: 18 
Achieved convergence tolerance: 0.0000038776

这是否是一个有用的“解决方案”将需要对原始值进行反向变换和绘制结果,或者可能根据理论绘制变换后的坐标。 A0值小于零并不奇怪。当然,从数据来看,趋势是向上的,如果exp(-s*x)为正,s*x通常会向下。