Fit and Plot Gaussian Function
68 views (last 30 days)
I'm trying to fit a Gaussian function to my data.
x_values = -250:1:250;
p = pdf(pd,x_values);
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))