% Sample data - how to create histograms in MatLab:
% Barukyah Shaparenko and John M. Cimbala

clear all
close all

% Import from Excel
% Save Excel data file in same folder as MatLab m-file

% sheet_number and data_range are optional
% Default is Sheet 1 with all (numerical) data imported

xls_filename = 'Temperature_data_analysis.xls';
sheet_number = 1;
data_range = 'B5:B1004';
T = xlsread(xls_filename,sheet_number,data_range);
x=T;

% Direct calculation of statistics:
average = mean(T);
stdev = std(T);


% Histogram:
% # bins = 10
[n1,Tout1] = hist(T);
% n = frequency, xout = bin centers
width = 1; % No gaps between bars

figure(1)
bar(Tout1,n1,width);
title('Histogram','FontWeight','Bold','FontSize',16)
xlabel({'Bin' 'T, °C'},'FontWeight','Bold')
ylabel('Frequency','FontWeight','Bold')
grid


% Nice Histogram:
nbins = 20;
left_edge = 26.0;
bin_width = 0.5;
right_edge = left_edge + nbins*bin_width;
nbins = nbins + 2; % Add bins for x <= left_edge and x > right_edge
bin_centers = [left_edge - 0.5*bin_width:bin_width:right_edge + 0.5*bin_width];
n = hist(T,bin_centers);

figure(2)
bar(bin_centers,n,width)
xmin = left_edge - bin_width;
xmax = right_edge + bin_width;
title('Nice Histogram','FontWeight','Bold','FontSize',16)
xlabel('Bin','FontWeight','Bold')
ylabel('Frequency','FontWeight','Bold')
xlim([xmin xmax])
grid


% Vertically normalized PDF:
Probability = n/length(x);
f_x = Probability/bin_width;

figure(3)
plot(bin_centers,f_x,'o')
title('Vertically normalized PDF','FontWeight','Bold','FontSize',16)
xlabel('x','FontWeight','Bold')
ylabel('f(x)','FontWeight','Bold')
grid


% Normalized PDF:
for i = 1:length(bin_centers)
    z(i) = (bin_centers(i) - average)/stdev;
    f_z(i) = stdev*f_x(i);
end

figure(4)
plot(z,f_z,'bo')
title('Normalized PDF','FontWeight','Bold','FontSize',16)
xlabel('z','FontWeight','Bold')
ylabel('f(z)','FontWeight','Bold')
grid


% Compare with the standard Normal (Gaussian) PDF:
Z = -4:0.05:4;
for i = 1:length(Z)
    F_Z(i) = exp(-Z(i)^2/2)/sqrt(2*pi);
end

figure(5)
hold on
plot(z,f_z,'bo')
plot(Z,F_Z,'r-','LineWidth',2)
title('Comparison with Gaussian PDF','FontWeight','Bold','FontSize',16)
xlabel('z','FontWeight','Bold')
ylabel('f(z)','FontWeight','Bold')
grid
hold off

figure(6)
plot(Z,F_Z,'r-','LineWidth',2)
title('Standard Normal Distribution Function','FontWeight','Bold','FontSize',16)
xlabel('z','FontWeight','Bold')
ylabel('f(z)','FontWeight','Bold')
grid