Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2012-12-31 02:55:47

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

Good curve-fitting routine

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

#2 2012-12-31 03:01:10

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

Re: Good curve-fitting routine

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

#3 2013-01-01 10:08:25

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

Re: Good curve-fitting routine

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

#4 2013-01-01 10:33:08

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

Re: Good curve-fitting routine

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

#5 2013-01-01 11:07:45

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

Re: Good curve-fitting routine

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

#6 2013-01-01 11:32:35

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

Re: Good curve-fitting routine

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

#7 2013-01-01 11:46:33

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

Re: Good curve-fitting routine

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

#8 2013-01-01 11:49:41

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

Re: Good curve-fitting routine

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

#9 2013-01-01 12:05:51

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

Re: Good curve-fitting routine

All the data are in decimal double precision.

Offline

#10 2013-01-01 12:06:28

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

Re: Good curve-fitting routine

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

#11 2013-01-01 12:09:59

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

Re: Good curve-fitting routine

Yes!

Offline

#12 2013-01-01 12:12:30

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

Re: Good curve-fitting routine

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

#13 2013-01-01 12:21:16

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

Re: Good curve-fitting routine

Hi bobbym

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


“Here lies the reader who will never open this book. He is forever dead.
“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

#14 2013-01-01 12:29:41

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

Re: Good curve-fitting routine

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

#15 2013-01-01 12:39:34

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

Re: Good curve-fitting routine

I attach examples for good and bad fits.

Offline

#16 2013-01-01 12:42:31

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

Re: Good curve-fitting routine

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

#17 2013-01-01 12:47:22

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

Re: Good curve-fitting routine

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

#18 2013-01-01 12:57:06

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

Re: Good curve-fitting routine

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

#19 2013-01-01 13:04:23

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

Re: Good curve-fitting routine

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

#20 2013-01-01 13:12:50

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

Re: Good curve-fitting routine

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

#21 2013-01-01 15:58:55

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

Re: Good curve-fitting routine

Hi;

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

Many thanks!

Offline

#22 2013-01-01 20:56:19

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

Re: Good curve-fitting routine

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?


“Here lies the reader who will never open this book. He is forever dead.
“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

#23 2013-01-02 03:23:16

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

Re: Good curve-fitting routine

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

#24 2013-01-02 06:41:37

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

Re: Good curve-fitting routine

Yes, it is.


“Here lies the reader who will never open this book. He is forever dead.
“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

#25 2013-01-02 06:53:28

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

Re: Good curve-fitting routine

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

Board footer

Powered by FluxBB