function d=ddz(z)
% First derivative matrix for independent variable z.
% Usage: d=ddz2(z,bc)
% z= column vector of evenly-spaced independent variable values
% Method: 2nd order centered differences, one-sided boundaries
% W. Smyth, Oregon State University, 2004, updated January 2019
% smythw@oregonstate.edu

% preliminary: check for equal spacing
if abs(std(diff(z))/mean(diff(z)))>.000001
    disp(['ddz: values not evenly spaced!'])
    d=NaN;
    return
end

% define grid spacing, array length
del=z(2)-z(1);N=length(z);

% compute derivative matrix
d=zeros(N,N);
for n=2:N-1
    d(n,n-1)=-1.;
    d(n,n+1)=1.;
end
d(1,1)=-3;d(1,2)=4;d(1,3)=-1.;
d(N,N)=3;d(N,N-1)=-4;d(N,N-2)=1;
d=d/(2*del);
return
end