Short proof using continuity and set conclusion I'm new to uni math and in my most recent assignment I got stuck trying to proof the following:  
Let $a,b \in \mathbb{R}$ and $a<b$. Suppose $\space f:[a,b] \rightarrow \mathbb{R}$ be continuous. Assume if $c,d \in \mathbb{R}$ and  $c=d$ then $[c,d] := \{c\}$ or $\{d\}$ .
Show that $f([a,b])=[c,d]$ for some $c,d \in \mathbb{R}$. 
My try-
 Define an interval M so that $$M:=f([a,b])]$$
Claim- $M=[min(f([a,b])),max(f([a,b]))$
Now for every $x \in \mathbb{R} \space \forall \space x \in M$ which means $M \subset \mathbb{R}$. Now assume a $[c,d]$ with $c,d \in \mathbb{R}$ and $[c,d] \subset \mathbb{R}$ so that $M \subset [c,d]$. I'm not sure this works, it doesn't feel right. What am I doing wrong?
And also: how do I now show that $[c,d] \subset M$ is also true, so that $[c,d] = M = f([a,b])$?
 A: Let $I\subset \mathbb R$ a connexe and compact set. 
Let show that $f(I)\subset \mathbb R$ is also compact.
Consider $\mathcal U$ a recovery of open set of $f(I)$. We have that $$f^{-1}\left(\bigcup_{U\in\mathcal U}U\right)=\bigcup_{u\in\mathcal U}f^{-1}(U)$$
By continuity of $f$ and by the compactness of $I$,
$$I\subset \bigcup_{U\in\mathcal U}f^{-1}(U)=\bigcup_{i=1}^n f^{-1}(U_i)$$ with $f^{-1}(U_i)$ are open set of $\mathbb R$.
And so
$$f(I)\subset f\left(\bigcup_{i=1}^n f^{-1}(U_i)\right)=\bigcup_{i=1}^n \underbrace{f(f^{-1}(U_i))}_{\subset U_i}\subset \bigcup_{i=1}^n U_i$$
and so $f(I)$ is compact. 
Let show that $f(I)\subset \mathbb R$ is connexe.
Suppose that $f(I)$ is not connexe. Then there exists open set $U,V\subset f(I)$ such that $U,V\neq f(I)$, $U,V\neq\emptyset$, $U\cup V=f(I)$ and $U\cap V=\emptyset$. Therefore, $$f^{-1}(U)\neq I\text{ and } f^{-1}(V)\neq I,$$ $$f^{-1}(U)\text{ and }f^{-1}(V)\neq \emptyset,$$ $$I=f^{-1}(f(I))=f^{-1}(U\cup V)=f^{-1}(U)\cup f^{-1}(V),$$ 
and
$$\emptyset=f^{-1}(\emptyset)=f^{-1}(U\cap V)=f^{-1}(U)\cap f^{-1}(V).$$
By continuity of $f^{-1}$, we have that $f^{-1}(U)$ and $f^{-1}(V)$ are open and so $$\Big(f^{-1}(U),f^{-1}(V)\Big)$$
is a separation of $I$ which is a contradiction with the fact that $I$ is connexe. Therefore $f(I)$ is connexe. 
Conclusion.
The compact and connexe set of $\mathbb R$ are the interval of the form $[a,b]$, therefore, for $I=[a,b]$, $f([a,b])$ being compact and connexe, there exists $c,d\in\mathbb R$ such that $f([a,b])=[c,d]$ what conclude the proof.
A: First prove that $M$ is well-defined, start with showing that $c:=\inf(f([a,b]))> -\infty$. If $c=-\infty$ then we could take a sequence of $x_n\in[a,b]$ such that $f(x_n)<-n$. The $x_n$ have a convergent subsequence $x_{n_k}\to r$ (for some $r\in[a,b]$) and clearly then $f(x_{n_k})\to -\infty$, but this is a contradiction since $f$ is continuous and $f(x_{n_k})\to f(r)\not= -\infty$. 
Note that $c=f(r)$ and hence the $\inf$ is "achieved", that is $c:=\min(f([a,b]))> -\infty$. In an analogous manner one proves that $d:=\max(f([a,b]))<\infty$, hence $M=[c,d]$ is well-defined. If $c=d$ then $M$ is a single point and we are done, so now assume that $c<d$. 
In short, since $[a,b]$ is connected, and $f$ is continuous, then $f([a,b])$ must also be connected, so $f([a,b])$ cannot be missing any points between $c$ and $d$, hence $f([a,b])=[c,d]$. In case you have not studied connectedness, here is  direct proof. Say there was a gap $g\in[c,d]\setminus f([a,b])$, that is $c<g<d$ and there is no $x\in[a,b]$ with $g=f(x)$. 
Pick $r_0,s_0\in[a,b]$ with $f(r_0)=c$ and $f(s_0)=d$, consider the case when $r_0<s_0$ (the other case is considered in a similar manner). By induction, if $r_n$, $s_n$ were defined with $f(r_n) < g < f(s_n)$ then let $x_n=\frac{r_n+s_n}2$ be the midpoint of $r_n,s_n$. If $f(x_n)<g$ then let $r_{n+1}=x_n$ and $s_{n+1}=s_n$, otherwise let $r_{n+1}=r_n$ and $s_{n+1}=x_n$. 
Then $r_n\le r_{n+1}$ and $s_{n+1}\le s_{n}$ for all $n$ so these two sequences converge, and since the distance between $r_n$ and $s_n$ goes to $0$ they must converge to the same limit, say $L$. Then $f(r_n)\to f(L)\le g$ and $f(s_n)\to f(L)\ge g$ which shows that $f(L)=g$. It follows that $f([a,b])$ cannot miss any points in $[c,d]$, that is $f$ maps $[a,b]$ onto $[c,d]$. 
