Lognormal Fitting with the Pore Size and its Probabilities
Show older comments
Dear all,
I have some pore sizes and its probabilites. Visually the pore size distribution follows the lognormal distribution, and the particle size distribution usually follows lognormal distribution, as the black line in the figure shows. So I try to fit it to a lognormal distribution with the following code:
Counts = [71 73 53 77 79 141 158 237 360 594 958 1659 3094 7183 19034 40051 125001 143248 164224 168209 172406 167600 160578 146530 124505 90064 67769 42179 21645 1070];
PoreSize = [89716.4 71264.3 56607.2 44964.7 35716.7 28370.8 22535.7 17900.8 14219.1 11294.6 8971.6 7126.4 5660.7 2837.1 2253.6 1790.1 712.6 566.1 449.6 357.2 283.7 225.4 179 142.2 112.9 89.7 71.3 56.6 45 35.7];
probs = Counts / sum(Counts);
ExpOfY = sum(PoreSize.*probs);
Ysqr = PoreSize.^2;
ExpOfYsqr = sum(Ysqr.*probs);
VarOfY = ExpOfYsqr - ExpOfY1^; % variance of the scores tabulated in vectors 1 & 2
normu = log(ExpOfY/sqrt(1+VarOfY/ExpOfY^2));
norvar = log(1+VarOfY/ExpOfY^2);
norsigma = sqrt(norvar);
dist = makedist('Lognormal','mu',normu1,'sigma',norsigma1);
x = linspace(0.001,200000,2000000); % Use whatever range is appropriate for data
pdfOfx1 = pdf(dist,x);
figure; hold on; plot(PoreSize,Counts);
plot(x,pdfOfx * 6200); % Multiply 6200 just to compare with the original data.
After fitting, the results look wired, comparing with the raw data point distribution. The agreement between the fitting and the original looks quite bed.I have also tried to use the makedist, the results look similar as well. Does anyone know the reason?

