How to applied 2d gaussian function

Hi,
I upload my code and data I don't know why the fit not working and the amplitude not in the center of the gaussain
I hope someone can help.
Thanks,

Answers (2)

Try to contact the auther of the fileexchange function first?
Matt J
Matt J on 3 Aug 2021
Edited: Matt J on 3 Aug 2021
You could try this alternative gaussian fitting routine, and see if you get different results:

4 Comments

Hi Matt,
I try it, but I got an error with this "zdata: Mx1 vector of corresponding samples z(xdata)"
I convert my data to one row by "zdata = imageData(:);" but I got an error:
Error using gaussfitn (line 66)
Expected zdata to be an array with number of elements equal to 351.
Thanks
Here's what I get,
load gauss
[X,Y]=ndgrid(1:length(imageData));
XY=[X(:),Y(:)];
params=gaussfitn(XY, imageData(:));
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
[D,A,mu,sig]=deal(params{:});
Zfit= D + A*exp( -0.5 * sum( (sig\(XY.'-mu)).*(XY.'-mu) ));
imageFit=reshape(Zfit,size(imageData));
montage({imageData,imageFit})
caxis(4000*[0.7615 0.8126])
Thanks Matt, it works but I need to plot the fir as an image like the attachement. Is there away with this function.
Once you have the fitted parameters, you can plot it any way you wish.

Sign in to comment.

Asked:

on 3 Aug 2021

Commented:

on 3 Aug 2021

Community Treasure Hunt

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

Start Hunting!