[英]Numerical Derivative in R?
I have generated some real data of the motion of a damping pendulum:我已经生成了一些阻尼摆运动的真实数据:
I took its derivative in R by taking the difference of consecutive points and dividing by difference in time.我通过取连续点的差异并除以时间差异,在 R 中取了它的导数。 There are 1202 data points in this picture.
这张图片有1202个数据点。
That gave this graph:这给出了这个图表:
I took the derivative of this graph again:我再次取了这张图的导数:
However, this graph is very erratic and unusable for analysis.但是,此图非常不稳定且无法用于分析。 I was wondering if there is a function in R which allows for accurate numerical differentiation?
我想知道 R 中是否有一个 function 允许精确的数值微分? I know of Fourier Transforms although I'm not sure how to directly apply them on a damping pendulum.
我知道傅立叶变换,尽管我不确定如何将它们直接应用到阻尼摆上。
This is a function I'm using in R to compute the derivative:这是我在 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))
}
The original dataset is here:原始数据集在这里:
Thanks谢谢
I ended up finding a simple and elegant solution.我最终找到了一个简单而优雅的解决方案。
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.