Why does this method fail for finding the Fourier series for $\cos\left(\frac{x}{2}\right)$ on the interval $-\pi \lt x \lt \pi$? This question is strongly related to this question (that does not have an answer).
From "Riley, Hobson and Bence - Mathematical methods for physics and engineering", Section 12.5 - "Non-periodic functions", page 417 
$$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? 
 A: If you "extend the period" you get a rather different looking curve

The question that they are asking seem to be to evaluate.
$a_n = \frac 1{2\pi}\int_{-\pi}^{\pi} \cos \frac x2 \cos nx\ dx\\
\frac {1}{4\pi} \int_{-\pi}^{\pi} \cos (n+\frac 12)x + \cos (n-\frac 12)x \ dx\\
$
A: $a_1=\frac{1}{2\pi}\int_{-2\pi}^{2\pi}cos^2(\frac{x}{2})dx=\frac{1}{4\pi}\int_{-2\pi}^{2\pi}1dx=1$.
A: 
This was generated with gnuplot - it shows a function that is defined by
$$f(x) = cos(x/2) \quad -\pi<x<\pi$$
and
$$f(x+2\pi)=f(x)$$
I think you need to stick to the range of $-\pi<x<\pi$ - which is what @DougM is saying in his answer

Edit about $x^2$ range extension...
The picture in the reference you gave is helpful here - where it shows the $x^2$ function being 'mirrored' or reflected in the $y$-axis. 
So $x^2$ is an even function and so in the Fourier series we will only have contributions from $\cos(kx)$ and not $\sin(kx)$
This means that the critical integrals of 
$$\int x^2 \cos(kx) dx $$
are even - or at least the $x^2 \cos(kx)$ function inside is even. 
Now if the function is even then integrating over the range $[0,2]$ will give exactly half the value that you get integrating over $[-2,2]$ - because everything is completely symmetrical about the $y$-axis or the line $x=0$.
In your case of the cos$(x/2)$ you also have an even function, but you are not expanding into the mirror image on the other side of the $y$-axis... instead you are making the range wider and the $\cos(x/2)$ function now is negative and things change. 
For example - with the extended range the $a_0$ term will be zero, but in the figure above everything is above the $x$-axis so $a_0$ will be positive and non-zero.
A: 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}.
$$
