# Engineering Analysis/Fourier Series

The L$_2$ space is an infinite function space, and therefore a linear combination of any infinite set of orthogonal functions can be used to represent any single member of the L$_2$ space. The decomposition of an L$_2$ function in terms of an infinite basis set is a technique known as the Fourier Decomposition of the function, and produces a result called the Fourier Series.

## Fourier Basis

Let's consider a set of L$_2$ functions, $\phi$, as follows:
    $\phi =\{1,\sin(\pi x),\cos(\pi x),\sin(2\pi x),\cos(2\pi x),\sin(3\pi x),\cos(3\pi x)...\}.\$
We can prove that over a range $[0,2\pi ]$, all of these functions are orthogonal:
    $\int _{0}^{2\pi }1\cdot \cos(n\pi x)dx=0$
    $\int _{0}^{2\pi }1\cdot \sin(n\pi x)dx=0$
    $\int _{0}^{2\pi }\sin(n\pi x)\sin(m\pi x)dx=0,n\neq m$
    $\int _{0}^{2\pi }\sin(n\pi x)\cos(m\pi x)dx=0$
    $\int _{0}^{2\pi }\cos(n\pi x)\cos(m\pi x)dx=0,n\neq m$
Because $\phi$ is as an infinite orthogonal set in L$_2$, $\phi$ is also a valid basis set in the L$_2$ space. Therefore, we can decompose any function in L$_2$ as the following sum:
[Classical Fourier Series]
    $\psi (x)=a_{0}(1)+\sum _{n=1}^{\infty }a_{n}\sin(n\pi x)+\sum _{m=1}^{\infty }b_{m}\cos(m\pi x)$
However, the difficulty occurs when we need to calculate the a and b coefficients. We will show the method to do this below:

## a$_0$: The Constant Term

Calculation of a$_0$ is the easiest, and therefore we will show how to calculate it first. We use the value of a$_0$ which minimizes the error in approximating $f(x)$ by the Fourier series.
First, define an error function, E, that is equal to the squared norm of the difference between the function f(x) and the infinite sum above:
    $E={\frac {1}{2}}\int _{0}^{2\pi }\|f(x)-a_{0}(1)-\sum _{n=1}^{\infty }a_{n}\sin(n\pi x)-\sum _{m=1}^{\infty }b_{m}\cos(m\pi x)\|^{2}dx$
For ease, we will write all the basis functions as the set φ, described above:
    $\sum _{i=0}^{\infty }a_{i}\phi _{i}=a_{0}+\sum _{n=1}^{\infty }a_{n}\sin(n\pi x)+\sum _{m=1}^{\infty }b_{m}\cos(m\pi x)$
Combining the last two functions together, and writing the norm as an integral, we can say:
    $E={\frac {1}{2}}\int _{0}^{2\pi }|\sum _{i=0}^{\infty }a_{i}\phi _{i}|^{2}dx$
We attempt to minimize this error function with respect to the constant term. To do this, we differentiate both sides with respect to a$_0$, and set the result to zero:
    $0={\frac {\partial E}{\partial a_{0}}}=\int _{0}^{2\pi }(f(x)-\sum _{i=0}^{\infty }a_{i}\phi _{i}(x))(-\phi _{0}(x))dx$
The φ$_0$ term comes out of the sum because of the chain rule: it is the only term in the entire sum dependent on a$_0$. We can separate out the integral above as follows:
    $\int _{0}^{2\pi }(f(x)-\sum _{i=0}^{\infty }a_{i}\phi _{i})(-\phi _{0})dx=-\int _{0}^{2\pi }f(x)\phi _{0}(x)dx+a_{0}\int _{0}^{2\pi }\phi _{0}(x)\phi _{0}(x)dx$
All the other terms drop out of the infinite sum because they are all orthogonal to φ$_0$. Again, we can rewrite the above equation in terms of the scalar product:
    $0=-\langle f(x),\phi _{0}(x)\rangle +a_{0}\langle \phi _{0}(x),\phi _{0}(x)\rangle$
And solving for a$_0$, we get our final result:
    $a_{0}={\frac {\langle f(x),\phi _{0}(x)\rangle }{\langle \phi _{0}(x),\phi _{0}(x)\rangle }}$

## Sin Coefficients

Using the above method, we can solve for the a$_n$ coefficients of the sin terms:
    $a_{n}={\frac {\langle f(x),\sin(n\pi x)\rangle }{\langle \sin(n\pi x),\sin(n\pi x)\rangle }}$

## Cos Coefficients

Also using the above method, we can solve for the b$_n$ terms of the cos term.
    $b_{n}={\frac {\langle f(x),\cos(n\pi x)\rangle }{\langle \cos(n\pi x),\cos(n\pi x)\rangle }}$