3.8.1 Curve Fitting
Built into Mathematica are various facilities for finding least-squares fits to data. The basic idea of the fits is to take a list of functions that you specify, and try to find a linear combination of them which approximates your data as well as possible. The goodness of fit is measured by the quantity , where is the value of your data point, and is the value obtained from the fit. The best fit is the one which minimizes .
The basic form of the Fit function.
Some fits to lists of data.
Here is a table of the first 20 primes.
Here is a plot of this "data".
This gives a linear fit to the list of primes. The result is the best linear combination of the functions 1 and x.
Here is a plot of the fit.
Here is the fit superimposed on the original data.
This gives a quadratic fit to the data.
Here is a plot of the quadratic fit.
This shows the fit superimposed on the original data. The quadratic fit is better than the linear one.
Polynomial fits are the most common kind to use. However, if you have a reason to believe that your data follows some other functional form, you can include the appropriate functions in the list you give to Fit.
This gives a table of the values of for from 1 to 10 in steps of 1.
This fit recovers the original functional form.
If you include other functions in the list, Fit determines that they occur with small coefficients.
You can use Chop to get rid of the terms with small coefficients.
There are several common reasons for doing fits.
If you have a particular model for some data, you can do a fit to try and determine the parameters of the model.
Another common use of fits is in finding approximate formulas to describe a particular set of data. You can use the form you get from a Fit as a summary of your actual data.
In the examples of Fit so far, the data points you give are assumed to correspond to the results of evaluating a function of one variable when the variable successively takes on values 1, 2, ... . You can also specify data that depends on several variables, each given an arbitrary sequence of values, not necessarily arranged in any kind of regular array.
Ways of specifying data in Fit.
This gives a table of the values of , and . You need to use Flatten to get it in the right form for Fit.
This produces a fit to a function of two variables.
Fit takes the list of functions you give, and finds the best fit to your data, according to the least-squares criterion, using these functions. There is absolutely no guarantee that the fit you get will in fact accurately reproduce your data. To find out whether it can, you should use statistical testing functions, such as those in the statistics packages available for Mathematica.
Finding exact fits.
If you have sufficiently few data points, then by including sufficiently many terms, you can construct a polynomial which fits your data exactly. In general, if you have data points, you may need a polynomial of degree up to . The function InterpolatingPolynomial gives you such a polynomial. Such polynomials can in fact be useful not only in fitting numerical data, but also in various algebraic algorithms.
This gives a degree 4 polynomial which fits the data given exactly.
When , the polynomial reproduces the fourth data point exactly.
This uses the polynomial fit to extrapolate the data.