Evaluating $\int_{0}^{2\pi}\frac{1}{\cos^2(\theta)+1}\, d\theta$ What would be the values of this definite Integral?
$$\int_{0}^{2\pi}\frac{1}{\cos^2(\theta)+1}\, d\theta$$
So, I have solved this definite integral using the substitution method, taking $u=\tan(\theta)$.
After some simplification, the solution to the definite integral I get is follows:
$$\frac{1}{\sqrt{2}} \, \tan^{-1}\left(\frac{\tan{\theta}}{\sqrt{2}}\right) \Biggr|_{0}^{2\pi}$$
Whenever I am evaluating the above result at the limits of the integration I am getting an answer of $0$.
My simplification,
$$=\frac{1}{\sqrt{2}} \, \left[ \tan^{-1}\left(\frac{\tan{2\pi}}{\sqrt{2}}\right) - \tan^{-1}\left(\frac{\tan{0}}{\sqrt{2}}\right) \right]$$
$$=\frac{1}{\sqrt{2}} \, \big[ \tan^{-1}(0) - \tan^{-1}(0) \big]$$
$$=\frac{1}{\sqrt{2}} \, \big[ 0-0]$$
$$=0$$
However, using Mathematical/Integral calculator, the value of this Integral is
$$2\pi$$
I am probably doing something very silly, as I can't figure out what I am doing wrong. Any help would be appreciated. Thanks!

You can find the solution below, and afterwards an explanation as of why your $u$ substitution did not work.
An alternative way is to write
$${\int_{0}^{2\pi}\frac{1}{1+\cos^2(x)} dx=4\int_{0}^{\frac{\pi}{2}}\frac{1}{1+\cos^2(x)}dx}$$
$${=4\int_{0}^{\frac{\pi}{2}}\frac{\sin^2(x) + \cos^2(x)}{1+\cos^2(x)}dx}$$
Now, dividing the top and bottom by ${\cos^2(x)}$ gives
$${4\int_{0}^{\frac{\pi}{2}}\frac{\tan^2(x) + 1}{\sec^2(x) + 1} dx}$$
Now using some more trig identities, we get
$${=4\int_{0}^{\frac{\pi}{2}}\frac{\sec^2(x)}{\tan^2(x) + 2}dx}$$
$${=4\int_{0}^{\infty}\frac{1}{2+u^2}du}$$
Now, solving that integral
$${\int_{0}^{\infty}\frac{1}{2+u^2}du=\frac{1}{2}\int_{0}^{\infty}\frac{1}{1+\left(\frac{u}{\sqrt{2}}\right)^2}du}$$
Now do ${k=\frac{u}{\sqrt{2}}}$:
$${=\frac{1}{\sqrt{2}}\int_{0}^{\infty}\frac{1}{1+k^2}dk=\frac{\pi}{2\sqrt{2}}}$$
So putting the whole thing together:
$${=4\times \frac{\pi}{2\sqrt{2}}=\sqrt{2}\pi}$$
Which is the correct answer :)
EDIT: After some reconsideration, I don't believe the issue in your original ${u}$ substitution was really to do with injectivity at all. In fact injectivity is not a strict requirement for $u$ substitution. If we redo all the steps we just did, but don't change the domain of integration, you end up with
$${\int_{0}^{2\pi}\frac{1}{1+\cos^2(x)}dx=\int_{0}^{2\pi}\frac{\sec^2(x)}{\tan^2(x) + 2}dx}$$
You may be tempted to once again do ${u=\tan(x)}$ (which is essentially what you did I believe?) but one requirement for $u$ substitution that is very clearly needed is for $u$ to be continuous on the domain of integration (it needs to be differentiable, and so clearly continuity is a requirement!). ${u=\tan(x)}$ certainly is not continuous on ${(0,2\pi)}$, and so you really end up with an improper integral of sorts. Now, we could split up the integral instead, just as we would with any old improper integral:
$${\int_{0}^{2\pi}\frac{\sec^2(x)}{\tan^2(x) + 2}dx=\int_{0}^{\frac{\pi}{2}}\frac{\sec^2(x)}{\tan^2(x) + 2}dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}\frac{\sec^2(x)}{\tan^2(x) + 2}dx + \int_{\frac{3\pi}{2}}^{2\pi}\frac{\sec^2(x)}{\tan^2(x) + 2}dx}$$
And now it is legitimate to do ${u=\tan(x)}$, since ${\tan(x)}$ will be differentiable and continuous on these domains (well, technically not at the endpoints - but you take a limit, as per the definition of how we deal with improper integrals). Indeed you will get the answer of ${\sqrt{2}\pi}$ if you evaluate this expression.
So if this was the issue, why did people jump to injectivity / bijectivity? Well there are some instances where this is (indirectly) the problem. An example:
$${\int_{0}^{2\pi}xdx}$$
Clearly the answer to this is ${2\pi^2}$. Now do the substitution ${u=\sin(x)}$ - our endpoints become ${\int_{0}^{0}}$... does this mean the integral is $0$? NO! Recall $u$ substitution only says that
$${\int_{a}^{b}f(\phi(x))\phi'(x)dx = \int_{\phi(a)}^{\phi(b)}f(u)du}$$
If you actually attempt to write ${\int_{0}^{2\pi}xdx}$ to match the form of the lefthand side so we can legit utilise $u$ substitution you will end up using some nasty ${\arcsin}$ rubbish - but the key point to take away is that the ${\arcsin}$ function only gives back principle values. ${\arcsin(\sin(x))}$ does not necessarily equal to ${x}$ for all ${x \in \mathbb{R}}$!. So in actuality you end up having a piecewise function to represent ${x}$, so you are forced to split the integral up. So in this case injectivity is in fact a sort of "requirement" indirectly (unless you split up the integral).
I hope this helped explain a bit better :)
