This question is strongly related to this question (that does not have an answer).
$$a_n=\frac{2}{L}\int_{x_0}^{x_0+L}f(x)\cos\left(\frac{2\pi nx}{L}\right)dx\tag{1}$$
similarly $$b_n=\frac{2}{L}\int_{x_0}^{x_0+L}f(x)\sin\left(\frac{2\pi nx}{L}\right)dx\tag{2}$$ where $x_0$ is arbitrary but is often taken as $0$ or $-\frac{L}{2}$ and $L$ is the period of $f(x)$.
The following question I asked on "Chegg Study" website is here:
Show that the Fourier series of $f(x)=\cos\left(\frac{x}{2}\right)$ for $-\pi\lt x \lt \pi$ is given by $$f(x)=\frac{2}{\pi}+\frac{4}{\pi}\sum_{n=1}^{\infty}\frac{(-1)^{n+1}\cos(nx)}{4n^2 - 1}$$
So my attempt at the finding the Fourier series for $\cos\left(\frac{x}{2}\right)$ on the interval $-\pi \lt x \lt \pi$ is by first noting that since $\cos\left(\frac{x}{2}\right)$ is not periodic on $-\pi \lt x \lt \pi$,$\,$ I must therefore make the function periodic by extending the interval to $-2\pi \lt x \lt 2\pi$. By the Dirichlet conditions for convergence the function must be periodic.
Now that the function is symmetric and periodic about $x=0$ the $b_n=0 \,\forall \,n \in \mathbb N$
A plot of the extended function is shown below:
Now by $(1)$, $$\begin{align}a_n &=\frac{2}{L}\int_{x_0}^{x_0+L}f(x)\cos\left(\frac{2\pi nx}{L}\right)dx \\&=\frac{2}{4\pi}\int_{-2\pi}^{2\pi}\cos\left(\frac{x}{2}\right)\cos\left(\frac{2\pi nx}{4\pi}\right)dx\\&=\frac{1}{2\pi}\int_{-2\pi}^{2\pi}\cos\left(\frac{x}{2}\right)\cos\left(\frac{nx}{2}\right)dx\\&=0\end{align}$$
and this has been verified here by Wolfram Alpha.
All I did was substitute the value of the period $L$ into the argument of the cosine, the limits and the denominator in front of the integral.
Why is this giving me the wrong answer when it clearly works for other functions like $x^2$ on $0 \lt x \le 2$ as can be seen here "Riley, Hobson and Bence - Mathematical methods for physics and engineering", Section 12.5 - "Non-periodic functions", page 422 and 423?
The method used in the $x^2$ and $\cos(x/2)$ case was general and did not specify any restrictions on the function being extended.
So, put in another way, is there some kind of rule that says extending a trigonometric function to a full period is always forbidden since integrating over the period will be zero?



You want to expand $\cos(x/2)$ on $[-\pi,\pi]$ in a Fourier series. The resulting series will converge to the periodic extension of $\cos(x/2)$ from $[-\pi,\pi]$ to $\mathbb{R}$ with period $2\pi$. The Fourier coefficients are $$ a_0 = \frac{1}{\pi}\int_{-\pi}^{\pi}\cos(x/2)dx \\ a_n = \frac{1}{\pi}\int_{-\pi}^{\pi}\cos(x/2)\cos(n x)dx,\;\; n \ge 1 \\ b_n = \frac{1}{\pi}\int_{-\pi}^{\pi}\cos(x/2)\sin(n x)dx=0,\;\; n \ge 1. $$ $b_n=0$ because the integrand is odd; $a_n$ can be computed using the identity $$ \cos(x/2)\cos(n x)=\frac{1}{2}\{\cos(x/2+n x)+\cos(x/2-n x)\} \\ %% \cos(x/2)\sin(n x)=\frac{1}{2}\{\sin(n x+x/2)+\sin(n x-x/2)\}. $$ Using this, $$ a_n = \left.\frac{1}{2\pi}\left\{\frac{\sin(x/2+nx)}{1/2+n}+\frac{\sin(x/2-nx)}{1/2-n}\right\}\right|_{x=-\pi}^{\pi} \\ = \frac{(-1)^n}{2\pi}\left[\frac{1}{n+1/2}-\frac{1}{n-1/2}\right] \\ = \frac{(-1)^{n+1}}{2\pi}\frac{1}{n^2-1/4}. $$