clc % clears the command window 
clear all % clears all variables as well as well as break points 
close all % close all previously run figures  

% Throughout this code ";" will be used at the end of individual lines of
% code, this is done to stop that line of code appearing in the command window  

SCR= xlsread('assignment_data.xlsx', 'B1:P1') % this imports excel data from the service call request field
Numdays= xlsread('assignment_data.xlsx', 'B2:P2') %this will import excel data from the number of days field


LB=SCR(1); % this is your lower boundary for class widths
span=3; % this is the size of your class width
HB= LB+span; % this is the higher boundary of the first class
[rN,cN]=size(SCR);
daycount=0;  % daycount is required to allow us to count up through each class group
K=1; % K is a counter that is initialised at 1, that will be used to defined different location within vector GNOD etc

for i = 1:cN % this tells matlab the number of times to repeate the for loop 
    if SCR(i) < HB % this informs Matlab which class to look in and 
        daycount=Numdays(i)+daycount; % this ensures each individual piece of 
        %information within each section of the given data is analysed 
    else % Else makes the code act in a different way if the if statement is not 
        % met, if it is not met it searches the next boundary class 
        GNOD(K)=daycount;
        K=K+1; %This 
        daycount=Numdays(i);
        HB=HB+span; % this defines new higher boundaries
    end % This ends the else section of the loop 
    
    
end % This ends the for statement completely 

GNOD(K)=daycount;   %without this line of code it will not display the last class grouped 
GNOD %This is the grouped number of data in each class 

Midclass_Value=(LB+span/2:3:HB)% this specifies the midpoint values of each specified data group 

figure(1) % this titles and creates the first plot of the work 

histogram ('BinEdges',LB:span:HB,'Bincounts',GNOD,'facecolor',[ 0 0 1]);
%This sets the parameters of the histogram
%Binedges and Bincounts   manually specifies bin edges and associated bin counts
%These are set by the LB:span:HB and GNOD
%Facecolor is set to blue by the [ 0 0 1] 
hold on% Hold on allows us to keep or save the first graph plot and allow for other charts to plot on top 
plot(Midclass_Value,GNOD,'color',[1 0 0 ],'LineWidth',1.5) 
%The above line of code plots the Midclass_Value as a line graph
%The line color is set by [1 0 0] and Line width sets the line size as 1.5 

yticks(0:1:25) % this sets the incremental step on the Y axis
xticks([15:1.5:33]) % this sets the incremental steps on the X axis 
title('Histogram and Polygon distribution of data set') % This titles the chart 
xlabel('Number of Request Classes') %This labels the X axis 
ylabel('Number of days') % This labels the Y axis
LOC=find(GNOD==max(GNOD)) % this finds the  located where 'GNOD' is equal to the 
% max of 'GNOD' 
modal=Midclass_Value(LOC) % This uses previous lines of information  to find modal value
%of given data 


text(modal,1, ['Modal Value==',num2str(modal)] ,'FontSize',13,'FontWeight','bold','HorizontalAlignment','center','color',[1 1 0])

% This above line of code inputs "Modal value onto the graph using the above formulas and functions
%, the 24-2 sets the
% location on graph, 'FontSize' dictates the size of text, 'FontWeigth' and
% 'HorizontalAllightment' set the bold style of text and positioning on
% cordinate. 'Color' then sets the colour of the text to yellow 



FrequencyDenc=sum(Numdays) % This line of code establishes the total number of individual enteries of data 
%which will allow you to work out the occurrence frequency of each group 



Frequ = GNOD/FrequencyDenc% Connected to the above line of code this calculates the frequency of each group 



Cum_frequ= cumsum  (Frequ); % the cumsum function is the cumulative sum of the frequency 

Cum_frequ1= 0.00 +  [ 0 Cum_frequ]% this displays the cumulative frequency data the 0.00 at the start adds the 0 figure to the data 


 

Inv_cum = 1 - [ 0 Cum_frequ ]% This line displays the Inverse frequency. The initial 0 value at the start of the square brackets adds the first figure of 1.0 to the result 

 
Inv2=Inv_cum(:, [1:1:6])%  This ensures the vector array will display the 
%correct six figures

figure(2)% This starts a a new graph plot 



plot ([15:3:33],Cum_frequ1,'LineStyle', 'none','Color',[0 0 1],'Marker','o'), grid on 
%The above code plots the Cum_frequ1 data, linestyle is set to no line
%the color is set to blue 
%The style of data markers is set by the "o"
%grid on displays the major grid lines on the plot 
hold on %this holds the above plot on the figure allowing for multiple plots to be charted on the same graph 

Smooth=interp1([15:3:33],Cum_frequ1,15:0.1:33,'pchip');
% this above line of code maps multiple plots on the Ogive chart creating
% the smooth appearance of the Cum-Frequ1 plot 

plot(15:0.1:33,Smooth,'LineStyle', ' - - ','Color',[0 0 1],'LineWidth',1.5)
%The above code plots the Cum_frequ1 data, linestyle is set to '- - ' or
%dashed line 
%the color is set to blue and the line width sets the size of the line to
%1.5
%The style of data markers is set by the "o"
%grid on displayes the major grid lines on the plot 
hold on %this holds the above plot on the figure allowing for multiple plots to be charted on the same graph 

