How to choose $f\in C^{2}(\mathbb{R})$ with compact support and takes value 1 on connected compact set? Let $0< \delta < \pi$. 
My questions: 

(1) How to construct(choose/method) $f\in C^{2}(\mathbb R)$(= First two derivatives ($f' \ \text{and}  \ f''$) of $f$ on $\mathbb R$ exists and are continuous on $\mathbb R$) which satisfies the  following property:
  $$ f (x)=\begin{cases}
1 & \text{if}  \ x\in [-\pi + \delta, \pi -\delta ]\\
0, & \text {if} \  |x|> \pi.
\end {cases}$$
  (In other-words, we need to choose $f$ with two continuous derivatives and has compact support and takes value 1 on connected compact set. )

(2)How to construct(choose/method) $f\in C^{3}(\mathbb R)$(= First three derivative ($f', f'' \ \text{and}  \ f'''$) of $f$ on $\mathbb R$ exists and are continuous on $\mathbb R$) which satisfies the  following property:
$$ f (x)=\begin{cases}
1 & \text{if}  \ x\in [-\pi + \delta, \pi -\delta ]\\
0, & \text {if} \  |x|> \pi.
\end {cases}$$
(3) Can we expect to generalize this idea; I mean, can we choose $f\in C^{n}(\mathbb R)$ with a above properties ?
Thanks;
 A: The following are standard. Actually you can make $f$ a $C^\infty$ function. Let 
$g_1(x)= e^{-1/x}$
when $x>0$ and is zero when $x\leq 0$. This is a smooth function. Then 
$g_2(x) = g_1(x)g_1(1-x)$ 
is a smooth function with support in $[0,1]$. Let 
$g_3(x) = \int_{-1}^x g_2(s)ds$.
Then $g_3(x)$ is zero when $x<0$ and is constant when $x>1$. Let 
$g_4(x) = g_3(x) g_3(3-x)$. 
Then $g_4(x)$ is zero when $x<0$ and $x>3$, and is constant when $1\leq x\leq 2$. Finally let $f$ to be a suitable translation of $g_4$. If you want some pictures, you can found this in Hirsch's "differential topology". 
The most general statement is the following: let $K_1$, $K_2$ be any disjoint closed sets in $\mathbb R^n$, then there is a smooth function $f:\mathbb R^n \to \mathbb R$ such that 
$$f^{-1}(0) = K_1,\ \ \ \ f^{-1}(1) = K_2$$
A: You can use piecewise polynomials (splines). The only real problem is to decide what function to use in the "ramp" regions $-\pi < x <-\pi+\delta$ and $\pi - \delta < x <\pi$. For the $C^2$ case, let $f$ be a polynomial of degree 5. It will therefore have 6 coefficients, which we can determine from the following six equations 
$$
f(\pi - \delta)  = 1  \quad ; \quad 
f'(\pi - \delta) = 0  \quad ; \quad 
f''(\pi - \delta) = 0
$$
$$
f(\pi)  = 0  \quad ; \quad 
f'(\pi) = 0  \quad ; \quad 
f''(\pi) = 0
$$
If you read about Hermite interpolation (like here, for example), or Bezier curves, you can actually write down an explicit formula for the $f$ satisfying these constraints, rather than solving the system of equations. For example, the Bezier-Bernstein polynomial
$$
f(x) = 10x^3(1-x)^2 + 5x^4(1-x) + x^5
$$
has the properties
$$
f(0)  = 0  \quad ; \quad 
f'(0) = 0  \quad ; \quad 
f''(0) = 0
$$
$$
f(1)  = 1  \quad ; \quad 
f'(1) = 0  \quad ; \quad 
f''(1) = 0
$$
and you can shift/scale it to suit your needs.
To make a piecewise function that's $C^n$, you will need to use polynomials of degree $2n-1$.
