Alglib.js Curve Fitting

An fitting a cubic equation (or arbitrary function) to a dataset using ALGLIB.js

Suppose that we are given a series of points on a graph and we need to fit them with a cubic polynomial. \[ (x_i,y_i) = [(-3, 8), (1,3), (5,3), (9,8), (10,16)]\]

We want to choose \(a_0\), \(a_1\), \(a_2\) and \(a_3\) so that the function \(f(x)\) is minimized where \[f(x) = a_3x^3+a_2x^2+a_1x+a_0\]

To do this we define a least square equations that gives the sum of the squares of the "error" of the function evaluated at all points. \[Sum Squared Error = \sqrt{\sum_{i=1}^5 (y_i - f(x_i))^2}\]

In theory this process can be extended to any arbitrary function.