<s>[INST] complex integral $\int_0^{2\pi} (\cos x)^n \;dx$ I tried to solve following integral $$\int_0^{2\pi} (\cos x)^n \;dx$$
by saying $z = e^{ix}$ we get $dx=\frac{-i \;dz}{z}$ and $\cos x = \frac{1}{2}\left( z + \frac{1}{z}\right)$
$$\int_0^{2\pi} (\cos x)^n \;dx = \frac{-i}{2^n}\oint_{|z|=1} \frac{1}{z}\left( z + \frac{1}{z}\right)^n \; dz = 2\pi i \left( \frac{-i}{2^n} \right) \cdot Res(f(z) = \frac{z^2 + 1} {z^{n+1}}; z_0 = 0)$$
$$= \frac{\pi}{2^{n-1} n!} \left[ z^2 + 1\right]^{(n)}(0)$$
if $n=0$ we get $\frac{\pi}{2^{-1}} = 2\pi$, if $n=1$ we get $0$, if $n=2$ we get $\frac{\pi}{2!} = \frac{\pi}{2}$ and for $n = 3$ and $n > 3$ we get $0$.
But if I use integral solver for $n=4$ the result is $\frac{3\pi}{4}$ and for $n=2$ we get $\pi$. Why residue theorem doesn't work here? Or have I done some stupid mistake somewhere and I can't spot it?
 [/INST] The problem is that the $z^2+1$ in the numerator should be raised to the same power as the $2z$ in the denominator.
$$\newcommand{\Res}{\operatorname*{Res}}
\begin{align}
\int_0^{2\pi}\cos^n(x)\,\mathrm{d}x
&=\int_{|z|=1}\left(\frac{z+\frac1z}2\right)^n\,\frac{\mathrm{d}z}{iz}\tag1\\
&=\frac{2\pi}{2^n}\Res_{z=0}\left(\frac1z\left(z+\frac1z\right)^n\right)\tag2\\[3pt]
&=[n\text{ is even}]\frac{2\pi}{2^n}\binom{n}{n/2}\tag3
\end{align}
$$
$(1)$: convert to complex integral
$(2)$: apply residue theorem
$(3)$: compute the residue; $[\dots]$ are Iverson brackets
</s>