Integrating $\int_0^{\frac{\pi}{2}} \frac{x}{\cos(x)+\sin(x)} \mathrm dx$ using complex analysis I am struggling with the integration of $$ \text{I} = \int_0^{\frac{\pi}{2}} \frac{x}{\cos(x)+\sin(x)} \mathrm dx$$ using complex analysis. I used the substitution $z=e^{ix} \rightarrow \mathrm dx = \frac{1}{iz}\mathrm dz$ and hence $\cos(x)+\sin(x)= ((z+\frac{1}{z})-i (z-\frac{1}{z}))$ because we have $ \cos(x)=\frac{1}{2}(e^{ix}+ e^{-ix})$, and $ \sin(x)=\frac{1}{2i}(e^{ix} -e^{-ix})$, and $x= -i \ln(z)$. However I am not successful to obtain the correct result when using real analysis, which gives $$\text{I}= \displaystyle\int_{0}^{\frac{\pi}{2}}\dfrac{x}{\sin\,x + \cos\,x}\,dx = 0.978959918 $$
Any idea how to calculate this integral using complex analysis and Cauchy integral theorem?
 A: Here is a proof using complex analysis (CA), or sincerely said, complex numbers, to some force extend, it is all i could do in the direction of a solution using analytic functions and properties of them. There may be some reasons to try to solve this problem using CA, but this is (at least didactically) not a good example to involve it. Reasons:


*

*Any proof in real analysis (RA) is one in complex analysis. In CA we also build primitives, and try to evaluate.  

*A proof in CA would be "veritable" only if we essentially use some main results from this field, for instance the residue theorem. But in this given case, it is hard to isolate some closed contour to integrate on it some function related to our integral. (The given integral, or the simpler one of constant$/(\cos t+\sin t)$ cannot be easily written as an integral over a closed contour.)

*When the algorithmic, straightforward proof in RA is so simple, why do we complicate things to give a CA-solution with a different personality?!


Computations follow.

