%Engineering Application & Study Skills. 
%M4X02063: Richard Leadbeater, 1902951

clc
clear all % when starting a script the clear all commands are used to clear the command window.

%To extract data from external files the xlsread is used, the file has to
%be in the "current folder" with the name and location of the cell in the
%bracks seen below. 
NoR = xlsread('AssignmentData.xlsx','B1:P1');%Gives the first row of "Numbers of service calls requires" in excel document.
NoD = xlsread('AssignmentData.xlsx','B2:P2');%Gives the second row of "Numbers of days" in excel document.
%The values are set with making veribles below.
[rN,cN]=size(NoR);
LB=NoR(1); %Lower boundary is the Number of service calls request.
span=3; %Number between class.
HB=LB+span; %Higer boundary  is Lower boundary plus the number between class.
daycount=0;
k=1;

%An "agrument" is writing below so that figure 1 can be created.
for i=1:cN;
    if NoR(i)<HB;
        daycount=NoD(i)+daycount;
    else
        GNOD(k)=daycount;
        daycount=NoD(i);
        k=k+1;
        HB=HB+span;
    end
end
GNOD(k)=daycount;
GNOD;
MCV=LB+span/2:span:HB-span/2;
% fprintf('MCV= ')
% fprintf('%g',MCV)
% fprintf('\n')

figure(1);
histogram('FaceColor','b','BinEdge',LB:span:HB,'BinCounts',GNOD);%Histogram bar color is done by Facecolor, Binedge gives vectors of histogram. 
yticks([ 0:1:25]);
xticks([ 15:1.5:33]);%x & Y ticks gives limits to both axis. 
hold on %This term is used so coding can continue without matlab rewriting code. 
plot(MCV,GNOD,'color',[1 0 0],'LineWidth',1.5);%This plot puts the red line marking highest points on the histogram. 
xlabel('Number of Repuest Classes');
ylabel('Number of Days');
title('Histogram and Polygon Distribution of Data Set');%labeling & titles for graph.
LOC=find(GNOD==max(GNOD));
Mode=MCV(LOC);% Mode is calucated. 
text(25.5,2,['Modal value=',num2str(Mode)],'Color',[1 1 0],'FontWeight','bold','FontSize',13,'HorizontalAlignment','center');%This inputs the "Mode/modal value" onto the graph.

n=sum(NoD);
FRQ=GNOD/sum(NoD);%Freq of Group.

CF=cumsum(FRQ);
CFstart=[0 CF];%Cum Freq of data at start


figure(2)
ICF=cumsum(FRQ,'reverse');
ICFend=[ICF 0];%Inverse Cum Freq.

plot([LB:span:HB],ICFend,'Color',[1 1 1],'MarkerEdgeColor',[1 0 0],'Marker','o');%This part of code plots the markers of inverse cum freq onto the second figure.
hold on
Y1=interp1(LB:span:HB,ICFend,LB:0.1:HB,'pchip');%This plots a smooth set of data onto figure. 
plot(LB:0.1:HB,Y1,'LineStyle','-.','Color',[1 0 0],'LineWidth',1.5);%The set of data is given color width and style which can be chanced to prefference 
hold on
plot([LB:span:HB],CFstart,'Color',[1 1 1],'MarkerEdgeColor',[0 0 1],'Marker','o');%The same set of code to plot the cum freq or other set of data. 
hold on
Y2=interp1(LB:span:HB,CFstart,LB:0.1:HB,'pchips');
plot(LB:0.1:HB,Y2,'LineStyle','--','Color',[0 0 1],'LineWidth',1.5);
xlabel('xi');
ylabel('Frequencies');
title('Ogive Distribution Frequenceies');%Titles and labels for figure 2.
xticks([LB:1:HB]);%limits set for x axis. 
grid on

diff=abs(Y2-Y1);
mindiff=min(diff);
ind=find(diff==min(diff));

xi=LB:0.1:HB;
median=xi(ind)%median is found by using the coding above. 

nc=GNOD.*MCV;
nc2=GNOD.*MCV.^2;

T=table({'Number of Requests xi';'Mid Class ci';'Number of Days ni';'Frequency';'Cumulated 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(FRQ);num2cell(CF);num2cell(ICF);num2cell(nc);num2cell(nc2)]);

writetable(T,'AssignmentData.xlsx','Sheet',2);
%writetable is used to export the codes data into the excel spreadsheet
%Table is used to set the correct cell positions and labeling of cells in
%exel document. 
M1=sum(nc);
Mean=M1/sum(NoD)%calucation to work out mean

Variance=sum(nc2)/sum(n)-Mean.^2%Calucation to work out Variance

SD=sqrt(Variance)%Calucations to work out Standard deviation. 

