function [m b fit_error] = slope( x, y, n )
%This function computes the slope of y by fitting a line to y over the
%surrounding n points.  It returns the slope (m).
%  Usage:  [m b fit_error] = slope( x, y, n )

ok = find(~isnan(x) & ~isnan(y));

sxy = n* smooth(x(ok) .* y(ok), n);
sx2 = n* smooth(x(ok) .* x(ok), n);
sy2 = n* smooth(y(ok) .* y(ok), n);
sx = n* smooth(x(ok), n);
sy = n* smooth(y(ok), n);

% Need to return arrays the length of the orginal array, filled in with
% NaNs at the right plase...
fit_error = NaN * x;

m1 = -((n * sxy - sx .* sy)./(sx.^2 - n * sx2));
b1 = -(( sx .* sxy - sx2 .* sy)./(-sx.^2 + n * sx2));
fit_error(ok) = sy2 + m1.^2 .* sx2 - 2* m1 .* sxy - 2 * b1 .* sy + 2*b1.*m1.*sx + n * b1.^2;

m = NaN * x;
b = NaN * x;
m(ok) = m1;
b(ok) = b1;
end

