\chapter{Linear function}
\section{Defined on $\mdr$}
\begin{theorem}[Solution formula for linear functions on $\mdr$]
    Let $f: \mdr \rightarrow \mdr $ be a linear function 
    $f(x) := m \cdot x + t$ with $m \in \mdr \setminus \Set{0}$ and 
    $t \in \mdr$ be a linear function.

    Then the points $(x, f(x))$ with minimal distance are given by:
    \[x = \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )\]
\end{theorem}

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north east,
            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= 0, % 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] {0.5*x};
          \addplot[domain=-5:5, thick,samples=50, blue, dashed] {-2*x+6};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
          \addplot[blue, nodes near coords=$f_\bot$,every node near coord/.style={anchor=225}] coordinates {(1.5, 3)};
          \addplot[red, nodes near coords=$f$,every node near coord/.style={anchor=225}] coordinates {(0.9, 0.5)};
          \addlegendentry{$f(x)=\frac{1}{2}x$}
          \addlegendentry{$f_\bot(x)=-2x+6$}
        \end{axis} 
    \end{tikzpicture}
    \caption{The shortest distance of $P$ to $f$ can be calculated by using the perpendicular}
    \label{fig:linear-min-distance}
\end{figure}

\begin{proof}
    With Theorem~\ref{thm:fermats-theorem} you get:
    \begin{align}
        0 &\stackrel{!}{=} (d_{P,f}(x)^2)'\\
        &= 2(x-x_P) + 2 (f(x) - y_P)f'(x)\\
        \Leftrightarrow 0 &\stackrel{!}{=} x - x_P + (f(x) - y_P) f'(x)\\
        &= x- x_P + (mx+t - y_P)\cdot m\\
        &= x (m+1) + m(t-y_P) - x_P\\
        \Leftrightarrow x &\stackrel{!}{=} \frac{x_p - m(t-y_p)}{m^2+1}\\
        &= \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )\label{eq:solution-linear-r}
    \end{align}
    It is obvious that a minium has to exist, the $x$ from Equation~\ref{eq:solution-linear-r}
    has to be this minimum.
\end{proof}
\clearpage

\section{Defined on a closed interval $[a,b] \subseteq \mdr$}
Let $f:[a,b] \rightarrow \mdr$, $f(x) := m\cdot x + t$ with $a,b,m,t \in \mdr$ and 
$a \leq b$, $m \neq 0$  be a linear function.

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north east,
            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= 0, % 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= 2:3, thick,samples=50, red] {0.5*x};
          \addplot[domain=-5:5, thick,samples=50, blue, dashed] {-2*x+6};
          \addplot[domain=1:1.5, thick, samples=50, orange] {3*x-3};
          \addplot[domain=4:5, thick, samples=50, green] {-x+5};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:1.5,1.5);
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:4,1);
          \addlegendentry{$f(x)=\frac{1}{2}x, D = [2,3]$}
          \addlegendentry{$f_\bot(x)=-2x+6, D=[-5,5]$}
          \addlegendentry{$h(x)=3x-3, D=[1,1.5]$}
          \addlegendentry{$h(x)=-x+5, D=[4,5]$}
        \end{axis} 
    \end{tikzpicture}
    \caption{Different situations when you have linear functions which
             are defined on a closed intervall}
    \label{fig:linear-min-distance-closed-intervall}
\end{figure}

The point with minimum distance can be found by:
\[\underset{x\in[a,b]}{\arg \min d_{P,f}(x)} = \begin{cases}
 S_1(f, P) &\text{if } S_1(f, P) \cap [a,b] \neq \emptyset\\
   \Set{a} &\text{if } S_1(f, P) \ni x < a\\
   \Set{b} &\text{if } S_1(f, P) \ni x > b
    \end{cases}\]

\todo[inline]{argument? proof?}
