%Every script should start with clc and clear all to ensure an empty command window at the beginning of each script run
clc
clear all

%xlsread= function to read data from excel
NOR=xlsread('Assignment_Data.xlsx','B1:P1'); %NOR= Number of service requests 
NOD=xlsread('Assignment_Data.xlsx','B2:P2'); %NOD= Number of days

%function size determines how many values in a vector
[rN,cN]=size(NOR);
LB=NOR(1);
span=3; %span = class width
HB=LB+span;
k=1;
daycount=0;

for i=1:cN;
    if NOR(i)<HB ;
        daycount=NOD(i)+daycount;
    else
        GNOD(k)=daycount;
        daycount=NOD(i);
        k=k+1;
        HB=HB+span;
    end
end
GNOD(k)=daycount;
% fprintf('GNOD = '); 
% fprintf('%g  ',GNOD);
% fprintf('\n\n');

MCV=LB+span/2:span:HB-span/2;
% fprintf('MCV = ');
% fprintf('%g ',MCV);
% fprintf('\n');

%TASK C
figure (1); %histogram graph
histogram('BinEdges',LB:span:HB,'BinCounts',GNOD,'FaceColor',[0 0 1]);
hold on;

plot(MCV,GNOD,'color',[1 0 0]);

LOC=find(GNOD==max(GNOD));

mode=MCV(LOC);

mg=max(GNOD);
%Finds the maximum value from GNOD as the mode is the max frequency 
ind=find(GNOD==mg);
%Finds the location of the maximum value in GNOD 
Mode=MCV(ind);
%Finds the mode as the value from MCV inthe location ind 
 
text(25.5,2,['Modal Value ',num2str(Mode)],'Color','y','FontSize',13,'FontWeight','bold','HorizontalAlignment','center');

 %TASK D
FRQ=GNOD/sum(NOD); %Freq of group.
CF=cumsum(FRQ,'forward'); %Cum.Freq of data.
CFextra=[0 CF];%Freq. extra.
ICF=cumsum(FRQ,'reverse');
ICFextra=[ICF 0]; %Inverse Cum.Freq.
 
figure (2);
%plot([LB:span:HB],CFextra,'Color',[0 0 1],'LineWidth',1.5,'LineStyle','--','Marker','o');
plot([LB:span:HB],CFextra,'Color',[1 1 1],'MarkerEdgeColor',[0 0 1],'Marker','o');
hold on
%plot([LB:span:HB],ICFextra,'Color',[1 0 0],'LineWidth',1.5,'LineStyle','-.','Marker','o');
plot([LB:span:HB],ICFextra,'Color',[1 1 1],'MarkerEdgeColor',[1 0 0],'Marker','o');

Yb=interp1(LB:span:HB,CFextra,LB:0.1:HB,'pchip');
hold on
plot(LB:0.1:HB,Yb,'Color',[0 0 1],'LineWidth',1.5,'LineStyle','--')

Yr=interp1(LB:span:HB,ICFextra,LB:0.1:HB,'pchip');
hold on
plot(LB:0.1:HB,Yr,'Color',[1 0 0],'LineWidth',1.5,'LineStyle','--')



grid on%gridlines
xlabel('xi');
ylabel('Frequencies');
title('Ogive Distribution Frequencies');
xticks([15:1:33]); 


%TASK E
diff=abs(Yb-Yr);
Mdiff=min(diff);
index=find(diff==min(diff));

xi=LB:0.1:HB;
Median=xi(index);
disp('The Median is: ')
disp(Median)


%TASK F - typed data always between {}
NC=(GNOD.*MCV);
NCS=(GNOD.*MCV.^2);

matrix=cell(9,7); %=number of variables Allocate all data into individual matrix cells
matrix(1,:)={'Var1_1' 'Var1_2' 'Var1_3' 'Var1_4' 'Var1_5' 'Var1_6' 'Var1_7'};
matrix{2,1}='Number of Requests xi';
matrix{3,1}='Mid Class ci';
matrix{4,1}='Number of Days ni';
matrix{5,1}='Frequency';
matrix{6,1}='Cumulated Frequency';
matrix{7,1}='Inversely Cumulated Frequency';
matrix{8,1}='ni.ci';
matrix{9,1}='ni.ci^2';
matrix{2,2}='[15-18[';
matrix{2,3}='[18-21[';
matrix{2,4}='[21-24[';
matrix{2,5}='[24-27[';
matrix{2,6}='[27-30[';
matrix{2,7}='[30-33[';
matrix(3,2:7)=num2cell(MCV);
matrix(4,2:7)=num2cell(GNOD);
matrix(5,2:7)=num2cell(FRQ);
matrix(6,2:7)=num2cell(CF);
matrix(7,2:7)=num2cell(ICF);
matrix(8,2:7)=num2cell(NC);
matrix(9,2:7)=num2cell(NCS);
xlswrite('Assignment_Data.xlsx',matrix,2)

Mean=sum(NC)/sum(GNOD); %hide figure, just display what is neccessary
disp('The Mean is: ')
disp(Mean);
Var=sum(NCS)/sum(GNOD)-Mean^2; %hide figure, just display what is neccessary
disp('The Variance is: ')
disp(Var)
SD=sqrt(Var); %hide figure, just display what is neccessary
disp('The Standard deviation is: ')
disp(SD)


% Other way of creating table - T=table({'Number of Requests';'Mid Class ci';'Number of Days ni';'Frequency';'Cumulated Frequency';'Inversely Cumulated Frequency';'ni.ci';'ni.ci^2'})




 



