how to fit gaussian model and plot it

78 views (last 30 days)
michael scheinfeild
michael scheinfeild on 23 Feb 2015
Edited: Liam Walsh on 13 Nov 2025 at 15:20
hi i have vector of 1000 numbers i want to fit Gaussian model i use
[n,cent]=hist(x,50)
then
bs = glmfit(cent,n,'normal');
then i want to plot the fit yfit = glmval(bs,cent_s); failed Error using glmval (line 64) At least three arguments are required. i want to plot
plot(cent_s,yfit)

Accepted Answer

zepp
zepp on 23 Feb 2015
You can do the following:
1) Estimate the mean and standard deviation using normfit
2) Calculate the probability estimates using normpdf
3) Plot the data and the estimates using plot
Example:
[m,s] = normfit(x);
y = normpdf(x,m,s);
plot(x,y,'.');

More Answers (1)

Liam Walsh
Liam Walsh on 11 Nov 2025 at 18:55
Edited: Liam Walsh 21 minutes ago
glmfit and glmval fit and evaluate generalized linear models, respectively. These can be used to model Gaussian data, but you need a set of predictors and responses (which are Gaussian).
If you have a single vector of Gaussian data that you want to fit to a Gaussian distribution, then there are two ways you can go about this. First, you can use normfit, normpdf, etc, as zepp shows in their answer.
Alternatively, you can make use of the fitdist function to create a NormalDistribution object, which has many convenience functions for visualizing the fit. To see all the options for working with the normal/Gaussian distribution that Statistics and Machine Learning Toolbox provides, please consult the following documentation page:
% Create some sample data
rng(0, 'twister') % For reproducibility
x = normrnd(10, 2, 100, 1);
% Example 1: normfit, normpdf
[mu, sig] = normfit(x)
mu = 10.2462
sig = 2.3248
grid = 3:.1:17;
pdfvals = normpdf(grid, mu, sig);
plot(grid, pdfvals)
% Example 2: fitdist
normdist = fitdist(x, "normal") % Same mu, sigma values as the first example
normdist =
NormalDistribution Normal distribution mu = 10.2462 [9.78488, 10.7075] sigma = 2.3248 [2.04119, 2.70066]
plot(normdist)

Categories

Find more on Probability Distributions and Hypothesis Tests 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!