Weighted least squares polynomial fit.
[p,S] = least2(x,y,n,w)
finds the coefficients of a polynomial p(x) of degree n that fits the data, p(x(i)) ~= y(i), in a weighted least-squares sense with weights w(i). The structure S contains additional info.
This routine is based on polyfit.
The regression problem is formulated in matrix format as:
A'*W*y = A'*W*A*p
where the vector p contains the coefficients to be found. For a 2nd order polynomial, matrix A would be:
A = [x.^2 x.^1 ones(size(x))];
polyfit polyval least2b