Construct a continuous functions $f:\mathbb{R}\to \mathbb{R}$ such that $0\leq f(x)\leq 1$ for all $x\in \mathbb{R}$, Let $\mathcal{C}$ be a closed, bounded subset of the real line and $\mathcal{U}$ an open set containing $\mathcal{C}$. Construct a continuous functions $f:\mathbb{R}\to \mathbb{R}$ such that


*

*$0\leq f(x)\leq 1$ for all $x\in \mathbb{R}$,

*$f(x)=1$ if and only if $x\in \mathcal{C}$, and

*$f(x)=0$ for all $x\not \in \mathcal{U}$.

 A: Just use the distance function from $x$.  For every point $x\in U\setminus C$ let $f(x)=\frac{d_{\bar U}}{d_{\bar U}+d_C}$ where $d_C$ is the distance to $C$ and $d_{\bar U}$ is the distance to the complement of $U$.
A: The function $d(x,C)$ is continuous on $\mathbb R.$ Because $C$ is compact, $\mathbb R \setminus U$ is closed, and they are disjoint, there exists $a>0$ such that $d(x,y)>a$ for all $x\in C,y\in \mathbb R \setminus U.$ Define
$$g(x) = \begin{cases} 1-x/a & 0\le x \le a \\ 
0 & x>a \end{cases}$$
Then $g$ is continuous. The function $g(d(x,C)))$ then does the job.
A: $\mathcal{C} \subset \mathcal{U} \subset \mathbb{R}$ So $\mathbb{R}$ is then the union of three disjoint sets (1)  $\mathbb{R} \setminus \mathcal{U}$ (2) $\mathcal{U} \setminus \mathcal{C}$ (3) $\mathcal{C}$
You already have the function defined on two of them, i.e. $f(x) = 1$ for $x \in \mathcal{C}$ and $f(x) = 0 $ for $x \in \mathbb{R} \setminus \mathcal{U}$, and $f$ being constant is continuous on these two sets. So it only remains to define $f$ so that it is continuous within $\mathcal{U} \setminus \mathcal{C}$ , $\ne 1$ on this set, and continuous at all boundaries between the three disjoint sets. 
The set $\mathcal{U} \setminus \mathcal{C}$ is an open set (being the complement of a closed set in an open set) and is therefore the (possibly infinite) union of open intervals. Let $I = (a, b) $ be any such open interval and pick $c$ with $a \lt c \lt b$. The limit points $a, b$ are either in $\mathbb{R} \setminus \mathcal{U}$ or $\mathcal{C}$ so $f(a), f(b)$ are defined and we can define $f$ on $I$ by 


*

*$f(x) = (c - x)f(a)/(c- a)$ for $x \in (a, c]$

*$f(x) = (x - c)f(b)/(b- c)$ for $x \in (c, b)$


You can observe that $f$ is continuous in $I$ and at its boundary points $a, b$, and $f(x) \ne 1 $ on $I$. Note that there are no boundaries between $\mathcal{C}$ and $\mathbb{R} \setminus \mathcal{U}$ so $f$ is continuous everywhere.