Best
18 Comments
Paul
on 8 Jan 2024
Is anyone else seeing that the graphics figures in the answers by @William Rose and @Torsten are small and kind of blurry (at least compared to what I'm used to seeing on the forum)? The plot in the answer of @Muhammad Hassaan Shah is better, but still not as good as usual. I'm using Win10/Edge.
Torsten
on 8 Jan 2024
I don't know whether there is a coincidence, but it happened after I installed Windows 11.
William Rose
on 8 Jan 2024
Edited: Rena Berman
on 9 Jan 2024
Paul
on 9 Jan 2024
Are there any known compatibility issues between Answers and Windows 11 with respect to display of graphics?
Rena Berman
on 9 Jan 2024
(Answers Dev) @Paul, I will need to investigate. Can you post a screenshot that shows what you are seeing?
Try running one of the codes under "Answers" and submit the result. I guess the plot will be shown larger (as in Muhammad Hassaan Shah's answer).
Paul
on 9 Jan 2024
From this comment

Rena Berman
on 9 Jan 2024
William Rose
on 9 Jan 2024
Rena Berman
on 10 Jan 2024
Adam Danz
on 10 Jan 2024
When I put William's comment into edit mode and re-run the code, I see the figure in its expected size.
OS Win 11; Browser: chrome & Edge
I don't have firefox installed so I didn't test that browswer.

Adam Danz
on 10 Jan 2024
What do you get when you query the figure size?
get(gcf,'Position')
It appears in expected size when generating the plot, but after submission, it shows up in the reduzed size. And it remains in reduced size when the answer is opened again. When re-running the code, the plot is again in its expected size. But after submission ...
At least in my case.
William Rose
on 10 Jan 2024
I have the same experience as @Torsten with plots generated in Matlab Answers: the plot gets small when I submit my answer or comment.
the cyclist
on 15 Feb 2024
@Rena Berman, I am seeing the same behavior. (I learned about this thread after posting my own question.) I see the same behavior that @Torsten describes, where the figure briefly appears as if it is going to be the correct size, but then it gets small.
I am running Mac OSX Sonoma.
@Rena Berman, the shrinking-graph thing still happens to me. If I insert an image, such as a jpeg, it does not get shrunken. If I make a plot using embedded code, it looks reasonable until I press Submit, and then it gets shrunken.
plot(1:10,1:10)
I run Windows 11 and use up-to-date Firefox.
Rena Berman
on 21 Feb 2024
Edited: Rena Berman
on 21 Feb 2024
(Answers Dev) @William Rose, @the cyclist, I have forwarded this on to the appropriate MW developers.
William Rose
on 21 Feb 2024
Accepted Answer
More Answers (2)
% Given Data
Counts = [71 73 53 77 79 141 158 237 360 594 958 1659 3094 7183 19034 40051 125001 143248 164224 168209 172406 167600 160578 146530 124505 90064 67769 42179 21645 1070];
PoreSize = [89716.4 71264.3 56607.2 44964.7 35716.7 28370.8 22535.7 17900.8 14219.1 11294.6 8971.6 7126.4 5660.7 2837.1 2253.6 1790.1 712.6 566.1 449.6 357.2 283.7 225.4 179 142.2 112.9 89.7 71.3 56.6 45 35.7];
probs = Counts / sum(Counts);
% Calculating Expected Values and Variance
ExpOfY = sum(PoreSize.*probs);
Ysqr = PoreSize.^2;
ExpOfYsqr = sum(Ysqr.*probs);
VarOfY = ExpOfYsqr - ExpOfY^2; % Corrected the typo here
% Calculating the parameters for the lognormal distribution
normu = log(ExpOfY/sqrt(1+VarOfY/ExpOfY^2));
norvar = log(1+VarOfY/ExpOfY^2);
norsigma = sqrt(norvar);
% Create a lognormal distribution object
dist = makedist('Lognormal','mu',normu,'sigma',norsigma);
% Define the range for your data (adjust as needed)
x = linspace(min(PoreSize), max(PoreSize), 1000);
% Calculate the PDF
pdfOfx = pdf(dist,x);
% Plotting the data
figure; hold on;
scatter(PoreSize,Counts, 'b'); % Scatter plot of the original data
plot(x,pdfOfx * max(Counts), 'r'); % Plot of the fitted distribution
xlabel('Pore Size');
ylabel('Counts');
title('Pore Size Distribution with Lognormal Fit');
legend('Data','Lognormal Fit');
hold off;
- Syntax and Typographical Errors: Ensure that all variable names and operations are correct. The expression VarOfY = ExpOfYsqr - ExpOfY1^; in your original code seems to contain errors. It's corrected here.
- Visualizing Data: I've used a scatter plot for the original data and a line plot for the fitted distribution. Adjust the scaling factor (max(Counts)) as needed to better compare the two.
- Data Range: The range of x in the line x = linspace(min(PoreSize), max(PoreSize), 1000); is set from the minimum to the maximum of your pore sizes. Adjust this as necessary.
- Distribution Parameters: The 'mu' and 'sigma' for a lognormal distribution are derived from the logarithm of your data. Ensure these are calculated correctly.
- Fitting and Model Choice: If the fit is still poor, consider alternative fitting methods or distributions. The fit quality can vary significantly based on the characteristics of your data.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Counts = [71 73 53 77 79 141 158 237 360 594 958 1659 3094 7183 19034 40051 125001 143248 164224 168209 172406 167600 160578 146530 124505 90064 67769 42179 21645 1070];
PoreSize = [89716.4 71264.3 56607.2 44964.7 35716.7 28370.8 22535.7 17900.8 14219.1 11294.6 8971.6 7126.4 5660.7 2837.1 2253.6 1790.1 712.6 566.1 449.6 357.2 283.7 225.4 179 142.2 112.9 89.7 71.3 56.6 45 35.7];
data = [];
for i = 1:numel(Counts)
data = [data,PoreSize(i)*ones(1,Counts(i))];
end
phat = mle(data,'Distribution','LogNormal')
x = linspace(0,2000,1000);
hold on
plot(x,lognpdf(x,phat(1),phat(2)),'g')
histogram(data,'Normalization','pdf')
xlim([0 2000])
probs = Counts / sum(Counts);
ExpOfY = sum(PoreSize.*probs);
Ysqr = PoreSize.^2;
ExpOfYsqr = sum(Ysqr.*probs);
VarOfY = ExpOfYsqr - ExpOfY^2; % variance of the scores tabulated in vectors 1 & 2
normu = log(ExpOfY^2/sqrt(VarOfY+ExpOfY^2))
norvar = log(1+VarOfY/ExpOfY^2);
norsigma = sqrt(norvar)
plot(x,lognpdf(x,normu,norsigma),'b')
hold off
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

















