Whats wrong with this simpleGaussian Fit?

2 views (last 30 days)
Jason
Jason on 8 Nov 2015
Commented: Jason on 10 Nov 2015
For some reason the fit doesn't fit!
%Gaussian Fit--------------------------------------------------------
gaussEqn = 'a*exp(-((x-b)/c).^2)+d'
f = fit(xdata',ydata',gaussEqn)
%Results of fit
coeffs=coeffvalues(f);
a=coeffs(1)
b=coeffs(2)
c=coeffs(3)
d=coeffs(4)
figure
plot(xdata',ydata','r.') %plot raw data
hold on;
%Increase resolution of x data
xdataFine=(linspace(xdata(1),xdata(end),30))
%plot high res fit
fitGaus = a*exp(-((xdataFine-b)/c).^2)+d
plot(fitGaus,'g-')
hold off.
  2 Comments
Jason
Jason on 9 Nov 2015
Edited: Jason on 9 Nov 2015
It seems the parameters fromt he fit are wrong. This is my data
and here are the results of the fit
f =
General model:
f(x) = a*exp(-((x-b)/c).^2)+d
Coefficients (with 95% confidence bounds):
a = 0.4218
b = 0.9157
c = 0.7922
d = 3351 (-646.9, 7349)
"b" should be close the 7 so I can't figure out whats going wrong with the fit, and then to plot the fit as well.
It fits fine in Mathcad.
Jason
Jason on 9 Nov 2015
Ok, it seems like its not possible to do the fit without using start guesses:
f = fit(x,y,gaussEqn,'Normalize','off', 'StartPoint',[max(y(:))-min(y(:)),xValue,c,min(y(:))]);

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 9 Nov 2015
Those red circles look like they are the result of binning and counting the data (and making a histogram-style figure). Are those counts what you are putting into the fit? Most fitting routines in MATLAB expect the underlying data, not counts.
fit points to several functions in MATLAB, depending on context. Which function is it?
  3 Comments
the cyclist
the cyclist on 10 Nov 2015
You misunderstood my last question. When you type
which fit
what MATLAB function comes up? (I'm trying to understand which function to look at to help you diagnose the issue.)
Jason
Jason on 10 Nov 2015
Im sorry. It comes up with:
C:\Program Files\MATLAB\R2014b\toolbox\curvefit\curvefit\fit.m

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!