Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Curve fitting problem

Subject: Curve fitting problem

From: Tazmusica

Date: 15 Dec, 2008 21:08:03

Message: 1 of 6

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

Subject: Curve fitting problem

From: Roger Stafford

Date: 16 Dec, 2008 09:49:02

Message: 2 of 6

"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...
> 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. ........

  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.

Roger Stafford

Subject: Curve fitting problem

From: Tazmusica

Date: 16 Dec, 2008 20:04:02

Message: 3 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7tie$jau$1@fred.mathworks.com>...
> "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...
> > 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. ........
>
> 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.
>
> Roger Stafford

Roger,
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.

Subject: Curve fitting problem

From: John D'Errico

Date: 16 Dec, 2008 23:56:02

Message: 4 of 6

"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7tie$jau$1@fred.mathworks.com>...
> > "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi6gvi$jlc$1@fred.mathworks.com>...
> > > 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. ........
> >
> > 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.
> >
> > Roger Stafford
>
> Roger,
> 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.


By the way, the method that Roger has suggested
is exactly what is implemented in fminspleas. You
can find it here on the file exchange:

http://www.mathworks.com/matlabcentral/fileexchange/10093

I've already done much of the work for you.

HTH,
John

Subject: Curve fitting problem

From: Roger Stafford

Date: 17 Dec, 2008 01:05:08

Message: 5 of 6

"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...
> Roger,
> 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.

  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.

  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.

  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.

Roger Stafford

Subject: Curve fitting problem

From: Tazmusica

Date: 18 Dec, 2008 20:45:06

Message: 6 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi9j84$miq$1@fred.mathworks.com>...
> "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <gi91ji$b7a$1@fred.mathworks.com>...
> > Roger,
> > 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.
>
> 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.
>
> 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.
>
> 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.
>
> Roger Stafford

Roger and John,
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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us