# $X$ compact metric space, $f:X\rightarrow\mathbb{R}$ continuous attains max/min

Let $X$ be a compact metric space, show that a continuous function $f:X\rightarrow\mathbb{R}$ attains a maximum and a minimum value on $X$.

Attempt: So the important thing is that I have previously shown that such a function is bounded and that for compact $X$, $f(X)$ is compact given $f$ continuous. In $\mathbb{R}$, compact $\implies$ closed and bounded. So $f(X)$ is closed and contains its accumulation points, and it is bounded so $\exists \sup(A),\inf(A)$ and since closed $\implies \sup(A)\in A, \inf(A)\in A$.

Did I miss anything/make an unwarranted leap of logic?

-
The ideas are all there, and they’re connected properly, but it’s not really a well-written proof as it stands. – Brian M. Scott Feb 15 '12 at 7:24
By the way, $\sup$ and $\inf$ are predefined: use \sup, \inf. – Brian M. Scott Feb 15 '12 at 7:25
It is better to avoid $\exists$ and $\implies$ symbols when writing math. Just write "there exist" and "then". After all, you write math in english not in other strange symbolic language. – leo Jun 9 '12 at 3:15

Here’s an example of how the same argument could be written up nicely.

Since $X$ is compact and $f$ is continuous, $f[X]$ is a compact subset of $\mathbb{R}$ and therefore closed and bounded. Since $f[X]$ is bounded, it has both a supremum and an infimum, and since it is closed, $\sup f[X]\in f[X]$ and $\inf f[X]\in f[X]$. Thus, there are $x_0,x_1\in X$ such that $f(x_0)=\inf f[X]$ and $f(x_1)=\sup f[X]$; that is, $f$ attains its minimum and maximum values at $x_0$ and $x_1$, respectively.

-
+1 Textbook proof. – user38268 Feb 15 '12 at 9:44
Nice proof, how does this generalize to X is an arbitrary topological space? will the same proof hold? – grayQuant Oct 19 '15 at 3:35
@grayQuant: The proof actually used only the compactness of $X$; the hypothesis that $X$ is metric isn’t needed. If $X$ is not compact, however, there may be continuous real-valued functions on $X$ that do not attain at least one extremum. – Brian M. Scott Oct 19 '15 at 3:39

Your argument is fundamentally sound, but you have to assume that your metric space is nonempty. Here is a direct proof that requires no other results (the proof generalizes, like yours, to arbitrary topological spaces):

Let $X$ be a nonempty metric space and $f:X\to\mathbb{R}$ a continuous function without a maximum. Then $X$ has an open cover without a finite subcover.

Proof:

1. Suppose $f(X)$ is unbounded. Then $\big\{f^{-1}\big((-\infty, n)\big):n\in\mathbb{N}\big\}$ is an open cover without finite subcover.

2. Suppose $f(X)$ is bounded, with supremum $s$. Since $f$ has no maximum, $s\notin f(X)$ and $\big\{f^{-1}\big((-\infty, s-1/n)\big):n\in\mathbb{N}\big\}$ is an open cover without finite subcover.

-