clc
clear all
%Always begin by clearing the script

%PART A

NoR=xlsread('Assignment_Data.xlsx','sheet1','B1:P1')
%Imports 'Number of service call requests' data from range B1:P1 in
%Excel into Matlab using function xlsread

NoD=xlsread('Assignment_Data.xlsx','sheet1','B2:P2')
%Imports 'Number of days' data from range B2:P2 in Excel into Matlab using
%function xlsread

[rN,cN]=size(NoR);
%Size function is used to define the number of rows and columns in vector
%'NoD'; rN for number of rows and cN for number of columns

%PART B

LB=NoR(1); %The first lower boundary is in row 1 of vector NoR
span=3; %3 is the width of the class
HB=LB+span; %The higher boundary is the lower boundary plus the class width
daycount=0; %To begin 0 is the dacount as 0 days have been accounted for
K=1; %K represents the place/column number of new vector GNOD. To begin,
%K=1 so the data will be entered in column 1

for i=1:cN
        %Defines the range between 1 and 15 which is the number of columns in
        %the vectors
    if  NoR(i)<HB
        daycount=NoD(i)+daycount;
        %If the number of service call requests defined by i is less than the
        %higher boundary of the group then the daycount is the number of days
        %defined by i plus the previously stated daycount
    else
        %If the statement NoR(i)<HB is false aas cirteria is not met then
        %the code in the else statement is executed
        GNOD(K)=daycount; %The current daycount represents the next figure
        %in vector GNOD
        daycount=NoD(i); %The new daycount is defined by the next value in
        %vector NoD
        K=K+1; %Defines the new value for K; K increases by 1 for each new
        %column in GNOD
        HB=HB+span; %Defines the new high boundary to represent data in
        %the next group
    end
end
GNOD(K)=daycount;
%Accounts for the last fugure/column in vector GNOD
GNOD
%Displays complete vector GNOD

MCV=LB+span/2:span:HB-span/2;
%The first value is the lower boundary plus half the width of the class
%The step is the width of the class (3)
%The last value is the higher boundary minus half the width of the class
MCV
%Displays vector MCV

%PART C

figure(1)
%Plots graphs on 1 figure until next figure (figure2) is set
histogram('BinEdges',15:3:33,'BinCounts',GNOD,'EdgeColor','k','FaceColor','b')
%Creates a histogram
%Bin edges determine the range that the bars cover; startnig at 15 up to 33 
%with a step of 3
%Bincounts determine the height of each bar which comes from GNOD
%EdgeColor k sets the outline colour to black
%FaceColor b sets the bar colours to blue
ylim([0,25])
%Sets the displayed range of the y axis ; 0 to 25

hold on
%Retains current plot and axes so new graphing commands can be added to the
%exisitng graph
plot(MCV,GNOD,'r','LineWidth',1.5)
%PLots a line graph on top of histogram
%X axis values as the midpoint values as per MCV
%Y axis values as the highest point of each bar as per GNOD
%'r' states that the line colour is red
%LineWidth command is used to display line width of 1.5
xticks([15:1.5:33])
%Specifies a label for each tick mark on x axis; 15 to 33 with step of 1.5
yticks([0:1:25])
%Specifies a label for each tick mark on y axis; 1 to 25 with step of 1
title('Histogram and Polygon Distribution of data set')
%Displays main heading as per inverted commas
xlabel('Number of Service Call Request Classes')
%Displays x axis label as per inverted commas
ylabel('Number of days')
%Displays y axis label as per inverted commas

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')
%Displays text on the graph:
%Displayed at location 25.5,2)
%Text displayed is 'Modal Value' followed by the previously calculated mode 
%displayed as text (in this case 25.5)
%Text edits are; yellow colour, size 13, bold and central alignment

%PART D

figure(2)
%Plots graphs on a new figure

Cum=(cumsum(GNOD)/sum(GNOD))
%The cumulative data is displayed in Cum. Cumsum function calculates the
%cumulative values, then each value is divided by the total number of days
y1=[0,Cum];
%This is to ensure X values start on 0 at the bottom of the graph

