9: Least-Squares Approximation
The method of least-squares is commonly used to fit a parameterized curve to experimental data. In general, the fitting curve is not expected to pass through the data points, making this problem substantially different from the one of interpolation.
We consider here only the most common situation: the fitting of a straight line through data with the same experimental error for all the data points. We assume that the data to be fitted are given by \(\left(x_{i}, y_{i}\right)\) , with \(i=1\) to \(n\) .
We write for the fitting curve
\[y(x)=\alpha x+\beta . \nonumber \]
The distance \(r_{i}\) from the data point \(\left(x_{i}, y_{i}\right)\) and the fitting curve is given by
\[\begin{aligned} r_{i} &=y_{i}-y\left(x_{i}\right) \\ &=y_{i}-\left(\alpha x_{i}+\beta\right) . \end{aligned} \nonumber \]
A least-squares fit minimizes the sum of the squares of the \(r_{i}\) ’s. This minimum can be shown to result in the most probable values of \(\alpha\) and \(\beta\) .
We define
\[\begin{aligned} \rho &=\sum_{i=1}^{n} r_{i}^{2} \\ &=\sum_{i=1}^{n}\left(y_{i}-\left(\alpha x_{i}+\beta\right)\right)^{2} \end{aligned} \nonumber \]
To minimize \(\rho\) with respect to \(\alpha\) and \(\beta\) , we solve
\[\frac{\partial \rho}{\partial \alpha}=0, \quad \frac{\partial \rho}{\partial \beta}=0 \nonumber \]
Taking the partial derivatives, we have
\[\begin{aligned} &\frac{\partial \rho}{\partial \alpha}=\sum_{i=1}^{n} 2\left(-x_{i}\right)\left(y_{i}-\left(\alpha x_{i}+\beta\right)\right)=0 \\ &\frac{\partial \rho}{\partial \beta}=\sum_{i=1}^{n} 2(-1)\left(y_{i}-\left(\alpha x_{i}+\beta\right)\right)=0 \end{aligned} \nonumber \]
These equations form a system of two linear equations in the two unknowns \(\alpha\) and \(\beta\) , which is evident when rewritten in the form
\[\begin{aligned} \alpha \sum_{i=1}^{n} x_{i}^{2}+\beta \sum_{i=1}^{n} x_{i} &=\sum_{i=1}^{n} x_{i} y_{i} \\ \alpha \sum_{i=1}^{n} x_{i}+\beta n &=\sum_{i=1}^{n} y_{i} \end{aligned} \nonumber \]
These equations can be solved numerically, and MATLAB provides a built-in subroutine called polyfit.m. With the data in the vectors \(x\) and \(y\) , the MATLAB call
\[\mathrm{p}=\text { polyfit }(\mathrm{x}, \mathrm{y}, 1) \text {; } \nonumber \]
returns the values \(p(1)=\alpha\) and \(p(2)=\beta\) , which can then be used to draw the fitting line.