# How do I find $\int e^{x \sin x +\cos x}(\frac{x^4 \cos^3 x - x \sin x + \cos x}{x^2 \cos ^2 x})\,dx$?

How do I find $$\int e^{x \sin x +\cos x}\left(\frac{x^4 \cos^3 x - x \sin x + \cos x}{x^2 \cos ^2 x}\right) dx\quad?$$

I tried to put $$e^{x \sin x +\cos x}$$ as some t and write the expression in terms of t, but it didn't work. How do I solve this integral? The answer is $$e^{x \sin x +\cos x}\left(x-\frac{1}{x \cos x}\right)$$.

Let $$I=\int e^{x \sin x+ \cos x} \left( \frac{x^4 \cos^3 x-x \sin x+\cos x}{x^2 \cos^2 x} \right)~dx.$$ Let us re-write $$I$$ as $$I= \int x [e^{x \sin x + \cos x} x \cos x]~ dx- \int e^{x \sin x+ \cos x} \left ( \frac{x \sin x + \cos x}{(x \cos x)^2} \right)~dx.$$ In the first integral above, the integral of the term in square brackets is doable. So, in integration by parts we take it as second function. In the second integral we take $$e^{x\sin x+ \cos x}$$ as the second function. Then $$I=\left( x e^{x\sin x+\cos x}- \int e^{x\sin x+\cos x}~ dx \right) - \left( e^{x\sin x+\cos x} (x \cos x)^{-1} - \int e^{x\sin x+\cos x}~dx \right).$$ Finally $$I= e^{x\sin x + \cos x} \left(x-\frac{1}{x \cos x}\right)+C.$$
Take the Ansatz $$f(x)\exp(x\sin x +\cos x)$$ so $$f(x)x\cos x+f^\prime(x)=x^2\cos x-\frac{1}{x}\sin x\sec^2 x+\frac{1}{x^2}\sec x.$$That $$x^2\cos x$$ term makes me want to consider $$g(x):=f(x)-x$$, satisfying $$g(x)x\cos x+g^\prime(x)=-\frac{1}{x}\sec x\tan x+\frac{1}{x^2}\sec x-1.$$But $$\left(\frac{1}{x\cos x}\right)^\prime=-\frac{1}{x^2}\sec x+\frac{1}{x}\sec x\tan x,$$so $$g(x)x\cos x+g^\prime(x)$$ has the desired value for $$g(x):=-\frac{1}{x\cos x}$$ as required.
Observing that: $$\frac{d}{dx}(x \sin x +\cos x)=x\cos x\\ \frac{d}{dx} x\cos x=\cos x-x\sin x$$ one obtains: $$\int e^{x \sin x +\cos x}\left(\frac{x^4 \cos^3 x - x \sin x + \cos x}{x^2 \cos ^2 x}\right) dx\\ =\int e^{x \sin x +\cos x}\left(x^2\cos x+\frac{\cos x-x \sin x}{x^2 \cos ^2 x}\right) dx\\ =\int x d e^{x \sin x +\cos x} -\int e^{x \sin x +\cos x} d\left(\frac{1}{x\cos x}\right)\\ =xe^{x \sin x +\cos x}-\int e^{x \sin x +\cos x}dx-e^{x \sin x +\cos x}\frac{1}{x\cos x}+\int\frac{1}{x\cos x} d e^{x \sin x +\cos x}\\ =xe^{x \sin x +\cos x}-\int e^{x \sin x +\cos x}dx-e^{x \sin x +\cos x}\frac{1}{x\cos x}+\int e^{x \sin x +\cos x}dx\\ =e^{x \sin x +\cos x}\left(x-\frac{1}{x\cos x}\right)+C.$$