TITLE: Alternative proof for the fact that a continuous function on a closed interval attains its boundaries.
QUESTION [5 upvotes]: Let $f:[a,b]\to \mathbb{R}$  be a continuous function. We are interested in showing that $\exists  \beta \in [a,b]$, such that $f(\beta) = M$, where M is its upper boundary.
I have managed to proof by myself the fact that any continuous function on a closed interval is bounded so I assume we can use this in our proof. 
I was playing a little with the pencil, doing some test work, and the following idea came to my mind:
I defiend a new function $g:[a,b]\to \mathbb{R}$, $g(x) = \frac{1}{M-f(x)}$. $g$ is obviously continuous on $[a,b]$, and we have $g(x)>0,\forall x \in [a,b]$.
Since $g$ is continouou on a closed interval, $g$ is bounded. Let $M_1$ be its upper boundary. Now we have that:
$$ \frac{1}{M-f(x)} \leq M_1  $$
This actually implies that:
$$f(x) \leq M - \frac{1}{M_1} < M $$
This can't be true! Because if it were true, $f$ wouldn't be bounded by $M$. So, here's what I don't get. I have arrived at a contradiction! But what have I managed to contradict? It seems to me that this contradiction arrived from the fact that $f$ was bounded. So have I proved that f can't be bounded? But this doesn't make any sense, because we know for sure that $f$ is bounded, since its continuous on a closed interval.
I feel that I am very close to prove the claim. Could you please help me realise what am I doing wrong here?

REPLY [5 votes]: You started your proof by saying "$g$ is obviously continuous", but this is not true in general.
However, it does follow if you assume that $f$ does not attain the value $M$.  So if you begin your proof by saying:

Assume $f$ does not attain the value $M$.  Let $g(x) = \frac{1}{M-f(x)}$.  Now $g$ is continuous...

You now have a valid argument.  Since it leads to a contradiction, you may conclude that the initial assumption that $f$ does not attain the value $M$ must have been false.  So you have proved that $f$ does attain the value $M$, as desired.