The LeastSquares function can be used to find a curve that best fits the data in a least-squares sense, that is, minimizes the sum of the squares of the differences between the estimated values and the actual data. Unlike the curves described in the previous section, the least-squares curve may not necessarily pass through the given points. The least-squares method is often used to fit models to experimental data.
The commands below show how the LeastSquares function is used to compute the best linear fit through the points defined at the beginning of the previous section. In the resulting plot, it can be seen that the curve passes near but not necessarily through all the points.
>
|
|
| |
In the previous example, the default curve used to fit the points is a linear polynomial av+b with parameters a and b. The LeastSquares function allows you to provide a different type of curve and to specify the parameters to optimize. In addition, weights associated with the data points can be defined. See the LeastSquares help page for more details about these options.
The LeastSquares function requires that the model curve be linear in the parameters. In some cases, a nonlinear model can be transformed to allow a least-squares fit. For example, if you wish to use the model
,
you can take the logarithm of both sides of the equation and apply the transformation
to obtain the model
,
which is linear in the parameters c and b.
>
|
|
| |
| (3.1) |
This result gives values for c and b. The following commands show the resulting plot, after transforming back to the original model.
>
|
|