Explanation of an integration trick for $\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}dx$ I'm not sure how to formulate my question correctly. Basically it comes from solving the integral:
$$
\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}dx\\
a^2 + b^2 \ne 0
$$
I haven't been able to solve the integral without the trick I've found after a while. This trick suggests to rewrite:
$$
a_1 \cos x + b_1 \sin x = \frac{a_1a + b_1b}{a^2 + b^2}(a\cos x + b\sin x) + \frac{a_1b - ab_1}{a^2 + b^2}(b\cos x - a\sin x)\tag{1}
$$
After using this trick the integral becomes almost elementary:
$$
I = \int \frac{a_1a + b_1b}{a^2 + b^2} dx + \int \frac{a_1b - ab_1}{a^2 + b^2}\frac{b\cos x - a\sin x}{a\cos x + b\sin x} dx
$$
The first part is trivial, for the second one substitute $u = a\sin x + b\cos x$.
My question is how on earth one could arrive at $(1)$. Is that some sort of well-known expression that I just missed?
Going from RHS to LHS in $(1)$ is easy, but how do I make it the other way round?
Thank you!
 A: Any linear combination of sine waves with the same period and different phase shifts can be written as a single sine wave with that same period and a suitable phase shift.
\begin{align}
& A\cos(x+\varphi) + B\cos(x+ \psi) \\[8pt]
= {} & A\big(\cos x\cos\varphi - \sin x \sin\varphi\big) \\
& {} + B\big(\cos x\cos\psi - \sin x \sin \psi\big) \\[8pt]
= {} & C\cos x + D\sin x
\end{align}
where
\begin{align}
C & = A\cos\varphi + B \cos\psi \\[8pt]
D & = -A\sin\varphi - B\sin\psi
\end{align}
and then
\begin{align}
& C\cos x + D\sin x \\[8pt]
= {} & \sqrt{C^2+D^2} \left( \frac C {\sqrt{C^2+D^2}} \cos x + \frac D {\sqrt{C^2+D^2}} \sin x\right) \\[8pt]
= {} & \sqrt{C^2+D^2} \big( E\cos x + F\sin x\big).
\end{align}
We now have $E^2+F^2=1$ so $E= \cos\chi$ and $F=\sin\chi$ for some angle $\chi.$ Thus we have
\begin{align}
& E\cos x + F\sin x \\[8pt]
= {} & \cos\chi\cos x + \sin\chi\sin x \tag 1 \\[8pt]
= {} & \cos(x-\chi).
\end{align}
Line $(1)$ above is what you have in the problem you're facing.
A: You could also do it in a standard way. Since this is a rational function of trigonometric functions, we know trigonometric substitutions work. In this case, $u=\tan x$ is a good one. We have $\mathrm{d}u=\sec^2 x\mathrm{d}x$, $\mathrm{d}x=\frac{\mathrm{d}u}{1+u^2}$ and so
$$ \begin{align}
\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}\,\mathrm{d}x &= \int \frac{a_1 + b_1 \tan x}{a + b\tan x}\,\mathrm{d}x \\
&= \int \frac{a_1 + b_1u}{(a + bu)(1+u^2)}\,\mathrm{d}u
\end{align}$$
Then you find partial fraction decomposition
$$\frac{a_1 + b_1u}{(a + bu)(1+u^2)}=\frac{A}{a + bu}+\frac{Bu+C}{1+u^2} $$
and the integral is
$$\frac Ab\ln|a+bu|+\frac B2\ln(1+u^2)+C\tan^{-1}(u)+\text{constant} $$
which simplifies (using the values of $A,B$ and $C$) to
$$\frac Ab \ln|a\cos x+b\sin x|+Cx+\text{constant}$$
Note that after back-substituting $u=\tan x$, you need to get rid of the tangent because it introduces discontinuities at $\cos x=0$, and you don't have these in the integrand. 
A: Steps to solve this:


*

*Write the denominator as $a \cos x + b \sin x = r \cos (x + \alpha)$.

*Substitute $t = x + \alpha$ and rewrite the integrand as $$\frac{a_1 \cos (t - \alpha) + b_1 \sin (t - \alpha)}{r \cos t}.$$

*Use addition formulae in the numerator and obtain an integral that is easily computed: $$\int \frac{a_2 \cos t  + b_2 \sin t}{\cos t} \, dt = a_2 t - b_2 \log \cos t + C.$$
A: In order to arrive at (1), you assume,
$$
a_1 \cos x + b_1 \sin x = p(a\cos x + b\sin x) + q(b\cos x - a\sin x)
$$
By matching the coefficients of $\cos x $ and $\sin x$, you get,
$$pa+qb=a_1$$
$$pb -qa = b_1$$
Solve the system of linear equations to obtain,
$$p=\frac{a_1a + b_1b}{a^2 + b^2}, \>\>\>\>\> q= \frac{a_1b - ab_1}{a^2 + b^2}$$
