
Lsqcurvefit for 2D gaussian: no error! but not fitting well to the data!
3 views (last 30 days)
Show older comments
I am addding an example: please refer to XYZ from the file.
Code:
x0 = [10,0,2,0,2]; %Inital guess parameters
%% --- Fit---------------------
[x,resnorm,residual,~] = lsqcurvefit(@D2GaussFunction,x0,xdata,Z);
Zfit=D2GaussFunction(x,xdata);
figure(1)
subplot(1,2,1)
surf(X,Y,Z)
subplot(1,2,2)
surf(X,Y,Zfit)
Here is my D2Gauss Function: based on: https://www.mathworks.com/matlabcentral/fileexchange/37087-fit-2d-gaussian-function-to-data
function F = D2GaussFunction(x,xdata)
F = x(1)*exp(-((xdata(:,:,1)-x(2)).^2/(2*x(3).^2)+(xdata(:,:,2)-x(4)).^2/(2*x(5).^2)));
I am getting the following figure where the fitting is clearly not good (subplot 1 is raw data, subplot2 is fitting). Could you please suggest what is going wrong here?

2 Comments
Alex Sha
on 21 Jul 2021
Hi, Sujay Ray, if adding one more to your original function like below:
F = x(1)*exp(-((xdata(:,:,1)-x(2)).^2/(2*x(3).^2)+(xdata(:,:,2)-x(4)).^2/(2*x(5).^2)))+x(6);
The result will be much better:
Root of Mean Square Error (RMSE): 15.8689661201125
Sum of Squared Residual: 72777.1607734494
Correlation Coef. (R): 0.975082856345198
R-Square: 0.95078657673831
Parameter Best Estimate
---------- -------------
x1 501.134541206993
x2 -0.471981642148694
x3 -1.43387418591403
x4 0.621256518406971
x5 -1.35781705474931
x6 413.42087378625

Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!