TITLE: Calculate $\int_{0}^{2\pi} \frac{\cos((2n+1)t)}{\cos(t)}dt$
QUESTION [6 upvotes]: Calculate the following integral for $n \in \mathbb{Z}$ with the
  residue theorem $$\int_{0}^{2\pi} \frac{\cos((2n+1)t)}{\cos(t)}dt$$

So far I have tried two approaches. Firsty, for $n\geq 0$:
$$\begin{align*}\int_{0}^{2\pi} \frac{\cos((2n+1)t)}{\cos(t)}dt &= \int_{C(0,1)^{+}} \frac{z^{2n+1}+z^{-(2n+1)}}{z+z^{-1}}\cdot \frac{1}{iz}dz \\
& = -i \int_{C(0,1)^{+}} \frac{z^{2n+1}+z^{-(2n+1)}}{z^2+1}dz \\
& = -i \int_{C(0,1)^{+}} \frac{z^{2(2n+1)}+1}{z^{2n+1}(z^2+1)}dz \\
& = -i \cdot 2\pi i \cdot Res_{z=0}\left(\frac{z^{2(2n+1)}+1}{z^{2n+1}(z^2+1)}\right) \\
& = \left.\frac{2\pi}{(2n)!} \left[ \frac{z^{2(2n+1)}+1}{z^2+1} \right]^{(2n)} \right\rvert_{z=0}
\end{align*}$$
For the first equality I used the reversed parametrization  $z(t) = e^{it}$, with $0 \leq t \leq 2\pi$. The last equality follows from $Res_{z=a} \left( \frac{f(z)}{(z-a)^{n+1}}\right) = \frac{f^{(n)}(a)}{n!}$. However, I'm not sure how to calculate that derivative.
Seconldy, I tried to integrate the function $f(z) = \frac{e^{i(2n+1)t}}{cos(t)}$. Using a similar technique this yields:
$$\begin{align*}
\int_{0}^{2\pi} \frac{e^{i(2n+1)t)}}{\cos(t)}dt &= \int_{C(0,1)^{+}} \frac{z^{2n+1}}{(z+z^{-1})/2}\cdot \frac{1}{iz}dz \\
& = -2i\int_{C(0,1)^+}\frac{z^{2n+1}}{z^2+1}dz
\end{align*}$$
However, for $n \geq 1$, the singularities lie on my contour over which I integrate. How do I fix this? Can I just ignore that?
Is this the right approach, or should I try differently? Thanks in advance.

REPLY [5 votes]: Notice that
$$z^{2(2n+1)} + 1 = (z^2)^{2n+1} + 1^{2n+1} = (z^2 + 1) \sum _{k=0} ^{2n} (z^2)^k (-1)^{2n-k} \ ,$$
and the terms with $k < n$ are killed by the derivative of order $2n$, while the ones with $k > n$ survive it but are killed by the evaluation at $0$, leaving $z^{2n}$ (corresponding to $k=n$) as the sole survivor, so your derivative becomes
$$\frac {2\pi} {(2n)!} \left[ \frac {z^{2(2n+1)}+1} {z^2+1} \right]^{(2n)} (0) = \frac {2\pi} {(2n)!} \left[ \sum _{k=0} ^{2n} (z^2)^k (-1)^{2n-k} \right]^{(2n)} (0) = \frac {2\pi} {(2n)!} \left[ z^{2n} (-1)^n \right]^{(2n)} (0) = 2\pi (-1)^n \ .$$