When I'm trying to integrate $\int \sqrt{x^2-a^2} dx$ using trigonometric substitution, I get stuck. Here's the complete solution so far:
$$ x(\theta)=a\sec{\theta}\\ x'(\theta)=a\tan{\theta}\sec{\theta}\\ \theta=\sec^{-1}\left(\frac{x}{a}\right)\implies \theta\in\left[0,\frac{\pi}{2}\right)\cup\left(\frac{\pi}{2},\pi\right]\\ $$
$$ \begin{align} \int \sqrt{x^2-a^2} dx &=\int \sqrt{[x(\theta)]^2-a^2}x'(\theta)d\theta\\ &=\int \sqrt{a^2\sec^2{\theta}-a^2}a\tan{\theta}\sec{\theta}d\theta\\ &=a^2\int \sqrt{\tan^2{\theta}}\tan{\theta}\sec{\theta}d\theta\\ &=a^2\int |\tan{\theta}|\tan{\theta}\sec{\theta}d\theta \end{align} $$
I take it that at this point I end up with two integrals one for when $\tan{\theta}>0$ (on $\left[0,\frac{\pi}{2}\right)$) and another one for when $\tan{\theta}<0$ (on $\left(\frac{\pi}{2},\pi\right]$):
$$ \theta\in\left[0,\frac{\pi}{2}\right): \int \sqrt{x^2-a^2} dx = a^2\int \tan^2{\theta}\sec{\theta}d\theta\\ \theta\in\left(\frac{\pi}{2},\pi\right]: \int \sqrt{x^2-a^2} dx = a^2\int (-\tan{\theta}\tan{\theta}\sec{\theta})d\theta = -a^2\int \tan^2{\theta}\sec{\theta}d\theta $$
But that doesn't seem to be right because the integral of a function has one unique answer, as far as I know. What am I doing wrong?
Your substitution needs to be defined as follows:
$$ x=g(\theta)=a\sec{\theta}, \ \ \ \ \theta\in\left[0,\frac{\pi}{2}\right)\cup\left[\pi,\frac{3\pi}{2}\right) \\ \implies \ g'(\theta)=a\tan{\theta}\sec{\theta}$$
Because:
$$ g'(\theta)=\left(a\sec{\theta}\right)'=\left(\frac{a}{\cos{\theta}}\right)'=a\frac{\sin{\theta}}{\cos^{2}{\theta}}=a\frac{\sin{\theta}}{\cos{\theta}}\frac{1}{\cos{\theta}}=a\tan{\theta}\sec{\theta} $$
Defines an inverse function (one to one) that returns positive tangents for the specified interval, since the sine and the cosine have the same sign in said intervals.