<s>[INST] Last step in evaluating: $\int {dx\over a\cos x + b\sin x}$ Seems like I've made a mistake somewhere in my calculation and am now struggling to find it without any success. Here is the problem statement:

Evaluate: 
  $$
\int {dx\over a\cos x + b\sin x}
$$

I've decided to use a $t = \tan{x\over 2}$ substitution:
$$
dx = {2\,dt\over 1+t^2}\\
\sin x = {2t\over 1+t^2}\\
\cos x = {1-t^2\over 1+t^2}\\
$$
That substitution yields:
$$
\begin{align}
I &= \int {2\,dt \over (1+t^2)\left({a(1-t^2)\over 1+t^2} + {2bt\over 1 + t^2}\right)} \\
&=\int {2\,dt \over a(1-t^2) + 2bt} \\
&=-2 \int {dt \over at^2 - 2bt - a}
\end{align}
$$
Factor the denominator:
$$
D = 4b^2 + 4a^2\\
R_{1,2} = {b \pm \sqrt{a^2 + b^2}\over a}\\
{1 \over at^2 - 2bt - a} = {1\over a(t-R_1)(t-R_2)}
$$
By that the integral becomes:
$$
I = -{2\over a}\int {dt\over (t-R_1)(t-R_2)}
$$
Using partial fraction decomposition:
$$
\begin{align}
I &= -{2\over a}\int \left({1\over (R_1 - R_2)(t-R_1)} - {1\over (R_1 - R_2)(t-R_2) }\right)\,dt \\
&= -{2\over a(R_1 - R_2)}\left(\ln\left|t -R_1\right|- \ln\left|t -R_2\right|\right) \\
&= - {2\over a(R_1 - R_2)}\ln\left|{t -R_1 \over t -R_2}\right|  + C\\
&= \boxed{ - {2\over a(R_1 - R_2)}\ln\left|{\tan{x\over 2} -R_1 \over \tan{x\over 2} -R_2}\right|  + C }
\end{align}
$$
My answer seems legit, here is a simulation in Desmos. The problem that the answer section suggests that:
$$
I = {1\over \sqrt{a^2 + b^2}}\ln\left|\tan \left({x\over 2} + {\phi \over 2}\right)\right| + C
$$
Where $\phi$ satisfies the following conditions:
$$
\sin \phi = {a\over \sqrt{a^2 + b^2}}\\
\cos \phi = {b\over \sqrt{a^2 + b^2}}
$$
I've tried manipulating my answer in different ways, but I still couldn't express it in the way it is expressed in the keys section. I would appreciate it if someone could explain to me the way to do it. Thank you!
 [/INST] $\tan(\frac x2+\frac\phi 2) = \dfrac{\tan\frac x2+\tan\frac\phi 2}{1 - \tan\frac x2\tan\frac\phi 2}$
but 
$$\tan\frac\phi 2 = \dfrac{\sin\phi}{1+\cos\phi} = \dfrac{a}{b+\sqrt{a^2+b^2}} = \dfrac{\sqrt{a^2+b^2} - b}{a} = -R_2.$$
Thus,
$$\ln\tan\left(\frac x2+\frac\phi 2\right) = \ln\dfrac{\tan\frac x2 - R_2}{1+R_2\tan\frac x2} = -\ln\dfrac{1 - \frac {1}{R_1}\tan\frac x2}{\tan\frac x2 - R_2}.$$
Can you see it now? The only nuisance was to use $\ln a = -\ln\frac 1a$, on top of $R_1R_2 = -1$ and when you multiply the last numerator by $R_1$ because it can be absorbed to the constant $C.$
</s>