<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] I am not going to answer your problem on the domain part. Instead, I am suggesting an alternate way to integrating the very last integral.
Edited
Claim-1) $P = \int  { \frac {dx}{\sqrt {1 -  x^2}}} = ... =  \sin ^{-1} x$. 
Let $x = \sin u$. Then, $dx =  du$.
∴ $P = \int  {\frac {(\cos u)du}{\sqrt {1 -  \sin ^2 u}}} = u  =  \sin ^{-1} x$

[The whole Claim-2 can be ignored. I just don't want to delete it.] 
Claim-2) $Q = \int {\frac {dx}{\sqrt {a^2 – x^2}}} = … = \sin ^{-1} \frac {x}{a}$.
Let $x = av$. Then, $dx = (a)dv$.
∴ $Q = \int {\frac {(a)dv}{\sqrt {a^2 – (av)^2}}}$
$ = \int {\frac {dv}{\sqrt {1 - v^2}}}$, which is essentially $P$.
$ =  \sin ^{-1} v$
$ = \sin ^{-1} (\frac {x}{a})$

Claim-3) $R = \int {\frac {dx}{x \sqrt {x^2 – a^2}}} = … = \frac {-1}{a} \sin ^{-1} \frac {a}{x}$.
Further edited 

Let $w = \frac {a}{x}$. Then, $dx = \frac {(-a) dw}{w^2}$
∴ $R = \int {\frac {-a (dw)/(w^2)}{(a/w) \sqrt {(a/w)^2 - a^2}}}$
$= - \int {\frac {dw}{w \sqrt{(a/w)^2 -a^2}}}$
$= - \int {\frac {dw}{aw \sqrt {(1/w)^2 - 1^2}}}$
$= \frac {-1}{a} \int  {\frac {dw}{\sqrt {1^2 - w^2}}}$

$= \frac {-1}{a} P$
$= \frac {-1}{a} \sin ^{-1} w$
$= \frac {-1}{a} \sin ^{-1} \frac {a}{x}$
</s>