Integral problem 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$$
My attempt:I tried putting $x \sin x+\cos x=t$ and cannot express it in the form of $\int e^t(f(t)+f'(t)) \, dt$
 A: \begin{align}
& \int e^{x\sin x+\cos x}(\frac{x^4\cos^3x-x\sin^2x+\cos x}{x^2\cos^2x})dx\\
& \hspace{5mm} =\int e^{x\sin x+\cos x}(x^2\cos x-\frac{x\sin^2x-\cos x}{x^2\cos^2x})dx\\
& \hspace{5mm} =\int e^{x\sin x+\cos x}(x^2\cos x-\frac{x\tan^2x-\sec x}{x^2})dx
\end{align}
Realize that $\frac{x\tan^2x-\sec x}{x^2}=\frac{d}{dx}\frac{\sec x}{x}$ and that you can make $\frac{\sec x}{x}$ appear elsewhere by factoring $x^2\cos x-1$ into $(x-\frac{\sec x}{x})(x\cos x)$. So the above is equal to:
\begin{align}
& \int e^{x\sin x+\cos x} \left((x-\frac{\sec x}{x})(x\cos x)+1-\frac{x\tan^2x-\sec x}{x^2}\right) \, dx\\
&=\int \left[e^{x\sin x+\cos x}\left(x-\frac{\sec x}{x}\right)(x\cos x)+e^{x\sin x+\cos x}\left(1-\frac{x\tan^2x-\sec x}{x^2}\right)\right]dx
\end{align}
Now realize that $e^{x\sin x+\cos x}(x\cos x)=\frac{d}{dx}e^{x\sin x+\cos x}$. The above is equal to:
$$\int \left[\left(x-\frac{\sec x}{x}\right)\frac{d}{dx}(e^{x\sin x+\cos x})+e^{x\sin x+\cos x}\frac{d}{dx}\left(x-\frac{\sec x}{x}\right)\right]\,dx
$$
Now, this looks exactly looks like the product rule with $u=x-\frac{\sec x}{x}$ and $v=e^{x\sin x+\cos x}$. So the integral is equal to $$(x-\frac{\sec x}{x})e^{x\sin x+\cos x}+C$$
(To be honest, I did use WolframAlpha to evaluate the integral and work backward to take the derivative by hand, and then reverse each step, but I don't see any other way of evaluating such a difficult integral by hand...)
A: You have the answer (without solution) as an answer and a very god hint.
Here goes a way of thinking:
If we should have any chance to get this one, I think the primitive has to be in the form
$$
e^{x\sin x+\cos x}f(x)
$$
for some function $f$. Moreover,
$$
De^{x\sin x+\cos x}f(x)=e^{x\sin x+\cos x}(f'(x)+x\cos x f(x)),
$$
so our $f$ must satisfy
$$
f'(x)+x\cos x f(x)=\frac{x^4\cos^3 x-x \sin x+\cos x}{x^2\cos^2 x}.
$$
We observe that he first term in the right-hand side reads (upon division) $x^2\cos x$. By comparing, this suggests that our function $f$ could be written
$$
f(x)=x+g(x)
$$
for some function $g$. But then $g$ should satisfy
$$
1+g'(x)+x\cos x g(x)=\frac{-x \sin x+\cos x}{x^2\cos^2 x},
$$
or, moving the $1$ to the right-hand side,
$$
g'(x)+x\cos x g(x)=\frac{-x \sin x+\cos x}{x^2\cos^2 x}-1.
$$
Next, the $x^2\cos^2x$ in the denominator suggests that the function $g$ can be written
$$
g(x)=\frac{h(x)}{x\cos x},
$$
for some function $h$. Differentiating gives
$$
g'(x)+x\cos x g(x)=\frac{h'(x)x\cos x-h(x)(\cos x-x\sin x)}{x^2\cos^2x}+h(x).
$$
Very lucky shot! With $h(x)=-1$, we are all set. Thus, a primitive is
$$
e^{x\sin x+\cos x}\Bigl(x-\frac{1}{x\cos x}\Bigr)
$$
A: The integral is of the form 
\begin{equation*}
\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^{g(x)}h(x)dx.
\end{equation*} 
This form recalls the well-known formula 
\begin{equation*}
\int e^{g(x)}\left( f^{\prime }(x)+g^{\prime }(x)f(x)\right)
dx=f(x)e^{g(x)}+C.
\end{equation*} 
Its proof maybe found at
Proof for formula $\int e^{g(x)}[f'(x) + g'(x)f(x)] dx = f(x) e^{g(x)}+C$
So we are done if we find a function $f(x)$ such that 
\begin{equation*}
h(x)=\frac{x^{4}\cos ^{3}x-x\sin x+\cos x}{x^{2}\cos ^{2}x}=f^{\prime
}(x)+g^{\prime }(x)f(x)=f^{\prime }(x)+(x\cos x)f(x)
\end{equation*} 
In what follows, I will show that $f(x)=x-\frac{1}{x\cos x}$ and therefore 
\begin{eqnarray*}
\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 &=&\left( f(x)\right) e^{g(x)}+C \\
&=&\left( x-\frac{1}{x\cos x}\right) e^{x\sin x+\cos x}+C.
\end{eqnarray*} 
Remark. If $f(x)=f_{1}(x)+f_{2}(x)$ then 
\begin{eqnarray*}
f^{\prime }(x)+g^{\prime }(x)f(x) &=&(f_{1}+f_{2})^{\prime }+g^{\prime
}(x)(f_{1}+f_{2}) \\
&=&\left( f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)\right) +\left(
f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)\right) 
\end{eqnarray*} 
conversely, if 
\begin{equation*}
h(x)=\left( f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)\right) +\left(
f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)\right) 
\end{equation*} 
then 
\begin{equation*}
h(x)=f^{\prime }(x)+g^{\prime }(x)f(x),\ \ with\ \ f(x)=f_{1}(x)+f_{2}(x).
\end{equation*} 
This remark suggests to look for $f$ by pieces! that is, if we can write 
\begin{equation*}
h(x)=\left( f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)\right) +something
\end{equation*} 
we reduce our task to look for $f_{2}$ such that 
\begin{equation*}
something=\left( f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)\right) 
\end{equation*} 
and therefore $h(x)$ would be $(f_{1}+f_{2})^{\prime }+g^{\prime
}(x)(f_{1}+f_{2}),$ that is we can take $f=f_{1}+f_{2}.$  
The procedure can be described as follows. First look for $g^{\prime }(x)$
inside $h(x).$ If some expression like $g^{\prime }(x)f_{1}(x)$ is found 
\begin{equation*}
h(x)=g^{\prime }(x)f_{1}(x)+something
\end{equation*} 
then add and subtract $f_{1}^{\prime }(x)$ and write 
\begin{equation*}
h(x)=\left( f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)\right)
+something-f_{1}^{\prime }(x).
\end{equation*} 
Next take $h_{1}(x)=something-f_{1}^{\prime }(x)$ and try to find $g^{\prime
}(x)f_{2}(x)$ inside $h_{1}(x)$. If found, add and subtract $f_{2}^{\prime
}(x).$ And so on. Since the integral to be evaluated is a reasonable
integral which come from an exercise textbook, then at some moment this
procedure should stop, for example when $f_{n}(x)$ is obtained, and
therefore 
\begin{equation*}
h(x)=\sum_{k=1}^{n}\left( f_{k}^{\prime }(x)+g^{\prime }(x)f_{k}(x)\right) 
\end{equation*} 
and 
\begin{equation*}
f(x)=\sum_{k=1}^{n}f_{k}(x).
\end{equation*}
According to my own experience, $n=1$ or $2.$ I never get $n=3.$ 
Let's go.!
The unique thing which is given in the statement is $g(x)=x\sin x+\cos x.$
So, the first thing we start with is to look for $g^{\prime }(x)=x\cos x$
inside what would be $h(x)=f^{\prime }(x)+g^{\prime }(x)f(x),$ that is,
inside 
\begin{equation*}
\frac{x^{4}\cos ^{3}x-x\sin x+\cos x}{x^{2}\cos ^{2}x}.
\end{equation*} 
If we separate the fraction into 3 fractions we get 
\begin{equation*}
x^{2}\cos x-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}.
\end{equation*} 
It is easy to see $g^{\prime }(x)=x\cos x$ in the first term 
\begin{equation*}
xg^{\prime }(x)-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}.
\end{equation*} 
This suggests to take $f_{1}(x)=x.$ So, we have to add and subtract $ 
f_{1}^{\prime }(x)$ to get the package $\left( f_{1}^{\prime }(x)+g^{\prime
}(x)f_{1}(x)\right) ,$ 
\begin{equation*}
\left( x^{\prime }+g^{\prime }(x)x\right) -\frac{\sin x}{x\cos ^{2}x}+\frac{1 
}{x^{2}\cos x}-1.
\end{equation*} 
Now we take 
\begin{equation*}
h_{2}(x)=-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}-1
\end{equation*} 
and try to find inside it $g^{\prime }(x)=x\cos x.$ This one is not present
in the first two fractions, but can be in the third as follows 
\begin{eqnarray*}
h_{2}(x) &=&-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}+(x\cos
x)\left( \frac{-1}{x\cos x}\right)  \\
&=&-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}+g^{\prime }(x)\left( 
\frac{-1}{x\cos x}\right) .
\end{eqnarray*} 
So we take $f_{2}(x)=\left( \frac{-1}{x\cos x}\right) ,$ and we have to add
and subtract $f_{2}^{\prime }(x)$ 
\begin{eqnarray*}
h_{2}(x) &=&\left( \left( \frac{-1}{x\cos x}\right) ^{\prime }+g^{\prime
}(x)\left( \frac{-1}{x\cos x}\right) \right) -\frac{\sin x}{x\cos ^{2}x}+ 
\frac{1}{x^{2}\cos x}-\left( \frac{-1}{x\cos x}\right) ^{\prime } \\
&=&\left( f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)\right) -\frac{\sin x}{ 
x\cos ^{2}x}+\frac{1}{x^{2}\cos x}-\left( \frac{-1}{x\cos x}\right) ^{\prime
}
\end{eqnarray*} 
However, easy computation shows that 
\begin{equation*}
-\frac{\sin x}{x\cos ^{2}x}+\frac{1}{x^{2}\cos x}-\left( \frac{-1}{x\cos x} 
\right) ^{\prime }=0.
\end{equation*} 
(This happens because the exercise is from a textbook) Therefore 
\begin{equation*}
h(x)=f_{1}(x)+f_{2}(x)=x-\frac{1}{x\cos x}
\end{equation*} 
and 
\begin{equation*}
\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=\left( x-\frac{1}{x\cos x}\right) e^{x\sin x+\cos
x}+C. 
\ \ \ 
\color{red} 
 \blacksquare 
\end{equation*}
