function d=ddz2(z)
% Second 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

% check for equal spacing
if abs(std(diff(z))/mean(diff(z)))>.000001
    disp(['ddz2: values not evenly spaced!'])
    d=NaN;
    return
end
del=z(2)-z(1);N=length(z);

d=zeros(N,N);
for n=2:N-1
    d(n,n-1)=1.;
    d(n,n)=-2.;
    d(n,n+1)=1.;
end

% one-sided derivatives at the edges
d(1,:)=0;
d(1,1)=2;d(1,2)=-5;d(1,3)=4;d(1,4)=-1;
d(N,:)=0;
d(N,N)=2;d(N,N-1)=-5;d(N,N-2)=4;d(N,N-3)=-1;

d=d/del^2;
return
end
