clc
clear all
%A script in Matlab should always begin with the above two commands to
%clear any existing data that could affect the new script. 

noR=xlsread('Assignment_Data.xlsx','B1:P1')%This imports the data from the first line of the Excel sheet and places it in vector noR.
noD=xlsread('Assignment_Data.xlsx','B2:P2')%This imports the data from the second line of the Excel sheet and places it in vector noD. 

[rn,cn]=size(noR);%The 'size' function tells us the size of the matrix.  This matrix is a vector with 1 row and 15 columns. 

LB=noR(1);%This states that the lower boundary is the first number in the vector noR. 
span=3; %This defines the class width.
HB=LB+span; %This defines the higher boundary.
daycount=0;
k=1;
T=60; %T describes the unit of time that the data was gathered within.  In this instance it was 1 semester which is equal to 60 days.

    for i=1:cn %This provides the range for the for loop that will start at 1 and end once it has completed all of the values in cn.
        if noR(i)<HB  
            daycount=noD(i)+daycount; %This states that if the value of noR is smaller than the HB then the daycount will equal the value of noD plus the daycount.  
        else GNOD(k)=daycount; %If the value of noR is not smaller than the HB then the vector GNOD is created. 
            daycount=noD(i);
            k=k+1;
            HB=HB+span;
        end
    end
GNOD(k)=daycount;

disp('GNOD =')
disp(GNOD)%This displays the vector GNOD.

MCV=LB+span/2:span:HB-span/2 %This defines the vector MCV.  The Lower Boundary plus the span defines the starting point of the range.  The step is defined by the span and the end point of the range is defined by the Higher Boundary minus half of the span.  

histogram('BinEdges',LB:span:HB,'BinCounts',GNOD,'FaceColor',[0 0 1])%This creates the histogram.  The bin edges are defined by the starting range being the Lower Boundary, the step being equal to the span and the last value being the Higher Boundary.  The 'FaceColor' followed by the RGB triplet [0 0 1] creates a histogram with blue bars. 
hold on %This keeps the above histogram and allows another layer to be added on to the graph. 
plot(MCV,GNOD,'color',[1 0 0],'LineWidth',1.5)%The 'plot' function creates the polygon distribution graph and places it over the top of the histogram.  The 'color' element followed by the RGB triplet [1 0 0] makes the line a red colour. 

title("Histogram and Polygon Distribution of Data Set")%This function set the title of the histogram to "Histogram and Polygon Distribution of Data Set".

yticks(0:1:25)%This defines the labelling of the y axis.  This starts at 0 and increases in a step of 1 until it gets to 25. 
ylabel("Number of Days")

xticks(LB:span/2:HB)%This defines the labelling of the x axis.  This starts at the Lower Boundary and increases in a step that is half of the span and ends when it reachers the Higher Boundary.
xlabel("Number of Request Classes")

text(22.5,2,"Modal Value 25.5",'FontSize',13,'color',[1 1 0],'FontWeight','bold') %This places the text "Modal Value 25.5" after 24 on the x axis and after 2 on the y axis. It also states that the font size is 13 and the vector [1 1 0] ensures that the text is in yellow.  the 'FontWeight' being bold ensures that the text appears on the histogram in a bold font.  

FREQUENCY=GNOD/T; %This allows us to work out the frequency.  This is done by dividing GNOD by the time period which is 60 days. 
disp('FREQUENCY =')
disp(FREQUENCY)%This displays the frequency as a vector.

cumulative=cumsum(FREQUENCY);%The cumsum function allows us to calculate the cumulative frequency and put the answers into another vector.
cumulativeEXTRA=[0,cumulative];%This ensures the vector starts with a 0.
disp("cumulative extra =")
disp(cumulativeEXTRA)%The cumulative extra vector has been created to ensure that the vectors for x and why are the same length.  This is because if a graph is created it must have vectors of the same length. 

inverse=cumsum(FREQUENCY,'reverse'); %This works out the inverse cumulative frequency.
inverseEXTRA=[inverse,0]; %This ensures the vector ends with a 0.
disp("inverse cumulative frequency extra =")
disp(inverseEXTRA)

figure(2)
plot([LB:span:HB],cumulativeEXTRA,'o','markeredgecolor','blue','LineWidth',1.5)%This plots the cumulative frequency which will run from 0 to 1 and from left to right on the ogive.  It also specifies that it wants blue marker circles with a line width of 1.5 on every value.
hold on
plot([LB:span:HB],inverseEXTRA,'o','markeredgecolor','red','LineWidth',1.5)%This plots the inverse cumulative frequency which will run from 1 to 0 and from right to left on the ogive. It also specifies that it wants red marker circles with a line width of 1.5 on every value. 
grid on %This applies the gridded background to the graph.
xlabel("xi")%This labels the x axis 'xi'. 
ylabel("Frequency") %This labels the y axis 'frequency'.
xticks([LB:1:HB])%This labels the x axis from the lower boundary to the upper boundary in a step of 1. 
title('Ogive Distribution Frequencies')%This displays the title, 'Ogive Distribution Frequencies' at the top of the graph.