Inv=(cumsum(GNOD,'reverse')/sum(GNOD))
%The inverse cumulative data is displayed in Y2. The cumsum reverse command
%calcualtes the inverse cumulative values, then each value is divided by 
%the total number of days 
y2=[Inv,0];
%This is to ensure X values end on 0 at the bottom of the graph

X=[LB:span:HB];
%This defines X values as all multiples of 3 from the lower boundary to the
%upper boundary

plot(X,y1,'Marker','o','Color',[1 1 1],'MarkerEdgeColor',[0 0 1])
%Displays the cumulative frequency graph with required marker style/colour
hold on
%Retains current plot and axes so new graphing commands can be added to the
%exisitng graph
plot(X,y2,'Marker','o','Color',[1 1 1],'MarkerEdgeColor',[1 0 0])
%Displays the inverse cumulative frequency graph with all specified styles
xticks([15:1:33])
%Specifies a label for each tick mark on x axis; 15 to 33 with step of 1
title('Ogive Distribution Frequencies')
%Displays main heading as per inverted commas
xlabel('xi')
%Displays x axis label as per inverted commas
ylabel('Frequencies')
%Displays y axis label as per inverted commas
grid on
%Displays grid lines at every whole number
grid minor
%Displays more and smaller grid lines

Y1=interp1(X,y1,15:0.1:33,'pchip');
plot(15:0.1:33,Y1,'LineStyle',' - - ','Color',[0 0 1],'LineWidth',1.5)
%Plots the cumulative data in a smooth curve with all requred line styles 
%and colour

Y2=interp1(X,y2,15:0.1:33,'pchip');
plot(15:0.1:33,Y2,'LineStyle','-.', 'Color',[ 1 0 0],'LineWidth',1.5)
%Plots the inverse data in a smooth curve with all requred line styles 
%and colour

%PART E

diff=abs(y1 - y2);
%Finds the difference
index=find(diff==min(diff));
%Finds the location of the minimum value
Median=X(index)
%Dsiplays the median by returning the value of X from location of minimum
%difference

%PART F

Freq=GNOD/sum(NoD);
%Calculates the frequency values so they can be entered in the table
Num_MC=MCV .* GNOD;
%Calculates desired 'Ni.Ci' value
format shortG 
%This specifies that the format used should be that which Matlab decides
%has the most compact display
Num_MCsquare= Num_MC .* MCV;
%Calculates desired 'Ni.Ci' squared value

TABLE=table([{'Number of Requests xi'},{'[15,18['},{'[18,21['},{'[21,24['},{'[24,27['},{'[27,30['},{'[30,33]'};...
    {'Mid Class value Ci'},num2cell(MCV); ...
    {'Number of Days  Ni'},num2cell(GNOD); ...
    {'Frequency'},num2cell(Freq); ...
    {'Cumulative Frequency'},num2cell(Cum); ...
    {'Inverse Cumulated Frequency'},num2cell(Inv); ...
    {'Ni.Ci'},num2cell(Num_MC); ...
    {'Ni.Ci ^ 2'},num2cell(Num_MCsquare)]);
%Each line here specifies the Row name in the table as stated in quotation 
%marks, followed by the data that is to be displayed in parallel rows. The 
%num2cell function converts each array of numbers into separate cell arrays   
TABLE=splitvars(TABLE)
%We use the splitvars function to split columns up into a neat table

writetable(TABLE,'Assignment_Data.xlsx','Sheet',2)
%Exports the table into page 2 of the desired excel document

%PART G

Mean=sum(Num_MC)/sum(NoD)
%Calculates the mean by dividing the total frequency by the number of days 

Var1=sum(Num_MCsquare);
Var2=(sum(Num_MC))^2;
Var3=Var2/sum(NoD);
Var4=Var1 - Var3;
Variance=Var4/sum(NoD)
%Calculates the Variance

Standard_Deviation=sqrt(Variance)
%Calculates the standard deviation; we know standard deviation is just the
%square toot of the variance so this is the function carried out.

