Why and How do certain manipulations in indefinite integrals "just work"? I am going to take a very simple example to elaborate my question.
When we integrate $\sec (x)\,dx$ we divide and multiply by $\sec (x) + \tan (x)$.
$$\int \sec(x)\,dx = \int \sec (x) \left[{\sec (x) + \tan (x) \over \sec (x) + \tan (x)}\right]\, dx$$
I am just solving from here.
$$\int {\sec^2(x) + \sec(x)\tan(x) \over \sec(x) + \tan(x)} \, dx $$
Then we let $\sec(x) + \tan(x) = u$
$$\implies du = (\sec^2(x) + \sec(x)\tan(x))\,dx$$
$$\implies \int {du \over u}$$
$$= \ln{\left|\sec(x) + \tan(x)\right|} + c$$
Now coming to my questions.

*

*Why do we HAVE to make that manipulation of multiplying $\sec(x) + \tan(x)$. Like I know its to get the answer...but why does it work so well?


*How to even think like that? Like "if I multiply $\sec(x) + \tan(x)$ in the numerator and denominator then I'll be able to solve this very easily." What in that integral gives one direction to think of such a manipulation ?
 A: Many otherwise-mysterious tricks in integrals involving trigonometric functions can be explained by expressing the trig functions in terms of exponentials, as in $\cos(x)=(e^{ix}+e^{-ix})/2$. The resulting rational expressions in exponentials can always be integrated...
EDIT: to explain why/how rational expressions on $e^{ix}$ can always be integrated: for example,
$$
\int {1\over 1+e^{ix}} \, dx
\;=\;
-i \int {1\over e^{ix}(1+e^{ix})}\;d(e^{ix})
\;=\;
-i \int {1\over t(1+t)}\;dt
$$
with $t=e^{ix}$. Then use partial fractions to break this up into easily-computable pieces.
Once I learned this, years ago, I mostly lost interest in the tricks, because equivalents of them can be recovered by using exponentials and complex numbers. No guessing is necessary.
Nevertheless, historically, I'm fully confident that people did just experiment endlessly until they found a trick to be able to compute a given indefinite integral, and then that trick was passed on to subsequent generations. In particular, if we do look at it that way, there's no real way that one can "anticipate" the necessary tricks...
A: I will show how Paul Garrett's Method can be used here.
Basics
Lemma 1: $e^{ix} = \cos x + i \sin x$
Proof: you need Taylor series, see my article (albeit not very rigorous)
Lemma 2: $\frac{e^{ix} + e^{-ix} }{2} = \cos x$
Proof: $$ e^{ix} = \cos x + i \sin x$$ $$ e^{-ix} = \cos(-x) + i \sin(-x) = \cos(x) - i \sin x$$
Adding the two equations and halving, we find the required.

