What is going wrong in the computation of the integral $ \int_0^{\pi} \frac{\sec ^2 x}{1 + \tan ^2 x} dx $? If make the substitution $ u= \tan x$ then the limits of the integral both go to 0, giving us the wrong result of 0. My book suggests that this error is due to the discontinuity of the tangent function in between the limits of the integral. However, I do not see why the discontinuity leads to a wrong answer.
Also, how can i rectify this error, still using the same substitution. Any help in this direction would be appreciated.
 A: On the one hand, $\int_0^{\pi/2}\frac{\sec^2 x dx}{1+\tan^2 x}=\int_0^\infty\frac{du}{1+u^2}=\frac{\pi}{2}$. On the other hand, $\int_{\pi/2}^\pi\frac{\sec^2 x dx}{1+\tan^2 x}=\int_{-\infty}^0\frac{du}{1+u^2}=\frac{\pi}{2}$. The divergence of $u$ at $x=\frac{\pi}{2}$ is to $\infty$ on one side but $-\infty$ on the other. This means we can't add the results together to get $\int_0^0\frac{du}{1+u^2}$, since the non-zero limits in the two pieces I've discussed differ. Note the final result, $\pi$, is obvious because the original integral is $\int_0^\pi dx$. The moral of the story is $\phi$ needs to be bijective for $x=\phi(u)$.
A: The reason it fails is the tangent function is not bijective on $[0,\pi]$. When you do a change of variable $x=\phi(u)$, you get
$$\int_{\phi(a)}^{\phi(b)}f(x)\mathrm dx=\int_a^bf(\phi(u))\phi'(u)\mathrm du$$
And the equality holds even if $\phi$ is not bijective.
Note, however, that you write the change of variable the other way around: $u=\tan x$, while it should be $x=\arctan u$. But to be able to invert the tangent to get the substitution right, you have to be on an interval where it's bijective, ant $\tan x$ is not defined for $x=\pi/2$. And anyway, $x=\arctan(u)$ can't get outside of $]-\pi/2,\pi/2[$.
A: Hint:
Split it as a sum of two improper integrals  $\;\int_0^{\pi/2}\frac{\sec^2x}{1+\tan^2x}\,\mathrm dx$ and  $\;\int_{\pi/2}^\pi\frac{\sec^2x}{1+\tan^2x}\,\mathrm dx$, and observe that, on its domain,  $\;\frac{\sec^2x}{1+\tan^2x}=1$.
