# How to integrate $\int_{-\pi}^\pi|\cos(x)|dx$

I need to calculate the average value $\mu$ with the formula: $$\mu = \frac{1}{b-a} \int_a^bf(x)\,dx$$ in my case: $$\mu = \dfrac{1}{2\pi}\int_{-\pi}^\pi |\cos(x)|\,dx =\dfrac{1}{\pi}\int_{0}^\pi |\cos(x)|\,dx$$ but the problem is that I don't know how to integrate the absolute value of $\cos$: '$|\cos(x)|$' How do I integrate it?

• $\cos x \geqslant 0$ for $0 \leqslant x \leqslant \pi/2$, and $\cos x \leqslant 0$ for $\pi/2 \leqslant x \leqslant \pi$. – Daniel Fischer Jan 21 '14 at 15:25
• Where is cos(x) negative? Where is cos(x) positive? On what interval? – Eleven-Eleven Jan 21 '14 at 15:27

Something to consider is that between $0$ and $\pi/2$, $cos$ is positive, and so $|cos| = cos$. And then between $\pi/2$ and $\pi$, $cos$ is negative, so $|cos| = -cos$.

All you need to do at that point is split your integral up:

$$\int_0^\pi |\cos(x)| \,dx = \int_0^{\pi/2} |\cos(x)|\, dx + \int_{\pi/2}^\pi |\cos(x)| \,dx = \int_0^{\pi/2} \cos(x) \,dx - \int_{\pi/2}^\pi \cos(x)\, dx.$$

You can go a step further to solving then by noting that $\int_{\pi/2}^\pi \cos(x)\, dx = -\int_0^{\pi/2} \cos(x)\, dx$, so all in all your integral resolves down to:

$$\int_0^\pi |cos(x)| \,dx = 2\int_0^{\pi/2} cos(x) \,dx.$$

But that last bit is extra to your original question :) Long story short, in the general case, when looking at integrals of absolute values you need to break it into the separate cases:

$$\int_x |f(x)| = \int_{x: f(x) \geq 0} f(x) - \int_{x: f(x) < 0} f(x)$$

Hope that helps you!

• Thank you, it really helped me :) – Mark Jan 21 '14 at 17:21

Just integrate the positive and negative parts: $$\int_0^\pi |\cos(x)|dx = \int_0^{\pi/2} |\cos(x)|dx + \int_{\pi/2}^\pi|\cos(x)|dx.$$ Knowing that $\cos(x)\geq 0$ for $x\in[0,\pi/2]$ and $\cos(x)\leq 0$ for $x\in[\pi/2, \pi]$, you have $$\int_0^{\pi/2} |\cos(x)|dx + \int_{\pi/2}^\pi|\cos(x)|dx = \int_0^{\pi/2} \cos(x)dx - \int_{\pi/2}^\pi\cos(x)dx$$ which is elementary.

$$\frac{1}{\pi}\int_0^\pi |\cos(x)|dx=\frac{1}{\pi}\int_0^{\pi/2} \cos(x)dx + \frac{1}{\pi}\int_{\pi/2}^{\pi} -\cos(x)dx=\frac{1}{\pi}[\sin(\pi/2)-\sin(0)-\sin(\pi)+\sin(\pi/2)]=\frac{1}{\pi}(1-0-0+1)=\frac{2}{\pi}$$

$\newcommand{\+}{^{\dagger}}% \newcommand{\angles}{\left\langle #1 \right\rangle}% \newcommand{\braces}{\left\lbrace #1 \right\rbrace}% \newcommand{\bracks}{\left\lbrack #1 \right\rbrack}% \newcommand{\ceil}{\,\left\lceil #1 \right\rceil\,}% \newcommand{\dd}{{\rm d}}% \newcommand{\down}{\downarrow}% \newcommand{\ds}{\displaystyle{#1}}% \newcommand{\equalby}{{#1 \atop {= \atop \vphantom{\huge A}}}}% \newcommand{\expo}{\,{\rm e}^{#1}\,}% \newcommand{\fermi}{\,{\rm f}}% \newcommand{\floor}{\,\left\lfloor #1 \right\rfloor\,}% \newcommand{\half}{{1 \over 2}}% \newcommand{\ic}{{\rm i}}% \newcommand{\iff}{\Longleftrightarrow} \newcommand{\imp}{\Longrightarrow}% \newcommand{\isdiv}{\,\left.\right\vert\,}% \newcommand{\ket}{\left\vert #1\right\rangle}% \newcommand{\ol}{\overline{#1}}% \newcommand{\pars}{\left( #1 \right)}% \newcommand{\partiald}[]{\frac{\partial^{#1} #2}{\partial #3^{#1}}} \newcommand{\pp}{{\cal P}}% \newcommand{\root}[]{\,\sqrt[#1]{\,#2\,}\,}% \newcommand{\sech}{\,{\rm sech}}% \newcommand{\sgn}{\,{\rm sgn}}% \newcommand{\totald}[]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}} \newcommand{\ul}{\underline{#1}}% \newcommand{\verts}{\left\vert\, #1 \,\right\vert}$ $$\color{#00f}{\large% \int_{-\pi}^{\pi}\verts{\cos\pars{x}}\,\dd x} =2\int_{0}^{\pi}\verts{\cos\pars{x}}\,\dd x =2\int_{-\pi/2}^{\pi/2}\verts{\sin\pars{x}}\,\dd x =4\int_{0}^{\pi/2}\sin\pars{x}\,\dd x = \color{#00f}{\large 4}$$

Hint: $\mu = \frac{2}{\pi} \int_{0}^{\frac{\pi}{2}}\cos(x) dx.$

• Sorry, but I don't really see this as a hint. I does not direct the reader towards the right question. The equation is correct, of course, but you only know it is if you already computed the thing. – 5xum Jan 21 '14 at 15:32
• Can't we conclude from the graph of $f(x)= |\cos (x)|$ ? – Inquisitive Jan 21 '14 at 15:36
• Indeed we can. But to make the answer really helpful, you should give a hint how one can read that off the graph. – Daniel Fischer Jan 21 '14 at 17:06
• I never like anyone who solves mathematic problems by "concluding" from the graph. A graph can be a good visualization of something you already know or it can give you ideas that you can then prove. If you just "see" something, but can't prove it, you can't consider it truth. Proofs "from graphs" are, in my mind, not a proof at all. – 5xum Jan 21 '14 at 20:07