# Least squares method

In previous sections, it is described how to determine polynomials which go exactly through a set of points using Lagrange interpolation and Spline interpolation. In this section obtaining a simple curve (like a line) which goes approximately through a set of data points is considered. For simplicity, just the linear approximation (constant and linear fit) is explained here.

# Constant least squares

Using constant least square method, it is possible to approximate a line $y = c$ through the set of data points considering the minimum deviation from each point. Therefore the error function $g$ should be minimum:
\begin{equation} g(c) = \sum\limits_{i=1}^m w_i(c-f_i)^2. \end{equation} Consequently, the constant $c$ can be derived considering $g'(x) = 0$:
\begin{equation} c = \frac{\sum w_i f_i}{\sum w_i}. \end{equation}

# Linear least squares

In this case, the polynomial is $y = a_0 + a_1 x$, and the error function can be expressed as:
\begin{equation} g(a_0,a_1) = \sum\limits_{i=1}^m w_i(a_0+a_1 x-f_i)^2. \end{equation} In order to minimize the error, the following linear system has to be solved to obtain coefficients $a_0$ and $a_1$:
\begin{equation} \begin{pmatrix} \sum w_i & \sum w_i x_i \\ \sum w_i x_i & \sum w_i x_i^2 \end{pmatrix} \begin{pmatrix} a_0\\ a_1 \end{pmatrix} = \begin{pmatrix} \sum w_i f_i\\ \sum w_i f_i x_i \end{pmatrix} \end{equation}

The relevant Matlab code can be found at the end of 1-D discrete data analysis section.