% file: homework_03.m
%
% LDA on two data sets

% open output file for writing results
%
output_file=fopen('output.text','w');


fprintf(output_file,'************************* Part 1 *******************************\n');

% the data sets were made using the file graphical_input.m

% assign data set values
%
set1=set_1;
set2=set_2;

% calculate means of each data set
%
m1=mean(set1);
m2=mean(set2);
orig_mean1=m1;
orig_mean2=m2;
fprintf(output_file,'\nmean for set 1: [%2.4f, %2.4f]\nmean for set 2: [%2.4f, %2.4f]\n\n',m1,m2);

% plot the data sets
%
main_data_plot=figure;
hold on;
box on;
grid on;
plot(set1(:,1),set1(:,2),'*');
plot(set2(:,1),set2(:,2),'r*');
set(gca,'Xtick',[-5:1:5],'Ytick',[-6:1:6]);
title('Part I');

fprintf(output_file,'************************* Part 2 *******************************\n');

fprintf(output_file,'\nThe test set:\n\n');

% define the test set
%
x1=[-1 -1];
x2=[0 0];
x3=[.5 .5];
x4=[.5,-.5];
fprintf(output_file,'x1: [%i, %i]\nx2: [%i, %i]\nx3: [%1.1f, %1.1f]\nx4: [%1.1f, %2.1f]\n\n', x1,x2,x3,x4);

% plot the test set
%
plot(x1(1),x1(2),'k*');
plot(x2(1),x2(2),'k*');
plot(x3(1),x3(2),'k*');
plot(x4(1),x4(2),'k*');
legend('Set 1','Set 2', 'Test Set');


fprintf(output_file,'************************* Part 3 *******************************\n');

fprintf(output_file,'\nTest set membership before transformations:\n\n');

% compute Euclidean distances and determine set membership for x1
%
fprintf(output_file,'x1 ');
distance=euclidean(x1,m1,m2,output_file);
fprintf(output_file,'Euclidean distances:\n');
fprintf(output_file,'x1 & set1 mean: %2.4f\n', distance(1));
fprintf(output_file,'x1 & set2 mean: %2.4f\n\n', distance(2));

% compute Euclidean distances and determine set membership for x2
%
fprintf(output_file,'x2 ');
distance=euclidean(x2,m1,m2,output_file);
fprintf(output_file,'Euclidean distances:\n');
fprintf(output_file,'x2 & set1 mean: %2.4f\n', distance(1));
fprintf(output_file,'x2 & set2 mean: %2.4f\n\n', distance(2));

% compute Euclidean distances and determine set membership for x3
%
fprintf(output_file,'x3 ');
distance=euclidean(x3,m1,m2,output_file);
fprintf(output_file,'Euclidean distances:\n');
fprintf(output_file,'x3 & set1 mean: %2.4f\n', distance(1));
fprintf(output_file,'x3 & set2 mean: %2.4f\n\n', distance(2));

% compute Euclidean distances and determine set membership for x4
%
fprintf(output_file,'x4 ');
distance=euclidean(x4,m1,m2,output_file);
fprintf(output_file,'Euclidean distances:\n');
fprintf(output_file,'x4 & set1 mean: %2.4f\n', distance(1));
fprintf(output_file,'x4 & set2 mean: %2.4f\n\n', distance(2));


fprintf(output_file,'************************** Part 4 *******************************\n\n');

% perform lda analysis
%
lda(set1,set2,cat(1,set1,set2),[x1;x2;x3;x4], output_file);


fprintf(output_file,'************************* Part 5 *******************************\n\n');

% plot the transformed data sets
%
figure;
hold on;
box on;
grid on;
plot(new_set1(:,1),new_set1(:,2),'*');
plot(new_set2(:,1),new_set2(:,2),'r*');
set(gca,'Xtick',[-5:1:5],'Ytick',[-6:1:6]);
title('Transformed Data Sets');

% close the output_file
%
fclose(output_file);
