function [dy,min,max] = derivation(y,dt,deriv)
%
%Instituto Superior Técnico - Instituto de Sistemas e Robótica - 			             09/2001
%OMNIVIEWS PROJECT    -   SVAVISCA sensor									[ccgd,jasv@isr.ist.utl.pt]
%
% Computes a derivative of vector y, whose points are separated by dt,
% Different methods can be used to computed the derivative according to the flag, "deriv".
%
% y - vector containing the data to differentiate, they correspond to the result of ode45 function
% dt : uniform step between points where y is evaluated, in x axis
%
% deriv = 0 : derivative computed using the MatLab diff function, [y(n+1)-y(n) ]/dt
%		    1 : based on symmetric finite difference, [y(n+1)-y(n-1) ]/2dt

n = length(y);

if deriv==0
   dy = diff(y)/dt;
   dy = [dy; 1000];      % padding last coordinate as 1000 to get same size as y.
   min = 1;
   max = n-1;
elseif deriv==1
   dy=1000*ones(size(y)); 
   dy(2:n-1) = ( y(3:n) - y(1:n-2) )/(2*dt);  % dy(n) = [y(n+1)-y(n-1) ]/2dt
   min = 2;
   max = n-1;
else disp('Error in the derivation method flag');
end
