Asked by K
on 24 Jul 2013

I'm trying to fit a Gaussian function to my data.

I use

pd=fitdist(y,'Normal');

x_values = -250:1:250;

p = pdf(pd,x_values);

plot(x_values,p)

to fit and plot the function.

It looks to be the right shape, however, the function itself is very small (the max only coming to about 4*10^-3). I know that a normal function dictates that the integral go to 1, but is there any way to keep the shape, just make it bigger so that it can plot on top of my data (X range -200, 200 Y range -250, 250)? Or is there another kind of function that will have the same shape but forgo the integration to one part?

Thanks in advance.

Answer by Richard Brown
on 24 Jul 2013

Edited by Richard Brown
on 24 Jul 2013

Sure, rather than trying to fit a distribution (which is not what you want), just fit the Gaussian itself.

Generate some noisy Gaussian data:

a = 5; b = 17; c = 40;

noise_sd = .1;

n = 100;

x = linspace(-250, 250, n);

y = a*exp(-(x-b).^2/c^2) + noise_sd * randn(size(x));

Define the parametrised functional form.

f = @(x, p) p(1) * exp(-((x - p(2))/p(3)).^2);

And solve using lsqnonlin. You might need to be a little bit careful with initial conditions:

p = lsqnonlin(@(p) f(x, p) - y, [1 1 1]);

plot(x, y, 'ro', x, f(x, p))

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/83065-fit-and-plot-gaussian-function#comment_161409

Sign in to comment.