繁体   English   中英

R 中的数值导数?

[英]Numerical Derivative in R?

我已经生成了一些阻尼摆运动的真实数据:

角度

我通过取连续点的差异并除以时间差异,在 R 中取了它的导数。 这张图片有1202个数据点。

这给出了这个图表:

角速度 R

我再次取了这张图的导数:

角加速度

但是,此图非常不稳定且无法用于分析。 我想知道 R 中是否有一个 function 允许精确的数值微分? 我知道傅立叶变换,尽管我不确定如何将它们直接应用到阻尼摆上。

这是我在 R 中使用的 function 来计算导数:

derivative <- function(x,y,deriv0){
  # deriv0 = value of the derivative at time zero
  deriv <- diff(y[2:length(x)]) / diff(x[2:length(y)])
  w = length(x)-2
  deriv <- c(deriv0,deriv[1:w])
  time <- x[1:length(x)-1]
  return(data.frame(time,deriv))
}

原始数据集在这里:

摆数据集

谢谢

我最终找到了一个简单而优雅的解决方案。

library(pspline)
t <- time vector
x <- data vector

For the first derivative:
deriv1st <- predict(sm.spline(t, x), t, 1)

plot(t,deriv1st)

For the second derivative:
deriv2nd <- predict(sm.spline(t, x), t, 2)

plot(t,deriv2nd)

暂无
暂无

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

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