How can I differentiate without decreasing the length of a vector?

I have some vectors and want to differentiate them up to second order. I don't want to use "diff" because it reduces the length of vector in higher orders! Is there any other function or method that I differentiate and keep the length of vector constant?

 採用された回答

Jan
Jan 2014 年 7 月 18 日

2 投票

gradient is smarter for calculating derivatives:
x = rand(1, 100);
d2 = gradient(gradient(x));
The Savtizky Golay smoothing filter can be applied to calculate a smoothed derivative by fitting polynmials to local parts of the signal. Look in the FileExchange for many different submissions:

1 件のコメント

John D'Errico
John D'Errico 2020 年 3 月 10 日
Jan is correct, of course. I might only add one idea, to fit the data using a smoothing spline, then differentiate the spline, and evaluate the derivative spline at the original data points.
spl = csaps(x,y);
spld = fnder(spl);
yprimepred = fnval(spld,x);
As I've done it here, this uses tools from the curve fitting toolbox, though there are alternative ways to implement it too.

サインインしてコメントする。

その他の回答 (1 件)

Daniel kiracofe
Daniel kiracofe 2014 年 7 月 18 日

0 投票

My standard approach is to use 2nd order centered difference for the main part of the vector, and use first order forward and backward difference at the boundaries:
function d = cdiff(x, dt)
if (nargin<2)
dt =1 ;
end
d(1) = (x(2) - x(1)) / dt;
d(length(x)) = ( x(end) - x(end-1) ) / dt;
ndx = 2:(length(x)-1);
d(ndx) = (x( ndx+1) - x(ndx-1)) / (2 * dt);

1 件のコメント

rnayek
rnayek 2020 年 3 月 10 日
That is what the function in MATLAB does too!

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 7 月 18 日

コメント済み:

2020 年 3 月 10 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by