TITLE: Minimum value of definite integral.
QUESTION [5 upvotes]: $f(x)$ is continuous in $[a,b]$, and $f(x)>0$. $$g(m)=\int_a^b |f(x)-m|\,dx.$$
  Claim: $g(m)$ has a minimum when $$m=\frac{\int_a^b f(x)\,dx}{b-a}.$$

Is this claim is true? If it is true, how to prove it? I tried using $$F(x)=\int f(x)\,dx$$ as an increasing function, but it didn't work well...

REPLY [5 votes]: We note that
$$g(m)=\int_a^b\sqrt{(m-f(x))^2}\,dx.$$
It follows that
\begin{align*}
g'(m)&=\frac{d}{dm} \int_a^b\sqrt{(m-f(x))^2}\,dx \\
&=\int_a^b\frac{\partial}{\partial m}\sqrt{(m-f(x))^2}\,dx \\
&=\int_a^b\frac{m-f(x)}{\sqrt{(m-f(x))^2}}\,dx \\
&=\int_a^b\operatorname{sgn}(m-f(x))\,dx.
\end{align*}
This integral is actually easier to evaluate using the Lebesgue integral (if the Riemann integral exists, the Lebesgue integral exists and they're equal). The result would be
$$\int_{[a,b]}\operatorname{sgn}(m-f(x))\,d\mu =\mu(\{x\in[a,b]:m-f(x)> 0\})-\mu(\{x\in[a,b]:m-f(x)<0\}). $$
We want this to be zero, and we can see that this will happen when, in the interval $[a,b],$ $m$ is chosen so that as much area is above $m$ (that is, above $m$ and below $f$) as is below $m$. That is the average you have written out above.