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 that best fits the values

Basic linear fitting.

Here is a table of the first 20 primes.
In[1]:=
Click for copyable input
Out[1]=
Here is a plot of this "data".
In[2]:=
Click for copyable input
Out[2]=
This gives a linear fit to the list of primes. The result is the best linear combination of the functions and .
In[3]:=
Click for copyable input
Out[3]=
Here is a plot of the fit.
In[4]:=
Click for copyable input
Out[4]=
Here is the fit superimposed on the original data.
In[5]:=
Click for copyable input
Out[5]=
This gives a quadratic fit to the data.
In[6]:=
Click for copyable input
Out[6]=
Here is a plot of the quadratic fit.
In[7]:=
Click for copyable input
Out[7]=
This shows the fit superimposed on the original data. The quadratic fit is better than the linear one.
In[8]:=
Click for copyable input
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.
In[9]:=
Click for copyable input
Out[9]=
This produces a fit to a function of two variables.
In[10]:=
Click for copyable input
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 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 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.
In[11]:=
Click for copyable input
Out[11]=
The result is the same as from Fit.
In[12]:=
Click for copyable input
Out[12]=
This fits to a nonlinear form, which cannot be handled by Fit.
In[13]:=
Click for copyable input
Out[13]=

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 , 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.
In[14]:=
Click for copyable input
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.
In[15]:=
Click for copyable input
Out[15]=
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.