plot ( [15:3:33],Inv_cum,'LineStyle', 'none', 'Color',[ 1 0 0],'LineWidth',1.5,'Marker','o'), 
%The above code plots the Inv_cum data, linestyle is set to no line 
%dot and dash style
%the color is set to red using  [1 0 0]  and the line width sets the size of the line to
%1.5
%The style of data markers is set by the "o"


Smooth2=interp1([15:3:33],Inv_cum,15:0.1:33,'pchip');
% this above line of code maps multiple plots on the Ogive chart creating
% the smooth appearance of the Inv_cum line 

plot(15:0.1:33,Smooth2,'LineStyle', '-.', 'Color',[ 1 0 0],'LineWidth',1.5)
%The above code plots the Cum_frequ1 data, linestyle is set to ' -.' or
%dash dot 
%the color is set to blue and the line width sets the size of the line to
%1.5
%The style of data markers is set by the "o"
%grid on displays the major grid lines on the plot 
hold on %this holds the above plot on the figure allowing for multiple plots to be charted on the same graph 

grid minor % This inputs the minor grid markings on Figure 2 
xticks(15:1:33)%Xticks sets the range on the X axis
yticks(0.10:0.10:1) %Yticks sets the range on the Y axis 
xlabel('Xi') % this labels the X axis on Figure 2
ylabel('Frequencies') % this labels the Y axis on Figure 2 
title('Ogive Distribution Frequencies') % This inserts the the title on Figure 2 



difference=abs(Cum_frequ1 - Inv_cum) % This calculates the difference by subtracting the Inv_cum data 
% from the Cum_freq1

index=find(difference==min(difference)); % here find is used to find where 
%difference is equal to the minimum difference 

XR=15:3:33; % this sets the parameters of the search 

XR(index); % looks at index with in parameters of XR

disp('The Median value on the Ogive plot is') % This displays the line of text 
% 'The Median value on the Ogive plot is'
disp(index)   % This displays the index result 


Num_midclass= Midclass_Value .* GNOD % This multiplies GNOD by midclass value
% to create a line of date required for later calculations involving the 
%mean variance and standard deviation of the grouped data  
format shortG % this formates the information to display in short form 

Num_midclasssquare= Num_midclass .* Midclass_Value % This multiplies the 
%Num_midclass vale by the Midclass_value again to create information
%required for later calculating the mean variance and standard deviation of
%the grouped data 


TABLE=table([{'Number of Requests xi'},{'[15,18['},{'[18,21['},{'[21,24['},{'[24,27['},{'[27,30['},{'[30,33]'};...
    {'Mid Class value Ci'},num2cell(Midclass_Value); ...
    {'Number of Days  Ni'},num2cell(GNOD); ...
    {'Frequency'},num2cell(Frequ); ...
    {'Cumulative Frequency'},num2cell(Cum_frequ); ...
    {'Inverse Cumulated Frequency'},num2cell(Inv2); ...
    {'Ni* Ci'},num2cell(Num_midclass); ...
    {'Ni * Ci ^ 2'},num2cell(Num_midclasssquare)]);

% This uses the previously created functions through the project to create
% a table 

  
TABLE=splitvars(TABLE) % 'Splitvars' takes the grouped vector data 
% from the functions created earlier to display the information in
% individual cells, as seen in the above table 

writetable(TABLE,'Assignment_Data.xlsx','Sheet',2) % This writes the table 
% created to the excel document the 'Sheet' and '2' dictates which page of
% the spreadsheet the table will be written to. In this case sheet 2 

 

Mean_midclass1 = sum (Num_midclass); % This adds the total sum of the 
%Num_Midclas


Mean_group = Mean_midclass1 / FrequencyDenc;  % This divides the Mean-midclasss
% by FrequenyDenc which in this case is 60 

disp('The Mean value of the grouped data is') % This displays the text 
%'The Mean value of the grouped data is'
disp(Mean_group) % This displays the figure for the Mean of the grouped data 

 

Var_1= sum(Num_midclasssquare); % this calculates the total sum of the 
% Ni * Ci ^2 vector 


Var_2= Mean_midclass1 ^2;% this caluclates the squared value of Mean_midclass1 

Var_3= Var_2 / FrequencyDenc ; % This calculates Var_3 divided by frequency density 


Var_4= Var_1 - Var_3; % this subtracts Var_3 from Var_1 

Var_5= Var_4 / FrequencyDenc; % this divides the total of 'Var_4' by
% the total number of occurrences across the grouped data( in this case 60)

disp('The Variance of the grouped data is') % this displays the text 
%'The Variance of the grouped data is'

disp(Var_5) % This displays the variance of the grouped data 




stnd_dev= sqrt(Var_5); % This calculates the standard deviation of the 
%grouped data by finding the square root using the 'squrt' function on the variance  
disp('The Standard Deviation of the grouped data is') 
% this displays the text 'The Standard Deviation of the grouped data is'
disp(stnd_dev) % this displays the Standard Deviation figure
