Let $C \subseteq_{\text {closed}} \mathbb R^n.$ Then there exists a smooth function $f : \mathbb R^n \longrightarrow \mathbb R$ such that
$(1)$ $0 \leq f(x) \leq 1$ for all $x \in \mathbb R^n.$
$(2)$ $f^{-1} (\{0\}) = C.$
Our instructor sketched a proof of the above in his lecture notes along the following lines $:$
Find an increasing sequence $\{K_m \}_{m \geq 1}$ of compact sets such that $\mathbb R^n \setminus C = \bigcup\limits_{m=1}^{\infty} K_m.$ For each $m \geq 1,$ pick compactly supported smooth functions $f_m$ on $\mathbb R^n$ such that
$(1)$ $0 \leq f_m (x) \leq 1$ for all $x \in \mathbb R^n.$
$(2)$ $f_m (x) = 1$ for all $x \in K_m.$
$(3)$ $\text {supp}\ (f_m) \subseteq \mathbb R^n \setminus C.$
Consider the function $f : \mathbb R^n \longrightarrow \mathbb R$ defined by $$f(x) = \sum\limits_{m = 1}^{\infty} \frac {1} {2^m} \frac {f_m (x)} {1 + \|f_m\|_{C^m (\mathbb R^n)}}$$ for all $x \in \mathbb R^n.$ Then it is clear that $f^{-1} (\{0\}) = C$ and $f$ can be shown to be smooth as well. $\blacksquare$
Question $:$ Why is $f$ smooth?
$f$ can be easily seen to be a uniform limit of smooth functions on $\mathbb R^n$ by Weierstrass $M$-test. But does it guarantee that $f$ is smooth?
Any help would be greatly appreciated. Thanks for investing time on my question.
Without working it out in full generality and leaving some details behind, here is my idea for this. You want to show that all mixed partials of $f$ exist. Let's look at just the first partial: $\partial_1f$. We have that $$\partial_1f(y_1,\dots,y_n)=\lim_{h\to0}\frac{1}{h}\left[\sum_{m\in\mathbb{N}}\frac{1}{2^m}\frac{f_m(y_1+h,y_2,\dots,y_n)-f_m(y_1,\dots,y_n)}{1+\|f_m\|_{C^m(\mathbb{R}^n)}}\right].$$ This in turn equals $$\lim_{h\to0}\lim_{k\to\infty}\sum_{m=1}^k\frac{1}{h}\frac{1}{2^m}\frac{f_m(y_1+h,y_2,\dots,y_n)-f_m(y_1,\dots,y_n)}{1+\|f_m\|_{C^m(\mathbb{R}^n)}}.$$ We have that $$\sum_{m=1}^k\frac{1}{h}\frac{1}{2^m}\frac{f_m(y_1+h,y_2,\dots,y_n)-f_m(y_1,\dots,y_n)}{1+\|f_m\|_{C^m(\mathbb{R}^n)}}$$ converges uniformly in $k$, as mentioned. Now consider the $h\to0$ limit. I believe we can say that this limit converges for each large $k$ to some $c_k$. Indeed, rearranging yields $$\sum_{m=1}^k\frac{1}{2^m(1+\|f_m\|_{C^m(\mathbb{R}^n)})}\lim_{h\to0}\frac{f_m(y_1+h,y_2,\dots,y_n)-f_m(y_1,\dots,y_n)}{h}.$$ The partial derivative of each $f_m$ must exist due to smoothness. Now applying some version of the Moore-Osgood theorem should tell us that we can interchange the limits in $h$ and in $k$. Doing so yields $$\partial_1f(y_1,\dots,y_n)=\sum_{m\in\mathbb{N}}\frac{1}{2^m}\frac{\partial_1f_m}{1+\|f_m\|_{C^m(\mathbb{R}^n)}}.$$ This should converge, and a similar but annoyingly long argument would probably work in the general case for mixed partials.