View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Fit 2D Gaussian with Optimization Toolbox

5.0 | 4 ratings Rate this file 27 Downloads (last 30 days) File Size: 2.55 KB File ID: #41938 Version: 1.2

Fit 2D Gaussian with Optimization Toolbox



24 May 2013 (Updated )

Fit a 2D rotated gaussian.

| Watch this File

File Information

FMGAUSSFIT performs a gaussian fit on 3D data (x,y,z).
  [fitresult,..., rr] = fmgaussfit(xx,yy,zz) uses ZZ for the surface
  height. XX and YY are vectors or matrices defining the x and y
  components of a surface. If XX and YY are vectors, length(XX) = n and
  length(YY) = m, where [m,n] = size(Z). In this case, the vertices of the
  surface faces are (XX(j), YY(i), ZZ(i,j)) triples. To create XX and YY
  matrices for arbitrary domains, use the meshgrid function. FMGAUSSFIT
  uses the lsqcurvefit tool, and the OPTIMZATION TOOLBOX. The initial
  guess for the gaussian is places at the maxima in the ZZ plane. The fit
  is restricted to be in the span of XX and YY.
    To fit a 2D gaussian:
      [fitresult, zfit, fiterr, zerr, resnorm, rr] =


This file inspired Revising Sunspot Numbers Since The Times Of Galileo.

Required Products Optimization Toolbox
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
09 Aug 2016 Juan Garcia

Hi Nathan,

You're right it is a constant, but if you want to actually retrieve the sigmas of the gaussian you need that constant.

Try the simple case
x = -100:100; y = x; [xx, yy] = meshgrid(x,y);
sigma = 20;
zz = exp(-(xx.^2 + yy.^2) ./ (2*sigma^2));

You obtain fitresult(3:4) = 28.2847 when you would like it equal to 20. It can be corrected afterwards by dividing fitresult(3:4)./sqrt(2), but just to be aware of it.


Comment only
03 Aug 2016 Nathan Orloff

Hi Juan,

I think that Zo is just an offset. As to the normalization of the sigma, I think it is just a constant. I omitted it for readability.

Thanks GBorg!

Comment only
03 Aug 2016 GBorghesan

The fitresults is

angle=fitresult(2)%in deg
sigma= fitresult(3:4)
hg=fitresult(1) % amplitude ;

and can be reused in function
gaussian2D=@(par,x,y)(par(7) + ...

22 Jul 2016 XYZ

XYZ (view profile)

13 Jul 2016 Juan Garcia

Nice function. I was having a look at it and I have a question and a comment.

Question: What is the meaning of z0 or par(7) in the gaussian2D function.

Comment: I would say that both sigmas in the gaussian2D function lack the multiplication by 2, or to maintain the power of 2 to the whole fraction, by sqrt(2).

function z = gaussian2D(par,xy)
% compute 2D gaussian
z = par(7) + ...
par(1) * exp(-(((xy{1}-par(5)).*cosd(par(2))+(xy{2}-par(6)).*sind(par(2)))./(sqrt(2)*par(3))).^2-...

Comment only
12 Jan 2015 Eric T

Eric T (view profile)

I get the following warning:
Warning: The Levenberg-Marquardt algorithm does not handle bound constraints; using the
trust-region-reflective algorithm instead.
> In lsqncommon at 83
In lsqcurvefit at 252
In fmgaussfit at 56

Comment only
07 Feb 2014 Nathan Orloff

fitresult = fit parameters used to generate the fit.

zfit = the z values of the fit

fiterr = error in the fitparameters assuming a confidence interval.

zerr = error in the z values assuming the uncertainty in the fit paramteres

resnorm = residual

rr = reduced chi-squared.

Comment only
30 Jan 2014 Olhado

Olhado (view profile)

This bit of code is doing exactly what I want I think but I wondering if it might be possible for the Author to expand on the meaning of the values the function outputs or point me in the direction as to where I can find the answer. Thanks a Ton

24 Nov 2013 Christopher

28 May 2013 Nathan Orloff

I edited this a little. The fit is better with z bounds. I will post it soon... If you have already downloaded this just add the following.

%% Set up the startpoint
[amp, ind] = max(zData); % amp is the amplitude.
xo = xData(ind); % guess that it is at the maximum
yo = yData(ind); % guess that it is at the maximum
ang = 45; % angle in degrees.
sy = 1;
sx = 1;
zo = median(zData(:))-std(zData(:));
xmax = max(xData)+2;
ymax = max(yData)+2;
zmax = amp*2; % amp is the amplitude.
xmin = min(xData)-2;
ymin = min(yData)-2;
zmin = min(zData)/2; % amp is the amplitude.

%% Set up fittype and options.
Lower = [0, eps, 0, 0, xmin, ymin, zmin];
Upper = [Inf, 180, Inf, Inf, xmax, ymax, zmax]; % angles greater than 90 are redundant
StartPoint = [amp, ang, sx, sy, xo, yo, zo];%[amp, sx, sxy, sy, xo, yo, zo];

Comment only
28 May 2013 1.1

5/28/2013: Changed bounds of fit to got to 180 angle, instead of 90. Changed r^2 to include degrees of freedom +1.

30 May 2013 1.2

changed the commenting text

Contact us