<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085</link>
    <title>MATLAB Central Newsreader - Curve fitting problem</title>
    <description>Feed for thread: Curve fitting problem</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 15 Dec 2008 21:08:03 -0500</pubDate>
      <title>Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617241</link>
      <author>Tazmusica </author>
      <description>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&lt;br&gt;
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&lt;br&gt;
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&lt;br&gt;
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&lt;br&gt;
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&lt;br&gt;
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.&lt;br&gt;
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.     &lt;br&gt;
&lt;br&gt;
%Function to fit data to a sum of 3 Lorentzians.&lt;br&gt;
&lt;br&gt;
function[estimates,model]=Lorentzian3(XData,YData);&lt;br&gt;
model=@LorentzFinal;&lt;br&gt;
options=optimset('FunValCheck', 'on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e-40,'TolFun',1e-40);&lt;br&gt;
estimates=fminsearch(model,start_point,options);&lt;br&gt;
&lt;br&gt;
	function[sse,FittedCurve]=LorentzFinal(params);&lt;br&gt;
		a(1)=params(1);&lt;br&gt;
		g(1)=params(2);&lt;br&gt;
		cent(1)=params(3);&lt;br&gt;
		a(2)=params(4);&lt;br&gt;
		g(2)=params(5);&lt;br&gt;
		cent(2)=params(6);&lt;br&gt;
		a(3)=params(7);&lt;br&gt;
		g(3)=params(8);&lt;br&gt;
		cent(3)=params(9);&lt;br&gt;
		vshift=params(10);&lt;br&gt;
		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;&lt;br&gt;
		ErrorVector=FittedCurve-YData;&lt;br&gt;
		sse=sum(ErrorVector.^2);&lt;br&gt;
	end&lt;br&gt;
&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Tue, 16 Dec 2008 09:49:02 -0500</pubDate>
      <title>Re: Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617320</link>
      <author>Roger Stafford</author>
      <description>&quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi6gvi$jlc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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&lt;br&gt;
&amp;gt; seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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 a-parameters 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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 16 Dec 2008 20:04:02 -0500</pubDate>
      <title>Re: Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617438</link>
      <author>Tazmusica </author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gi7tie$jau$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi6gvi$jlc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; 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&lt;br&gt;
&amp;gt; &amp;gt; seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   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 a-parameters 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
Roger,&lt;br&gt;
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.</description>
    </item>
    <item>
      <pubDate>Tue, 16 Dec 2008 23:56:02 -0500</pubDate>
      <title>Re: Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617470</link>
      <author>John D'Errico</author>
      <description>&quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi91ji$b7a$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gi7tie$jau$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi6gvi$jlc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; 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&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; seems to work fine. When I try and fit data that is a sum of 3 or more Lorentzians, it seems to have difficulty. ........&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;   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 a-parameters 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.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger,&lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
By the way, the method that Roger has suggested&lt;br&gt;
is exactly what is implemented in fminspleas. You &lt;br&gt;
can find it here on the file exchange:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/10093&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/10093&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
I've already done much of the work for you.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Wed, 17 Dec 2008 01:05:08 -0500</pubDate>
      <title>Re: Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617480</link>
      <author>Roger Stafford</author>
      <description>&quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi91ji$b7a$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Roger,&lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 18 Dec 2008 20:45:06 -0500</pubDate>
      <title>Re: Curve fitting problem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241085#617921</link>
      <author>Tazmusica </author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gi9j84$miq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Tazmusica &quot; &amp;lt;tazmusica2@deletethis.gmail.com&amp;gt; wrote in message &amp;lt;gi91ji$b7a$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Roger,&lt;br&gt;
&amp;gt; &amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
Roger and John,&lt;br&gt;
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. </description>
    </item>
  </channel>
</rss>

