# Calculating cosine coefficient of a Fourier series

I have highlighted steps for indefinite integration of the following function:

$$f\left( x \right) =\int { { e }^{ a x }\cos(n x)dx } \\$$

After integration by parts twice the following results is obtained: $$=\quad \frac { { e }^{ ax }\sin { \left( nx \right) } }{ n } -\frac { a }{ n } \left( -\frac { { e }^{ ax }\cos { \left( nx \right) } }{ n } -\left( -\frac { a }{ n } \int { { e }^{ ax }\cos { \left( nx \right) dx } } \right) \right)$$

The final result which I cannot figure how was calculated is: $$=\frac { { e }^{ ax }\left( a\cos { \left( nx \right) +n\sin { \left( nx \right) } } \right) }{ { a }^{ 2 }+{ n }^{ 2 } }$$

Can some one clarify how this final result was obtained?

Let $$I =\int { { e }^{ a x }\cos(n x)dx } \\$$.
Then $$I=\quad \frac { { e }^{ ax }\sin { \left( nx \right) } }{ n } -\frac { a }{ n } \left( -\frac { { e }^{ ax }\cos { \left( nx \right) } }{ n } -\left( -\frac { a }{ n } \int { { e }^{ ax }\cos { \left( nx \right) dx } } \right) \right)$$ $$=\quad \frac { { e }^{ ax }\sin { \left( nx \right) } }{ n } + \frac { { ae }^{ ax }\cos { \left( nx \right) } }{ n^2 } -\frac { a^2 }{ n^2 } \int { { e }^{ ax }\cos { \left( nx \right) dx } }$$ $$=\quad \frac { { e }^{ ax }\sin { \left( nx \right) } }{ n } + \frac { { ae }^{ ax }\cos { \left( nx \right) } }{ n^2 } -\frac { a^2 }{ n^2 } \cdot I$$ $$I+\frac { a^2 }{ n^2 } \cdot I=I\left(1+\frac { a^2 }{ n^2 }\right) =I\left(\frac { a^2+n^2 }{ n^2 }\right) =\quad \frac { { e }^{ ax }\sin { \left( nx \right) } }{ n } + \frac { { ae }^{ ax }\cos { \left( nx \right) } }{ n^2 }=\frac{e^{ax}}{n}\left(\sin\left(nx\right)+\frac{a\cos \left(nx\right)}{n}\right)$$ Hence $$I=\left(\frac{e^{ax}}{n}\left(\sin\left(nx\right)+\frac{a\cos \left(nx\right)}{n}\right)\right)\cdot \left(\frac { n^2 }{ a^2+n^2 }\right)$$ which gives the required answer upon simplifying.
OK, you're almost there, after simplifying your expression a little you have: $$\int e^{ax} \cos(nx) dx = \frac{e^{ax}}{n} \left[ \sin(nx) + \frac{\cos(nx)}{n} \right] - \frac{a^2}{n^2} \int e^{ax} \cos(nx) dx.$$
Now all that needs to be done is group the quantity you want to solve for, i.e. $\int e^{ax} \cos(nx) dx$ on the same side. So upon doing this we get
$$\left[ 1 + \frac{a^2}{n^2}\right] \int e^{ax} \cos(nx) dx = \frac{e^{ax}}{n} \left[ \sin(nx) + \frac{\cos(nx)}{n} \right].$$