%
% Script file:  P3dataX.m - linearfit.m (plotdataX.m)
% A Linear Least Square Fit for (No. of age 0, No. of age 65)
%
Data=xlsread('dataX.xlsx','A2:B23'); 
m=2;  n=17;
X=Data(1:n,1); Y=Data(1:n,2);
X=X/10000; Y=Y/10000;
A=[sum(X.*X), sum(X);  sum(X), n];
b=[sum(X.*Y); sum(Y)];
v=A\b;  % (-0.0992, 16.8250)
v
for j=1:n
  t=14+j;
  X1(j)=t;
  Y1(j)=t*v(1)+v(2);
end
fout=fopen('dataX.txt','wb');
fprintf(fout,'Births and At Age 65 during 1995~2011, inclusively');
fprintf(fout,'\n');
for i=1:17
  fprintf(fout,'%6d %6d',X(i),Y(i));
  fprintf(fout,'\n');
end
fclose(fout);
T=15:31;
plot(X1,Y1,'r-',X,Y,'bo'); axis([15, 31, 9, 25]); grid;
legend('Best fitting line is Y=-0.0992X+16.8250','Location','NorthEast')
title('No. of 10000*people of age 0 vs. age 65 during 1995 \sim 2011')
ylabel('No. of 10000*people at Age 65')
xlabel('No. of 10000*people at Age 0')