clc %clears the command window
clear all %clears everyhting if needed

NoR=xlsread('Assignment_Data.xlsx','B1:P1'); %Gives the first row of numbers from the excel document(requests)
NoD=xlsread('Assignment_Data.xlsx','B2:P2'); %gives the second row from the excel document(days)

[rN,cN]=size(NoR);%This
LB=NoR(1); %This is how we achieve the lower boundary
span=3; %This represents the width of the class
HB=LB+span; %This is how we achieve the higher boundary
daycount=0;
K=1;

for i=1:cN %for loops allow an action to be carried out multiple times
    if NoR(i)<HB %if number of requests is lower than the highest boundary
        daycount=NoD(i)+daycount;
    else  %if the number of requests is not lower than ther highest boundary
        GNOD(K)=daycount;
        daycount=NoD(i);
        K=K+1;
        HB=HB+span;
    end
end
GNOD(K)=daycount;
GNOD;      %grouped number of days as shown in the data sheet
MCV=LB+span/2:span:HB-span/2;
% fprintf('MCV= ');
% fprintf('%g ',MCV);
% fprintf('\n');

%Task C 

histogram('FaceColor','b','BinEdges',LB:span:HB,'BinCounts',GNOD);
hold on %keeps data on the graph
plot(MCV,GNOD,'color',[1 0 0]);
xticks(15:1.5:33);%gives the start number, end number and difference in between numbers on the x axis
yticks(0:1:25);%gives the start number, end number and difference in between numbers on the y axis
xlabel('number of request classes');%the label on the x axis of the graph
ylabel('number of days');%the label displayed on the y axis of the graph

title('histogram and polygon distribution of data set')%the title displayed above the graph
LOC=find(GNOD==max(GNOD));
mode=MCV(LOC);
text(mode,1, ['modal value',num2str(mode)] , 'Color' , 'yellow' , 'FontSize' , 13 , 'FontWeight' , 'bold' , 'HorizontalAlignment' , 'center')
%in order of command 
%Represents mode on my graph in the form of words
%assigns my text the colour yellow
%assigns the font size 13 to my text
%assigns the bold weight to my text
%gives my text horizontal allignment in the centre of the graph

%Task D

figure(2)
T=sum(NoD); %This specifies the period of time over which the data is taken
frequency= GNOD/T; %This tells us that the frequncy is found be mutiplying GNOD by the time
Cumfrequency=cumsum(frequency, 'forward');%This shows the cumulative frequency is achieved by using cumsum which adds all frequencies together
InvCumfrequency=cumsum(frequency, 'reverse');
Cumfrequency1= 0.00 + [ 0 Cumfrequency];
Inversecum=1 - [ 0 Cumfrequency ]; %how inverse cumulative frequency is achieved
plot(LB:span:HB,Cumfrequency1,'Color',[1 1 1],'MarkerEdgeColor',[0 0 1],'Marker','o');
grid on %this applies grid lines to your graph at the whole numbers
hold on %keep the data on the graph
plot(LB:span:HB,Inversecum,'Color',[1 1 1],'MarkerEdgeColor',[1 0 0],'Marker','o');
%in order of command 
%This provided detail on which number the graph should start,end, and how big each step in between should be on the X axis 
%This provides detail that the numbers associated with my inverse cumulative frequency should be plotted on Y axis 
%This applies the desired line style (dash dotted for inverse and dashed for cumulative)
%This applies the desired colour of the lines (Blue for cumulative, red for inverse)
%This appliess the desired line width (1.5)
%This displays the raw data as a circle marker 
grid minor % This applies smaller and more grid lines
xticks(15:1:33)%gives the start number, end number and difference in between numbers on the x axi
yticks(0.10:0.10:1)%gives the start number, end number and difference in between numbers on the y axis
xlabel('Xi') %This applies a label to the x axis
ylabel('Frequencies')%This applies a label to the Y axis 
title('Ogive Distribution Frequencies') %determines what title will be placed on the graph


Yr=interp1(LB:span:HB,Inversecum,LB:0.1:HB,'pchip');
hold on 
plot(LB:0.1:HB,Yr,'Color',[1 0 0],'LineWidth',1.5,'LineStyle','--');


Yb=interp1(LB:span:HB,Cumfrequency1,LB:0.1:HB,'pchip');
hold on
plot(LB:0.1:HB,Yb,'Color',[0 0 1],'LineWidth',1.5,'LineStyle','--');



difference=abs(Yb-Yr);
Mdiff=min(difference);
index=find(difference==min(difference));

xi=LB:0.1:HB;
Median=xi(index);
disp('The Median is: ')
disp(Median)

C=(GNOD.*MCV);
NCS=(GNOD.*MCV.^2);


Tab=table({'Number of Requests xi'; 'Mid Class ci'; 'Number of Days ni'; 'Frequency'; 'cumulative frequency';'Inversely Cumulated Frequency'; 'ni.ci'; 'ni.ci^2'}, ...
    [{'[15,18[','[18,21[','[21,24[','[24,27[','[27,30[','[30,33]'};num2cell(MCV);num2cell(GNOD);num2cell(frequency);num2cell(Cumfrequency);num2cell(InvCumfrequency);num2cell(C);num2cell(NCS)]);

writetable(Tab,'Assignment_Data.xlsx','Sheet',2)

Mean=sum(C)/sum(GNOD) 

Var=sum(NCS)/sum(GNOD)- Mean^2 

SD=sqrt(Var)

