clear all
clf
echo on
format rat
disp('simple best line fit with 3 data points')
t=[1;2;3]
disp('design matrix is')
X=[ones(3,1) t]
y=[2;5 ;7]
beta=X\y
disp('hit return to continue')
pause
beta2=(X'*X)\(X'*y)
beta3=inv(X'*X)*(X'*y)
disp(['differences between betas: ',...
           num2str([norm(beta-beta2) norm(beta-beta3)])])
disp('hit return to continue')
pause
plot(t,y,'ob')
disp('hit return to continue')
pause
hold on
%plot([1:5],[beta(1)+beta(2)*[1:5]],'c+:')
plot(t,X*beta,'c+:')
hold off
disp(['three residuals are: ',num2str([(y-X*beta)'])])
format short
disp('use command    return    to continue')
keyboard
for degree=1:6,
   disp(['now approx. sin x (odd function) using polynomial degree ',...
                                 num2str(degree)])
   t=((2*pi*[0:20])/20)';
   y=sin(t);
   X=[];
   for i=0:degree,
      X=[X t.^i];
   end
   disp('design matrix is')
      X
   beta=X\y;
   plot(t,y,'ob')
disp('use command    return    to continue')
   keyboard
   hold on
   yp=X*beta;
   plot(t,X*beta,'c+:')
   disp(['degree and norm of residual is ',num2str([degree norm(y-yp)])])
   beta
disp('use command    return    to continue')
   keyboard
   hold off
end
disp('use command    return    to continue')
keyboard
for degree=1:6,
   disp(['now approx. cos x (even function) using polynomial degree ',...
                                 num2str(degree)])
   t=((pi*[0:20])/20)';
   y=sin(t);
   X=[];
   for i=0:degree,
      X=[X t.^i];
   end
   beta=X\y;
   plot(t,y,'ob')
disp('use command    return    to continue')
   keyboard
   hold on
   yp=X*beta;
   plot(t,X*beta,'c+:')
   disp(['degree and norm of residual is ',num2str([degree norm(y-yp)])])
   beta
disp('use command    return    to continue')
   keyboard
   hold off
end
