%
% Define data
%

x = [0; 1; 2; 3; 4; 5];
y = [2; 3; 2.5; 2.2; 0.4; 1.7];

%
% Setup overdetermined m x n linear system,
%  where m is number of data points and
%        n is number of basis functions
%

m = length(x);
n = 4;

for i = 1:m
  for j = 1:n
    A(i,j) = evalbasis(j, x(i));
  end
end

alpha = transpose(A) * A \ transpose(A)*y

%
% Evaluate/plot resulting function at 100 test points
% in [a,b].
%

a = 0; b = 5;
xtest = linspace(a, b, 100);

for i = 1:100
  ytest(i) = evalf(n, alpha, xtest(i));
end

subplot(2,1,1);
plot(x, y, 'o', xtest, ytest, '-')

%
% Do a plot of the scaled residuals.
%
for i = 1:m
  res(i) = y(i) - evalf(n, alpha, x(i));
end
norm(res,2)
s = norm(res,2) / sqrt(m-n);
res = (1/s) * res;
subplot(2,1,2);
plot(x, res, 'o');
