This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / Standard Packages / NumericalMath /


The Mathematica function Fit does linear least squares fitting to data. It allows any combination of basis functions. However, it is often the case that you want to do polynomial fitting and it would be easier if you did not have to explicitly list the basis functions. In addition, for high-degree fitting, things often get rather unstable numerically if you give simple powers of a variable as basis functions. PolynomialFit avoids these problems.

Fitting polynomials to data.

PolynomialFit uses the function Fit, but it chooses a more appropriate set of basis functions than simple powers of a variable. It also returns a pure function rather than an expression and evaluates the function in a numerically stable way. If you want, you can evaluate the function at a symbolic point and expand the result to get an ordinary looking polynomial, but this is not advised; doing so will result in something that could be numerically very unstable. That is, extreme loss of accuracy could result due to cancellation of digits.

This loads the package.

In[1]:= <<NumericalMath`PolynomialFit`

Here is the cubic polynomial that fits the data.

In[2]:= p = PolynomialFit[{1,4,9,16,25,36,49},3]


This evaluates the polynomial at 10.

In[3]:= p[10]


This gives the ordinary form of the polynomial. Doing this and then replacing x by some numerical value is not advised, especially for high-degree polynomials.

In[4]:= Expand[p[x]]