function [z_deriv, x_deriv] = derivative(z,x)
%% Calculation of derivative

% input:
% z                    =  vertical values
% x                    =  cross shore distance
% 
% output:
% z_deriv              =  derivative
% x_deriv              =  cross shore location of the derivative 
%   --------------------------------------------------------------------

% $Id: $
% $Date: $
% $Author: $
% $Revision: $
% $HeadURL: $
% $Keywords: $
%   --------------------------------------------------------------------

%clear;
%close all;
%%

x_deriv  =  NaN(1,length(z)-1);
z_deriv  =  NaN(1,length(z)-1);

for i = 1 : length(z) - 1
    z_deriv(i) = (z(i+1) - z(i)) / (x(i+1) - x(i));
    x_deriv(i) = (x(i+1) + x(i)) / 2; % derivative defined in the middle
end
