# How to find $\int|\cos x|\,dx$?

How do I find closed form for $\int|\cos x|\,dx$ for all real $x$?

It can be expressed as incomplete elliptic integral of the second kind:

$$\int|\cos x|\,dx=\int\sqrt{1-1^2\sin^2x}\,dx=E(x,1)$$

However, I feel that form without special function exists, because definite integral can be computed using only elementary functions by dividing it into several integrals.

• |cos x| = +cos x or - cos x so when integral cos x= sin x +c it depends on the sign of cos x .... so multiply right side of integral by the sign of cos (x) Sep 17, 2014 at 17:03

Experimenting, I found following:

$$\int |\cos x| \, dx=\sin x\,\text{sgn}(\cos x)+\left\lfloor\frac{x}{\pi}+\frac{1}{2}\right\rfloor+\left\lceil\frac{x}{\pi}-\frac{1}{2}\right\rceil+C$$

Floor and ceiling functions are required to make antiderivative continuous everywhere. Then it can be used to get correct results when using this antiderivative in definite integral calculation such as:

$$\int_0^{12\pi}|\cos (x)|\,dx=24$$

Without floor and ceiling part we would get $$0$$ which is obviously incorrect.

To prove that given function is correct antiderivative, just differentiate it. Note that floor, ceiling and signum functions have derivative $$0$$ (where it's defined). To prove that it indeed is continuous, first note, that cosine is continuous everywhere, signum is continuous everywhere except at $$0$$ and floor and ceiling functions are continuous everywhere except at integers. Then $$\text{sgn}(\cos x)$$ is discontinuous where cosine is $$0$$, that is $$x=\frac{\pi}{2}+ \pi n,n\in\mathbb{Z}$$. It happens that floor and ceiling functions here are discontinuous at the same exact points. To prove that whole function is continuous, values and left and right limits at those points should be evaluated and be the same.

Value at $$x=\frac{\pi}{2}+\pi n$$:

$$\sin \left(\frac{\pi}{2}+ \pi n\right)\,\text{sgn}\left(\cos \left(\frac{\pi}{2}+ \pi n\right)\right)+\left\lfloor\frac{\frac{\pi}{2}+ \pi n}{\pi}+\frac{1}{2}\right\rfloor+\left\lceil\frac{\frac{\pi}{2}+ \pi n}{\pi}-\frac{1}{2}\right\rceil=$$ $$=0+(n+1)+n=2n+1$$

Limit at same $$x$$: $$\lim_{x\to\frac{\pi}{2}+\pi n}\left(\sin x\,\text{sgn}(\cos x)+\left\lfloor\frac{x}{\pi}+\frac{1}{2}\right\rfloor+\left\lceil\frac{x}{\pi}-\frac{1}{2}\right\rceil\right)=$$

$$=\lim_{\epsilon\to 0}\left(\sin \left(\frac{\pi}{2}+ \pi n+\epsilon\right)\,\text{sgn}\left(\cos \left(\frac{\pi}{2}+ \pi n+\epsilon\right)\right)+\left\lfloor\frac{\frac{\pi}{2}+ \pi n+\epsilon}{\pi}+\frac{1}{2}\right\rfloor+\left\lceil\frac{\frac{\pi}{2}+ \pi n+\epsilon}{\pi}-\frac{1}{2}\right\rceil\right)=$$

$$=\lim_{\epsilon\to 0}\left(\cos \left(\pi n+\epsilon\right)\,\text{sgn}\left(-\sin\left(\pi n+\epsilon\right)\right)+\left\lfloor n+1+\frac{\epsilon}{\pi}\right\rfloor+\left\lceil n+\frac{\epsilon}{\pi}\right\rceil\right)=$$

$$=2n+1+\lim_{\epsilon\to 0}\left((-1)^n\cos\epsilon\,\text{sgn}\left(-(-1)^n\sin\epsilon\right)+\left\lfloor\frac{\epsilon}{\pi}\right\rfloor+\left\lceil\frac{\epsilon}{\pi}\right\rceil\right)=$$

$$=2n+1+\lim_{\epsilon\to 0}\left(-\text{sgn }\epsilon+\left\lfloor\frac{\epsilon}{\pi}\right\rfloor+\left\lceil\frac{\epsilon}{\pi}\right\rceil\right)$$

Now consider left and right limits separately:

$$\lim_{\epsilon\to 0^-}\left(-\text{sgn }\epsilon+\left\lfloor\frac{\epsilon}{\pi}\right\rfloor+\left\lceil\frac{\epsilon}{\pi}\right\rceil\right)=-(-1)+(-1)+0=0$$

$$\lim_{\epsilon\to 0^+}\left(-\text{sgn }\epsilon+\left\lfloor\frac{\epsilon}{\pi}\right\rfloor+\left\lceil\frac{\epsilon}{\pi}\right\rceil\right)=-1+0+1=0$$

They are equal, that means limit at $$x$$ exists, also this limit is equal to function value at same point, so function is continuous at $$x$$ and, putting all together, we know that function is continuous everywhere.

Also, note that this function can be used to find $$\int |\sin x| \, dx=\int |\cos(x-\frac{\pi}{2})| \, dx$$

Thanks @darya khosrotash for comment and @UserX for partial answer, as well as @Barry Cipra for pointing out that previous version of this answer was not continuous everywhere.

• I'm not sure where you are getting the extra floor function part from. Sep 17, 2014 at 17:24
• @taninamdar, you need the floor function to make the anti-derivative differentiable at points where $\cos x=0$. Without it, the function $\sin x\, \text{sgn}(\cos x)$ isn't even continuous at those points. Sep 17, 2014 at 17:32
• @Philipp ... $\sin x\,\text{sgn}(\cos x)+2\left\lfloor\frac{x}{\pi}+\frac{1}{2}\right\rfloor$ is continuous. The limits from the left and from the right as $x \to \pi/2$ are both $1$. Sep 15, 2021 at 14:43
• @GEdgar But this modification would not work because $\sin(3\pi/2) = -1$. I think that this expression is simply incorrect, and Christian Blatter's answer should be the accepted answer. Sep 15, 2021 at 16:04
• There just no way to get around $x = \pi/2$ and $x = 3\pi/2$ to make this expression work. Sep 15, 2021 at 16:14

Denote by $S$ the set of odd multiples of ${\pi\over2}$. When $x\notin S$ then there is a well defined $k\in{\mathbb Z}$ with $$x\in J_k:=\left]k\pi-{\pi\over2},\ k\pi+{\pi\over 2}\right[\ .$$ For $x\in J_k$ let $$\hat x:=x-k\pi\ .$$ Then $|\hat x|<{\pi\over2}$, and $x-\hat x$ is constant on $J_k$.

The function $$F(x):=\cases{\sin\hat x+{2\over\pi}(x-\hat x)&(x\notin S) \cr {2x\over\pi}&(x\in S)\cr}$$ has derivative $$F'(x)=\cos\hat x=|\cos x|\qquad(x\notin S)\ ,$$ and for any $\xi\in S$ we get $\lim_{x\to\xi} F'(x)=0$. We now show that $F$ is continuous also in the points of $S$. This then will definitively establish $F$ as a primitive of the function $f(x):=|\cos x|$.

Let $\xi=(2n+1){\pi\over2}\in S$. Then $\sin\xi=(-1)^n$.

When $x\to{\xi-}\>$ we have $\hat x=x-n\pi$ and therefore $$F(x)=(-1)^n\sin x+{2\over\pi}(n\pi)\to(-1)^n\sin\xi+2n=1+2n=F(\xi)\qquad(x\to\xi-)\ .$$ When $x\to\xi+$ we have $\hat x=x-(n+1)\pi$ and therefore $$F(x)=(-1)^{n+1}\sin x+{2\over\pi}(n+1)\pi\to(-1)^{n+1}\sin\xi+2(n+1)=-1+2(n+1)=F(\xi)\qquad(x\to\xi+)\ .$$ Here is a picture of $F$:

By the definition of the absolute value function: $$\left| f(x)\right| = \sqrt{f(x)^2}= \begin{cases} f(x) &\mbox{, } f(x)\geq0 \\ -f(x) & \mbox{, } f(x)<0 \end{cases}$$ So, naturally, $$\left| \cos x\right| = \begin{cases} \cos (x) &\mbox{, } 2n\pi - \frac{\pi}{2}\leq x \leq 2n\pi + \frac{\pi}{2} \\ -\cos(x) & \mbox{, } 2n\pi + \frac{\pi}{2} < x < 2n\pi + \frac{3\pi}{2} \end{cases} ,\text {where }n \in \mathbb Z$$ Integrating with respect to $x$, $$\int{\left| \cos x\right|}\, \text{d}x = \begin{cases} \sin (x) + C&\mbox{, } \cos(x)\geq 0\\ -\sin(x) - C& \mbox{, } \cos(x) < 0 \end{cases} \text{, where C is the constant of integration}$$

The above statement is equivalent to $$\int \left| \cos(x) \right| \, \text{d}x = \sin(x)\cdot\text{sgn}(\cos x) + c$$

It is interesting to note that the graph of this is a bunch of integral symbols:

The important thing to note is the property of absolute value function, i.e. $|x| = x\ \ \forall x \ge 0$ and $|x| = -x\ \ \forall x \le 0$
Thus, for the values of x where $\cos x$ is non-negative, the integral is $\int \cos x\ \ dx = \sin x + c$.
Similarly, for the values of x where $\cos x$ is negative, the integral is $\int -\cos x\ \ dx = -\sin x + c$.
Combining the two parts, $\int \cos x dx = \text{sgn}(\cos x) \sin x + c$, where $\text{sgn(x)}$ is the Sign Function.

• If using Newton–Leibniz formula with this integral then you get incorrect value. Sep 17, 2014 at 17:41

Like darya suggested in the comments, just multiply the RHS by $\mathrm{sgn}(\cos x)$

So $$\int |\cos x| \mathrm{d}x=\sin (x) \mathrm{sgn}(\cos x)+c$$

• If using Newton–Leibniz formula with this integral then you get incorrect value. Sep 17, 2014 at 17:16
• Note $\int_0^N |\cos x|\;dx \to \infty$ as $N \to \infty$, but your integral is bounded. Sep 15, 2021 at 14:37