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 / The Mathematica Book / Advanced Mathematics in Mathematica / Numerical Operations on Data /

3.8.2 Curve Fitting

There are many situations where one wants to find a formula that best fits a given set of data. One way to do this in Mathematica is to use Fit.

Basic linear fits.

Here is a table of the first 20 primes.

In[1]:= fp = Table[Prime[x], {x, 20}]

Out[1]=

Here is a plot of this "data".

In[2]:= gp = ListPlot[ fp ]

Out[2]=

This gives a linear fit to the list of primes. The result is the best linear combination of the functions 1 and x.

In[3]:= Fit[fp, {1, x}, x]

Out[3]=

Here is a plot of the fit.

In[4]:= Plot[%, {x, 0, 20}]

Out[4]=

Here is the fit superimposed on the original data.

In[5]:= Show[%, gp]

Out[5]=

This gives a quadratic fit to the data.

In[6]:= Fit[fp, {1, x, x^2}, x]

Out[6]=

Here is a plot of the quadratic fit.

In[7]:= Plot[%, {x, 0, 20}]

Out[7]=

This shows the fit superimposed on the original data. The quadratic fit is better than the linear one.

In[8]:= Show[%, gp]

Out[8]=

Ways of specifying data.

If you give data in the form , , ... then Fit will assume that the successive correspond to values of a function at successive integer points 1, 2, ... . But you can also give Fit data that corresponds to the values of a function at arbitrary points, in one or more dimensions.

Multivariate fits.

This gives a table of the values of , and . You need to use Flatten to get it in the right form for Fit.

In[9]:= Flatten[ Table[ {x, y, 1 + 5x - x y},
{x, 0, 1, 0.4}, {y, 0, 1, 0.4} ], 1]

Out[9]=

This produces a fit to a function of two variables.

In[10]:= Fit[ % , {1, x, y, x y}, {x, y} ]

Out[10]=

Fit takes a list of functions, and uses a definite and efficient procedure to find what linear combination of these functions gives the best least-squares fit to your data. Sometimes, however, you may want to find a nonlinear fit that does not just consist of a linear combination of specified functions. You can do this using FindFit, which takes a function of any form, and then searches for values of parameters that yield the best fit to your data.

Searching for general fits to data.

This fits the list of primes to a simple linear combination of terms.

In[11]:= FindFit[fp, a + b x + c Exp[x], {a, b, c}, x]

Out[11]=

The result is the same as from Fit.

In[12]:= Fit[fp, {1, x, Exp[x]}, x]

Out[12]=

This fits to a nonlinear form, which cannot be handled by Fit.

In[13]:= FindFit[fp, a x Log[b + c x], {a, b, c}, x]

Out[13]=

By default, both Fit and FindFit produce least-squares fits, which are defined to minimize the quantity , where the are residuals giving the difference between each original data point and its fitted value. One can, however, also consider fits based on other norms. If you set the option NormFunction -> u, then FindFit will attempt to find the fit that minimizes the quantity u[r], where r is the list of residuals. The default is NormFunction -> Norm, corresponding to a least-squares fit.

This uses the -norm, which minimizes the maximum distance between the fit and the data. The result is slightly different from least-squares.

In[14]:= FindFit[fp, a x Log[b + c x], {a, b, c}, x,
NormFunction -> (Norm[#, Infinity] &)]

Out[14]=

FindFit works by searching for values of parameters that yield the best fit. Sometimes you may have to tell it where to start in doing this search. You can do this by giving parameters in the form a, , b, , ... . FindFit also has various options that you can set to control how it does its search.

Options for FindFit.