Numerical differentiation.
dydx = deriv(y) dydx = deriv(x,y)
This function computes the first derivative of the vector or
matrix y
and returns it in dydx
, which has the
same size as y
. If y
is a matrix, differentiation occurs along columns. If the abscissa x
is not supplied, it is assumed to be 1:length(y)
for vectors and
1:size(y,1)
for matrices.
There is a caveat about using deriv
. A numerical derivative is computed
by taking differences of adjacent elements in y
. So it is save to use
deriv
for smooth signals, but it gives very noisy derivative for noisy
signals. In such a case, it is much better to apply a simultaneous smoothing/differentiation
using the function datasmooth.
To compute the first derivative of a sin function
x = linspace(0,5*pi,1001); y = sin(x); dydx = deriv(x,y); plot(x,y,'k',x,dydx,'g'); legend('original function','derivative');