%  Find the polynomial of degree 10 that best fits the function
%  b(t) = cos(4t) at 50 equally spaced points between 0 and 1.
%  Compute the coefficients using the Matlab command A\b (which
%  uses a QR factorization of A) and by solving the normal equations
%  A'*Ax = A'*b.  Compare the results.

t=[0:.02:1]';   % Generate t values.
b = cos(4*t);   % Generate right-hand side vector.
A = zeros(length(t),11);  % Form matrix A, whose dimensions are
                          % no. of equations by no. of unknown coeffs.
for j=1:11,
  A(:,j) = t.^(j-1);
end;

xQR = A\b;                % Compute coeffs using QR factorization
xnormal = (A'*A)\(A'*b);  % Compute coeffs by solving normal eqs
format long e
fprintf('        coeffs from QR          coeffs from normal eqs\n')
[xQR xnormal]
