clear all;
close all;

%reading data sets: first-test scores,
%and final-test scores
fid=fopen("file_03_11a.txt","r");
Y=[];
s=fgets(fid);
while(s!=(-1))
Y=[Y,str2num(s)];
s=fgets(fid);
end
fclose(fid);

%reading data sets: second-test scores,
%and final-test scores
fid=fopen("file_03_11b.txt","r");
W=[];
s=fgets(fid);
while(s!=(-1))
W=[W,str2num(s)];
s=fgets(fid);
end
fclose(fid);
%forming variables:
%X-the first-test scores
%U - the second-test scores
%Z - the final-test scores
X=[];Z=[];U=[];
n=length(Y);
for i=1:n/2
  X(i)=Y(2*i-1);
  U(i)=W(2*i-1);
  Z(i)=Y(2*i);
end

%the correlation coefficient
%X' - vector X as a column vector (transposition)

r=corr(X',Z');

%the regression straight line y=b*x+a;

b=r*std(Z)/std(X);
a=mean(Z)-b*mean(X);
f=@(x)b*x+a;

figure();
%the scatterplot of (X,Z)
scatter (X,Z, 225, 'b','.');
xlabel("the first test scores","FontSize",16);
ylabel("the final exam scores","FontSize",16,'rotation',90);
hold on;
%adding the regression line
fplot(f,[110,180])
s=sprintf("Final exam versus the first test, r= %f",r);
title (s,"FontSize",16);

figure();

%the correlation coefficient
%U' - vector U as a column vector (transposition)

r=corr(U',Z');
%the regression straight line y=b*x+a;
b=r*std(Z)/std(U);
a=mean(Z)-b*mean(U);
f=@(x)b*x+a;
%the scatterplot of (U,Z)
scatter (U,Z, 225, 'r','.');
hold on;
xlabel("the second test scores","FontSize",16);
ylabel("the final exam scores","FontSize",16,'rotation',90);
%adding the regression line
fplot(f,[110,180])
s=sprintf("Final exam versus the second test, r= %f",r);

title (s,"FontSize",16);


figure();


XX=[X,U]; ZZ=[Z,Z];
%the correlation coefficient
%XX' - vector XX as a column vector (transposition)

r=corr(XX',ZZ');
b=r*std(ZZ)/std(XX);
a=mean(ZZ)-b*mean(XX);
f=@(x)b*x+a;

%the joint scatterplot of (X,Z) and (U,Z)
%the vector G=[1,...,1,2,...,2] determines colours of
G=[ones(1,8),ones(1,8)+1];
%the dots in the picture
%'br' - colours of dots: b-blue, r-red
%'..'- the form of the dots
%[16,16]-size of vectors XX and ZZ
gscatter(XX,ZZ,G,'br','..',[16,16]);
xlabel("the first and second test scores","FontSize",16);
ylabel("the final exam scores","FontSize",16,'rotation',90);

hold on;
%adding the regression line
fplot(f,[110,180])
s=sprintf("Final exam versus the first and second test, r= %f",r);
title (s,"FontSize",16);