Computing the integral:
$$ \int \sec x \,dx = \int \frac{1}{\cos x} \, dx = \int \frac{2 \, dx }{e^{ix} + e^{-ix} } = \int 2\frac{e^{ix} }{e^{2ix} +1} \, dx$$
The last integral calls for a natural substitution of $e^{ix} = t$, then we have a standard polynomial integral which is intuitive to evaluate.
A: You don't have to use that trick. There's a much simpler one:
$$
\int\frac{1}{\sin t\cos t}\,dt
=\int\frac{\cos^2t+\sin^2t}{\sin t\cos t}\,dt
=\int\Bigl(\frac{\cos t}{\sin t}+\frac{\sin t}{\cos t}\Bigr)\,dt
=\log\lvert\tan t\rvert+c
$$
How can you transform $\cos x$ into $a\sin t\cos t$? Easy: set $x=2t-\pi/2$. Et voilà
$$
\int\frac{1}{\cos x}\,dx=\int\frac{1}{\sin t\cos t}\,dt=\log\lvert\tan t\rvert+c=\log\Bigl|\tan\Bigl(\frac{x}{2}+\frac{\pi}{4}\Bigr)\Bigr|+c
$$
By the way, this also yields, with $x=2t$,
$$
\int\frac{1}{\sin x}\,dx=\int\frac{1}{\sin t\cos t}\,dt=\log\Bigl|\tan\frac{x}{2}\Bigr|+c
$$
A: There is a more elementary way to see this — to see why one would do this — that I don't think the other answers have really highlighted.
First of all, one must be familiar with logarithms, and the fact that $\frac{f^\prime}{f}$ is the derivative of $\ln\left|f\right|$.
Next you look at your trig derivatives. The functions $\sin$ and $\cos$ are a pair, and their derivatives are just each other, with a minus sign thrown in when appropriate. Couldn't be simpler! (Well, it could be simpler, if the minus sign weren't a thing, but that behavior is reserved for $\sinh$ and $\cosh$.)
Another nice pair of functions is $\sec$ and $\tan$. What makes them a pair? Well, they both have the same domains, because they have $\cos$ in their denominators. They also live together in a version of the Pythagorean identity: $\tan^2x + 1 = \sec^2x$. So let's look at their derivatives.
We have $\frac{d}{dx}\tan x=\sec^2 x$, and $\frac{d}{dx}\sec x=\sec x\tan x$. Their derivatives aren't exactly "each other", but they are each $\sec x$ times the other function. That means that, because addition is commutative, the derivative of their sum has a nice property: $$\frac{d}{dx}(\sec x+\tan x) = \sec x(\sec x+\tan x)$$ Taking $f(x) = \sec x+\tan x$, we have $$f'(x) = \sec x\cdot f(x)$$ or: $$\frac{f'(x)}{f(x)}=\sec x$$
From here, it's just a matter of putting the pieces together.
A: (This answer could perhaps be seen as an expansion of the one given by Steven Stadnicki.)
There are general approaches to "algebraizing" the problem and getting rid of the transcendental functions like $\sec(x)$. One approach is to use complex valued functions as (essentially) explained in the answer by Paul Garrett.
Another approach (which avoids complex numbers) uses the rational parametrisation of the circle obtained by "projecting" a point $(\cos x, \sin x)$ from the circle from the point $(-1,0)$ to point $(0,t)$ on the vertical axis. We easily check that $t=\tan(x/2)$ using the included angle theorem in high school geometry. This gives the identities:
$$
\begin{align*}
     \cos(x) &= \frac{1-t^2}{1+t^2} \\
     \sin(x) &= \frac{2t}{1+t^2} \\
\end{align*}
$$
So $x=2\tan^{-1}(t)$ where $t$ lies in $(-\infty,\infty)$.
Note that $dx=\frac{2~dt}{1+t^2}$.
All trigonometric functions can be written as rational functions of $t$, so can integrals. For example,
$$
    \int \sec(x) dx = \int \frac{1+t^2}{1-t^2}\frac{2dt}{1+t^2}
    =
    \int \frac{2}{1-t^2} dt     
$$
which can be integrated using partial fractions.
$$
    \int \frac{2}{1-t^2} dt
    =
    \int \left(\frac{1}{1+t} + \frac{1}{1-t}\right)dt
    = \log(1+t) - \log(1-t) + c
$$
Substituting $t$ in terms of $x$ gives the answer.
Edit: TIL, thanks to Steve Stadnicki's answer, that this is known as the Weierstrass substitution and was known (at least) to Euler; which is not surprising since it leads to the formulation of the arc length of an ellipse in terms of elliptic integrals.
A: I initially posted this as a comment, but with my master's degree and with my 10,000 rep, I think I will get away with post this as an answer. Actually with my anger at the system but at no particular person, I don't really care.
Part 1: it's easy:

*

*know how to integrate secant a better way.

*differentiate and then figure out the trick of the sec + tan thingy

*show it to beginning calculus students and wait for stackexchange questions like this to come up.

Part 2: seriously this is a big coincidence discovered by simple differentiation after integrating secant by better methods. Then it's presented to beginning calculus students as an amazing trick. Look at the antiderivative itself. It's $$\ln|\text{something}|.$$
This sets off alarm bells that the since derivative of $\ln |f|$ is $\frac{f'}{f}$ then we'll multiply $\sec$ by $\frac{f}{f}$ with $f = \sec + \tan$. I forgot the details myself (Edit: Ah, see this other answer) but if you think about differentiating the $\ln|\text{something}|$, then I don't think it should be too hard to come up with $\frac{\sec + \tan}{\sec + \tan}$. I suppose we could test out calculus students in highschool as some experiment like if they come up with $\frac{\sec + \tan}{\sec + \tan}$ after you tell them the antiderivative and hint them to differentiate the antiderivative and then maybe hint again to multiply with $\frac{\text{something}}{\text{the same thing}}$. Perhaps do some control group vs treatment group where some students are taught this unrelated thing about integrating $\frac{f'}{f}$

*

*Edit: Wait...yeah I remember now! Derivative of $\ln|f|$ is $\frac{f'}{f}$ Soooo we observe that derivative $f'$ of $f=\sec + \tan$ is $f'=\sec(\sec + \tan)$, sooo...yeah I think it's easy if you know already know the antiderivative.

I seriously seriously doubt anyone thought of this one day from scratch. I seriously seriously think someone (oh apparently someone named James Gregory) thought of this after they already integrated secant with partial fractions (oh apparently this has a name: Barrow's approach) or the 'sneakiest substitution': Weierstrass (says michael spivak, the living legend who invented 'e').
A: You don’t have to do it that way, but it’s still a nice way to go about it, as you get something like $\int \frac{f’(x)}{f(x)} \, dx$. A more natural way, maybe, is the following (although the idea is the same)
$$\int \frac{dx}{\cos x} \\ =\int \frac{\cos x}{\cos^2 x} \, dx \\ = \int \frac{\cos x}{1-\sin^2x} \,dx $$ Let $\sin x = t \implies \cos x \ dx = dt$: $$=\int \frac{dt}{1-t^2} \\ =\frac 12 \ln \left | \frac{t+1}{t-1} \right |  + C \\ = \frac 12 \ln\left | \frac{1+\sin x}{1-\sin x} \right | + C$$
A: I'll answer your second question. It's a great question, and asking it is really important for getting better at solving math problems. In general, with integral questions, I would say that thinking about these kinds of things just comes with experience and seeing a lot of similar problems.
However, in this case, such intuition isn't enough (in my opinion). I think rather that this is one of those things where people tried for a while to find an antiderivative for $\operatorname{sec}(x)$, and someone happened to come up with this after messing around for a while.
(Also, practically speaking, I think that such a question would be too difficult for an exam, if students hadn't already seen the trick.)
A: (1) By writing the integral as
$$
\int \frac{\sec^2 x + \sec x \tan x}{\tan x + \sec x} \,  dx \, ,
$$
you have (rather miraculously) put it into the form
$$
\int \frac{g'(x)}{g(x)} \, dx = \ln|g(x)|+C\, .
$$
More generally,
$$
\int f'(g(x)) \cdot g'(x) \, dx =f(g(x))+C \, .
$$
This is a direct consequence of the chain rule.
(2) I agree with you that this substitution is a "magic-trick", and it would only be obvious to someone who already knows the value of the integral. Here is an approach that I hope you could imagine coming up with yourself (albeit, with a fair amount of trial and error):

$$
\int \sec x \, dx = \int \frac{1}{\cos x } \, dx = \int \frac{\cos x }{\cos^2x} \, dx = \int \frac{\cos x}{1-\sin^2 x} \, dx \, .
$$
Make the substitution $u=\sin x$ and use trig identities to simplify .

Again, the idea behind this approach is to write the integral in the form
$$
\int f'(g(x)) \cdot g'(x) \, dx \, ,
$$
here with $g(x)=\sin x$. Making the substitution $u=\sin x$ is just a practical way of applying the chain rule in reverse as described above. For more information, see this post about integration of secant.
