View License

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

» Watch video

Highlights from
Fit 2D gaussian function to data

5.0 | 9 ratings Rate this file 141 Downloads (last 30 days) File Size: 3.56 KB File ID: #37087 Version: 1.5
image thumbnail

Fit 2D gaussian function to data



08 Jun 2012 (Updated )

Fits a 2D Gaussian function to simulated data.

| Watch this File

File Information

The program generates a 2D Gaussian. The program then attempts to fit the data using the MatLab function “lsqcurvefit “ to find the position, orientation and width of the two-dimensional Gaussian.
Execute “mainD2GaussFitRot.m” with not input parameters.
The two-dimensional Gaussian function is defined by the function “D2GaussFunctionRot.m” and “D2GaussFunction.m”


This file inspired Fit 1 D And 2 D Gaussian To Noisy Data.

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
15 Dec 2016 grüne Sonne

25 Aug 2016 Martin Ahrens

22 Jul 2016 Neema Prakash

Thank you for this code.

Comment only
27 Apr 2016 ZY

ZY (view profile)

Thank you for the code. What are the errors associated with the the fitting parameters?

Comment only
17 Oct 2015 Bin Xu

Bin Xu (view profile)

Good work . I would glad to know how the code work

31 Aug 2015 Brian.L

Great work Gero! I also would like to know how the code might be adapted for 3D system...

12 Feb 2015 Jozsef Orbán

Could you please provide the code with dependencies (as lsqncommon, lsqcurvefit, ...)

Comment only
07 Feb 2015 YuTing Huang

I would like to know the x(6) in the script of D2GaussFunctionRot function. I know it means the angle(in rad), however, I don't understand what the angle exactly based on?

Comment only
11 Dec 2014 Nathan Neff-Mallon

Jan Simon's criticism is misplaced, only minor changes are needed to plot real data. If you have a regular grid of points, all you must do is:
change line 29 to the appropriate initial guesses
change line 30 to xs=x0;
change line 31 to noise=0;
change line 38 to [X,Y] = meshgrid(x,y);
where x and y a vectors with your x an y axis points
change line 47 to Z = zdata;
where zdata is a length(x) by length(y) matrix with the data you wish to fit such that zdata(i,j)=z(x_i,y_j).
Line 48 should be commented out.

If your grid points are not regular, you must set up the X, Y, and Z matrices so that they are all the same size and element i of X, Y and Z all correspond to the point (X(i),Y(i),Z(i)). There might be additional restrictions on the size and shape (I think it must be a matrix and not an array), but I'm not certain.

At any rate, thank you very much Gero. This is very helpful. :)

04 Mar 2014 Leo

Leo (view profile)

27 Feb 2014 xavier

xavier (view profile)

03 Feb 2014 Gaszton

29 Jul 2013 Jan Simon

Jan Simon (view profile)

What a pity that this is a script, which generates the data by a random function. Before it can be used for real problems, substantial changes are required.

It would increase the usability, if the calculations are move to a function and separated from the generation of test data.

Comment only
11 Oct 2012 Gar Waterman

Works well for 2D system...any chance of broadening it to 3D?

12 Jun 2012 1.5

file was renamed and the definition of the gaussian width was changed.
Also a screenshot was added.

Contact us