簡體   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