Evaluation of $\int_0^{2\pi} \frac{1}{1+3\cos^2(\theta)} \, d\theta$ For a test I had to evaluate $\int_0^{2\pi} \frac{1}{1+3\cos^2(\theta)}d\theta$. First I used substitution, with $z=e^{i\theta}$ and $d\theta=\frac{1}{iz}dz$, as shown:
$$\int_0^{2\pi} \frac{1}{1+3\cos^2(\theta)}d\theta=\int_{|z|=1}\frac{1}{1+\frac{3}{4}(z+z^{-1})(z+z^{-1})}\frac{1}{iz}dz=\frac{4}{i}\int_{|z|=1}\frac{z}{3z^4+10z^2+3}dz$$
Then I used the residue theorem:
$$\int_{|z|=1}\underbrace{\frac{z}{3z^4+10z^2+3}}_{f(z)}dz=\int_{|z|=1}\frac{z}{(z^2+3)(z^2+\frac{1}{3})}dz=\int_{|z|=1}\frac{z}{(z-i\sqrt{3})(z+i\sqrt{3})(z-i\frac{i\sqrt{3}}{3})(z+\frac{i\sqrt{3}}{3})}dz$$
As only $\frac{i\sqrt{3}}{3}$ and $-\frac{i\sqrt{3}}{3}$ lie in the integration domain, I don't need to compute the other residues:
$$\operatorname{Res}(f;\frac{i\sqrt{3}}{3})=\frac{3}{16}$$ and
$$\operatorname{Res}(f;-\frac{i\sqrt{3}}{3})=\frac{3}{16}$$
Finally:
$$\int_0^{2\pi} \frac{1}{1+3\cos^2(\theta)}d\theta=\frac{4}{i}2\pi i \frac{6}{16}=3\pi $$
However MATLAB doesn't agree with that:
EDU>> f=@(z) 1./(1+3*cos(z).^2);
quad(f,0,2*pi)

ans =
3.1416

Where did I go wrong ? I don't see what's wrong in using theorems this way and I did the computations 3 times. Ask for more steps in calculation if needed.

Right after the first appearance of $f(z)$, when you factored the denominator, you forgot a factor $3$.
Note: Complex analysis might not be the simplest approach here, since the change of variables $t=\tan(θ)$ yields directly that, for every $a\gt-1$,
$$
\int_0^{2\pi}\frac{\mathrm d\theta}{1+a\cos^2(\theta)}=\frac{2\pi}{\sqrt{a+1}}. 
$$
