Polynomial curve fitting - POLYFIT

Server IP is:


I was always fascinated by the idea of non-linear curve fitting the given data. Especially the polynomial fit one, as opposed to piece-wise splines, because piece-wise only hold true within the bounds of data points and are only good for interpolation and never extrapolation guesses later on. Thus, I have decided to implement my favorite feature from MATLAB the “polyfit” method.


MATLAB describes it as such:

image from MATLAB


Under "Algorithms" section in MATLAB docs it shows the way it solves for polynomial coefficients:

image from MATLAB


I then searched the internet for the best implementation and of Vandermonde matrix algorithm and after sorting/filtering through numerous less useful resources I found one great one that actually does a great job in explaining/simplifying and providing the best simple solution formula to follow. The website link is here: https://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html


This site provides a great explanation and the solution flow to reduce it to a rather simple matrix algebra operations here:

image from MATLAB


So, I have followed this equation path and created a javascript function to find the best data curve fit given X-Y data points and desired polynomial degree:

image from MATLAB


Along with some other related functions that may be useful to test/compare and extend the use case:

image from MATLAB


eval_expression function is sort of another javascript implementation of another MATLAB function polyval. However, is more generic as it can be used to evaluate any symbolically defined expression.

All code related for this project can be found at "public" GITHUB repository here:https://github.com/andreylisovskiy/polyfit.git

TEST DRIVE..

Insert comma separated X and Y coordinates of data points bellow. Make sure to use comma (,) as separator: