# fitting sum of gaussians into a data set (100 rows and 1 column)

24 views (last 30 days)
Abhishek on 10 Jun 2013
Getting difficulty in fitting and plotting a data set (100x1 matrix) by using gmdistribution.fit....i have to git a large number of sum of gaussians...but i guess it needs a matrix with mu and sigma values...can anyone help me in fitting this data into sum of gaussians and plotting it..i want a one dimensional plot... thanks

Pourya Alinezhad on 10 Jun 2013
you can use the function "lsqcurvefit".first define Gaussian formula in a function mfile.then the lsqcurvefit function gives the fitted data .
Abhishek on 14 Jun 2013
I am using 15 gaussians. I have an one dimensional data set [101,1].its not fitting efficiently using lsqcurvefit
This is the command X = lsqcurvefit(@FUN,X0,x,y,[],[],options); where,
1)function ret= FUN(X,x) F=zeros(101,1); for i=1:15, F=F+ X(i,1)*exp((-(x-X(15+i,1))./X(30+i,1)).^2); end ret=F; return
2) X0=ones(3*15,1)/1;
3) x=[0:0.01:1]';
4) y=12*exp(-abs(x-0.00001)/.01)+8*exp(-abs(x-0.5)/.01)+12*exp(-abs(x-1)/.01); rmic=zeros(siz,20); h=[1:1:20]'; for i=1:siz;for j=1:20; rmic(i,j)=cos(2*pi*j*x(i));end;end; fore=rmic'*y;
5)options = optimset('TolX',1e-8,'Display','iter','MaxIter',1000000,'TolFun',1e-10,'MaxFunEvals',10000000,'DiffMaxChange',.1000,'LineSearchType','cubicpoly');
My data sets are : y (x): [ 11.9880 4.4190 1.6256 0.5980 0.2200 0.0809 0.0298 0.0110 0.0040 0.0015 0.0005 0.0002 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0004 0.0010 0.0027 0.0073 0.0198 0.0539 0.1465 0.3983 1.0827 2.9430 8.0000 2.9430 1.0827 0.3983 0.1465 0.0539 0.0198 0.0073 0.0027 0.0010 0.0004 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0002 0.0005 0.0015 0.0040 0.0109 0.0297 0.0809 0.2198 0.5974 1.6240 4.4146 12.0000]
and as mentioned, x=[0:0.01:1]';
I am not able to get the correct curve fit......is it the initial guess, X0.....or anything else.....please help me out...I am stuck on this..
thanks,

Alan Weiss on 14 Jun 2013
Edited: Alan Weiss on 14 Jun 2013
As clearly stated in the Input Arguments section of the documentation, you should not return the sum of squares, but should return a vector of values for your objective function. Making a sum of squares as you have done slows or stops convergence.
I am not sure that I understand your formulation completely, but perhaps you should check a similar sort of example in the documentation.
Alan Weiss
MATLAB mathematical toolbox documentation
Abhishek on 14 Jun 2013
Well I am taking sum of Gaussian in the function...I mean is there any other way to use the sum of Gaussian to fit..