how to plot a gaussian 1D in matlab

763 visualizaciones (últimos 30 días)
Gadadhar Sahoo
Gadadhar Sahoo el 1 de Dic. de 2017
Editada: Adam Danz el 13 de Nov. de 2024 a las 16:11
for k = 1 : K
ax = linspace(min_x,max_x,100);
y = my_gaussian(x,means,vars);
plot(ax,y);
end

Respuestas (3)

Adam Danz
Adam Danz el 14 de Jul. de 2020
Editada: Adam Danz el 13 de Nov. de 2024 a las 16:11
Parameterized Gaussian distribution function (no toolboxes needed)
This anonymous function produces a normal probability density curve at the values in x with a mean of mu and a standard deviation of sigma.
gausDist = @(x,mu,sig)(1./(sig.*sqrt(2*pi))).*exp((-(x-mu).^2)./(2.*sig.^2));
  • x is an array of x-values.
  • mu is the mean
  • sig is the standard deviation
The anonymous function is equivalent to normpdf(x,mu,sig) (Statistics and Machine Learning Toolbox) except that normpdf returns NaN values when the standard deviation (sig) is negative whereas the anonymous function will evaluate the negative value.
Comparison between normpdf and the anonymous function:
x = -4:.2:6;
mu = -1.5;
sig = 1.8;
y = gausDist(x,mu,sig);
y2 = normpdf(x,mu,sig);
plot(x,y,'b-','LineWidth',3,'DisplayName','Anonymous fcn')
hold on
plot(x,y2,'r--','LineWidth',3,'DisplayName','normpdf')
legend
grid on
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f', mu, sig))
The doc page on the Normal Distribution may also be helpful.
Fully parameterized Gaussian function (no toolboxes needed)
This anonymous function creates a Gaussian curve that includes parameters for amplitude and vertical offset.
gaus = @(x,mu,sig,amp,vo)amp*exp(-(((x-mu).^2)/(2*sig.^2)))+vo;
  • x is an array of x-values.
  • mu is the mean
  • sig is the standard deviation
  • amp is the (positive or negative)
  • vo is the vertical offset from baseline (positive or negative)
To add noise along the y-axis of the guassian,
y = gaus(___);
yh = y + randn(size(y))*amp*.10; % noise is 10% of the amp
Demo:
x = linspace(-5,25,100);
mu = 10;
sig = 5;
amp = 9;
vo = -5;
y = gaus(x,mu,sig,amp,vo);
% Plot gaussian
figure()
plot(x, y, 'b-', 'LineWidth',3)
% Add noise
yh = y + randn(size(y))*amp*.10;
hold on
plot(x, yh, 'ro','markersize', 4)
grid on
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...
mu, sig, amp, vo))
normpdf from the Statistics and Machine Learning Toolbox
y = normpdf(x,mu,sigma) produces a normal probability density curve at the values in x with a mean of mu and a standard deviation of sigma. Unlike the parameterized anonymous function above, the output to normpdf carries a specifc meaning. Therefore, amplitude and vertical offset are not specified in normpdf.
This demo uses the same argument values as above.
y = normpdf(x,mu,sig);
figure()
plot(x,y,'-b','LineWidth',3)
grid on
gaussmf from the Fuzzy Logic Toolbox
y = gaussmf(x,[sigma,mu]) produces a Gaussian membership function which differs from the probability density function produced by normpdf. A key difference is that the Gaussian membership function always has a maximum value of 1. Given a sufficiently wide range of x values, the curve ranges from near 0 to 1 along the y axis. Thus, amplitude (amp) and vertical offset (vo) can be applied to the curve by,
y = amp * gaussmf(x,[sigma,mu]) + vo;
This demo uses the same argument values as above and compares gausmf (red) and the parameterized anonymous function (blue).
y = gaus(x,mu,sig,amp,vo); % parameterized anonymous function
y2 = amp*gaussmf(x,[sig,mu])+vo; % fuzzy Logic toolbox
figure()
plot(x, y, 'b-', 'LineWidth',3, 'DisplayName','Parameterized fcn')
hold on
plot(x, y2, 'r--', 'LineWidth',4, 'DisplayName','gaussmf()')
grid on
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...
mu, sig, amp, vo))
legend()

M
M el 1 de Dic. de 2017
Editada: Adam Danz el 14 de Jul. de 2020
You can use Matlab function to construct Gaussian function :
x = 0:0.1:10;
y = gaussmf(x,[2 5]);
plot(x,y)
  4 comentarios
Gadadhar Sahoo
Gadadhar Sahoo el 1 de Dic. de 2017
i am not getting the gaussian bell curve..here is my code
clc clear load fisheriris [N, M] = size(meas); x = meas(:,1)'; max_x = max(max((x))); min_x = min(min(x)); K = 3; means = min_x + (max_x - min_x)*rand(1, K); vars = ones(1, K); prior = ones(1,K)/K; prob = zeros(N, K); for g = 1 : 1 for p = 1 : N for k = 1 : K gaussian = (1/sqrt(2*pi*vars(k)))*exp(-(x(p)-means(k)).^2/(2*vars(k))); prob(p,k) = gaussian* prior(k); end sum_probs = sum(prob(p,:)); prob(p,:) = prob(p,:)/sum_probs; end for k = 1 : K means(k) = sum(prob(:,k)'.*x)/N; vars(k) = sum(prob(:,k)'.*(x - means(k)).^2)/N; prior(k) = sum(prob(:,k))/N; end end figure scatter(x,zeros(1,N)); hold on for k = 1 : K ax = linspace(min_x,max_x,100); y = gaussmf(ax,[means,vars]); plot(ax,y);
end
Chad MacDonald
Chad MacDonald el 2 de Ag. de 2023
Editada: Chad MacDonald el 12 de Nov. de 2024 a las 16:09
Do not use the gaussmf function from Fuzzy Logic Toolbox to compute a Gaussian distribution. This function evaluates a Gaussian membership function for a fuzzy logic system, which is not the same thing as a Gaussian distribution. For more information on Gaussian probability distributions, see Normal Distribution.

Iniciar sesión para comentar.


Chad MacDonald
Chad MacDonald el 12 de Nov. de 2024 a las 15:44
Editada: Chad MacDonald el 12 de Nov. de 2024 a las 16:09
If you have Statistics and Machine Learning Toolbox, you can compute a Gaussian probability distributon using the normpdf function. For example, the following code computes and plots a normal distribution with a mean of 5 and a standard deviation of 1.
x = 0:0.1:10;
mu = 5;
sigma = 1;
y = normpdf(x,mu,sigma);
plot(x,y)

Categorías

Más información sobre Fuzzy Logic Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by