When can we apply the method of complexifying the integral. Recently I was scrolling through YouTube, and saw the method of complexifying the integral https://m.youtube.com/watch?v=CpM1jJ0lob8. I tried some integrals out and it worked just fine.
However, I tried to take it up a notch, and tried finding.
$$\int \frac{e^x}{\mathrm{cos}x} dx$$
Which didn't work out. My guess was that it didn't work out because the function we are integrating is discontinuous at some points. So my question is under what circumstances can we apply the method of complexifying the integral?
My work:
$$=Re{\int \frac{e^x}{e^{ix}} dx}$$
$$=Re{\int e^{(1-i)x} dx}$$
$$=Re{\frac{e^{(1-i)x}}{1-i}}+c$$
With a little more algebra (and verified through wolphy I get):
$$\frac{1}{2}e^x(\mathrm{sin}x+\mathrm{cos}x)+c$$
Which looks incorrect because it is the same as when I evaluated:
$$\int e^x \mathrm{cos}x dx$$
 A: The problem is in saying that $\frac{e^x}{\cos(x)}$ is the real part of $\frac{e^x}{e^{ix}}$. In general it is not true that $\text{Re}\left(\frac{z_1}{z_2}\right) = \frac{\text{Re}(z_1)}{\text{Re}(z_2)}$ for $z_1,z_2 \in \mathbb{C}$. Let's take a look at $\frac{e^x}{e^{ix}}$:
\begin{align*}
\frac{e^x}{e^{ix}} &= \frac{e^x}{\cos(x)+i\sin(x)}\\
&= \frac{e^x}{\cos(x)+i\sin(x)} \cdot \frac{\cos(x)-i\sin(x)}{\cos(x)-i\sin(x)}\\
&= \frac{e^x(\cos(x)-i\sin(x))}{\cos^2(x)+\sin^2(x)}\\
&=e^x(\cos(x)-i\sin(x)).
\end{align*}
So in fact $\text{Re}\left(\frac{e^x}{e^{ix}}\right)=e^x\cos(x)\neq \frac{e^x}{\cos(x)}$. Off the top of my head I can't think of how to recognize $\frac{e^x}{\cos(x)}$ as the real part of a complex function. That's not to say it can't be done, but I'm not sure how.
A: I've used this technique often, and I came up with it independently. Didn't know they were teaching at at MIT as "complexifying the integral", hehe. :)
The lecturer's method works for integrands of the form $k\cdot z$, where $k$ is a real expression and $z$ is a complex expression. This is because $\text{Re}(kz) = k\text{Re}(z)$, which is easy to show.
However, it doesn't work for expressions of the form $\frac{k}{z}$, since $\text{Re}(\frac kz) \neq \frac {k}{\text{Re}(z)}$, in general. In fact, $\text {Re}(\frac kz) = \frac{k}{|z|}\cdot \text {Re}(z)$.
I would suggest a more general way of "complexifying", by using:
$\cos x = \frac 12(e^{ix} + e^{-ix})$ and $\sin x = \frac {1}{2i}(e^{ix} - e^{-ix})$.
A little more algebra, but they always work out.
A: $ e^x/\cos(x)$ does not have an elementary antiderivative.  This can be shown using the Risch algorithm.
EDIT: The antiderivative can be expressed in terms of the Lerch Phi function:
$$ i{{\rm e}^{ \left( 1-i \right) x}}{\it LerchPhi} \left( -{{\rm e}^{-2
\,ix}},1,1/2+i/2 \right) 
$$
where $${\it LerchPhi}(z,a,v) = \sum_{n=0}^\infty \dfrac{z^n}{(v+n)^a}$$
