Before reading this page, please check out the Linear Curve Fitting page. Many of the principles mentioned there will be re-used here, and will not be explained in as much detail.
A graph showing how different degree polynomials can be fitted to data.
where: n is the number of points of data (each data point is an x,y pair) f(x) is the function which describes our polynomial curve of best fit
Since we want to fit a polynomial, we can write f(x) as:
f(x)=a0+a1x+a2x2+...+anxn=a0+j=1∑kajxj
where: k is the order of the polynomial
Substituting into above:
err=i=1∑n(yi−(a0+j=0∑kajxj))2
How do we find the minimum of this error function? We use the derivative. If we can differentiate err, we have an equation for the slope. We know that the slope will be 0 when the error is at a minimum.
We have k unknowns, a0,a1,...,ak. We have to take the derivative of each unknown separately: