You are not logged in.

- Topics: Active | Unanswered

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

I am looking for a reliable curve fitting routine (preferrably in C or C++). Currently I use Levenberg-Marquardt routine from numerical recipes but it fails frequently. Your adivce will be appreciated.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi JamesW;

Doesn't Numerical recipes have a least square routine?

Or why not try Octave, Maxima or geogebra they all have built in routines for least square curve fitting. Octave, uses Levenberg-Marquardt!

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Hi bobbym

Thank you for your reply. The problem with these general packages is that they are not flexible to use especially when the fitting model is too complicated. I spent several days in vain trying to do the fitting by Matlab. I don't think these packages can do better than Matlab.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi;

They sure can. Matlab is actually not even on my list. It actually is a very weak Computer Algebra system.

Some general questions:

1) This is a least squares fit or minimax or collocation?

2)What does one of your fitting models look like?

3) How many points are you fitting to?

4) The most important question, how many variables are you fitting for. Or how high is the degree of the polynomial you are fitting for. Because if you have more than 15 variables or higher than a 15 degree polynomial then least squares using the 1, x, x^2, x^3... as the basis is going to be a dismal failure in C++.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Hi;

1) It is a least squares fit.

2) It consists of several Fano profiles (typically 5) superimposed on a logarithmic quadratic polynomial. It is not a simple model.

3) Between 3000-4000 points.

4) Typically 8 variables. The maximum is usually 13.

Do you think Octave, Maxima or geogebra can do this and which one would you recommend most.

By the way, I used Levenberg-Marquardt from numerical recipes and it sometimes produces a very good fit, but in most cases it goes wild. I tried many tricks but no one can do the fitting in all cases correctly.

Many thanks for your advice

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi;

That is a monstrously over determined system ( more equations than unknowns )

but in most cases it goes wild. I tried many tricks but no one can do the fitting in all cases correctly.

Let me ask what you mean by correctly and a good fit. Take a look at the drawing below. You can click on it to make it larger. It represents a least squares fit through 11 points using a linear equation.

That may look like a bad, a very bad fit but it is actually a very good one. My point is, sometimes a fit that does not go through a single point is the best that can be done with the model you are using.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Hi

I appreciate your point and agree with most of your post, but my problem is different because first my data have a very distinctive shape (profiles sitting on a background) and not like your data which are scattered. My aim is to try to fit a curve to be as close as possible to the original data points. What I get from Levenberg-Marquardt in most cases is not an ideal fit because when I play with my initial values I get better fits (both visually and in a least squares sense). Because the model is complex and the data is large, I cannot afford playing with the initial values to get the 'best fit'. What I am looking for is some routine that is not easy to be trapped in local minima and could find its way to the global minimum spontaneously without interaction and manual adjustment.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

That is what I meant. If it is failing in a least square sense then it is failing.

Is the data in integer values or is it in decimal? If it is in decimal, what precision is it?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

All the data are in decimal double precision.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

They are in (x,y) coordinate form?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Yes!

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

What kind of forms are you fitting for? Please give me one that failed.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**anonimnystefy****Real Member**- From: Harlan's World
- Registered: 2011-05-23
- Posts: 16,037

Hi bobbym

Can you recommend any good books on interpolation and/or curve-fitting available online?

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

The knowledge of some things as a function of age is a delta function.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi;

Good books are hard to find. I have used the Scheid book on numerical analysis my whole life. Except for Forman's stuff it is the best ever written.

If you hunt around you might find it online. I did, I do not know where though or if it is still there.

I would be willing to go over it as we have done with everything else. Open up a thread and ask a question and I will be there.

Did you know that when I was your age or a little younger, curve fitting ( collocation ) was the reason I got into mathematics?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

I attach examples for good and bad fits.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

I presume you are interested in the drawing on the right.

What equations did you use for those fits, right and left?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Yes, I wish to get fits as good as the one on the right. In most cases I get fits between these two extremes.

In both cases I used 5 Fano profiles with a quadratic background. (Please refer to Fano resonance on Wiki)

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi James;

Here is the way I work. Slowly and step by step. Empirically coming to the solution (hopefully). That means experimentally, call me a dabbler if you like but that is what gets the cheese in numerical and computational analysis.

What does this mean to you? It means like your doctor you can not hold anything back from me. I have to know what you know. If the data you have for some reason you feel is secret then I can not assist. I need the equations you are using and hopefully the data.

I have worked on two jobs in my life that the person who hired me would not tell me what the job was for. Wished to keep everything a secret. It took ten times longer and the solution was not maximal. There is a guy in here right now who is asking me all kinds of questions about a roulette wheel. I am unable to help him any further because he is unwilling to provide the data.

I need to see and know everything about the job to have any chance of solving it. If this is impossible please let me know now.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Hi bobbym

I have no secret project. Please give me your eamil and I will send you the data and the equations which I used to do these fittings.

Many thanks!

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi;

I have sent you an email. I have used the email you registered with.

I am going to go eat now and may the force be with me for your question.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**JamesW****Member**- Registered: 2012-12-31
- Posts: 13

Hi;

I sent the data and detailed description of the model. Please let me know if anything is missing or ambiguous.

Many thanks!

Offline

**anonimnystefy****Real Member**- From: Harlan's World
- Registered: 2011-05-23
- Posts: 16,037

bobbym wrote:

Hi;

Good books are hard to find. I have used the Scheid book on numerical analysis my whole life. Except for Forman's stuff it is the best ever written.

If you hunt around you might find it online. I did, I do not know where though or if it is still there.

I would be willing to go over it as we have done with everything else. Open up a thread and ask a question and I will be there.

Did you know that when I was your age or a little younger, curve fitting ( collocation ) was the reason I got into mathematics?

I have found a book of his called Schaum's outline of theory and problems in numerical analysis. Is that the one?

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

The knowledge of some things as a function of age is a delta function.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi anonimnystefy;

Is it the 2nd edition?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**anonimnystefy****Real Member**- From: Harlan's World
- Registered: 2011-05-23
- Posts: 16,037

Yes, it is.

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

The knowledge of some things as a function of age is a delta function.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Hi;

Is it in english? I can not tell because it is already gone!

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline