Continuous extension of $f$ from $E$ to $\mathbb{R}$ If f is a real continuous function defined on a closed set $E \subset \mathbb{R}$ , prove that there exist continuous real function g on $\mathbb{R}$ such that $g(x)=f(x)$ for all $x\in E$.
 A: The idea here is straightforward, but the proof (or at least, my proof) laborious.
First a simplification: 
Assume $E $ is unbounded above and below. If $B < x$ for all $x \in E$, then replace $E$ by $ E \cup (-\infty, B]$ and extend $f$ to be zero on this extension. Similarly if $E$ is bounded above. It is clear that the replacement $E$ is closed and that the extended $f$ is continuous on the replacement $E$.
The following are key to the proof:
Let $l(x) = \max \{ y | y \le x, y \in E \}$ and $r(x) = \min \{ y | y \ge x, y \in E \}$.
Then $l(x) = x$ iff $x \in E$ since $E$ is closed, and similarly for $r$. We have $l(x) \le x \le r(x)$. Furthermore, $l(x),r(x) \in E$ for all $x$.
It is straightforward to check that $l$ is continuous from the right and that $r$ is continuous from the left. Both functions are non-decreasing.
Note that if $x \notin E$, then some small ball around $x$ is also contained in $E^c$, and $l,r$ will be constant on this neighbourhood.
Define $\tilde{f}(x) = \begin{cases} f(x), & x \in E \\
f(l(x)) + { x-l(x) \over r(x)-l(x)} (f(r(x))-f(l(x))), & \text{otherwise}\end{cases}$.
It should be clear that $\tilde{f}$ is continuous on $E^c$ (since it is open and $l,r$ are constant on small enough neighbourhoods of $E^c$), and we have $\tilde{f}(x) = f(x)$ for $x \in E$.
Note that for $x \notin E$, $0 \le { x-l(x) \over r(x)-l(x)} \le 1$, in particular it is bounded.
Now suppose $x \in E$. First we will show that $\tilde{f}$ is continuous from the right, so suppose $x_n \downarrow x$. We need to show that $\tilde{f}(x_n) \to \tilde{f}(x)$.
If $x_n \in E$, then $\tilde{f}(x_n) = f(x_n) = f(l(x_n))$, and so
$|\tilde{f}(x)-\tilde{f}(x_n)| = |\tilde{f}(l(x))-\tilde{f}(l(x_n))| $.
If $x_n \notin E$, then we need two estimates depending on the value of $\underline{r} =\lim_n r(x_n)$.
First suppose $\underline{r} = r(x)$. Then
\begin{eqnarray}
|\tilde{f}(x)-\tilde{f}(x_n)| &=& |\tilde{f}(l(x))-\tilde{f}(l(x_n))-\left( { x_n-l(x_n) \over r(x_n)-l(x_n)} (f(r(x_n))-f(l(x_n)) \right) | \\
&\le& |\tilde{f}(l(x))-\tilde{f}(l(x_n))| + |f(r(x_n))-f(l(x_n) | \\
&=& |f(l(x))-f(l(x_n)| + |f(r(x_n))-f(l(x_n) |
\end{eqnarray}
So we see that the latter equation actually holds for all $x_n$. Since $l$ are is continuous from the right, and $f$ is continuous on $E$ (and $l,r$ map into $E$), we see that
$\tilde{f}(x_n) \to \tilde{f}(x)$.
Now suppose $\underline{r} > r(x)$. Note that $f(r(x_n))-f(l(x_n))$ is bounded by say $K$. Also, $l(x_n) \to l(x) \le r(x) < \underline{r}$. In particular, for large enough $n$, we have $r(x_n)-l(x_n) \ge \delta > 0$ for some $\delta$. Then:
\begin{eqnarray}
|\tilde{f}(x)-\tilde{f}(x_n)| &=& |\tilde{f}(l(x))-\tilde{f}(l(x_n))-\left( { x_n-l(x_n) \over r(x_n)-l(x_n)} (f(r(x_n))-f(l(x_n)) \right) | \\
&\le& |\tilde{f}(l(x))-\tilde{f}(l(x_n))| + K\left|{ x_n-l(x_n) \over r(x_n)-l(x_n)} \right| \\
&\le& |f(l(x))-f(l(x_n)| + {K \over \delta} |x_n-l(x_n) |
\end{eqnarray}
Again, we see that the latter equation actually holds for all $x_n$.
Since $l(x) = x \le l(x_n) \le x_n$, we see that
$\tilde{f}(x_n) \to \tilde{f}(x)$.
The same argument, mutatis mutandis, applies to the $x_n \uparrow x$ case.
A: For the special case $E \subset \mathbb{R}$, we don't need the cavalry (Tietze's theorem), we can construct a continuous extension almost explicitly.
Let $a = \inf E$, and $b = \sup E$. If $a = -\infty$, let $E_1 = E$ and $f_1 = f$, else let $E_1 = E \cup (-\infty, a-1]$ and set $f_1 = f$ on $E$, $f_1 = 0$ on $(-\infty,a-1]$. If $b = +\infty$, let $E_2 = E_1$ and $f_2 = f_1$, else let $E_2 = E_1 \cup [b+1,+\infty)$ and $f_2 = f_1$ on $E_1$, $f_2 = 0$ on $[b+1,\infty)$.
Then $E_2$ is closed, $f_2$ continuous on $E_2$, and $f_2 \lvert_E = f$, so any continuous extension $g$ of $f_2$ will do.
For $x \notin E_2$, let $\alpha(x) = \max \{ y \in E_2 : y < x\}$ and $\beta(x) = \min \{ y\in E_2 : y > x\}$. Then $\alpha(x), \beta(x) \in E_2$, and $(\alpha(x),\beta(x))\cap E_2 = \varnothing$. Interpolate linearly, set
$$g(x) = \frac{x-\alpha(x)}{\beta(x)-\alpha(x)} f_2(\beta(x)) + \frac{\beta(x)-x}{\beta(x)-\alpha(x)} f_2(\alpha(x)).$$
For $x\in E_2$, set $g(x) = f_2(x)$. Verify that $g$ is continuous in all $x \in \mathbb{R}$.
If $x \notin E_2$, $g$ is (affine) linear in a neighbourhood of $x$, hence continuous in $x$. If $x\in E_2$, let us consider right and left continuity separately. If every interval $(x-\eta,x)$ contains points of $E_2$, then for a given $\varepsilon > 0$, choose $\delta$ by the continuity of $f_2$ such that $\lvert f_2(y) - f_2(x)\rvert < \varepsilon$ for all $y \in E_2$ with $x-\delta \leqslant y < x$. Pick any $y \in E_2 \cap [x-\delta,x)$, then we have $\lvert g(z) - g(x)\rvert < \varepsilon$ for all $z \in [y,x]$, since on the complement of $E_2$ in that interval, $g$ is obtained by linear interpolation of values in $(g(x)-\varepsilon, g(x)+\varepsilon)$. Hence $g$ is left-continuous in $x$. If there is an $\eta > 0$ such that $(x-\eta,x)\cap E_2 = \varnothing$, let $y = \max \{ z \in E_2 : z < x\}$. On $[y,x]$, $g$ is (affine) linear, hence $g$ is left-continuous in $x$. The right-continuity in $x$ is seen in the same manner.
A: This is true (Tietze extension theorem) in the more general setting of normal topological spaces, as mentioned in other answers. 
On $\mathbb R$, though, the idea is simple: A set $E$ is closed iff its complement is the disjoint union of open intervals. Say one of the intervals is $(a,b)$. Then $a,b\in E$, and we define the extension $g$ on $(a,b)$ as the "line segment" that connects $(a,g(a))$ with $(b,g(b))$. If the interval is unbounded, say $(a,\infty)$, define $g$ on $(a,\infty)$ to be constantly equal to $f(a)$. 
Verifying that this is continuous amounts to proving continuity at points of $E$ that are limit of other points of $E$, and in that case this follows from the assumption that $f$ is continuous: The point is that if $f(a)$ and $f(b)$ are both close to $f(c)$, then any point between $f(a)$ and $f(b)$ is close as well. 
A: This is consequence of the Tietze Extension Theorem.
