How can I differentiate without decreasing the length of a vector?
Show older comments
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?
Accepted Answer
More Answers (1)
Daniel kiracofe
on 18 Jul 2014
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 Comment
rnayek
on 10 Mar 2020
That is what the
function in MATLAB does too!
Categories
Find more on Smoothing and Denoising in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!