0

I have generated some real data of the motion of a damping pendulum:

Angle

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:

Angular Velocity R

I took the derivative of this graph again:

Angular acceleration

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

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 17 '20 at 23:55
  • @MrFlick I added them, thanks – KnowledgeWonder Apr 18 '20 at 00:42
  • Check this [question and answer](https://stackoverflow.com/questions/11081069/calculate-the-derivative-of-a-data-function-in-r) – Tony Ladson Apr 18 '20 at 02:00

1 Answers1

1

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)