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.
| Fit[{f1,f2,...},{fun1,fun2,...},x] | find a linear combination of the that best fits the values  |
Basic linear fitting.
Here is a table of the first 20 primes.
| Out[1]= |  |
Here is a plot of this "data".
| Out[2]= |  |
This gives a linear fit to the list of primes. The result is the best linear combination of the functions

and

.
| Out[3]= |  |
Here is a plot of the fit.
| Out[4]= |  |
Here is the fit superimposed on the original data.
| Out[5]= |  |
This gives a quadratic fit to the data.
| Out[6]= |  |
Here is a plot of the quadratic fit.
| Out[7]= |  |
This shows the fit superimposed on the original data. The quadratic fit is better than the linear one.
| Out[8]= |  |
| {f1,f2,...} | data points obtained when a single coordinate takes on values  |
| {{x1,f1},{x2,f2},...} | data points obtained when a single coordinate takes on values  |
| {{x1,y1,...,f1},{x2,y2,...,f2},...} | data points obtained with values of a sequence of coordinates |
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

. But you can also give
Fit data that corresponds to the values of a function at arbitrary points, in one or more dimensions.
| Fit[data,{fun1,fun2,...},{x,y,...}] | fit to a function of several variables |
Multivariate fitting.
This gives a table of the values of

,

, and

. You need to use
Flatten to get it in the right form for
Fit.
| Out[9]= |  |
This produces a fit to a function of two variables.
| 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.
| FindFit[data,form,{par1,par2,...},x] | search for values of the that make form best fit data |
| FindFit[data,form,pars,{x,y,...}] | fit multivariate data |
Searching for general fits to data.
This fits the list of primes to a simple linear combination of terms.
| Out[11]= |  |
The result is the same as from
Fit.
| Out[12]= |  |
This fits to a nonlinear form, which cannot be handled by
Fit.
| 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

, 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.
| 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

.
FindFit also has various options that you can set to control how it does its search.
| FindFit[data,{form,cons},pars,vars] | finds a best fit subject to the parameter constraints cons |
Searching for general fits to data.
This gives a best fit subject to constraints on the parameters.
| Out[15]= |  |
Options for FindFit.