Differentiable function whose level set is non-convex, compact, simply connected Is it possible to construct a differentiable function $f: \mathbb R^n \to \mathbb R$ such that the zero level set
$$\mathcal S = \{x \in \mathbb R^n : f(x) = 0\}$$
is non-convex, compact, simply connected, and non-empty? If so, how might we write an explicit formula for $f(x)$ for all $x \in \mathbb R^n$? Is the aforementioned compactness essential to such a construction? 
 A: For $n = 1$ it is impossible because any connected subset of $\mathbb R$  is an interval.
For $n \ge 2$ let us first give non-compact examples.
Let $q : \mathbb R  \to \mathbb R, q(t) = e^t$ and $g : \mathbb R^n  \to \mathbb R, g(x) = x_1^2 + \dots + x_n^2 - 1$. These are $C^\infty$-functions. Now define
$$f = g \circ (id_{\mathbb R^{n-1}} \times q) .$$
$d = id_{\mathbb R^{n-1}} \times q$ establishes a diffeomorphism between $\mathbb R ^n$ and $\mathbb R^{n-1} \times (0,\infty)$. We have $g^{-1}(0) = S^{n-1}$ so that $S = g^{-1}(0) \cap \left( R^{n-1} \times (0,\infty) \right)$ is an open hemisphere which is simply connected. It is now easy to see that $f^{-1}(0) = d^{-1}(S)$ is not convex, but simply connected.
For $n \ge 3 $ compact examples are given by $g$ since $g^{-1}(0) = S^{n-1}$. To cover also $n = 2$, we proceed as follows.
The function $r : \mathbb R \to \mathbb R, r(t) = e^{-1/t^2}$ for $t > 0$ and $r(t) = 0$ for $t \le 0$  is $C^\infty$, thus so is $h : \mathbb R^n \to \mathbb R, h(x_1,\dots,x_n) = r(x_n)$. Now define
$$f = g^2 + h .$$
Since $g^2$ and $h$ are non-negative, $x$ is a zero of $f$ iff $g(x)^2 = 0$ and $h(x) = 0$. This means that $x \in S^{n-1}$ and $x_n \le 0$, i.e. $f^{-1}(0)$ is a closed hemisphere of $S^{n-1}$ which is simply connected and not convex.
