Least Squares Fitting—Polynomial

Author

Eric W. Weisstein
March 15, 2009

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/LeastSquaresFittingPolynomial.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html.

©2009 Wolfram Research, Inc. except for portions noted otherwise

Example

PolynomialFit[data_,n_]:=Module[{k,x,y,XT},
{x,y}=Transpose[data];
XT=Table[x^k,{k,0,n}];
Inverse[XT.Transpose[XT]].XT.y
]

data=Table[{x,-x^3+2x^2+5x+3}+0.001RandomReal[{0,1},2],{x,0,20}]

fit=PolynomialFit[data,3]

Plot[x^Range[0,3].fit,{x,0,20},Epilog->{Point[data]}]