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

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