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

You are not logged in.

## #1 2013-01-01 01:55:47

JamesW
Member

Offline

### 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.

## #2 2013-01-01 02:01:10

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #3 2013-01-02 09:08:25

JamesW
Member

Offline

### 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.

## #4 2013-01-02 09:33:08

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #5 2013-01-02 10:07:45

JamesW
Member

Offline

### 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.

## #6 2013-01-02 10:32:35

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #7 2013-01-02 10:46:33

JamesW
Member

Offline

### 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.

## #8 2013-01-02 10:49:41

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #9 2013-01-02 11:05:51

JamesW
Member

Offline

### Re: Good curve-fitting routine

All the data are in decimal double precision.

## #10 2013-01-02 11:06:28

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

JamesW
Member

Offline

Yes!

## #12 2013-01-02 11:12:30

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #13 2013-01-02 11:21:16

anonimnystefy
Real Member

Offline

### Re: Good curve-fitting routine

Hi bobbym

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

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #14 2013-01-02 11:29:41

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #15 2013-01-02 11:39:34

JamesW
Member

Offline

### Re: Good curve-fitting routine

I attach examples for good and bad fits.

## #16 2013-01-02 11:42:31

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #17 2013-01-02 11:47:22

JamesW
Member

Offline

### 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)

## #18 2013-01-02 11:57:06

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #19 2013-01-02 12:04:23

JamesW
Member

Offline

### 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!

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

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #21 2013-01-02 14:58:55

JamesW
Member

Offline

### 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!

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

anonimnystefy
Real Member

Offline

### 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?

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

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

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

## #24 2013-01-03 05:41:37

anonimnystefy
Real Member

Offline

### Re: Good curve-fitting routine

Yes, it is.

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #25 2013-01-03 05:53:28

bobbym

Online

### 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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.