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

Fit[{f1,f2,},{fun1,fun2,},x]find a linear combination of the funi that best fits the values fi

Basic linear fitting.

Here is a table of the first 20 primes:
Click for copyable input
Here is a plot of this "data":
Click for copyable input
This gives a linear fit to the list of primes. The result is the best linear combination of the functions 1 and x:
Click for copyable input
Here is a plot of the fit:
Click for copyable input
Here is the fit superimposed on the original data:
Click for copyable input
This gives a quadratic fit to the data:
Click for copyable input
Here is a plot of the quadratic fit:
Click for copyable input
This shows the fit superimposed on the original data. The quadratic fit is better than the linear one:
Click for copyable input
{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:
Click for copyable input
This produces a fit to a function of two variables:
Click for copyable input

Fit takes a list of functions, and uses a definite and efficient procedure to find what linear combination of these functions gives the best leastsquares 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 pari 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:
Click for copyable input
The result is the same as from Fit:
Click for copyable input
This fits to a nonlinear form, which cannot be handled by Fit:
Click for copyable input

By default, both Fit and FindFit produce leastsquares 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 leastsquares fit.

This uses the norm, which minimizes the maximum distance between the fit and the data. The result is slightly different from leastsquares:
Click for copyable input

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:
Click for copyable input
option name
default value
NormFunctionNormthe norm to use
AccuracyGoalAutomaticnumber of digits of accuracy to try to get
PrecisionGoalAutomaticnumber of digits of precision to try to get
WorkingPrecisionAutomaticprecision to use in internal computations
MaxIterationsAutomaticmaximum number of iterations to use
StepMonitorNoneexpression to evaluate whenever a step is taken
EvaluationMonitorNoneexpression to evaluate whenever form is evaluated
MethodAutomaticmethod to use

Options for FindFit.