Q:

Preparing for exam and don't understand how to calculate this integral via integrating by parts twice

It seems to be a pretty simple integral but I am blanking out on how to find this integral:
$$\int_0^1 e^{-x}\cos(n\pi x) \;dx$$
I tried Mathematica, but it seems to use some formula instead of integrating by parts.
Thanks for your help!

A:

I don't believe it matters which part you integrate and which part you take the derivative of.  If you took the derivative of $e^{-x}$ the first time, do it for the second integration by parts as well.
If you have done it correctly, you should have an equation with the same integral on each side, yet they won't cancel out.  Simply combine like terms and you should have your answer.

A:

When you’ve a product of an exponential and a sine or cosine, you can split it either way. I’ll set $u=e^{-x}$ and $dv=\cos n\pi x\,dx$, so that $du=-e^{-x}dx$ and $v=\frac1{n\pi}\sin n\pi x$. Then 
$$\begin{align*}
\int_0^1 e^{-x}\cos n\pi x\,dx&=\left[\frac1{n\pi}e^{-x}\sin n\pi x\right]_0^1-\int_0^1\frac1{n\pi}\sin n\pi x(-e^{-x})dx\\
&=\frac1{n\pi}\int_0^1 e^{-x}\sin n\pi x\,dx\;.
\end{align*}$$
Now repeat, making sure to split the new integral the same way: $u=e^{-x}$ and $dv=\sin n\pi x\,dx$, so $du=-e^{-x}dx$, $v=-\frac1{n\pi}\cos n\pi x$, and the integral is
$$\begin{align*}
\frac1{n\pi}\int_0^1 e^{-x}\sin n\pi x\,dx&=\frac1{n\pi}\left(\left[-\frac1{n\pi}e^{-x}\cos n\pi x\right]_0^1-\frac1{n\pi}\int_0^1e^{-x}\cos n\pi x\,dx\right)\\
&=\frac1{n^2\pi^2}\left(\left[e^{-x}\cos n\pi x\right]_0^1-\int_0^1e^{-x}\cos n\pi x\,dx\right)\;.
\end{align*}$$
Call the original integral $I$; then 
$$I=\frac1{n^2\pi^2}\left(\left[e^{-x}\cos n\pi x\right]_1^0-I\right)\;,$$
so $$\left(1+\frac1{n^2\pi^2}\right)I=\frac1{n^2\pi^2}\left[e^{-x}\cos n\pi x\right]_1^0=\frac1{n^2\pi^2}\left(1-\frac1e\cos n\pi\right)=\frac1{n^2\pi^2}\left(1-\frac{(-1)^n}e\right)\;,$$ since $\cos n\pi=(-1)^n$. Finally,
$$I=\frac{\dfrac1{n^2\pi^2}\left(1-\dfrac{(-1)^n}e\right)}{1+\dfrac1{n^2\pi^2}}=\frac{e-(-1)^n}{e(n^2\pi^2+1)}\;,$$
barring possible careless errors.