Yi=interp1(LB:span:HB,cumulativeEXTRA,LB:0.1:HB,'pchip'); %The interp1 function is used along with the pchip function to smooth the curve.  It does this by adding values from the lower boundary to the upper boundary in a step of 0.1.
hold on
plot(LB:0.1:HB,Yi,'color','blue','LineWidth',1.5,'LineStyle','--');%This applies the new smoothed line and includes the functions to set the colour of the line to blue, have a line width of 1.5 with a dashed line style. 

Yr=interp1(LB:span:HB,inverseEXTRA,LB:0.1:HB,'pchip');%The interp1 function is used along with the pchip function to smooth the curve.  It does this by adding values from the lower boundary to the upper boundary in a step of 0.1.
hold on
plot(LB:0.1:HB,Yr,'color','red','LineWidth',1.5,'LineStyle','-.');%This applies the new smoothed line and includes the functions to set the colour of the line to red, have a line width of 1.5 with a dashed dotted line style. 
 
difference=abs(Yi-Yr); %This is finding all of the values of difference between the Yi and Yr lines.
mindiff=min(difference); %This locates the point at which the difference between the two lines is at its smallest. 
index=find(difference==min(difference));

xi=LB:0.1:HB; %This defines that xi starts at the lower boundary which is 15 and ends at the higher boundary which is 33 and increases with a step of 0.1.
median=xi(index);%This function calculates the median number.
disp('The median value =')%This displays the text, 'The median value ='.  This is used to lay out an answer in a clear way. 
disp(median)%This displays the median value underneath the text 'The median value ='.

%Ni.Ci=(num2cell(GNOD)).(num2cell(MCV));  
%Ni.Ci2=(GNOD.((MCV)*(MCV));

%The above has attempted to work out Ni.Ci and Ni.Ci squared.  However, I
%have been unable to figure this out. 

%The below T2 is creating a table that is 9x7.
T2(1,:)={'Var1_1' 'Var1_2' 'Var1_3' 'Var1_4' 'Var1_5' 'Var1_6' 'Var1_7'};
%The above adds the titles for each column. 

T2{2,1}='Number of Requests xi';
T2{3,1}='Mid Class ci';
T2{4,1}='Number of Days ni';
T2{5,1}='Frequency';
T2{6,1}='Cumulated Frequency';
T2{7,1}='Inversely Cumulated Frequency';
T2{8,1}='ni.ci';
T2{9,1}='ni.ci^2';
%The above defines what is going in each of the rows as column one
%descends. 

T2{2,2}='[15-18[';
T2{2,3}='[18-21[';
T2{2,4}='[21-24[';
T2{2,5}='[24-27[';
T2{2,6}='[27-30[';
T2{2,7}='[30-33]';
%The above sets classes in a span of 3 across the first row of the table. 

T2(3,2:7)=num2cell(MCV);%This applies the mid class value vector to the second row of the table starting from column 2 and ending at column 7.
T2(4,2:7)=num2cell(GNOD);%This applies the grouped number of days vector to the third row of the table starting from column 2 and ending at column 7.
T2(5,2:7)=num2cell(FREQUENCY);%This applies the frequency vector to the fourth row of the table starting from column 2 and ending at column 7.
T2(6,2:7)=num2cell(cumulative);%This applies the cumulative frequency vector to the fifth row of the table starting from column 2 and ending at column 7.
T2(7,2:7)=num2cell(inverse);%This applies the inverse cumulative frequency vector to the sixth row of the table starting from column 2 and ending at column 7.
%T2(8,2:7)=num2cell(Ni.Ci)
%T2(9,2:7)=num2cell(Ni.Ci2)
%The above two would have applied the Ni.Ci and Ni.Ci squards vectors to
%the table, however, these had not been successfully worked out. 

sheet = 2; %This specifies the sheet number ready for when the information is transferred back into the 
xlswrite('Assignment_Data.xlsx',T2,sheet)

Mr = mean(noR);%This is working out the mean number of service call requests.
disp('Mean number of service call requests =')
disp(Mr)

Md = mean(noD);%This is working out the mean number of days.
disp('Mean number of days =')
disp(Md)

Sr = std(noR);%This is working out the standard deviation of the number of service call requests.
disp('Standard deviation of service call requests =')
disp(Sr)

Sd = std(noD);%This is working out the standard deviation of the number of days. 
disp('Standard deviation of days =')
disp(Sd)

Vr = var(noR);%This is working out the variance of the number of servie call requests.
disp('Variance of service call requests =')
disp(Vr)

Vd = var(noD);%This is working out the variance of the number of days.
disp('Variance of days =')
disp(Vd)