Polynomial Fitting in C++ using Boost

A while back I needed to implement an algorithm utilizing an equivalent function of polyfit/polyval in LabView. The original algorithm was created in LabView for research purposes and since we were in the process of creating a commercial version of the device, the algorithm implementation in C++ needed to mirror the LabView implementation. I found that there is no simple and neat implementation of polyfit() polyval() in C++ so through a bit of digging, research, and implementation I came up with my own functions.

Polynomial fitting function returns coefficients of a polynomial representing a given data. MathWorks has a decent explanation of the function. Below is a graph showing 1000 randomly varying points and the polynomial curve in red generated from the given points.
PolyFit

I used boost::ublas since it provides simple matrix class and fast LU decomposition. If you are unfamiliar with boost you may find more information on Boost C++ site. If you wish to install Boost libraries on windows, I recommend downloading the recompiled install package from BoostPro site.

PolyFitExample was writen in C++/CLI since windows forms provides a simple graph component. You may access the source by clicking the link.

One thought on “Polynomial Fitting in C++ using Boost

Comments are closed.