# Least-squares function approximation

In mathematics, least squares function approximation applies the principle of least squares to function approximation, by means of a weighted sum of other functions. The best approximation can be defined as that which minimizes the difference between the original function and the approximation; for a least-squares approach the quality of the approximation is measured in terms of the squared differences between the two.

## Functional analysis

A generalization to approximation of a data set is the approximation of a function by a sum of other functions, usually an orthogonal set:
    $f(x)\approx f_{n}(x)=a_{1}\phi _{1}(x)+a_{2}\phi _{2}(x)+\cdots +a_{n}\phi _{n}(x),\$
with the set of functions {$\ \phi _{j}(x)$} an orthonormal set over the interval of interest, say [a, b]: see also Fejér's theorem. The coefficients {$\ a_{j}$} are selected to make the magnitude of the difference ||f − f$_n$||$^2$ as small as possible. For example, the magnitude, or norm, of a function g (x ) over the interval [a, b] can be defined by:
    $\|g\|=\left(\int _{a}^{b}g^{*}(x)g(x)\,dx\right)^{1/2}$
where the ‘*’ denotes complex conjugate in the case of complex functions. The extension of Pythagoras' theorem in this manner leads to function spaces and the notion of Lebesgue measure, an idea of “space” more general than the original basis of Euclidean geometry. The { $\phi _{j}(x)\$ } satisfy orthonormality relations:
    $\int _{a}^{b}\phi _{i}^{*}(x)\phi _{j}(x)\,dx=\delta _{ij},$
where δ$_{ij}$ is the Kronecker delta. Substituting function f$_n$ into these equations then leads to the n-dimensional Pythagorean theorem:
    $\|f_{n}\|^{2}=|a_{1}|^{2}+|a_{2}|^{2}+\cdots +|a_{n}|^{2}.\,$
The coefficients {a$_j$} making ||f − f$_n$||$^2$ as small as possible are found to be:
    $a_{j}=\int _{a}^{b}\phi _{j}^{*}(x)f(x)\,dx.$
The generalization of the n-dimensional Pythagorean theorem to infinite-dimensional  real inner product spaces is known as Parseval's identity or Parseval's equation. Particular examples of such a representation of a function are the Fourier series and the generalized Fourier series.

## Further discussion
### Using linear algebra

It follows that one can find a "best" approximation of another function by minimizing the area between two functions, a continuous function $f$ on $[a,b]$ and a function $g\in W$ where $W$ is a subspace of $C[a,b]$:
    ${\text{Area}}=\int _{a}^{b}\left\vert f(x)-g(x)\right\vert \,dx,$
all within the subspace $W$. Due to the frequent difficulty of evaluating integrands involving absolute value, one can instead define
    $\int _{a}^{b}[f(x)-g(x)]^{2}\,dx$
as an adequate criterion for obtaining the least squares approximation, function $g$, of $f$ with respect to the inner product space $W$.
As such, $\lVert f-g\rVert ^{2}$ or, equivalently, $\lVert f-g\rVert$, can thus be written in vector form:
    $\int _{a}^{b}[f(x)-g(x)]^{2}\,dx=\left\langle f-g,f-g\right\rangle =\lVert f-g\rVert ^{2}.$
In other words, the least squares approximation of $f$ is the function $g\in {\text{ subspace }}W$ closest to $f$ in terms of the inner product $\left\langle f,g\right\rangle$. Furthermore, this can be applied with a theorem:
    Let $f$ be continuous on $[a,b]$, and let $W$ be a finite-dimensional subspace of $C[a,b]$. The least squares approximating function of $f$ with respect to $W$ is given by
       $g=\left\langle f,{\vec {w}}_{1}\right\rangle {\vec {w}}_{1}+\left\langle f,{\vec {w}}_{2}\right\rangle {\vec {w}}_{2}+\cdots +\left\langle f,{\vec {w}}_{n}\right\rangle {\vec {w}}_{n},$
    where $B=\{{\vec {w}}_{1},{\vec {w}}_{2},\dots ,{\vec {w}}_{n}\}$ is an orthonormal basis for $W$.