\chapter{Constant functions}
\section{Defined on $\mdr$}
\begin{lemma}
Let $f:\mdr \rightarrow \mdr$, $f(x) := c$ with $c \in \mdr$ be a constant function. 

Then $(x_P, f(x_P))$ is the only point on the graph of $f$ with 
minimal distance to $P$.
\end{lemma}

The situation can be seen in Figure~\ref{fig:constant-min-distance}.
\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north west,
            legend cell align=left,
            axis x line=middle,
            axis y line=middle,
            grid = major,
            width=0.8\linewidth,
            height=8cm,
            grid style={dashed, gray!30},
            xmin=-5, % start the diagram at this x-coordinate
            xmax= 5, % end   the diagram at this x-coordinate
            ymin= 0, % start the diagram at this y-coordinate
            ymax= 3, % end   the diagram at this y-coordinate
            axis background/.style={fill=white},
            xlabel=$x$,
            ylabel=$y$,
            tick align=outside,
            minor tick num=-3,
            enlargelimits=true,
            tension=0.08]
          \addplot[domain=-5:5, thick,samples=50, red] {1};
          \addplot[domain=-5:5, thick,samples=50, green] {2};
          \addplot[domain=-5:5, thick,samples=50, blue, densely dotted] {3};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
          \addplot[blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 3)};
          \addplot[green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates {(2, 2)};
          \addplot[red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 1)};
          \draw[thick, dashed] (axis cs:2,0) -- (axis cs:2,3);
          \addlegendentry{$f(x)=1$}
          \addlegendentry{$g(x)=2$}
          \addlegendentry{$h(x)=3$}
        \end{axis} 
    \end{tikzpicture}
    \caption{Three constant functions and their points with minimal distance}
    \label{fig:constant-min-distance}
\end{figure}

\begin{proof}
The point $(x, f(x))$ with minimal distance can be calculated directly:
\begin{align}
    d_{P,f}(x) &= \sqrt{(x - x_P)^2 + (f(x) - y_P)^2}\\
               &= \sqrt{(x^2 - 2x_P x + x_P^2) + (c^2 - 2 c y_P + y_P^2)} \\
               &= \sqrt{x^2 - 2 x_P x + (x_P^2 + c^2 - 2 c y_P + y_P^2)}\label{eq:constant-function-distance}\\
 \xRightarrow{\text{Theorem}~\ref{thm:fermats-theorem}} 0 &\stackrel{!}{=} (d_{P,f}(x)^2)'\\
              &= 2x - 2x_P\\
  \Leftrightarrow x &\stackrel{!}{=} x_P
\end{align}

So $(x_P,f(x_P))$ is the only point with minimal distance to $P$. $\qed$
\end{proof}

This result means:

\[S_0(f, P) = \Set{x_P} \text{ with } P = (x_P, y_P)\]
\clearpage

\section{Defined on a closed interval $[a,b] \subseteq \mdr$}
\begin{theorem}[Solution formula for constant functions]
Let $f:[a,b] \rightarrow \mdr$, $f(x) := c$ with $a,b,c \in \mdr$ and 
$a \leq b$ be a constant function. 

Then the point $(x, f(x))$ of $f$ with minimal distance to $P$ is
given by:
\[\underset{x\in [a,b]}{\arg \min d_{P,f}(x)} = \begin{cases}
 S_0(f,P) &\text{if } S_0(f,P) \cap [a,b] \neq \emptyset \\
  \Set{a} &\text{if } S_0(f,P) \ni x_P < a\\
  \Set{b} &\text{if } S_0(f,P) \ni x_P > b
    \end{cases}\]
\end{theorem}

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north west,
            legend cell align=left,
            axis x line=middle,
            axis y line=middle,
            grid = major,
            width=0.8\linewidth,
            height=8cm,
            grid style={dashed, gray!30},
            xmin=-5, % start the diagram at this x-coordinate
            xmax= 5, % end   the diagram at this x-coordinate
            ymin= 0, % start the diagram at this y-coordinate
            ymax= 3, % end   the diagram at this y-coordinate
            axis background/.style={fill=white},
            xlabel=$x$,
            ylabel=$y$,
            tick align=outside,
            minor tick num=-3,
            enlargelimits=true,
            tension=0.08]
          \addplot[domain=-5:-2, thick,samples=50, red] {1};
          \addplot[domain=-1:3, thick,samples=50, green] {1.5};
          \addplot[domain=3:5, thick,samples=50, blue, densely dotted] {3};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};

          \addplot[blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(3, 3)};
          \addplot[green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates {(2, 1.5)};
          \addplot[red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(-2, 1)};

          \draw[thick, dashed] (axis cs:2,1.5) -- (axis cs:2,2);
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:-2,1);
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:3,3);
          \addlegendentry{$f(x)=1, D = [-5,-2]$}
          \addlegendentry{$g(x)=1.5, D = [-1,3]$}
          \addlegendentry{$h(x)=3, D = [3,5]$}
        \end{axis} 
    \end{tikzpicture}
    \caption{Three constant functions and their points with minimal distance}
    \label{fig:constant-min-distance-closed-intervall}
\end{figure}

\begin{proof}
\begin{align}
    \underset{x\in[a,b]}{\arg \min d_{P,f}(x)} &= \underset{x\in[a,b]}{\arg \min d_{P,f}(x)^2}\\
   &=\underset{x\in[a,b]}{\arg \min} \big ((x-x_P)^2 + \overbrace{(y_P^2 - 2 y_P c + c^2)}^{\text{constant}} \big )\\
   &=\underset{x\in[a,b]}{\arg \min} (x-x_P)^2
\end{align}

which is optimal for $x = x_P$, but if $x_P \notin [a,b]$, you want
to make this term as small as possible. It gets as small as possible when
$x$ is as similar to $x_p$ as possible. This yields directly to the
solution formula.$\qed$
\end{proof}
