# Test function whose derivative is positive on a given compact set

Let $U \subset \mathbb{R}$ be open and $K \subset U$ compact set with $\mu(K) > 0$ (Lebesgue measure).

Is it true that there always exists a test function $\varphi \in C^\infty_c(U)$ (compactly supported smooth function) whose derivative $\varphi'$ is strictly positive on whole of $K$?

I guess one could mollify some kind of "affine" function, for example if $U = (-2,2)$ and $K = [0,1]$ then mollify $f(x) = (x+1) \chi_{[0,1]}(x)$. Maybe more generally something like $$f(x) = \chi_K(x) \left(1 + \frac{x - \inf K}{\sup K - \inf K} \right)\,.$$

Another idea was to start with defining $\psi: U \to \mathbb{R}$ on $K$ by $\psi(x) = 1$ for all $x \in K$ and then somehow define the values of $\psi$ on $U \setminus K$ in such a way that the integral function of $\psi$ is a test function.

If there exists a counterexample, is there an additional assumption that makes the statement true?

For every $x\in K$ take $\epsilon_x>0$ such that $B_{\epsilon_x}(x)\subset U$, and note that there exist $x_1,\ldots,x_n\in K$ such that $K\subseteq \bigcup_{j=1}^n B_{\epsilon_{x_j}/4}(x_j):= G$. Denote $V:= \bigcup_{j=1}^n B_{\epsilon_{x_j}/2}(x_j)$ and observe $$K\subseteq G\subseteq \overline{G}\subseteq V\subseteq \overline{V}\subseteq U.$$ Now, $\overline{G}$ and $\mathbb{R}\setminus V$ are both finite unions of disjoint closed intervals (and two closed rays, for the latter), so we can build a smooth transition function $g:\mathbb{R}\to\mathbb{R}$ such that $g\upharpoonright_{\overline{G}}\equiv 1$ and $g\upharpoonright_{\mathbb{R}\setminus V}\equiv 0$.
Define $f(t):=t g(t)$, and note that $x\in K\subseteq G$ implies that $g(x)=1$ in a neighborhood of $x$, hence also $g^\prime(x)=0$, and: $$f^\prime(x) = g(x) + t g^\prime(x) = 1$$
Finally, note that $f$ is supported in $\overline{V}\subseteq U$, a compact set (because $g$ is).