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);

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$. – Did Aug 23 '12 at 20:29
I got different residues than you did. Mine are 1/16 – Euler....IS_ALIVE Aug 23 '12 at 20:31
@did: you're right, thanks and.. damn! – snickers Aug 23 '12 at 20:35
should I erase the question ? – snickers Aug 23 '12 at 20:35
I changed $cos^2\theta$ to $\cos^2\theta$. This not only prevents $\cos$ from being italicized as if it were three juxtaposed variables, but also provides proper spacing in epxressions like $5\cos x$. – Michael Hardy Aug 23 '12 at 20:43

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}}.$$