If $f$ is convex and concave on interval $I$, then $f$ is affine on $I$ 
Prove that if $f$ is convex and concave on interval $I$, then $f$ is affine on $I$.

Attempt. Although this topic has been discussed before, i would like to point out some technique issues.
1) Let $I=[a,b]$. Then $x= \frac{b-x}{b-a}\,a+\frac{x-a}{b-a}\,b\in [a,b]$ and since $f$ is simultaneously convex and concave:
$$f(x)= \frac{b-x}{b-a}\,f(a)+\frac{x-a}{b-a}\,f(b)=\frac{bf(a)-af(b)}{b-a}+\frac{f(b)-f(a)}{b-a}\,x.$$
2) Let $I=\mathbb{R}$. Then $g=f-f(0)$ satisfies the Cauchy functional equation $g(x+y)=g(x)+g(y)$ for all $x,~y\in I$: easily $g(0)=0$ so $g\big(\lambda y\big) = \lambda g(y)$ for all $\lambda \in [0,1]$. Therefore for all $x,\,y\in \mathbb{R}$:
$$g(x+y)=g\left(\frac{1}{2}\,2x+\frac{1}{2}\,2y\right)=\frac{1}{2}g(2x)+\frac{1}{2}g(2y)=g\left(\frac{1}{2}\,2x\right)+g\left(\frac{1}{2}\,2y\right)=g(x)+g(y).$$ Since $g$ is convex defined on open interval $\mathbb{R}$, by a classic result, $g$ is continuous and therefore $g$ is linear (continuous solutions of Cauchy equation are the linear functions), so $f$ is affine. 
3) Let $I=(a,b)$. According to $1$, $f$ is locally affine. How can we conclude that $f$ is affine on all $I$? One thought is that $f$ can be continuously extended as $\tilde{f}$ (why?) on $[a,b]$, so  $\tilde{f}$ is affine and so $f$ is affine.
4) Let $I$ be unbounded ($\neq \mathbb{R}$). In that case we cannot work as on $2$. How can we conclude that $f$ is affine on all $I$?
Thanks in advance for the help.
 A: Geometrically, if $f$ is a convex function and $a,b$ are two distinct points within its domain, then the secant line at $a$ and $b$ must:


*

*Stay above the graph of $f$ between $a$ and $b$;

*Stay below the graph of $f$ outside that interval.


Both of these can be deduced from the inequality $f(y) \le \frac{z-y}{z-x} f(x) + \frac{y-x}{z-x} f(z)$ for $x \le y \le z$, applied to $a$, $b$, and a third point in whichever order they come in. But in my opinion, the secant line description gives you more intuition for what, algebraically, will come out of applying that inequality.
Similarly, if $f$ is a concave function and $a,b$ are two distinct points within its domain, then the secant line at $a$ and $b$ must:


*

*Stay below the graph of $f$ between $a$ and $b$;

*Stay above the graph of $f$ outside that interval.


Both of these can only happen if the graph of $f$ coincides with the secant line everywhere, making $f$ affine. In particular, the second bullet point lets us deduce $f$'s behavior outside the interval $[a,b]$. So if we have an open or unbounded interval $I$, we can still win by choosing any two distinct $a,b \in I$.
A: Let $a,b\in I,$ and let $l_1(x)$ be the line through $(a,f(a)), (b,f(b)).$ Suppose $c>b$ and $f(c)>l_1(c).$ Then the line $l_2(x)$ through $(a,f(a)), (c,f(c))$ lies above $l_1$ on $[a,c].$ Thus $f(b)<l_2(b).$ This contradicts the concavity of $f$ on $[a,c].$ Therefore $f(c)\le l_1(c).$ Similar contradiction if $f(c)<l_1(c).$ Therefore $f(c)= l_1(c)$ for all $c>b, c\in I.$ The argument is the same for $c<a.$ Thus $f(c) = l_1(c)$ for all $c\in I$ as desired.
