$\int_0^{\frac{\pi}{2}} \frac{cos(x)}{p\sin(x) + q\cos(x)}dx$, where $p,q$ are positive constants 
Let $$I = \int_0^a \frac{\cos(x)}{\sin(x) + \cos(x)}dx $$ and $$J =
 \int_0^a \frac{\sin(x)}{\sin(x) + \cos(x)} dx$$
where $0 \leq a < \frac{3\pi}{4}$. By considering $I+J$ and $I-J$,
show that $2I = a+\ln(\sin(a) + \cos(a))$
Find also:
(i) $$\int_0^{\frac{\pi}{2}} \frac{\cos(x)}{p\sin(x) + q\cos(x)}dx$$,
where $p,q$ are positive constants.

Workings: The first part is easy enough. $I+J = \int_0^a\frac{\cos(x) + \sin(x)}{sin(x)+\cos(x)} dx = a$
$I-J = \int_0^a \frac{\cos(x) - \sin(x)}{\cos(x) + \sin(x)}dx = \ln(\sin(a) + \cos(a))$
$ \therefore 2I = a + \ln(\sin(a) + \cos(a))$
(i)
Now for the second part, which I am struggling with. I have tried to replicate the above technique by letting $qI = \int_0^{\frac{\pi}{2}} \frac{q\cos(x)}{p\sin(x) + q\cos(x)} dx$ and $pJ = \int_0^{\frac{\pi}{2}} \frac{p\sin(x)}{p\sin(x) + q\cos(x)} dx$. This results in  $qI + pJ = \int_0^{\frac{\pi}{2}}\frac{q\cos(x) + p\sin(x)}{p\sin(x) + q\cos(x)}dx = \frac{\pi}{2}$.
However, I am having trouble with this integral $qI - pJ =  \int_0^{\frac{\pi}{2}}\frac{q\cos(x) - p\sin(x)}{p\sin(x) + q\cos(x)}dx$
 A: The form of the second linear combination in (i) suggests the correct coefficients for (ii):
$$pI-qJ=\int_0^{\pi/2}\frac{p\cos x-q\sin x}{p\sin x+q\cos x}\,dx=\ln\frac pq$$
This leads to a linear system with determinant $p^2+q^2>0$ for $I,J$ which can be easily solved:
$$I=\frac{q\pi+2p\log p/q}{2(p^2+q^2)}\qquad J=\frac{p\pi-2q\log p/q}{2(p^2+q^2)}$$
A: You have defined$$I=\int_0^{\pi/2}\frac{\cos(x)}{p\sin(x)+q\cos(x)}\,\mathrm dx.$$If you do $x=\frac\pi2-y$ and $\mathrm dx=-\mathrm dy$, then you deduce that$$I=-\int_{\pi/2}^0\frac{\sin(y)}{p\cos(y)+q\sin(y)}\,\mathrm dy=\int_0^{\pi/2}\frac{\sin(x)}{q\sin(x)+p\cos(x)}\,\mathrm dx.$$By the same argument,$$J=\int_0^{\pi/2}\frac{\cos(x)}{q\sin(x)+p\cos(x)}\,\mathrm dx,$$and therefore$$pI-qJ=\int_0^{\pi/2}\frac{p\sin(x)-q\cos(x)}{q\sin(x)+p\cos(x)}\,\mathrm dx.$$And now if you differentiate the denominator, you get minus the numerator. So,$$pI-qJ=\log(p)-\log(q).$$Since $qI+pJ=\frac\pi2$, you get that$$I=\frac{-2p\log(q)+2p\log(p)+\pi q}{2\left(p^2+q^2\right)}\quad\text{and that}\quad J=\frac{-2q\log(p)+2q\log(q)+\pi p}{2 \left(p^2+q^2\right)}.$$
