简体   繁体   中英

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. There are 1202 data points in this picture.

That gave this graph:

角速度 R

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? 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:

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:

Pendulum Dataset

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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