What is the derivative of max and min functions? [closed]

If I define a function:

$f(x) = \max[g(x),h(x)]$

What is $f'(x)$?


I assume that $f$ and $g$ are differentiable. You can write $$ \max(f(x),g(x)) = \frac{f(x) + g(x) + |f(x) - g(x)|}{2}$$ and calculate the derivative of your function at those points where it exists (note that $x \mapsto |x|$ is not differentiable at $0$, so it is not clear that the derivative exists at those points where $f(x) = g(x)$.) Distinguishing the cases in the different regions, what we obtain is the following

$$ \frac{d}{dx} \max(f(x),g(x)) = \begin{cases} f'(x) & \text{if} \quad f(x) = g(x) \text{ and } f'(x) =g'(x) \\ g'(x) & \text{if} \quad g(x) > f(x) \\ f'(x) & \text{if} \quad f(x) > g(x) \\ \text{undefined} & \text{if} \quad f(x) = g(x) \text{ and } f'(x)\neq g'(x) \end{cases}$$


This function does not need to have a derivative. For example, pick $g(x) = x$ and $h(x)=-x$. Then we obtain $$ f(x) = \max(x,-x) = |x| $$ which does not have a derivative at $x=0$. By picking uglier fuctions $g$ and $h$ you can create more of these points.