function f = PolynomialLSQ(n,m)
% f = PolynomialLSQ(n,m)
% computes and displays a polynomial
% least-squares fit to n equally spaced data 
% sampled from the function 1/(1+x^2) with a 
% polynomial of degree m.
% The vector f returned is a vector of values
% of the fitting polynomial evaluated on an 
% equally spaced grid of 201 points in [-5,5].
xi = linspace(-5,5,201);
eta = 1./(xi.^2+1);
hold on
plot(xi,eta,'LineWidth',2)
if m == n-1
    title(sprintf('Polynomial Interpolation: %d data points, degree %d polynomial', n, m));
else
    title(sprintf('Polynomial Least Squares: %d data points, degree %d polynomial', n, m));
end
x = linspace(-5,5,n);
y = 1./(x.^2+1);
plot(x,y,'xg','LineWidth',2,'MarkerSize',10)
p = polyfit(x,y,m);
f = polyval(p,xi);
plot(xi,f,'r','LineWidth',2,'MarkerSize',10)
legend('Data function','Data points', 'Polynomial',-1);
hold off