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 the Wolfram Language is to use (RefLink[Fit, paclet:ref/Fit]).

Basic linear fitting.

Here is a table of the first 20 primes:

fp=Table[Prime[x],{x,20}]

Here is a plot of this "data":

gp=ListPlot[fp]

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

Fit[fp,{1,x},x]

Here is a plot of the fit:

Plot[%,{x,0,20}]

Here is the fit superimposed on the original data:

Show[%,gp]

This gives a quadratic fit to the data:

Fit[fp,{1,x,x^2},x]

Here is a plot of the quadratic fit:

Plot[%,{x,0,20}]

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

Show[%,gp]

Ways of specifying data.

If you give data in the form {(Subscript[f, 1]),(Subscript[f, 2]),…} then (RefLink[Fit, paclet:ref/Fit]) will assume that the successive (Subscript[f, i]) correspond to values of a function at successive integer points {1,2,…}. But you can also give (RefLink[Fit, paclet:ref/Fit]) data that corresponds to the values of a function at arbitrary points, in one or more dimensions.

Multivariate fitting.

This gives a table of the values of x, y, and 1+5x-xy. You need to use (RefLink[Flatten, paclet:ref/Flatten]) to get it in the right form for (RefLink[Fit, paclet:ref/Fit]):

Flatten[Table[{x,y,1+5x-x y},{x,0,1,0.4},{y,0,1,0.4}],1]

This produces a fit to a function of two variables:

Fit[%,{1,x,y,x y},{x,y}]

(RefLink[Fit, paclet:ref/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 (RefLink[FindFit, paclet:ref/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:

FindFit[fp,a+b x+c Exp[x],{a,b,c},x]

The result is the same as from (RefLink[Fit, paclet:ref/Fit]):

Fit[fp,{1,x,Exp[x]},x]

This fits to a nonlinear form, which cannot be handled by (RefLink[Fit, paclet:ref/Fit]):

FindFit[fp,a x Log[b+c x],{a,b,c},x]

By default, both (RefLink[Fit, paclet:ref/Fit]) and (RefLink[FindFit, paclet:ref/FindFit]) produce least-squares fits, which are defined to minimize the quantity (χ^2)==(∑_i)|(Subscript[r, i])(|^2), where the (Subscript[r, i]) 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 (RefLink[NormFunction, paclet:ref/NormFunction])->u, then (RefLink[FindFit, paclet:ref/FindFit]) will attempt to find the fit that minimizes the quantity u[r], where r is the list of residuals. The default is (RefLink[NormFunction, paclet:ref/NormFunction])->(RefLink[Norm, paclet:ref/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:

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

(RefLink[FindFit, paclet:ref/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,(Subscript[a, 0])},{b,(Subscript[b, 0])},…}. (RefLink[FindFit, paclet:ref/FindFit]) also has various options that you can set to control how it does its search.

Searching for general fits to data.

This gives a best fit subject to constraints on the parameters:

FindFit[fp,{a x Log[b+c x],{0<=a<=1,0<=b≤1,c≥1}},{a,b,c},x]

Options for (RefLink[FindFit, paclet:ref/FindFit]).