Why does $\int_{-L}^{L} \sum_{n=1}^{\infty}a_n\cos \frac{n\pi x}{L}=\sum_{n=1}^{\infty}a_n\int_{-L}^{L}\cos \frac{n\pi x}{L}$ Why does $$\int_{-L}^{L} \sum_{n=1}^{\infty}a_n\cos \frac{n\pi x}{L}=\sum_{n=1}^{\infty}a_n\int_{-L}^{L}\cos \frac{n\pi x}{L}$$
This is used in a derivation of the Fourier coefficients. I see why they flip them but I don't understand why this is true and when we are allowed to do such a thing.
Thanks
 A: If $f$ is square integrable on $[-L,L]$, then
$$
           \lim_{N\rightarrow\infty}\int_{-L}^{L}\left|f(x)-a_0-\sum_{n=1}^{N}\{a_n\sin(n\pi x/L)+b_n\cos(n\pi x/L)\}\right|^2dx =0,
$$
where $a_n$, $b_n$ are the Fourier coefficients. That's enough to say that, for all square integrable $g$ on $[-L,L]$, the following holds
\begin{align}
        \int_{-L}^{L}f(x)g(x)dx &= a_0\int_{-L}^{L}g(x)dx \\
      &+ \sum_{n=1}^{\infty}a_n\int_{-L}^{L}\cos(n\pi x/L)g(x)dx  \\
      &+ \sum_{n=1}^{\infty}b_n\int_{-L}^{L}\sin(n\pi x/L)g(x)dx.
\end{align}
That includes $g(x)=1$, $g(x)=\cos(n\pi x/L)$ and $g(x)=\sin(n\pi x/L)$, all of which are square integrable on $[-L,L]$. So the basic answer to your questions is that you can always interchange summation and integration in the cases where the functions involved are square integrable. You couldn't know that a priori, but it does come out that way in the end. It's really quite a remarkable fact of Fourier analysis because discussions of pointwise convergence are not needed at all.
A: These are conditions allowing the interchange of integration and summation, from stronger to weaker:


*

*$\sum_{n=1}^\infty|a_n|<\infty$.

*$\sum_{n=1}^\infty a_n\cos\dfrac{n\,\pi\,x}{L}$ converges uniformly.

*$\sum_{n=1}^\infty a_n\cos\dfrac{n\,\pi\,x}{L}$ is the Fourier series of a Riemann integrable function (wether or not the series converges.)


In the study of Fourier series, it is assumed that the interchange is valid to motivate the definition of the Fourier coefficients.
