function [p, s] = polyfit_nan(x, y, n)
%POLYFIT_NAN Polynomial curve fitting but handles NaNs.
%function [p, s] = polyfit_nan(x, y, n)
%	POLYFIT_NAN(x,y,n) finds the coefficients of a polynomial
%	p(x) of degree n that fits the data, p(x(i)) ~= y(i),
%	in a least-squares sense.  This is the same as the standard
%       matlab function polyfit except that the vectors X and Y may
%       contain NaNs.
%
%	See also POLY, POLYVAL, ROOTS.

%     Copyright J. V. Mansbridge, CSIRO, november 15 1993

if nargin ~= 3
  error('There must be be exactly 1 argument to polyfit_nan.m')
end

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

if nargout == 1
  p = polyfit(x, y, n);
elseif nargout == 2
  [p, s] = polyfit(x, y, n);
else
  error('There must be either 1 or 2 output arguments to polyfit_nan.m')
end

  