C for Polynomial Fit

Howdy :

Does anybody know where I can find code for a C function to fit an (N-1)th order polynomial to N (x,y) data points. Actually I need to fit a 5th order polynomial to 11 x,y points.

Thanks folks, Gary

Reply to
Gary Pace
Loading thread data ...

Numerical Recipies in C

formatting link

Vladimir Vassilevsky DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

Since you have fewer terms in the polynomial than data points, you will need to define what is an optimum fit. One popular (but certainly not the only) criterion is to minimize the sum of the error squared at each data point. It's easy to compute (derivative == 0 at minima).

formatting link

In real applications you may actually wish to minimize the maximum abs. value of error at all data points.

Best regards, Spehro Pefhany

--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

There are a number of places where you can get C source for polynomial fits. When I needed that code about 5 years ago, I found source code at a group that appeared to be a spinoff of CalTech. IIRC, it was a few hundred bucks for clean, well-commented source code. It worked well, and I later purchased code for a FFT routine used in another application. (I'm two computers and a few hard disk crashes away from that project, so I don't have a link handy.)

I bought the source code because I'm not an expert on math routines of this sort, and it was cheaper to buy the code than to bill the client for the time it would have taken for me to code, test, and understand the routines by my self.

Mark Borgerson

Reply to
Mark Borgerson

Simply look up the general art of "Least squares fit". Be advised that the resultant matrices can be fairly unstable.

--
Merry Christmas, Happy Hanukah, Happy New Year
 Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
Reply to
CBFalconer

Indeed. If I was to be charged with designing code to fit data in runtime I'd more than likely opt for something along the lines of a cubic spline fit, which uses more data storage but is not as prone to blow itself into smithereens (numerically speaking).

Best regards, Spehro Pefhany

--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

I would try the least squares method first using a math package. It should be easy to implement and test to see if it meets your needs. If the least squares method doesn't work I would try the Levenberg- Marquardt algorithm which can be use to fit both linear and non-linear functions but it may be more work and it will take up more resources. I can't imagine using either is an embedded application as they use a lot of resources. If you know your data set is small and fixed as you say then I would consider solving for the 6 coefficients symbolically using a symbolic math software. That way you have just 6 big ugly equations that are as efficient as they can be for embedded use. This way one avoid the matrix library or macros that take up a lot of code.

Peter Nachtwey

Reply to
pnachtwey

Hmm... you've just contradicted yourself with the space of two sentences. So which is it going to be (N-1)th order, order some arbitrary Mth order, with M

Reply to
Hans-Bernhard Bröker

... snip ...

It seems to me that this is a required technique for all sorts of embedded systems. The area of differences will probably be in selecting points for rejection (in the fitted samples), probabably on the base of deviation from the fitted curve.

--
Merry Christmas, Happy Hanukah, Happy New Year
 Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
Reply to
CBFalconer

In answer to Herr Bröker's post, I posted here because I hoped to get intelligent and helpful suggestions - which I did - many thanks to those folks who posted. (Actually, I need the code for an embedded application, but it will run in the background so I'm not overly concerned about resources).

I at least know where to start...I suppose it was always a faint hope that somebody would say "follow this link to the source for int UnconditionalPerfectPolyFit(int n, int m, double *X, double *Y, double

*K)", but hey, it was worth a try.
Reply to
Gary Pace

You were given such a link.

--
Grant Edwards
grante@visi.com
Reply to
Grant Edwards

Are you referring the the NR link ? I ordered the book and CD so hopefully I have everything I need.

Reply to
Gary Pace

Add to the other suggestions the Gnu Scientific Library, if your application is not intolerant to the the license.

formatting link

--
David M. Palmer  dmpalmer@email.com (formerly @clark.net, @ematic.com)
Reply to
David M. Palmer

Gary, for some theory, independent on the language, see

formatting link

Rene

--
Ing.Buero R.Tschaggelar - http://www.ibrtses.com
& commercial newsgroups - http://www.talkto.net
Reply to
Rene Tschaggelar

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.