function C = lspoly(X,Y,m)
% C = lspoly(X,Y)
% Construct the least squares polynomial.
% X is an 1xn abscissa vector, input.
% Y is an 1xn ordinate vector, input.
% m is the degree of the polynomial, input.
% C is a coefficient list for the polynomial, output.
n = length(X);
B = zeros(1:m+1);
F = zeros(n,m+1);
for k=1:m+1, % Powers of Xk are in columns of F.
F(:,k) = X'.^(k-1);
end
A = F'*F; % Compute the matrix A.
B = F'*Y'; % Compute the column vector B.
C = A\B; % Solve the linear system A*C=B for C.
C = flipud(C);