How to calculate $\int{\frac {x^2}{ ( x\cos x -\sin x ) ^2}}\,{\rm d}x$? When I want to calculate
$$\int{\frac {x^2}{ ( x\cos x -\sin x ) ^2}}\,{\rm d}x$$
I have tested with software and get
$${\frac {x\sin \left( x \right) +\cos \left( x \right) }{x\cos \left( x
 \right) -\sin \left( x \right) }}$$
But I can not come to this conclusion, neither using integration by parts, nor using trigonometric identities, nor multiplying by their conjugate, Even by rational trigonometric substitution. I do not know what else to try. Could you give me any suggestions?
 A: We can rewrite $$\begin{align}\int \frac{x^2}{(\sin x - x\cos x)^2} \, \mathrm{d}x &= \int \frac{x\sin x(x\sin x + \cos x)}{(\sin x - x\cos x)^2} \, \mathrm{d}x - \int \frac{x\cos x}{\sin x - x\cos x} \, \mathrm{d}x \\ & =  -\frac{x\sin x + \cos x}{\sin x - x\cos x} \color{green}{+} \int \frac{x\cos x}{\sin x- x\cos x} \, \mathrm{d}x \color{red}{-} \int \frac{x\cos x}{\sin x - x\cos x} \, \mathrm{d}x \\ & = -\frac{x\sin x + \cos x}{\sin x - x\cos x}\end{align}$$
via IBP with $u = x\sin x + \cos x \implies u' = x\cos x$ and $$\mathrm{d}v = \frac{x\sin x}{(\sin x - x\cos x)^2} \implies v = -\frac{1}{\sin x -x\cos x}$$ via straightforward $f = \sin x - x\cos x \implies f' = x\sin x$ sub.
A: We can tackle the integral by integration by parts as below:
$$
\begin{aligned}
& \int \frac{x^{2}}{(x \cos x-\sin x)^{2}} d x \\
=& \int \frac{x}{\sin x} d\left(\frac{1}{x \cos x-\sin x}\right) \\
=& \frac{x}{\sin x(x \cos x-\sin x)} -\int \frac{1}{x \cos x-\sin x} \cdot \frac{\sin x-x \cos x}{\sin ^2x}dx\\=& \frac{x}{\sin x(x \cos x-\sin x)} +\int \csc ^{2} x d x \\
=& \frac{x}{\sin x(x \cos x-\sin x)}-\cot x+C\\=& \frac{x \sin x+\cos x}{x \cos x-\sin x}+C
\end{aligned}
$$
