Code covered by the BSD License  

Highlights from
Fit 2D Gaussian with Optimization Toolbox

5.0 | 2 ratings Rate this file 53 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] =

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 (5)
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