clc % this clears command window 
clear all % clears all variables 
close all % This will close all figures run previously within this code 

% Throughout ";" will be used at the end of individual lines 
% this prevents that line appearing in the command window  

SCR= xlsread('assignment_data.xlsx', 'B1:P1') % this imports the data from the service call request field outlined in the provided assessment 
Numdays= xlsread('assignment_data.xlsx', 'B2:P2') %this will import excel data from the number of days field outlines in the provided assessment

LB=SCR(1); % this is the lower boundary for class widths
span=3; % this is the size of the given class width
HB= LB+span; % this is the higher boundary for your loop
[rN,cN]=size(SCR); 
daycount=0;  % the daycount is required to allow us to count up through each class group
K=1; % K is the provided constant - 1

for i = 1:cN % this informs Matlab of the number of times to repeat the for loop 
    if SCR(i) < HB % this informs Matlab the class in which to look
        daycount=Numdays(i)+daycount;
    else % else function pushes the code in another direction should the parameter not be met
        GNOD(K)=daycount;
        K=K+1;
        daycount=Numdays(i);
        HB=HB+span; % this allows for the definition of new higher boundaries
    end % This ends the else section of the loop 
       
end % this end function will close the for loop completely

GNOD(K)=daycount;   %this line of code is needed to 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 names and creates the first plot of the given data 

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 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 save the first graph plot and allow for other charts to be plotted
plot(Midclass_Value,GNOD,'color',[1 0 0 ],'LineWidth',1.5) 
%The above code plots the Midclass_Value as a line graph
%The line color is set [1 0 0] and Line width sets the line size

yticks(0:1:25) % this sets the incremental step on the Y axis
xticks([15:1.5:33]) % this sets the incremental step on the X axis 
title('Histogram and Polygon distribution of data set') % This names chart
xlabel('Number of Request Classes') %This names X axis 
ylabel('Number of days') % This names Y axis
LOC=find(GNOD==max(GNOD)) % find highlights where GNOD is equal to 
% the upper limit of GNOD value
modal=Midclass_Value(LOC)
% this uses previous function to find modal value

text(modal,1, ['Modal Value==',num2str(modal)] ,'FontSize',13,'FontWeight','bold','HorizontalAlignment','center','color',[1 1 0])
% This above line inputs "Modal value onto the graph, the 24-2 sets the
% location, 'FontSize' provides the text size, 'FontWeigth' and
% 'HorizontalAllightment' set the bold text and positioning on
% the coordinate. 'Color' then sets to yellow 



FrequencyDencity=sum(Numdays) % This line of code establishes the total number of individual data entries
%this allows you to work out the occurrence frequency of each group 

Frequency = GNOD/FrequencyDencity% Connected to the above code this calculates the frequency of each grouping

Cumulative_frequency= cumsum  (Frequency); % cumsum function is the cumulative sum of the frequency 

Cum_frequ_1= 0.00 +  [ 0 Cumulative_frequency]% this displays the cumulative frequency data the 0.00 at the start adds the 0 figure to the data 

Inverse_cum = 1 - [ 0 Cumulative_frequency ]% This displays the Inverse frequency. The initial 0 value of the square brackets adds the first figure of 1.0 to the result 
 
Inverse2=Inverse_cum(:, [1:1:6]) 

figure(2)% This starts a new graph plot 

plot ([15:3:33],Cum_frequ_1,'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 and the line width sets the line to
%1.5
%The data markers style is set by the "o"
%grid on displays the major grid lines of the plot 
hold on %this holds the above plot allowing for multiple plots to be charted on the same graph 

Smooth=interp1([15:3:33],Cum_frequ_1,15:0.1:33,'pchip');

plot(15:0.1:33,Smooth,'LineStyle', ' - - ','Color',[0 0 1],'LineWidth',1.5)

hold on
plot ( [15:3:33],Inverse_cum,'LineStyle', 'none', 'Color',[ 1 0 0],'LineWidth',1.5,'Marker','o'), 
%The above plots the Inv_cum data, linestyle is set to no line 
%dot and dash style
%the color is set to red  [1 0 0]  and the line width sets the line to
%1.5
%The style of data markers is set by the "o"

Smooth2=interp1([15:3:33],Inverse_cum,15:0.1:33,'pchip');

plot(15:0.1:33,Smooth2,'LineStyle', '-.', 'Color',[ 1 0 0],'LineWidth',1.5)

grid minor % This inputs the minor grid markings of Figure 2 
xticks(15:1:33)%Xticks sets the range of X axis
yticks(0.10:0.10:1) %Yticks sets the range of Y axis 
xlabel('Xi') % this labels the X axis
ylabel('Frequencies') % this labels the Y axis 
title('Ogive Distribution Frequencies') % This inserts the title



difference=abs(Cum_frequ_1 - Inverse_cum) % cum_freq1 minus inv_cum

index=find(difference==min(difference)); %finds where difference is equal to
%minimum difference 

XR=15:3:33; %this sets limits of search 
XR(index);% this looks at the index with the parameters set by XR 


disp('The Median value of Ogive plot is')
disp(index)


Num_midclass= Midclass_Value .* GNOD

format shortG 
Num_midclasssquare= Num_midclass .* Midclass_Value 

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(Frequency); ...
    {'Cumulative Frequency'},num2cell(Cumulative_frequency); ...
    {'Inverse Cumulated Frequency'},num2cell(Inverse2); ...
    {'Ni* Ci'},num2cell(Num_midclass); ...
    {'Ni * Ci ^ 2'},num2cell(Num_midclasssquare)])

% Above code created from previous functions 
  
TABLE=splitvars(TABLE) %This splits the table into individual columns

writetable(TABLE,'Assignment_Data.xlsx','Sheet',2)% writes TABLE to specified document 

Mean_midclass1 = sum (Num_midclass); %this is the sum of Num_midclass


Mean_Group_DATA = Mean_midclass1 / FrequencyDencity ; %this divides the Mean_midclass by
%the FrequencyDenc

disp('The Mean value of the data is') 
disp(Mean_Group_DATA)

Variance_1= sum(Num_midclasssquare); % this calculates the sum of the 
% Ni * Ci ^2

Variance_2= Mean_midclass1 ^2; % this displays the mean of the midclass range

Variance_3= Variance_2 / FrequencyDencity ; % This displays the frequency density 

Variance_4= Var_1 - Variance_3; % this subtracts Var_3 from Var_1 

Variance_5= Variance_4 / FrequencyDencity; % this divides the total of 'Var_4' by
% the total number of occurrences

disp('The Variance of the grouped data is')
disp(Variance_5) % This displays the variance of group data 

standard_dev= sqrt(Variance_5); % This calculates the standard deviation of the 
%grouped data by finding square root using the 'squrt' function on the variance  
disp('The Standard Deviation of the data is') 
disp(standard_dev) % this displays the Standard Deviation figure
