<s>[INST] Problem using trigonometric substitution: Domain of $\theta$ I'm studying calculus from Rogawski's Calculus. In trigonometric substitution $x=a\sec \theta$ he made a note:

In the substitution $x = a \sec θ$ , we choose $0\le  θ \le π$
2 if $x \ge a$ and $π \le θ < \frac{3π}2$ if
$x \le −a$. With these choices, $a \tan \theta$ is the
positive square root $\sqrt{x^2 − a^2}$.

When I work on the integral:
$$\int \frac {\mathrm{d}x}{x\sqrt{x^2-9}}$$
Using the substitution of $x=3\sec\theta$ with the domain of $\theta$ shown above, the integration will be :
$$\int \frac {dx}{x\sqrt{x^2-9}}= \int \frac {3\sec\theta\tan\theta d\theta}{(3\sec\theta)\sqrt{9\sec^2-9}}=\int \frac {\tan\theta d\theta}{3\sqrt{\tan^2 \theta}}$$
$$= \int \frac{d\theta}{3}= \frac\theta 3+ \mathrm{C}$$
$$\int \frac {dx}{x\sqrt{x^2-9}}= \frac 13 \sec^{-1}\left(\frac x3\right)+ \mathrm{C}$$
Which is very wrong in the negative part of the domain of $x$ as shown in this graph:

Notice that the slope of the blue function in the negative domain should be positive not negative!.
The problem of this substitution is $2$ things:
$1)$ The domain of $\theta$ is chosen so that inverse cannot be done, since $\theta =\sec^{-1}x\notin  (\pi,\frac{3\pi}2) $ which is  the domain chosen for $\theta $.
$2)$ Depending on first problem, we should choose $\theta \in (0,\pi)-\{\frac\pi 2\}$ which makes $\sqrt{\tan^2 \theta}=|\tan \theta|$. The integral then should be re-written as a piecewise function:
$$\int \frac {dx}{x\sqrt{x^2-9}}= \int \frac {3\sec\theta\tan\theta d\theta}{(3\sec\theta)\sqrt{9\sec^2-9}}$$
$$\int \frac {\tan\theta d\theta}{3\sqrt{\tan^2 \theta}} = \begin{cases} = \int \frac {d\theta}{3} = \frac 13 \sec^{-1}(\frac x3)+ C & \text{if $\theta \in (0,\frac \pi 2)$} \equiv x>3 \\= \int \frac {-d\theta}{3} = \frac {-1}3 \sec^{-1}(\frac x3)+ C & \text{if $\theta \in (\frac \pi 2,\pi)$}\equiv x<-3 \end{cases}$$
My questions are:
Is this thinking right ? there is mistake to choose the domain of $\theta$ as mentioned in the book ?
Mathematica gives me the answer of $-\dfrac {1}{3} \tan^{-1} \left(\dfrac{3}{\sqrt{x^2-9}}\right) +\mathrm{C}$ which is right when graphed. But how to construct this integral ?
Thanks for help.
 [/INST] To get to the answer Mathematica gives, you can let$$  u = \sqrt{x^2 -9}.$$  You should get $$ \frac{1}{3} \arctan \left( \frac{\sqrt{x^2-9}}{3} \right) + \text{constant} $$ and use the fact that $$\arctan(\frac{1}{x}) = \frac{\pi}{2} - \arctan(x)~~~,x\gt 0$$ and 
$$\arctan(\frac{1}{x}) = -\frac{\pi}{2} - \arctan(x)~~~,x\lt 0 $$
</s>