# There exists a continuous function that satisfies this property

Let $X$ be a non-compact subset of $\mathbb{R}$.

I want to show that there a continuous function $f: X \to \mathbb{R}$ such that $f$ is bounded but does not attain its bounds.

I think that there must be an example that suffices, but I cannot find it.

-
I deleted my answer which gave the example $f : (0, 1) \to \mathbb{R}$, $f(x) = x$. As the OP pointed out in a comment, they want, for any choice of non-compact subset $X$, a function $f_X$ with the desired properties. – Michael Albanese Apr 20 '13 at 15:59

Let $X$ be an arbitrary (non-empty) non-compact subset of $\mathbb R$.
If $X$ is not bounded (neither from above nor from below), then $f(x)=\arctan x$ is an example (as $\sup_{x\in X} f(x)=\frac\pi2$ and $\inf_{x\in X} f(x)=-\frac\pi2$). Other sigmoidal curves can be used for the same purpose, for example $f(x)=\frac{x}{\sqrt{1+x^2}}$.
If on the other hand $X$ is bounded, $X$ is not closed, that is there exists a point $a\notin X$ but a sequence $x_n$ in $X$ such that $x_n\to a$. Then $f(x)=\max\{(x-a)^2,-1\}$ is bounded by $-1$ and $0$ and does not attain its upper bound $0$. In order to find bounded $f$ such that neither the upper nor the lower bound is attained, one needs (for the general example) a bit more work: Define $f(x_n)=(-1)^n\cdot(1-\frac1n)$ and interpolate. More precisely, we may assume without loss of generality that the sequence $(x_n)$ has no duplicates. Then let $y_n=(-1)^n\cdot(1-\frac1n)$ and set $$f(x) = \begin{cases} 0&\text{if }x<a\text{ and }\forall i\colon x_i>a,\\ 0&\text{if }x>a\text{ and }\forall i\colon x_i<a,\\ y_k&\text{if }x\le x_k=\min_{i\in\mathbb N}\{x_i\}<a,\\ y_k&\text{if }x\ge x_k=\max_{i\in\mathbb N}\{x_i\}>a,\\ \frac{(x-x_k)y_m+(x_m-x)y_k}{x_m-x_k}&\text{if }x_k=\max\{x_i\mid x_i\le x\}, x_m=\min\{x_i\mid x_i> x\}. \end{cases}$$ (Note that min and max exist because $a$ is the only limit point of $(x_n)$). To see that $f$ is continuous, note that the only interesting points are the $x_i$ and all partial formulas agree to result in $x_i$ as $x\to x_i$.
Finally, if $X$ is bounded only from one side (i.e. only from above or only from below), say $x<M$ for all $x\in X$, then we may replace $X$ with its homeomorphic image $\{\frac1{M+1-x}\mid x\in X\}$, which is bounded from both above and below.