function LeastSquaresDegree2(t,b)
% usage:  t is independent variable vector, b is function data 
% i.e., b = f(t)
% This works only for second degree polynomial 
% Try t = -5:0.05:5 with SecondDegreeTestData.dat

dataSize = length(b);
A = zeros(dataSize,3);

% Given basis phi(z) = 1 + z + z^2
A(1:dataSize,1) = 1;
A(1:dataSize,2) = t(1,1:dataSize);
A(1:dataSize,3) = (t(1,1:dataSize)).^2;

b = b';
[Q,R] = qr(A);
c = Q'*b;
x = R\c;

plot(t(1:dataSize),b(1:dataSize),'r*', t(1:dataSize), x(1,1) + x(2,1)*t(1:dataSize) + x(3,1)*(t(1:dataSize)).^2,'k')
title('Data plotted with 2nd Degree Least Squares Polynomial', 'FontWeight', 'bold', 'Color', 'b', 'FontSize', 14)
xlabel('independent variable t', 'FontWeight', 'bold', 'FontSize', 12)
ylabel('function values f(t)', 'FontWeight', 'bold', 'FontSize', 12)
legend('Input Data', 'Second Degree Polynomial')
axis tight

% For debugging:
% length(t);
% length(b);

% Check
Soln = A\b
Resid = Soln - x

% Code by Eric Johnson -- Focus | Numeric
end