Problem with Gaussian fit to Data

1 view (last 30 days)
Jason
Jason on 6 Nov 2015
Edited: Jason on 6 Nov 2015
What is wrong with this 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)
xValue=7;
sp=3;
%Create fine data to plot fit
xdataF=(linspace(xValue-sp,xValue+sp,10*sp))
fitGaus = a*exp(-((xdataF-b)/c)^2)+d
yvalue=ym;
xpeak=xdataF(find(fity==ym));
xpeak=mean(xpeak(:)); %Take mean incase more than one peak found
the fit is fine:
f =
General model:
f(x) = a*exp(-((x-b)/c)^2)+d
Coefficients (with 95% confidence bounds):
a = 0.7094
b = 0.7547
c = 0.276
d = 1482 (-1823, 4787)
But I get the error:
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Error in HiSeqDistortionTool>fitLocalPeak (line 726)
fitGaus = a*exp(-((xdataF-b)/c)^2)+d

Answers (1)

Thorsten
Thorsten on 6 Nov 2015
gaussEqn = 'a*exp(-((x-b)/c).^2)+d'
  1 Comment
Jason
Jason on 6 Nov 2015
Edited: Jason on 6 Nov 2015
OK thanks. I'm still running into problems. This is my x&y data:
ydata =
0 814 2254 4636 3608 1355 65
xdata =
4 5 6 7 8 9 10
I want to do the fit, and then plot at a higher resolution and then find the max y and x value of this in high resolution 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;
plot(f,'r--') %plot fit
%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.
%Find max value and its x location
ym=max(fitGaus);
yvalue=ym;
xpeak=xdataFine(find(fitGaus==ym));
xpeak=mean(xpeak(:)); %Take mean incase more than one peak found

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!