Continuous function with local maxima everywhere but no global maxima

Can there be such a function:

$f \colon \mathbb R \to \mathbb R$ is continuous and non-constant. It has a local maxima everywhere, i.e., for all $x \in \mathbb R$ there is some $\delta_x>0$ such that $f(x)\geq f(y)$ for all $y \in B(x,\delta_x)$. And, yet $f$ has no global maxima?

Thank you.

PS: $\mathbb R$ is with the usual topology. This is true for $\mathbb R$ with upper-limit topology.

Solutions Collecting From Web of "Continuous function with local maxima everywhere but no global maxima"

No such function exists. If $f$ is continuous and has a local maximum everywhere, then $f$ is constant. To see this, let $a$ be a real number. By continuity, $\{x:f(x)\leq f(a)\}$ is closed, and by the hypothesis on local maxima, $\{x:f(x)\leq f(a)\}$ is open. The set is nonempty because it contains $a$, so it is all of $\mathbb{R}$ by connectedness. Therefore, for all $a$ and $b$ in $\mathbb{R}$, $f(b)\leq f(a)$ and similarly $f(a)\leq f(b)$.

Surprisingly enough, there are a few papers on this (like here). So other people have considered variations of this problem.

I think that you should also be familiar with a related but incredibly interesting fact: it is possible to have a continuous function that has a local maxima/minima at every rational number. That’s a dense subset, which is astounding enough as it is. (One should note that having a countable number of local maxima is all one can ask for. To see this, note that around every maxima one can assign an interval over which it is the maximum, from the definition of a local maximum. But there is a rational number in this interval, and so there can be at most countably many).

One such function is the Weierstrass function. It seems not so hard to alter this so that it has countably many maxima and minima, but no global maxima or minima.

To extend the answer Jonas gave, let me note that if $f$ is continuous and has a local minimum or maximum everywhere, then $f$ is constant. See Problem2010-4/B here for a proof (very recently published).

EDIT (elaborating). If $f$ has a local minimum or maximum everywhere, then the range of $f$ is countable (a proof is provided in the above link; see also here). Hence if $f$ is further continuous it must be constant, for otherwise, by the intermediate value theorem, the range of $f$ would be uncountable, a contradiction.