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

さんによって質問されました 2014 年 7 月 18 日

### Daniel kiracofe (view profile)

さんによって 回答されました 2014 年 7 月 18 日
Jan

### Jan (view profile)

さんの 回答が採用されました
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?

#### 0 件のコメント

サインイン to comment.

## 3 件の回答

### Jan (view profile)

2014 年 7 月 18 日
採用された回答

gradient is smarter for calculating derivatives:
x = rand(1, 100);
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:

#### 0 件のコメント

サインイン to comment. ### Star Strider (view profile)

2014 年 7 月 18 日

The only approaches I can think of are:
1. Fit your vectors with polyfit, use polyder to calculate the derivatives of the polynomial function, and then use polyval with the results of polyder to calculate the actual values at the x-values you choose;
2. Take the derivative using diff, then use interp1 (with the 'extrap' option if necessary) to interpolate (and extrapolate) the derivative.
These approaches both assume your data are smooth and noise-free. Taking the derivative of a noisy signal is generally not recommended because the noise will predominate in the derivative.

#### 0 件のコメント

サインイン to comment.

### Daniel kiracofe (view profile)

2014 年 7 月 18 日

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);

#### 0 件のコメント

サインイン to comment.