% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

function [dfdx,xmid] = derivative(f,x)
% calculate the numerical derivative of a vector f() representing a function f(x)
% tabulated at the points stored in the vector x()

% some error handling
if length(x) ~= length(f)
    disp('derivative: vectors have different lengths!')
    dfdx = 0;
    xmid = 0;
    return
end

% the derivative
df   = diff(f);
dx   = diff(x);
dfdx = df./dx;

% the mid-points
N    = length(x);
il   = 1:N-1;
xmid = x(il)+dx./2;
end