Generating a histogram from lognormally distributed data
이전 댓글 표시
Good Afternoon,
I am trying to modify the following script
mu = 0.015; % mean particle size
sg = 1.6; % standard deviation
Ntot = 1000; % total number concentration of particles
nbins = 50; % number of bins
data = lognrnd(mu,sg,[Ntot,1]); % generate data
h = histogram(data,nbins); % display data as histogram
Nconcs = h.Values; % determine number concentration
rMin = min(data); % find the minimum particle radius in the distribution
rMax = max(data); % find the maximum particle radius in the distribution
rs = linspace(rMin,rMax,nbins); % create vector containing particle radii
scatter(rs,Nconcs); % plot data
I would like to be able to provide the values for minumum and maximum particle radius but receive a similar output. The trouble I am having is I cannot figure out a way of generating the random data between these values. Because the data does not have to be random I have also tried using lognpdf and providing the values...
mu = 0;
sg = 1;
rs = linspace(0.05:0.01:1.25); % range of particle radii
data = lognpdf(rs,mu,sg); % lognormal pdf
...after which I am finding myself getting stuck. Below I shall state what Inputs I have and the Outputs I am trying to acheive.
Inputs:
- mu and sg: the mean particle size and the standard deviation
- rMin and rMax: the minimum and maximum particle size
- Ntot: the total number concentration of particles
Outputs:
- rs: a vector containing particle radii (ideally lognormally spaced)
- Nconcs: a vector containing the approximate number concentration of each particle size corresponding to each element in rs
- figure(1) : a plot with rs on the x axis and Nconcs on the y axis (ideally with a lognormally spaced x-axis)
- figure(2): a histogram seperating radii into 50 bins of equal width showing the number concentration within each bin
I hope someone can help and i'm not missing anything too simple!!!
답변 (1개)
Jeff Miller
2021년 2월 23일
What about something like this:
mu = 0.015; % mean particle size
sg = 1.6; % standard deviation
Ntot = 1000; % total number concentration of particles
nbins = 50; % number of bins
data = lognrnd(mu,sg,[Ntot,1]); % generate data
% only keep data from the range you want:
inRange = (data >= rMin) & (data <= rMax);
data = data(inRange);
h = histogram(data,nbins); % display data as histogram
Nconcs = h.Values; % determine number concentration
rMin = min(data); % find the minimum particle radius in the distribution
rMax = max(data); % find the maximum particle radius in the distribution
rs = linspace(rMin,rMax,nbins); % create vector containing particle radii
scatter(rs,Nconcs); % plot data
댓글 수: 6
Liam Holbeche-Smith
2021년 2월 24일
편집: Liam Holbeche-Smith
2021년 2월 24일
Jeff Miller
2021년 2월 24일
Sorry, I can't really understand what you are trying to do. But it sounds like 'makedist' and 'truncate' would help.
lndist = makedist('lognormal',mu,sg); % create a lognormal distribution object
trunclndist = truncate(lndist,lnrMin,lnrMax); % make a truncated version of it
rspdf = pdf(trunclndist,rs); % compute the probability density at each point in rs, conditional on being in the required interval
(I am a bit confused about which numbers are values come from the underlying normal distribution and which numbers are logs of those values.)
About the vector elements adding up to one...no, that isn't correct. The integral of the pdf over its entire range is 1, but the individual pdf values can be anything consistent with that integral. For example, if the range is only 0.1 units wide, then the average pdf value has to be 10--already much more than 1 at each point--to make the integral 1.
Hope something in here is helpful...
Liam Holbeche-Smith
2021년 2월 24일
Jeff Miller
2021년 2월 24일
pdf(pdT,x)
is the continuous pdf, and you can set it with whatever parameters you want. Note that Ntot is not really a parameter of the pdf. The pdf is a representation of what happens with a single observation, whereas Ntot is the number of observations.
You can discretize into bins however you like. For any bin i extending from x_low(i) to x_hi(i), the probability of an observation being in that bin is
bin_probability(i) = cdf(pdT,x_hi(i))-cdf(pdT,x_low(i))
Multiply the bin probability by Ntot and you will have the theoretically expected number in each bin. Of course the midpoint of each bin is just the average of its low and hi values.
Still not exactly sure what you are trying to do, but note that you could (if you wanted) also look at each bin separately with something like
pdT(i) = truncate(pd,x_low(i),x_hi(i));
This would be useful if you wanted, say, the mean in each bin rather than the midpoint (these might be a bit difference if the pdf is not flat or symmetric within the bin).
Liam Holbeche-Smith
2021년 2월 25일
Jeff Miller
2021년 2월 25일
Sorry I am not being helpful. I 'm not sure I understand this background and have nothing to add to my previous suggestion.
BTW, your code attempt for pdf does not look like a legal pdf to me, because I think it will integrate to Ntot, whereas pdfs should integrate to 1.
카테고리
도움말 센터 및 File Exchange에서 Lognormal Distribution에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

