TITLE: Prove or disprove that $f$ is convex if and only if $\frac1{b-a}\int_a^b f(x)\,dx \le \frac{f(a)+f(b)}2 .$
QUESTION [5 upvotes]: Let $f:\mathbb{R} \to \mathbb{R}$ be a continuous function.
Prove or disprove that $f$ is convex if and only if
$$\frac1{b-a}\int_a^b f(x)\,dx \le \frac{f(a)+f(b)}2 $$
for any $a<b \in \mathbb{R}$.

It is easy to prove that $f$ is convex implies the inequality
$$\frac1{b-a}\int_a^b f(x)\,dx \le \frac{f(a)+f(b)}2 $$
for any $a<b \in \mathbb{R}$.
Just use the substitution $x=(1-t)a+tb, t\in[0,1]$,
\begin{align*}
\frac1{b-a}\int_a^b f(x)\,dx
  &=\int_0^1 f((1-t)a+tb)\,dt\\
  &\le\int_0^1(1-t)f(a)+tf(b)\,dt\\
  &=\frac{f(a)+f(b)}2.
\end{align*}
What I want to know is that: does the inequality
$$\frac1{b-a}\int_a^b f(x)\,dx \le \frac{f(a)+f(b)}2,\forall a<b \in \mathbb{R} $$
imply that $f$ is convex.
The following is a characterization for convex functions: Characterization convex function.
Any help and hint will welcome!

REPLY [3 votes]: Assume $f$ is not convex. Then there are $x>y$ and $\lambda\in (0,1)$ such that
$$
f(\lambda x + (1-\lambda)y ) > \lambda f(x) + (1-\lambda)f(y).
$$
This inequality will be satisfied if we perturb $\lambda$ a little bit. The idea is now to take $[a,b]$ to be the maximal interval of these points, where this inequality is true (or convexity fails). Then evaluate the integral, to get a contradiction.
Define $g$ by
$$
g(t):=f(t x + (1-t)y ) -( t f(x) + (1-t)f(y) ).
$$
Due to continuity,  $g(t)>0$ in a neighborhood of $\lambda$. In addition, $g(0)=g(1)=0$.
Define
$$
A:=\{s\le\lambda: \ g(t)>0 \quad \forall t\in [s,\lambda]\}
$$
and
$$
B:=\{s\ge \lambda: \ g(t)>0\quad \forall t\in [\lambda,s]\}.
$$
Set $s_a:=\inf A$, $s_b:=\sup B$. Then $0\le s_a<\lambda<s_b\le 1$ and $g(s_a)=0=g(s_b)$.
Set $a:=s_a x+ (1-s_a)y$, $b:=s_b x+ (1-s_b)y$, which implies $b-a = (s_b-s_a)(x-y)>0$.
Then
$$\begin{split}
\int_a^b f(x)dx &= (x-y) \int_{s_a}^{s_b} f(sx+(1-s)y)ds \\
&> (x-y) \int_{s_a}^{s_b} sf(x)+(1-s)f(y)ds\\
&= \frac12(x-y)[ (s_b^2-s_a^2)f(x) + (2s_b-2s_a - (s_b^2-s_a^2)) f(y)]\\
&= \frac12(x-y)(s_b-s_a)( (s_b+s_a)f(x) + (2-s_b-s_a)f(y))\\
&= \frac{b-a}2 (f(a) + f(b)),
\end{split}
$$
which contradicts the assumption.