MATLAB Bar Chart Issues

I need to calculate the probability density of the 1/2-x function, where x is a randomly generated number from 0 to 1.

My code is as follows:

n=10^6;
b=10^2;

x=(1./(2-(rand(1,n))));

a=histfit(x,b,'kernel'); % Requires Statistics and Machine Learning Toolbox
xlim([0.5 1.0])

And I get a decent schedule that looks like this:

enter image description here

It may seem that there are several problems with this:

  • MATLAB draws a match other than my histogram because it counts in empty space outside the range of the [0.5 1]function. This leads to a distorted fit to the edges. (The reason you do not see the empty space, because I entered xlim there)

  • I do not know how I could divide each value along the Y axis by 10 ^ 6, which would give me my probability density.

Thanks in advance.

+4
2

, hist ( , 2010b, histogram histfit) , :

n=10^6;
b=10^2;
x=(1./(2-(rand(1,n))));

[counts,centers]=hist(x,b);
density = counts./trapz(centers, counts);
%// Thanks to @Arpi for this correction 
polyFitting = polyfit(centers,density,3)
polyPlot = polyval(polyFitting,centers)
figure
bar(centers,density)
hold on
plot(centers,polyPlot,'r','LineWidth',3)

, b, 100. , , .

the result looks like

+3

1. , ksdensity .

2. hist, , .

:

rng(125)
n=10^6;
b=10^2;

x=(1./(2-(rand(1,n))));

subplot(1,2,1)
a = histfit(x,b,'kernel');
title('Original')
xlim([0.5 1.0])

[f,c] = hist(x,b);
% normalization to get density
f = f/trapz(c,f);

% kernel density
pts = linspace(0.5, 1, 100);
[fk,xk] = ksdensity(x, pts, 'Support', [0.5, 1]);

subplot(1,2,2)
bar(c,f)
hold on
plot(xk,fk, 'red', 'LineWidth', 2)
title('Improved')
xlim([0.5 1.0])

: enter image description here

EDIT: :

pts = linspace(0.5, 1, 500);
[fk,xk] = ksdensity(x, pts, 'Support', [0.5, 1]);
bar(c,f)
hold on
plot(xk(2:end-1),fk(2:end-1), 'red', 'LineWidth', 2)
title('Improved_2')
xlim([0.5 1.0])

enter image description here

+2

All Articles