277 views (last 30 days)

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.

Richard Brown
on 24 Jul 2013

Edited: 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))

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

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

⋮## Direct link to this comment

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

Sign in to comment.