Histcounts not giving me the same answers as my histogram
Show older comments
Hi all, I plotted a histogram, and normalized it as a probability. I wanted to get those probabilities through code instead of looking at the bins manually, but when I use histcounts it doesn't seem to go correctly. I just get 100% probabilities. I need to keep the bins separate as individual histograms. I'm trying to create a mapping from delta_h (also referred to as EFH) to IFH through MC simulation
%% Monte Carlo Simulations for IFH
n = 100000;
g = 32.2; %ft/s^2
Cd = 0.94;
af = [0.01 0.95 0.99]; %Flow Areas
time = 3600; % Time duration
l_int = 25; %Length of room [ft]
w_int = 25; %Width of room [ft]
A_int = l_int*w_int;
% Randomly generate flood heights for each flood height discretization bin
delta_h = [rand(n,1)*3, rand(n,1)+3, rand(n,1)+4, rand(n,1)+5, rand(n,1)+6, rand(n,1)+7, rand(n,1)+8, rand(n,1)+9, rand(n,1)+10, rand(n,1)+11, rand(n,1)+12, rand(n,1)+13, rand(n,1)+14, (rand(n,1)*5)+15] ;
figure
histogram(delta_h,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH bin 1
hold on
for i=3:14
histogram(delta_h,'BinEdges',i:(i+1),'Normalization','probability') %Histogram for next set of bins
end
histogram(delta_h,'BinEdges',[15 20],'Normalization','probability') % Histogram for last bin
title('Histogram of EFH')
xlabel('External Flood Height (ft)')
ylabel('Probability')
grid on
% Converting EFH to IFH
IFH_cf=[];
IFH_pf=[];
IFH_nf=[];
for i = 1:14
IFH_cf = [IFH_cf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(3))*time/A_int]; % Take the rows, make it a column and stack next to what we have
IFH_pf = [IFH_pf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(2))*time/A_int];
IFH_nf = [IFH_nf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(1))*time/A_int];
end
% Replacing all IFH that are greater than EFH with the maximum EFH.
IFH_cf = min(IFH_cf,delta_h);
IFH_pf = min(IFH_pf,delta_h);
IFH_nf = min(IFH_nf,delta_h);
%% Plotting Histogram of IFH Separately
figure
histogram(IFH_cf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_cf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_cf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for Complete Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
figure
histogram(IFH_pf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_pf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_pf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for Partial Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
figure
histogram(IFH_nf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_nf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_nf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for No Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
%% Getting Histcount
IFH_cf_histcount = [];
IFH_pf_histcount = [];
IFH_nf_histcount = [];
h_int_edges = [0 3 4 5 6 7 8 9 10 11 12 13 14 15 20];
for i = 1:14
IFH_cf_histcount = [IFH_cf_histcount; histcounts(IFH_cf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
IFH_pf_histcount = [IFH_pf_histcount; histcounts(IFH_pf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
IFH_nf_histcount = [IFH_nf_histcount; histcounts(IFH_nf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
end
Accepted Answer
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!