Find a Continuous Function with Cantor Set Level Sets
Solution 1:
As the Cantor set is self-similar, it makes sense to try to construct a graph with some kind of self-similar structure. The only way the graph of a function can be strictly self-similar is if it is a line segment. Thus, we explore a slight generalization using a self-affine graph. I'll first present an example that is specifically designed to have the desired property and then comment on a couple of cases where such a structure arises quite naturally.
A rigged example
For our first example, we start with the Iterated Function System of six affine functions defined as follows: $$ \begin{align} f_1(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & 1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) \\ f_2(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & 1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) + \left(\begin{array}{c} 1/6\\1/2\end{array}\right) \\ f_3(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & -1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) + \left(\begin{array}{c} 2/6\\1\end{array}\right) \\ f_4(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & -1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) + \left(\begin{array}{c} 3/6\\1/2\end{array}\right)\\ f_5(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & 1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) + \left(\begin{array}{c} 4/6\\0\end{array}\right)\\ f_6(x,y) &= \left(\begin{array}{cc} 1/6 & 0 \\ 0 & 1/4 \end{array}\right) \left(\begin{array}{c} x\\y\end{array}\right) + \left(\begin{array}{c} 5/6\\1/2\end{array}\right) \end{align} $$
The geometric action of this IFS on the upwardly oriented unit square is illustrated in the following figure:
This image shows the oriented initial set, the first two levels of approximation using rectangles, and a higher order approximation to the attractor, which is the graph of a continuous function. Now, it's pretty easy to see that the intersection with the level $n$ approximation by rectangles with a horizontal line segment is just a collection of line segments and that these contain the corresponding intersection at level $n-1$. The intersection of all these is perfect, non-empty, and no where dense. Thus, it's a Cantor set.
This function maps $[0,1]\to[0,1]$ but it's easy to extend to $\mathbb R$, if desired.
Natural examples
There are some examples of functions with Cantor type level sets that occur naturally - i.e. constructed for some other purpose but happen to have the desired property.
Space-filling curves
The coordinate functions of Hilbert's space filling curve have Cantor type level sets. I proved this in my paper The Hausdorff Dimension of Hilbertʼs Coordinate Functions. I also published a more elementary exposition in The Math Mag. If you understood the previous example, you can see what's going on in figure 7 of that paper:
The Takagi function
Finally, the Takagi function has this property at many points. More specifically, let $\varphi(x)$ denote the distance from $x$ to the nearest integer and let $$\tau(x) = \sum_{k=0}^{\infty} \frac{1}{2^k}\varphi(2^kx).$$ Then, the maximum value of $\tau$ is $2/3$ and $\tau^{-1}(2/3)$ is a self-similar Cantor set with dimension $1/2$. The partial self-similar structure of the Takagi function implies that the level sets are Cantor sets for a dense set of points in $[0,2/3]$.
This an many other fun facts are proved in The Takagi function: a survey. It's not hard at all to see what's going on by examining the even partial sums of the series that generates $\tau$:
The image shows $$\tau(x) = \sum_{k=0}^{n} \frac{1}{2^k}\varphi(2^kx)$$ for the first few even values of $n$. Note that the top is always a set of segments. As we move from $n=2$ to $n=4$, the one segment is replaced by two smaller sub-segments. The partial self-similar structure of the object suggests that this pattern repeats and, in the limit, the top of the object is a Cantor set.
Again referring to the partial self-similar structure, there is a dense set of points in $[0,2/3]$ whose inverse image is a Cantor set. This is formalized in Lemma 3.3 of the linked paper. Specifically, the portion of the graph of $\tau$ above the interval $[k/2^{2m},(k+1)/2^2m]$ is a similar copy of the full graph reduced by the factor $1/2^{2m}$ and shifted up by $\tau(k/2^{2m})$.