math-minimal-distance-to-cubic-function.tex 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. \documentclass[a4paper]{scrartcl}
  2. \usepackage{amssymb, amsmath} % needed for math
  3. \usepackage{mathtools} % \xRightarrow
  4. \usepackage[utf8]{inputenc} % this is needed for umlauts
  5. \usepackage[english]{babel} % this is needed for umlauts
  6. \usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
  7. \usepackage[margin=2.5cm]{geometry} %layout
  8. \usepackage{hyperref} % links im text
  9. \usepackage{braket} % needed for \Set
  10. \usepackage{parskip}
  11. \usepackage[colorinlistoftodos]{todonotes}
  12. \usepackage{pgfplots}
  13. \pgfplotsset{compat=1.7,compat/path replacement=1.5.1}
  14. \usepackage{tikz}
  15. \usepackage[framed,amsmath,thmmarks,hyperref]{ntheorem}
  16. \usepackage{framed}
  17. \usepackage{nicefrac}
  18. \usepackage{siunitx}
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. % Define theorems %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. \theoremstyle{break}
  23. \setlength\theoremindent{0.7cm}
  24. \theoremheaderfont{\kern-0.7cm\normalfont\bfseries}
  25. \theorembodyfont{\normalfont} % nicht mehr kursiv
  26. \def\mdr{\ensuremath{\mathbb{R}}}
  27. \renewcommand{\qed}{\hfill\blacksquare}
  28. \newframedtheorem{theorem}{Theorem}
  29. \newframedtheorem{lemma}[theorem]{Lemma}
  30. \newtheorem{plaindefinition}{Definition}
  31. \newenvironment{definition}{\begin{plaindefinition}}{\end{plaindefinition}}
  32. \newenvironment{definition*}{\begin{plaindefinition*}}{\end{plaindefinition*}}
  33. \newtheorem{example}{Example}
  34. \theoremstyle{nonumberplain}
  35. \newtheorem{proof}{Proof:}
  36. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. \title{Minimal distance to a cubic function}
  38. \author{Martin Thoma}
  39. \hypersetup{
  40. pdfauthor = {Martin Thoma},
  41. pdfkeywords = {},
  42. pdftitle = {Minimal Distance}
  43. }
  44. \def\mdr{\ensuremath{\mathbb{R}}}
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. % Begin document %
  47. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  48. \begin{document}
  49. \maketitle
  50. \begin{abstract}
  51. When you want to develop a selfdriving car, you have to plan which path
  52. it should take. A reasonable choice for the representation of
  53. paths are cubic splines. You also have to be able to calculate
  54. how to steer to get or to remain on a path. A way to do this
  55. is applying the \href{https://en.wikipedia.org/wiki/PID_algorithm}{PID algorithm}.
  56. This algorithm needs to know the signed current error. So you need to
  57. be able to get the minimal distance of a point to a cubic spline combined with the direction (left or right).
  58. As you need to get the signed error (and one steering direction might
  59. be prefered), it is not only necessary to
  60. get the minimal absolute distance, but might also help to get all points
  61. on the spline with minimal distance.
  62. In this paper I want to discuss how to find all points on a cubic
  63. function with minimal distance to a given point.
  64. As other representations of paths might be easier to understand and
  65. to implement, I will also cover the problem of finding the minimal
  66. distance of a point to a polynomial of degree 0, 1 and 2.
  67. \end{abstract}
  68. \section{Description of the Problem}
  69. Let $f: \mdr \rightarrow \mdr$ be a polynomial function and $P \in \mdr^2$
  70. be a point. Let $d_{P,f}: \mdr \rightarrow \mdr_0^+$
  71. be the Euklidean distance of a point $P$ and a point $\left (x, f(x) \right )$
  72. on the graph of $f$:
  73. \[d_{P,f} (x) := \sqrt{(x_P - x)^2 + (y_P - f(x))^2}\]
  74. Now there is finite set $M = \Set{x_1, \dots, x_n}$ of minima for given $f$ and $P$:
  75. \[M = \Set{x \in \mdr | d_{P,f}(x) = \min_{\overline{x} \in \mdr} d_{P,f}(\overline{x})}\]
  76. But minimizing $d_{P,f}$ is the same as minimizing $d_{P,f}^2$:
  77. \begin{align}
  78. d_{P,f}(x)^2 &= \sqrt{(x_P - x)^2 + (y_P - f(x))^2}^2\\
  79. &= x_p^2 - 2x_p x + x^2 + y_p^2 - 2y_p f(x) + f(x)^2
  80. \end{align}
  81. \begin{theorem}[Fermat's theorem about stationary points]\label{thm:required-extremum-property}
  82. Let $x_0$ be a local extremum of a differentiable function $f: \mathbb{R} \rightarrow \mathbb{R}$.
  83. Then: $f'(x_0) = 0$.
  84. \end{theorem}
  85. \clearpage
  86. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  87. % Constant functions %
  88. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  89. \section{Minimal distance to a constant function}
  90. Let $f(x) = c$ with $c \in \mdr$ be a constant function.
  91. \begin{figure}[htp]
  92. \centering
  93. \begin{tikzpicture}
  94. \begin{axis}[
  95. legend pos=north west,
  96. axis x line=middle,
  97. axis y line=middle,
  98. grid = major,
  99. width=0.8\linewidth,
  100. height=8cm,
  101. grid style={dashed, gray!30},
  102. xmin=-5, % start the diagram at this x-coordinate
  103. xmax= 5, % end the diagram at this x-coordinate
  104. ymin= 0, % start the diagram at this y-coordinate
  105. ymax= 3, % end the diagram at this y-coordinate
  106. axis background/.style={fill=white},
  107. xlabel=$x$,
  108. ylabel=$y$,
  109. tick align=outside,
  110. minor tick num=-3,
  111. enlargelimits=true,
  112. tension=0.08]
  113. \addplot[domain=-5:5, thick,samples=50, red] {1};
  114. \addplot[domain=-5:5, thick,samples=50, green] {2};
  115. \addplot[domain=-5:5, thick,samples=50, blue, densely dotted] {3};
  116. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  117. \addplot[blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 3)};
  118. \addplot[green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates {(2, 2)};
  119. \addplot[red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 1)};
  120. \draw[thick, dashed] (axis cs:2,0) -- (axis cs:2,3);
  121. \addlegendentry{$f(x)=1$}
  122. \addlegendentry{$g(x)=2$}
  123. \addlegendentry{$h(x)=3$}
  124. \end{axis}
  125. \end{tikzpicture}
  126. \caption{Three constant functions and their points with minimal distance}
  127. \label{fig:constant-min-distance}
  128. \end{figure}
  129. Then $(x_P,f(x_P))$ has
  130. minimal distance to $P$. Every other point has higher distance.
  131. See Figure~\ref{fig:constant-min-distance}.
  132. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  133. % Linear functions %
  134. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  135. \section{Minimal distance to a linear function}
  136. Let $f(x) = m \cdot x + t$ with $m \in \mdr \setminus \Set{0}$ and
  137. $t \in \mdr$ be a linear function.
  138. \begin{figure}[htp]
  139. \centering
  140. \begin{tikzpicture}
  141. \begin{axis}[
  142. legend pos=north east,
  143. axis x line=middle,
  144. axis y line=middle,
  145. grid = major,
  146. width=0.8\linewidth,
  147. height=8cm,
  148. grid style={dashed, gray!30},
  149. xmin= 0, % start the diagram at this x-coordinate
  150. xmax= 5, % end the diagram at this x-coordinate
  151. ymin= 0, % start the diagram at this y-coordinate
  152. ymax= 3, % end the diagram at this y-coordinate
  153. axis background/.style={fill=white},
  154. xlabel=$x$,
  155. ylabel=$y$,
  156. tick align=outside,
  157. minor tick num=-3,
  158. enlargelimits=true,
  159. tension=0.08]
  160. \addplot[domain=-5:5, thick,samples=50, red] {0.5*x};
  161. \addplot[domain=-5:5, thick,samples=50, blue] {-2*x+6};
  162. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  163. \addlegendentry{$f(x)=\frac{1}{2}x$}
  164. \addlegendentry{$g(x)=-2x+6$}
  165. \end{axis}
  166. \end{tikzpicture}
  167. \caption{The shortest distance of $P$ to $f$ can be calculated by using the perpendicular}
  168. \label{fig:linear-min-distance}
  169. \end{figure}
  170. Now you can drop a perpendicular $f_\bot$ through $P$ on $f(x)$. The
  171. slope of $f_\bot$ is $- \frac{1}{m}$ and $t_\bot$ can be calculated:\nobreak
  172. \begin{align}
  173. f_\bot(x) &= - \frac{1}{m} \cdot x + t_\bot\\
  174. \Rightarrow y_P &= - \frac{1}{m} \cdot x_P + t_\bot\\
  175. \Leftrightarrow t_\bot &= y_P + \frac{1}{m} \cdot x_P
  176. \end{align}
  177. The point $(x, f(x))$ where the perpendicular $f_\bot$ crosses $f$
  178. is calculated this way:
  179. \begin{align}
  180. f(x) &= f_\bot(x)\\
  181. \Leftrightarrow m \cdot x + t &= - \frac{1}{m} \cdot x + \left(y_P + \frac{1}{m} \cdot x_P \right)\\
  182. \Leftrightarrow \left (m + \frac{1}{m} \right ) \cdot x &= y_P + \frac{1}{m} \cdot x_P - t\\
  183. \Leftrightarrow x &= \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )
  184. \end{align}
  185. There is only one point with minimal distance. See Figure~\ref{fig:linear-min-distance}.
  186. \clearpage
  187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  188. % Quadratic functions %
  189. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  190. \section{Minimal distance to a quadratic function}
  191. Let $f(x) = a \cdot x^2 + b \cdot x + c$ with $a \in \mdr \setminus \Set{0}$ and
  192. $b, c \in \mdr$ be a quadratic function.
  193. \begin{figure}[htp]
  194. \centering
  195. \begin{tikzpicture}
  196. \begin{axis}[
  197. legend pos=north west,
  198. axis x line=middle,
  199. axis y line=middle,
  200. grid = major,
  201. width=0.8\linewidth,
  202. height=8cm,
  203. grid style={dashed, gray!30},
  204. xmin=-3, % start the diagram at this x-coordinate
  205. xmax= 3, % end the diagram at this x-coordinate
  206. ymin=-0.25, % start the diagram at this y-coordinate
  207. ymax= 9, % end the diagram at this y-coordinate
  208. axis background/.style={fill=white},
  209. xlabel=$x$,
  210. ylabel=$y$,
  211. tick align=outside,
  212. minor tick num=-3,
  213. enlargelimits=true,
  214. tension=0.08]
  215. \addplot[domain=-3:3, thick,samples=50, red] {0.5*x*x};
  216. \addplot[domain=-3:3, thick,samples=50, green] { x*x};
  217. \addplot[domain=-3:3, thick,samples=50, blue] { x*x + x};
  218. \addplot[domain=-3:3, thick,samples=50, orange,dotted] { x*x + 2*x};
  219. \addplot[domain=-3:3, thick,samples=50, black,dashed] {-x*x + 6};
  220. \addlegendentry{$f_1(x)=\frac{1}{2}x^2$}
  221. \addlegendentry{$f_2(x)=x^2$}
  222. \addlegendentry{$f_3(x)=x^2+x$}
  223. \addlegendentry{$f_4(x)=x^2+2x$}
  224. \addlegendentry{$f_5(x)=-x^2+6$}
  225. \end{axis}
  226. \end{tikzpicture}
  227. \caption{Quadratic functions}
  228. \end{figure}
  229. \subsection{Calculate points with minimal distance}
  230. In this case, $d_{P,f}^2$ is polynomial of degree 4.
  231. We use Theorem~\ref{thm:required-extremum-property}:\nobreak
  232. \begin{align}
  233. 0 &\overset{!}{=} (d_{P,f}^2)'\\
  234. &= -2 x_p + 2x -2y_p f'(x) + \left (f(x)^2 \right )'\\
  235. &= -2 x_p + 2x -2y_p f'(x) + 2 f(x) \cdot f'(x) \rlap{\hspace*{3em}(chain rule)}\label{eq:minimizingFirstDerivative}\\
  236. \Leftrightarrow 0 &\overset{!}{=} -x_p + x -y_p f'(x) + f(x) \cdot f'(x) \rlap{\hspace*{3em}(divide by 2)}\label{eq:minimizingFirstDerivative}\\
  237. &= -x_p + x -y_p (2ax+b) + (ax^2+bx+c)(2ax+b)\\
  238. &= -x_p + x -y_p \cdot 2ax- y_p b + (2 a^2 x^3+2 a b x^2+2 a c x+ab x^2+b^2 x+bc)\\
  239. &= -x_p + x -2y_p ax- y_p b + (2a^2 x^3 + 3 ab x^2 + 2acx + b^2 x + bc)\\
  240. &= 2a^2 x^3 + 3 ab x^2 + (1 -2y_p a+ 2ac + b^2)x +(bc-by_p-x_p)\label{eq:quadratic-derivative-eq-0}
  241. \end{align}
  242. This is an algebraic equation of degree 3.
  243. There can be up to 3 solutions in such an equation. Those solutions
  244. can be found with a closed formula.
  245. \todo[inline]{Where are those closed formulas?}
  246. \begin{example}
  247. Let $a = 1, b = 0, c= 1, x_p= 0, y_p = 1$.
  248. So $f(x) = x^2 + 1$ and $P(0, 1)$.
  249. \begin{align}
  250. 0 &\stackrel{!}{=} 4 x^3 - 2x\\
  251. &=2x(2x^2 - 1)\\
  252. \Rightarrow x_1 &= 0 \;\;\; x_{2,3} = \pm \frac{1}{\sqrt{2}}
  253. \end{align}
  254. As you can easily verify, only $x_1$ is a minimum of $d_{P,f}$.
  255. \end{example}
  256. \subsection{Number of points with minimal distance}
  257. \begin{theorem}
  258. A point $P$ has either one or two points on the graph of a
  259. quadratic function $f$ that are closest to $P$.
  260. \end{theorem}
  261. In the following, I will do some transformations with $f = f_0$ and
  262. $P = P_0$ .
  263. Moving $f_0$ and $P_0$ simultaneously in $x$ or $y$ direction does
  264. not change the minimum distance. Furthermore, we can find the
  265. points with minimum distance on the moved situation and calculate
  266. the minimum points in the original situation.
  267. First of all, we move $f_0$ and $P_0$ by $\frac{b}{2a}$ in $x$ direction, so
  268. \[f_1(x) = ax^2 - \frac{b^2}{4a} + c \;\;\;\text{ and }\;\;\; P_1 = \left (x_p+\frac{b}{2a},\;\; y_p \right )\]
  269. Because:\footnote{The idea why you subtract $\frac{b}{2a}$ within
  270. $f$ is that when you subtract something from $x$ before applying
  271. $f$ it takes more time ($x$ needs to be bigger) to get to the same
  272. situation. So to move the whole graph by $1$ to the left whe have
  273. to add $+1$.}
  274. \begin{align}
  275. f(x-\nicefrac{b}{2a}) &= a (x-\nicefrac{b}{2a})^2 + b (x-\nicefrac{b}{2a}) + c\\
  276. &= a (x^2 - \nicefrac{b}{a} x + \nicefrac{b^2}{4a^2}) + bx - \nicefrac{b^2}{2a} + c\\
  277. &= ax^2 - bx + \nicefrac{b^2}{4a} + bx - \nicefrac{b^2}{2a} + c\\
  278. &= ax^2 -\nicefrac{b^2}{4a} + c
  279. \end{align}
  280. Then move $f_1$ and $P_1$ by $\frac{b^2}{4a}-c$ in $y$ direction. You get:
  281. \[f_2(x) = ax^2\;\;\;\text{ and }\;\;\; P_2 = \Big (\underbrace{x_P+\frac{b}{2a}}_{=: z},\;\; \underbrace{y_P+\frac{b^2}{4a}-c}_{=: w} \Big )\]
  282. \textbf{Case 1:} As $f_2(x) = ax^2$ is symmetric to the $y$ axis, only points
  283. $P = (0, w)$ could possilby have three minima.
  284. Then compute:
  285. \begin{align}
  286. d_{P,{f_2}}(x) &= \sqrt{(x-0)^2 + (f_2(x)-w)^2}\\
  287. &= \sqrt{x^2 + (ax^2-w)^2}\\
  288. &= \sqrt{x^2 + a^2 x^4-2aw x^2+w^2}\\
  289. &= \sqrt{a^2 x^4 + (1-2aw) x^2 + w^2}\\
  290. &= \sqrt{\left (a^2 x^2 + \frac{1-2 a w}{2} \right )^2 + w^2 - (1-2 a w)^2}\\
  291. &= \sqrt{\left (a^2 x^2 + \nicefrac{1}{2}-a w \right )^2 + \big (w^2 - (1-2 a w)^2 \big)}
  292. \end{align}
  293. The term
  294. \[a^2 x^2 + (\nicefrac{1}{2}-a w)\]
  295. should get as close to $0$ as possilbe when we want to minimize
  296. $d_{P,{f_2}}$. For $w \leq \nicefrac{1}{2a}$ you only have $x = 0$ as a minimum.
  297. For all other points $P = (0, w)$, there are exactly two minima $x_{1,2} = \pm \sqrt{aw - \nicefrac{1}{2}}$.
  298. \textbf{Case 2:} $P = (z, w)$ is not on the symmetry axis, so $z \neq 0$. Then you compute:
  299. \begin{align}
  300. d_{P,{f_2}}(x) &= \sqrt{(x-z)^2 + (f(x)-w)^2}\\
  301. &= \sqrt{(x^2 - 2zx + z^2) + ((ax^2)^2 - 2 awx^2 + w^2)}\\
  302. &= \sqrt{a^2x^4 + (1- 2 aw)x^2 +(- 2z)x + z^2 + w^2}\\
  303. 0 &\stackrel{!}{=} \Big(\big(d_{P, {f_2}}(x)\big)^2\Big)' \\
  304. &= 4a^2x^3 + 2(1- 2 aw)x +(- 2z)\\
  305. &= 2 \left (2a^2x^2 + (1- 2 aw) \right )x - 2z\\
  306. \Leftrightarrow 0 &\stackrel{!}{=} (2a^2x^2 + (1- 2 aw)) x - z\\
  307. &= 2 a^2 x^3 + (1- 2 aw) x - z\\
  308. \Leftrightarrow 0 &\stackrel{!}{=} x^3 + \underbrace{\frac{(1- 2 aw)}{2 a^2}}_{=: \alpha} x + \underbrace{\frac{-z}{2 a^2}}_{=: \beta}\\
  309. &= x^3 + \alpha x + \beta\label{eq:simple-cubic-equation-for-quadratic-distance}
  310. \end{align}
  311. The solution of Equation~\ref{eq:simple-cubic-equation-for-quadratic-distance}
  312. is
  313. \[t := \sqrt[3]{\sqrt{3 \cdot (4 \alpha^3 + 27 \beta^2)} -9\beta}\]
  314. \[x = \frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t}\]
  315. When you insert is in Equation~\ref{eq:simple-cubic-equation-for-quadratic-distance}
  316. you get:
  317. \begin{align}
  318. 0 &= \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right )^3 + \alpha \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right ) + \beta\\
  319. &= (\frac{t}{\sqrt[3]{18}})^3 - 3 (\frac{t}{\sqrt[3]{18}})^2 \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} + 3 (\frac{t}{\sqrt[3]{18}})(\frac{\sqrt[3]{\frac{2}{3}} \alpha }{t})^2 + (\frac{\sqrt[3]{\frac{2}{3}} \alpha }{t})^3 + \alpha \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right ) + \beta\\
  320. &= \frac{t^3}{18} - \frac{3t^2}{\sqrt[3]{18^2}} \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} + \frac{3t}{\sqrt[3]{18}} \frac{\sqrt[3]{\frac{4}{9}} \alpha^2 }{t^2} + \frac{\frac{2}{3} \alpha^3 }{t^3} + \alpha \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right ) + \beta\\
  321. &= \frac{t^3}{18} - \frac{\sqrt[3]{18} t \alpha}{\sqrt[3]{18^2}} + \frac{\sqrt[3]{12} \alpha^2}{\sqrt[3]{18} t} + \frac{\frac{2}{3} \alpha^3 }{t^3} + \alpha \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right ) + \beta\\
  322. &= \frac{t^3}{18} - \frac{t \alpha}{\sqrt[3]{18}} + \frac{\sqrt[3]{2} \alpha^2}{\sqrt[3]{3} t} + \frac{\frac{2}{3} \alpha^3 }{t^3} + \alpha \left (\frac{t}{\sqrt[3]{18}} - \frac{\sqrt[3]{\frac{2}{3}} \alpha }{t} \right ) + \beta\\
  323. &= \frac{t^3}{18} - \frac{t \alpha}{\sqrt[3]{18}} + \frac{\frac{2}{3} \alpha^3 }{t^3} + \frac{\alpha t}{\sqrt[3]{18}} + \beta\\
  324. &= \frac{t^3}{18} + \frac{\frac{2}{3} \alpha^3 }{t^3} + \beta\\
  325. \end{align}
  326. \todo[inline]{verify this solution}
  327. \goodbreak
  328. So the solution is given by
  329. \begin{align*}
  330. x_S &:= - \frac{b}{2a} \;\;\;\;\; \text{(the symmetry axis)}\\
  331. \underset{x\in\mdr}{\arg \min d_{P,f}(x)} &= \begin{cases}
  332. x_1 = +\sqrt{a (y_p + \frac{b^2}{4a} - c) - \frac{1}{2}} + x_S \text{ and } &\text{if } x_P = x_S \text{ and } y_p + \frac{b^2}{4a} - c > \frac{1}{2a} \\
  333. x_2 = -\sqrt{a (y_p + \frac{b^2}{4a} - c) - \frac{1}{2}} + x_S\\
  334. x_1 = x_S &\text{if } x_P = x_S \text{ and } y_p + \frac{b^2}{4a} - c \leq \frac{1}{2a} \\
  335. x_1 = todo &\text{if } x_P \neq x_S
  336. \end{cases}
  337. \end{align*}
  338. \clearpage
  339. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  340. % Cubic %
  341. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  342. \section{Minimal distance to a cubic function}
  343. Let $f(x) = a \cdot x^3 + b \cdot x^2 + c \cdot x + d$ be a cubic function
  344. with $a \in \mdr \setminus \Set{0}$ and
  345. $b, c, d \in \mdr$ be a function.
  346. \begin{figure}[htp]
  347. \centering
  348. \begin{tikzpicture}
  349. \begin{axis}[
  350. legend pos=south east,
  351. axis x line=middle,
  352. axis y line=middle,
  353. grid = major,
  354. width=0.8\linewidth,
  355. height=8cm,
  356. grid style={dashed, gray!30},
  357. xmin=-3, % start the diagram at this x-coordinate
  358. xmax= 3, % end the diagram at this x-coordinate
  359. ymin=-3, % start the diagram at this y-coordinate
  360. ymax= 3, % end the diagram at this y-coordinate
  361. axis background/.style={fill=white},
  362. xlabel=$x$,
  363. ylabel=$y$,
  364. tick align=outside,
  365. minor tick num=-3,
  366. enlargelimits=true,
  367. tension=0.08]
  368. \addplot[domain=-3:3, thick,samples=50, red] {x*x*x};
  369. \addplot[domain=-3:3, thick,samples=50, green] {x*x*x+x*x};
  370. \addplot[domain=-3:3, thick,samples=50, blue] {x*x*x+2*x*x};
  371. \addplot[domain=-3:3, thick,samples=50, orange] {x*x*x+x};
  372. \addlegendentry{$f_1(x)=x^3$}
  373. \addlegendentry{$f_2(x)=x^3 + x^2$}
  374. \addlegendentry{$f_2(x)=x^3 + 2 \cdot x^2$}
  375. \addlegendentry{$f_1(x)=x^3 + x$}
  376. \end{axis}
  377. \end{tikzpicture}
  378. \caption{Cubic functions}
  379. \end{figure}
  380. %
  381. %\subsection{Special points}
  382. %\todo[inline]{Write this}
  383. %
  384. %\subsection{Voronoi}
  385. %
  386. %For $b^2 \geq 3ac$
  387. %
  388. %\todo[inline]{Write this}
  389. \subsection{Calculate points with minimal distance}
  390. \begin{theorem}
  391. There cannot be an algebraic solution to the problem of finding
  392. a closest point $(x, f(x))$ to a given point $P$ when $f$ is
  393. a polynomial function of degree $3$ or higher.
  394. \end{theorem}
  395. \begin{proof}
  396. Suppose you could solve the closest point problem for arbitrary
  397. cubic functions $f = ax^3 + bx^2 + cx + d$ and arbitrary points $P = (x_P, y_P)$.
  398. Then you could solve the following problem for $x$:
  399. \begin{align}
  400. 0 &\stackrel{!}{=} \left ((d_{P,f}(x))^2 \right )'
  401. &=-2 x_p + 2x -2y_p(f(x))' + (f(x)^2)'\\
  402. &= 2 f(x) \cdot f'(x) - 2 y_p f'(x) + 2x - 2 x_p\\
  403. &= f(x) \cdot f'(x) - y_p f'(x) + x - x_p\\
  404. &= \underbrace{f'(x) \cdot \left (f(x) - y_p \right )}_{\text{Polynomial of degree 5}} + x - x_p
  405. \end{align}
  406. General algebraic equations of degree 5 don't have a solution formula.\footnote{TODO: Quelle}
  407. Although here seems to be more structure, the resulting algebraic
  408. equation can be almost any polynomial of degree 5:\footnote{Thanks to Peter Košinár on \href{http://math.stackexchange.com/a/584814/6876}{math.stackexchange.com} for this one}
  409. \begin{align}
  410. 0 &\stackrel{!}{=} f'(x) \cdot \left (f(x) - y_p \right ) + (x - x_p)\\
  411. &= \underbrace{3 a^2}_{= \tilde{a}} x^5 + \underbrace{5ab}_{\tilde{b}}x^4 + \underbrace{2(2ac + b^2 )}_{=: \tilde{c}}x^3 &+& \underbrace{3(ad+bc-ay_p)}_{\tilde{d}} x^2 \\
  412. & &+& \underbrace{(2 b d+c^2+1-2 b y_p)}_{=: \tilde{e}}x+\underbrace{c d-c y_p-x_p}_{=: \tilde{f}}\\
  413. 0 &\stackrel{!}{=} \tilde{a}x^5 + \tilde{b}x^4 + \tilde{c}x^3 + \tilde{d}x^2 + \tilde{e}x + \tilde{f}
  414. \end{align}
  415. \begin{enumerate}
  416. \item For any coefficient $\tilde{a} \in \mdr_{> 0}$ of $x^5$ we can choose $a$ such that we get $\tilde{a}$.
  417. \item For any coefficient $\tilde{b} \in \mdr \setminus \Set{0}$ of $x^4$ we can choose $b$ such that we get $\tilde{b}$.
  418. \item With $c$, we can get any value of $\tilde{c} \in \mdr$.
  419. \item With $d$, we can get any value of $\tilde{d} \in \mdr$.
  420. \item With $y_p$, we can get any value of $\tilde{e} \in \mdr$.
  421. \item With $x_p$, we can get any value of $\tilde{f} \in \mdr$.
  422. \end{enumerate}
  423. The first restriction guaratees that we have a polynomial of
  424. degree 5. The second one is necessary, to get a high range of
  425. $\tilde{e}$.
  426. This means, that there is no solution formula for the problem of
  427. finding the closest points on a cubic function to a given point,
  428. because if there was one, you could use this formula for finding
  429. roots of polynomials of degree 5. $\qed$
  430. \end{proof}
  431. \subsection{Another approach}
  432. Just like we moved the function $f$ and the point to get in a
  433. nicer situation, we can apply this approach for cubic functions.
  434. \begin{figure}[htp]
  435. \centering
  436. \begin{tikzpicture}
  437. \begin{axis}[
  438. legend pos=south east,
  439. axis x line=middle,
  440. axis y line=middle,
  441. grid = major,
  442. width=0.8\linewidth,
  443. height=8cm,
  444. grid style={dashed, gray!30},
  445. xmin=-3, % start the diagram at this x-coordinate
  446. xmax= 3, % end the diagram at this x-coordinate
  447. ymin=-3, % start the diagram at this y-coordinate
  448. ymax= 3, % end the diagram at this y-coordinate
  449. axis background/.style={fill=white},
  450. xlabel=$x$,
  451. ylabel=$y$,
  452. tick align=outside,
  453. minor tick num=-3,
  454. enlargelimits=true,
  455. tension=0.08]
  456. \addplot[domain=-3:3, thick,samples=50, red] {x*x*x};
  457. \addplot[domain=-3:3, thick,samples=50, green] {x*x*x+x};
  458. \addplot[domain=-3:3, thick,samples=50, orange] {x*x*x-x};
  459. \addplot[domain=-3:3, thick,samples=50, blue, dotted] {x*x*x+2*x};
  460. \addplot[domain=-3:3, thick,samples=50, lime, dashed] {x*x*x+3*x};
  461. \addlegendentry{$f_1(x)=x^3$}
  462. \addlegendentry{$f_2(x)=x^3 + x$}
  463. \addlegendentry{$f_1(x)=x^3 - x$}
  464. \addlegendentry{$f_2(x)=x^3 + 2 \cdot x$}
  465. \addlegendentry{$f_2(x)=x^3 + 3 \cdot x$}
  466. \end{axis}
  467. \end{tikzpicture}
  468. \caption{Cubic functions with $b = d = 0$}
  469. \end{figure}
  470. First, we move $f_0$ by $\frac{b}{3a}$ to the right, so
  471. \[f_1(x) = ax^3 + \frac{b^2 (c-1)}{3a} x + \frac{2b^3}{27 a^2} - \frac{bc}{3a} + d \;\;\;\text{ and }\;\;\;P_1 = (x_P + \frac{b}{3a}, y_P)\]
  472. because
  473. \begin{align}
  474. f_1(x) &= a \left (x - \frac{b}{3a} \right )^3 + b \left (x-\frac{b}{3a} \right )^2 + c \left (x-\frac{b}{3a} \right ) + d\\
  475. &= a \left (x^3 - 3 \frac{b}{3a}x^2 + 3 (\frac{b}{3a})^2 x - \frac{b^3}{27a^3} \right )
  476. +b \left (x^2 - \frac{2b}{3a} x + \frac{b^2}{9a^2} \right )
  477. +c x - \frac{bc}{3a} + d\\
  478. &= ax^3 - bx^2 + \frac{b^2}{3a}x - \frac{b^3}{27 a^2}\\
  479. & \;\;\;\;\;\;+ bx^2 - \frac{2b^2}{3a}x + \frac{b^3}{9a^2}\\
  480. & \;\;\;\;\;\;\;\;\;\;\;\; + c x - \frac{bc}{3a} + d\\
  481. &= ax^3 + \frac{b^2}{3a}\left (1-2+c \right )x + \frac{b^3}{9a^2} \left (1-\frac{1}{3} \right )- \frac{bc}{3a} + d
  482. \end{align}
  483. \todo[inline]{Which way to move might be clever?}
  484. \subsection{Number of points with minimal distance}
  485. As there is an algebraic equation of degree 5, there cannot be more
  486. than 5 solutions.
  487. \todo[inline]{Can there be 3, 4 or even 5 solutions? Examples!
  488. After looking at function graphs of cubic functions, I'm pretty
  489. sure that there cannot be 4 or 5 solutions, no matter how you
  490. chose the cubic function $f$ and $P$.
  491. I'm also pretty sure that there is no polynomial (no matter what degree)
  492. that has more than 3 solutions.}
  493. \section{Newtons method}
  494. \todo[inline]{When does Newtons method converge? How fast?
  495. How to choose starting point?}
  496. \section{Quadratic minimization}
  497. \todo[inline]{TODO}
  498. \section{Conclusion}
  499. \todo[inline]{TODO}
  500. \end{document}