File Exchange

image thumbnail

Fit 2D Gaussian with Optimization Toolbox

version 1.2 (2.55 KB) by

Fit a 2D rotated gaussian.



View License

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] =

Comments and Ratings (12)


Sergey (view profile)

Could you please tell me, how should I modify the code to fit data with symmetric gaussian, i.e. i want sigmas to be equal ; I am not sure I matched code in gaussian2D with formulas correctly.

thank you!

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.


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!


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) + ...


XYZ (view profile)

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-...

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

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.


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


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];



changed the commenting text


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

MATLAB Release
MATLAB 8.0 (R2012b)

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

» Watch video