|
From: Thymian <Th...@gm...> - 2007-04-04 17:34:18
|
Peter Hewitt schrieb: > I have a few points on a graph, and would like a C++ function that > calculates a best fit equation such as a n degree polynomial. > > Are there functions already available to do this, or any ideas how I would > write one. > I've checked on the web but can't seem to find anything useful. > > Thanks, > Pete > > I recommend you to use a special form of polynomial interpolation, the "spline interpolation". After understanding the mathematical background, it is relatively easy to implement a spline function into a given computer language like C or C++. Years ago I implemented it even in assembler for an embedded real-time system with very restricted hardware ressources, later in C, when RAM and EEPROM together with the 8/16-bit microcontroller become cheaper. http://de.wikipedia.org/wiki/Spline http://de.wikipedia.org/wiki/Spline-Interpolation http://en.wikipedia.org/wiki/Spline_%28mathematics%29 http://en.wikipedia.org/wiki/Spline_interpolation If you are too lazy to implement it by your own, you could try to use the GSL (GNU Scientific Library). The GNU Scientific Library (GSL) is a numerical library for C and C++ programmers. It is free software under the GNU General Public License. The library provides a wide range of mathematical routines such as interpolation, polynomials, random number generators, special functions and least-squares fitting. There are over 1000 functions in total with an extensive test suite. http://www.gnu.org/software/gsl/ http://www.gnu.org/software/gsl/manual/html_node/ http://www.gnu.org/software/gsl/manual/html_node/Interpolation.html Another possibility for your purpose could be Scilab / Scicos, an "open source" scientific math package, IMHO written in C / C++ and its source code free available. You could try to extract pieces of the source code that you need. http://en.wikipedia.org/wiki/Scilab To try an example with your values (points in your graph) there is a spline calculator, written in Java, that you could take a look at: http://www.arndt-bruenner.de/mathe/scripts/kubspline.htm#rechner I hope your German is better then my English ;-) Gruß, Werner Kitzmann . |