# How to calculate $\left| \sin x \right|$ derivative in a more elegant way?

I am trying to calculate the derivative of $$\left| \sin x \right|$$

Given the graphs, we notice that the derivative of $$\left| \sin x \right|$$ does not exist for $$x= k\pi$$.

Graph for $$\left|\sin x\right|$$:

We can rewrite the function as

$$\left| \sin(x) \right| = \left\{ \begin{array}{ll} \sin(x),& 2k\pi < x < (2k+1)\pi \\ -\sin(x), & \text{elsewhere} \\ \end{array} \right.$$

Therefore calculate its derivative as:

$$(\left| \sin(x) \right|)^{'} = \left\{ \begin{array}{ll} \cos(x),& 2k\pi < x < (2k+1)\pi \\ -\cos(x), & \text{elsewhere} \\ \end{array} \right.$$

Is there a way to rewrite this derivative, in a more elegant way (as a non-branch function) $$(\left| \sin(x) \right|)^{'} = g(x)$$?

• Note that the derivative doesn't exist at $x=k\pi$. Elsewhere you have it as a piecewise function. Commented Jul 11, 2020 at 7:25
• True, I am editing the question, thank you. Commented Jul 11, 2020 at 7:26
• @Dimitris I know that you already accepted VIVID's answer, but since you were looking for an elegant way you may consider to at least upvote my answer. Commented Jul 31, 2021 at 1:51
• @jjagmath done :) Commented Aug 1, 2021 at 2:11

The better way, for me, is as follows: $$f(x)=\left|\sin(x)\right|=\sqrt{\sin^2(x)}$$ Now, differentiate both sides to get $$f'(x)=\frac{1}{2\sqrt{\sin^2(x)}}\cdot2\sin(x)\cos(x)=\frac{\sin(2x)}{2\left|\sin(x)\right|}$$ Therefore, $$\left(\left|\sin(x)\right|\right)'=\frac{\sin(2x)}{2\left|\sin(x)\right|}, \ \ \ \ x \neq k\pi, k\in \mathbb{Z}$$

Appendum: This approach can easily be extended to a general case of finding $$\left(|f(x)|\right)'$$.

First, we rewrite $$|f(x)| = \sqrt{f^2(x)}$$ Then, repeating the work above: $$|f(x)|' = \frac{1}{2\sqrt{f^2(x)}}[2f(x)f'(x)] = \frac{f(x)}{|f(x)|}f'(x)$$ we get $$\boxed{|f(x)|' = \frac{f(x)}{|f(x)|}f'(x)}$$

• Simple and nice, I'll accept this answer, thank you @VIVID Commented Jul 11, 2020 at 7:30

Apparently other answers missed the following direct approach: we have $$|x|' = \frac{x}{|x|}$$ for $$x\ne 0$$.

So, for example, VIVID's formula $$|f(x)|' = \frac{f(x)}{|f(x)|} f'(x)$$ is just the chain rule.

• A standard but little-used notation for the signum function is $sgn (y)$, defined as $sgn (y)=y/|y|$ when$0\ne y\in\Bbb R$ and $sgn (0)=0$. In the 18th century two men got hold of the British school system and did their best to destroy the ease of phonetic spelling, insisting there must be a "g" in "sign", a "b" in "debt" , a "p" in "pneumonia", and so on. Commented Jul 31, 2021 at 2:35
• I'm familiar with the sign function. I didn't use it on purpose because $|x|'$ doesn't exists at $0$, while $sgn$ is well defined there. Commented Jul 31, 2021 at 3:06
• Yes, it is more direct, but when I was asked to find $|x|'$ for the first time, I did exactly the same thing as in my answer i.e. $|x| = \sqrt{x^2}$ and then differentiating. Otherwise, the way to prove it would be the same as how the OP did for $|\sin x|$ himself, the method which he was not satisfied. So, my answer is actually using your idea hiddenly with its proof. Commented Jul 31, 2021 at 6:01

I think an even nicer way of thinking about it is in terms of the sign of $$\sin(x)$$ applied to the derivative of $$\sin(x)$$. A function $$f$$ that returns the sign of another function $$g$$ is: $$f(x)=\frac{\lvert(g(x))\rvert}{g(x)}$$ .
So quite simply $$\lvert \sin(x) \rvert'=\frac{\lvert \sin(x)) \rvert}{\sin(x)}\cdot\cos(x)$$

This is essentially just $$\cos(x)$$ but reflected across the axis at the intervals where $$\sin(x)$$ is negative, that is: $$\cos(x)$$ for $$x \in [0,\pi]$$ and $$-\cos(x)$$ for $$x \in [\pi,2\pi]$$ and so on. The reason for this is not difficult to see: when we take the absolute value of a function, then the gradients of the points belonging to the intervals that were flipped, here where $$x \in [\pi,2\pi]$$, are also flipped. So what ends up happening is that the derivative essentially stays the same, but is flipped on the intervals where the original function was flipped.

• Notice that $|y|/y = y/|y|,$ so we can show that this formula is equivalent to the other answer, although the derivation is different. Commented Jul 11, 2020 at 16:15