The given integral is 
$\newcommand{\arctan}{\operatorname{arctan}}$
$$
\begin{aligned}
I 
&= 
\int_0^{\pi/2}
\frac t{\cos t+\sin t}\; dt
= 
\int_0^{\pi/2}
\frac t{\sqrt 2(\cos (t-\pi/4)}\; dt
\\
&\qquad\qquad\text{Substitution: }u=t-\pi/4\ ,
\\
&= 
\frac 1{\sqrt 2}
\int_{-\pi/4}^{\pi/4}
\frac {u+\pi/4}{\cos u}\; du
\\
&= 
\frac 1{\sqrt 2}
\int_{-\pi/4}^{\pi/4}
\frac {\pi/4}{\cos u}\; du
+
\underbrace{
\frac 1{\sqrt 2}
\int_{-\pi/4}^{\pi/4}
\frac {u}{\cos u}\; du
}_{=0\text{ odd function}}
\\
&\qquad\text{(CA argument: integral on contur $-\pi/4\to\pi/4\to-\pi/4$)}
\\
&= 
\frac 1{\sqrt 2}\cdot
\frac\pi 4
\underbrace{
\int_{-\pi/4}^{\pi/4}
\frac 1{\cos u}\; du}_{=:J\text{ (notation)}}\ ,
\end{aligned}
$$
so it is enough to understand the integral
$$
\begin{aligned}
J&:=
\int_{-\pi/4}^{\pi/4}
\frac 1{\cos u}\; du
=
\int_{-\pi/4}^{\pi/4}
\frac 1{\frac 12(e^{iu}+e^{-iu})}\; du
\\
&\qquad\qquad\text{Substitution: }z=e^{iu}\ ,\ dz=ie^{iu}\; du=iz\; du
\\
&= 
\int_{\exp-i\pi/4}^{\exp+i\pi/4}
\frac 1{\frac 12\left(z+\frac 1z\right)}\; 
\frac 1{iz}\; dz
\\
&= 
\frac 2i
\int_{\exp-i\pi/4}^{\exp+i\pi/4}
\frac 1{z^2+1}\; dz
\\
&= 
\frac 2i
\Big(\arctan e^{i\pi/4}-\arctan e^{-i\pi/4} \Big)
\\
&= 
\frac 2i
\arctan 
\frac
{e^{i\pi/4}-e^{-i\pi/4}}
{1+e^{i\pi/4}\cdot  e^{-i\pi/4}}
=
\frac 2i
\arctan 
\frac{\frac 1{\sqrt 2}(1+i)-\frac 1{\sqrt 2}(1-i)}{1+1}
=
\frac 2i
\arctan 
\frac i{\sqrt 2}
\\
&=
\frac 2i
\cdot i\log(1+\sqrt 2)
=2\log(1+\sqrt2)\ .
\end{aligned}
$$
(The computation of the $\arctan$ of $i/\sqrt 2$ is possibly harder as solving the integral in RA... But our problem is the quick computation of an integral, so we substitute inside CA, write the primitive, compute the values of the primitive at the ends of the contour.)
Here we check the $\arctan$ value, write $a$ for $i\log(1+\sqrt 2)$ for short, then:
$$
\tan a
=\frac{\sin a}{\cos a}
=\frac 1i\cdot
 \frac{e^{ia}-e^{-ia}}{e^{ia}+e^{-ia}}
=\frac 1i\cdot
 \frac{1-e^{-2ia}}{1+e^{-2ia}}
=\frac 1i\cdot
 \frac{1-(1+\sqrt2)^2}{1+(1+\sqrt2)^2}
=\frac 1i\cdot \frac {-1}{\sqrt 2}=\frac i{\sqrt 2}\ .
$$

An other way to proceed above is to write $$
\frac 1{z^2+1}
=\frac 1{2i}\left(\frac 1{z-i}+\frac 1{z+i}\right)$$
and to use the $\log$-primitives (instead of the $\arctan$ primitive above).
(This explains in part the value of the $\arctan$-computation.)

Alternatively, we could integrate $1/(z^2+1)$ on the closed contour $\gamma$ from $0$ radially to $\exp-i\pi/4$, then on the unit circle in trigonometric sense to $\exp-i\pi/4$, then radially to the starting point $0$. Now we are in real analysis, and compute primitives, take them in the end points, well, these are more or less the $\arctan$ values above, with the double insertion of the point $0$, these contribution cancel each other, so we come back to the argument above.

The above idea is sometimes good, when the two radial integrals can combine in a good manner. (E.g. when we integrate a sum of fractions $f\pm \bar f$, and when the obtained fraction, after building the product of the denominators, a real polynomial, and of the corresponding numerator, also real, ... so when this expression is very easy to be integrated. Too much to compute in this case.)

There is a related idea to use $w=z^{1/4}$ and rephrase the "sector contour" $\gamma$ above into a contour $0$ to $-1$ radially, then on the full unit circle to $-1$, then to $0$ back again. (Also, around $0$ draw a full small circle of radius $\epsilon$ when there is a pole in there.) This idea can be used when the function to be integrated has a pole in $0$ and the two radial integrals cancel each other. This is not the case in our case. No pole in zero after substituion, which may survive after passing to the limit $\epsilon\to 0$, no poles in the unit circle (or equivalently in the starting sector delimited by $\gamma$.

Just as a comparison, the proof in RA (so in particular, also in CA)
$$
\int_{-\pi/4}^{+\pi/4}\frac 1{\cos u}\; du
=
\int_{-\pi/4}^{+\pi/4}\frac {\cos u}{\cos^2 u}\; du
=
\int_{-\pi/4}^{+\pi/4}\frac {\cos u}{1-\sin^2 u}\; du
=
\int_{-1/\sqrt 2}^{+1/\sqrt 2}\frac {1}{1-s^2}\; du
=
\log\frac{1+1/\sqrt 2}{1-1/\sqrt 2}
=
\log\frac{\sqrt 2+1}{\sqrt 2-1}
=
\log(\sqrt 2+1)^2
=
2\log(\sqrt 2+1)\ ,
$$
and no intermediary step was omitted.
