Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Curve fitting problem
Date: Mon, 15 Dec 2008 21:08:03 +0000 (UTC)
Organization: Tufts University
Lines: 32
Message-ID: <gi6gvi$jlc$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229375283 20140 172.30.248.37 (15 Dec 2008 21:08:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Dec 2008 21:08:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 967938
Xref: news.mathworks.com comp.soft-sys.matlab:507179


I am trying to fit several data sets to a sum of Lorentzians. I have written code that uses fminsearch. For data that is a sum of two Lorentzians, it
seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. One thing I tried was to
iteratively run the fit, starting with an initial guess and then feeding the results of that as initial guesses to a second round of fitting. If I choose initial values
that are close to what they should be, often the fit stalls, but if I choose values a little farther, sometimes it works, sometimes it doesn't (depending on the data
set). The other issue I have is that I don't know how to put restrictions on what values the parameters I am trying to fit can have. (for example, the
widths at half height should be positive, as should be the heights) I need help figuring out where I am going wrong, so that I can get proper fits consistently.
I can send samples of data. Below is code for fitting a sum of 3 Lorentzians. I would appreciate any help anyone can offer. Thanks.     

%Function to fit data to a sum of 3 Lorentzians.

function[estimates,model]=Lorentzian3(XData,YData);
model=@LorentzFinal;
options=optimset('FunValCheck', 'on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e-40,'TolFun',1e-40);
estimates=fminsearch(model,start_point,options);

	function[sse,FittedCurve]=LorentzFinal(params);
		a(1)=params(1);
		g(1)=params(2);
		cent(1)=params(3);
		a(2)=params(4);
		g(2)=params(5);
		cent(2)=params(6);
		a(3)=params(7);
		g(3)=params(8);
		cent(3)=params(9);
		vshift=params(10);
		FittedCurve=a(1)*(g(1)^2./((XData-cent(1)).^2+(g(1)).^2))+a(2)*(g(2)^2./((XData-cent(2)).^2+(g(2)).^2))+a(3)*(g(3)^2./((XData-cent(3)).^2+(g(3)).^2))+vshift;
		ErrorVector=FittedCurve-YData;
		sse=sum(ErrorVector.^2);
	end

end