%
% Script file:  linefit.m
% A Linear Least Square Fit for (GPA_hight school, GPA_university)
%
fin=fopen('dataGPA.txt','r'); 
fgetl(fin);
m=2;  n=20;
T=fscanf(fin,'%f',[m n]);
fclose(fin);
T=T';
X=T(:,1);
Y=T(:,2);
A=[sum(X.*X), sum(X);  sum(X), n];
b=[sum(X.*Y); sum(Y)];
v=A\b;  % (0.8822, 0.0298)
for j=1:n,
  t=2.0+0.2*j;
  X1(j)=t;
  Y1(j)=t*v(1)+v(2);
end
plot(X1,Y1,'r-',X,Y,'bo'); axis([2 4  2 4]); grid;
legend('Best fitting line is y=0.8822x+0.0298','Location','NorthWest')
title('University GPA vs. High School GPA')
ylabel('University GPA')
xlabel('High School GPA')