http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085
MATLAB Central Newsreader  Curve fitting problem
Feed for thread: Curve fitting problem
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 15 Dec 2008 21:08:03 +0000
Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617241
Tazmusica
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<br>
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<br>
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<br>
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<br>
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<br>
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.<br>
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. <br>
<br>
%Function to fit data to a sum of 3 Lorentzians.<br>
<br>
function[estimates,model]=Lorentzian3(XData,YData);<br>
model=@LorentzFinal;<br>
options=optimset('FunValCheck', 'on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e40,'TolFun',1e40);<br>
estimates=fminsearch(model,start_point,options);<br>
<br>
function[sse,FittedCurve]=LorentzFinal(params);<br>
a(1)=params(1);<br>
g(1)=params(2);<br>
cent(1)=params(3);<br>
a(2)=params(4);<br>
g(2)=params(5);<br>
cent(2)=params(6);<br>
a(3)=params(7);<br>
g(3)=params(8);<br>
cent(3)=params(9);<br>
vshift=params(10);<br>
FittedCurve=a(1)*(g(1)^2./((XDatacent(1)).^2+(g(1)).^2))+a(2)*(g(2)^2./((XDatacent(2)).^2+(g(2)).^2))+a(3)*(g(3)^2./((XDatacent(3)).^2+(g(3)).^2))+vshift;<br>
ErrorVector=FittedCurveYData;<br>
sse=sum(ErrorVector.^2);<br>
end<br>
<br>
end

Tue, 16 Dec 2008 09:49:02 +0000
Re: Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617320
Roger Stafford
"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...<br>
> 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<br>
> seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........<br>
<br>
Here's one suggestion that might help. You can, in effect, reduce the total number of parameters you are dealing with by writing your function to be minimized in such a way as to automatically adjust the aparameters and vshift to achieve a minimum square. Your function of Lorenzians is linear in these four parameters and for any given value of the other six, a minimum with respect to these four can always be achieved without iteration. It is simply the solution to a set of four linear equations. So your function can be rewritten to always achieve such a minimum and therefore would involve only the other six parameters to be handed to fminsearch for variation. Six parameters is a whole lot easier for fminsearch to deal with than ten in terms of running into blind alleys or wandering around aimlessly.<br>
<br>
Roger Stafford

Tue, 16 Dec 2008 20:04:02 +0000
Re: Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617438
Tazmusica
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7tie$jau$1@fred.mathworks.com>...<br>
> "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...<br>
> > 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<br>
> > seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........<br>
> <br>
> Here's one suggestion that might help. You can, in effect, reduce the total number of parameters you are dealing with by writing your function to be minimized in such a way as to automatically adjust the aparameters and vshift to achieve a minimum square. Your function of Lorenzians is linear in these four parameters and for any given value of the other six, a minimum with respect to these four can always be achieved without iteration. It is simply the solution to a set of four linear equations. So your function can be rewritten to always achieve such a minimum and therefore would involve only the other six parameters to be handed to fminsearch for variation. Six parameters is a whole lot easier for fminsearch to deal with than ten in terms of running into blind alleys or wandering around aimlessly.<br>
> <br>
> Roger Stafford<br>
<br>
Roger,<br>
Thank you for the suggestion. I can see that the Lorentzian is linear in the a and vshift terms, and I see that this reduces the number of parameters that fminsearch has to deal with. So if the function of Lorentzians were called L(x), and each lorentzian in the sum were l(x), then L(x)=a1*l1(x)+a2*l2(x)+a3*l3(x)+vshift. What I am unclear on is how to rewrite the function to always achieve the minimum of the a and vshift values. I can see where it should be the solution to a system of linear equations that would automatically minimize the a and vshift values, but I guess I am not seeing how I can get this into a form that I can pass to fminsearch using only the remaining 6 parameters. I apologize for being so obtuse, and I thank you for all of your help.

Tue, 16 Dec 2008 23:56:02 +0000
Re: Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617470
John D'Errico
"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7tie$jau$1@fred.mathworks.com>...<br>
> > "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...<br>
> > > 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<br>
> > > seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........<br>
> > <br>
> > Here's one suggestion that might help. You can, in effect, reduce the total number of parameters you are dealing with by writing your function to be minimized in such a way as to automatically adjust the aparameters and vshift to achieve a minimum square. Your function of Lorenzians is linear in these four parameters and for any given value of the other six, a minimum with respect to these four can always be achieved without iteration. It is simply the solution to a set of four linear equations. So your function can be rewritten to always achieve such a minimum and therefore would involve only the other six parameters to be handed to fminsearch for variation. Six parameters is a whole lot easier for fminsearch to deal with than ten in terms of running into blind alleys or wandering around aimlessly.<br>
> > <br>
> > Roger Stafford<br>
> <br>
> Roger,<br>
> Thank you for the suggestion. I can see that the Lorentzian is linear in the a and vshift terms, and I see that this reduces the number of parameters that fminsearch has to deal with. So if the function of Lorentzians were called L(x), and each lorentzian in the sum were l(x), then L(x)=a1*l1(x)+a2*l2(x)+a3*l3(x)+vshift. What I am unclear on is how to rewrite the function to always achieve the minimum of the a and vshift values. I can see where it should be the solution to a system of linear equations that would automatically minimize the a and vshift values, but I guess I am not seeing how I can get this into a form that I can pass to fminsearch using only the remaining 6 parameters. I apologize for being so obtuse, and I thank you for all of your help.<br>
<br>
<br>
By the way, the method that Roger has suggested<br>
is exactly what is implemented in fminspleas. You <br>
can find it here on the file exchange:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/10093">http://www.mathworks.com/matlabcentral/fileexchange/10093</a><br>
<br>
I've already done much of the work for you.<br>
<br>
HTH,<br>
John

Wed, 17 Dec 2008 01:05:08 +0000
Re: Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617480
Roger Stafford
"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...<br>
> Roger,<br>
> Thank you for the suggestion. I can see that the Lorentzian is linear in the a and vshift terms, and I see that this reduces the number of parameters that fminsearch has to deal with. So if the function of Lorentzians were called L(x), and each lorentzian in the sum were l(x), then L(x)=a1*l1(x)+a2*l2(x)+a3*l3(x)+vshift. What I am unclear on is how to rewrite the function to always achieve the minimum of the a and vshift values. I can see where it should be the solution to a system of linear equations that would automatically minimize the a and vshift values, but I guess I am not seeing how I can get this into a form that I can pass to fminsearch using only the remaining 6 parameters. I apologize for being so obtuse, and I thank you for all of your help.<br>
<br>
Fortunately John has come to the rescue with his 'fminspleas'. I was either unaware of, or had long since forgotten, its presence in the File Exchange, but happily that saves me a lot of work explaining how to carry out this procedure. Hopefully my earlier remarks should at least give you an idea of the theory behind 'fminpleas' action.<br>
<br>
As you can probably surmise, if the number of Lorentzians were to get up to or past five with two parameters each, you may still encounter the sort of difficulties you have experienced, even using 'fminpleas'. Sending a large number (like ten) of nonlinear parameters to fminsearch may still make life difficult for it in converging to a successful solution.<br>
<br>
A lot could depend on the accuracy of your initial estimate in getting the iteration process off to a good start and with ten parameters that could be tricky. I would suggest playing around with the plot function in arriving at rough curve fits to assist in arriving at such estimates.<br>
<br>
Roger Stafford

Thu, 18 Dec 2008 20:45:06 +0000
Re: Curve fitting problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617921
Tazmusica
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi9j84$miq$1@fred.mathworks.com>...<br>
> "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...<br>
> > Roger,<br>
> > Thank you for the suggestion. I can see that the Lorentzian is linear in the a and vshift terms, and I see that this reduces the number of parameters that fminsearch has to deal with. So if the function of Lorentzians were called L(x), and each lorentzian in the sum were l(x), then L(x)=a1*l1(x)+a2*l2(x)+a3*l3(x)+vshift. What I am unclear on is how to rewrite the function to always achieve the minimum of the a and vshift values. I can see where it should be the solution to a system of linear equations that would automatically minimize the a and vshift values, but I guess I am not seeing how I can get this into a form that I can pass to fminsearch using only the remaining 6 parameters. I apologize for being so obtuse, and I thank you for all of your help.<br>
> <br>
> Fortunately John has come to the rescue with his 'fminspleas'. I was either unaware of, or had long since forgotten, its presence in the File Exchange, but happily that saves me a lot of work explaining how to carry out this procedure. Hopefully my earlier remarks should at least give you an idea of the theory behind 'fminpleas' action.<br>
> <br>
> As you can probably surmise, if the number of Lorentzians were to get up to or past five with two parameters each, you may still encounter the sort of difficulties you have experienced, even using 'fminpleas'. Sending a large number (like ten) of nonlinear parameters to fminsearch may still make life difficult for it in converging to a successful solution.<br>
> <br>
> A lot could depend on the accuracy of your initial estimate in getting the iteration process off to a good start and with ten parameters that could be tricky. I would suggest playing around with the plot function in arriving at rough curve fits to assist in arriving at such estimates.<br>
> <br>
> Roger Stafford<br>
<br>
Roger and John,<br>
Thank you very much for your help. I am going to work my way through fminspleas to see if I can figure out how it works. I appreciate your input. Most of my data sets should be fine using that. I am a little bit concerned about fminspleas having problems with my data sets that need to fit to 5 or more lorentzians, as you predicted. Is there a better was for me to do fits in those cases? Thanks again for everything.