Calculating the integral is not giving the same simplified function I calculated this integral below :
$$
\int_{-\pi/2}^{\pi/2} \cos(x)\cos(nx) dx
$$
And I got this result :
$\dfrac{\left(\left(n-1\right)\sin\left(\frac{{\pi}n+{\pi}}{2}\right)+\left(n+1\right)\sin\left(\frac{{\pi}n-{\pi}}{2}\right)\right)}{n^2-1}$
I had a check at the end at the solution :
$-\dfrac{2\cos\left(\frac{{\pi}n}{2}\right)}{n^2-1}$
So apparently there is some kind of simplification to do but I don't actually know how .

Observe that
$$\sin\left(\frac{\pi n+ \pi}{2}\right)=\sin\left(\frac{\pi n}{2}\right)\cos\left(\frac{\pi}{2}\right)+ \sin\left(\frac{\pi}{2}\right)\cos\left(\frac{\pi n}{2}\right) = \cos\left(\frac{\pi n}{2}\right)$$
$$\sin\left(\frac{\pi n- \pi}{2}\right)=\sin\left(\frac{\pi n}{2}\right)\cos\left(\frac{\pi}{2}\right)- \sin\left(\frac{\pi}{2}\right)\cos\left(\frac{\pi n}{2}\right) = -\cos\left(\frac{\pi n}{2}\right)$$
when $n\in\mathbb{N}$.
Then,
\begin{equation*}
\begin{split}
(n-1)\sin\left(\frac{\pi n+ \pi}{2}\right) & + (n+1)\sin\left(\frac{\pi n- \pi}{2}\right) = (n-1)\cos\left(\frac{\pi n}{2}\right) - (n+1)\cos\left(\frac{\pi n}{2}\right) \\
& = -2\cos\left(\frac{\pi n}{2}\right)
\end{split}
\end{equation*}
