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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 also 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 $d_{P,f}$ of a point $P$ and a point $\left (x, f(x) \right )$:
  72. \[d_{P,f} (x) := \sqrt{(x_P - x)^2 + (y_P - f(x))^2}\]
  73. Now there is \todo{Should I proof this?}{finite set} $x_1, \dots, x_n$ such that
  74. \[\forall \tilde x \in \mathbb{R} \setminus \{x_1, \dots, x_n\}: d_{P,f}(x_1) = \dots = d_{P,f}(x_n) < d_{P,f}(\tilde x)\]
  75. Essentially, you want to find the minima $x_1, \dots, x_n$ for given
  76. $f$ and $P$.
  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. \todo[inline]{Hat dieser Satz einen Namen? Gibt es ein gutes Buch,
  83. aus dem ich den zitieren kann? Ich habe ihn aus \href{https://github.com/MartinThoma/LaTeX-examples/tree/master/documents/Analysis I}{meinem Analysis I Skript} (Satz 21.5).}
  84. \begin{theorem}\label{thm:required-extremum-property}
  85. Let $x_0$ be a relative extremum of a differentiable function $f: \mathbb{R} \rightarrow \mathbb{R}$.
  86. Then: $f'(x_0) = 0$.
  87. \end{theorem}
  88. %bzw. 22.3
  89. %\begin{theorem}[Minima of polynomial functions]\label{thm:minima-of-polynomials}
  90. % Let $n \in \mathbb{N}, n \geq 2$, $f$ polynomial function of
  91. % degree $n$, $x_0 \in \mathbb{R}$, \\
  92. % $f'(x_0) = f''(x_0) = \dots = f^{(n-1)} (x_0) = 0$
  93. % and $f^{(n)} > 0$.
  94. %
  95. % Then $x_0$ is a local minimum of $f$.
  96. %\end{theorem}
  97. \clearpage
  98. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99. % Constant functions %
  100. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  101. \section{Minimal distance to a constant function}
  102. Let $f(x) = c$ with $c \in \mdr$ be a constant function.
  103. \begin{figure}[htp]
  104. \centering
  105. \begin{tikzpicture}
  106. \begin{axis}[
  107. legend pos=north west,
  108. axis x line=middle,
  109. axis y line=middle,
  110. grid = major,
  111. width=0.8\linewidth,
  112. height=8cm,
  113. grid style={dashed, gray!30},
  114. xmin=-5, % start the diagram at this x-coordinate
  115. xmax= 5, % end the diagram at this x-coordinate
  116. ymin= 0, % start the diagram at this y-coordinate
  117. ymax= 3, % end the diagram at this y-coordinate
  118. axis background/.style={fill=white},
  119. xlabel=$x$,
  120. ylabel=$y$,
  121. tick align=outside,
  122. minor tick num=-3,
  123. enlargelimits=true,
  124. tension=0.08]
  125. \addplot[domain=-5:5, thick,samples=50, red] {1};
  126. \addplot[domain=-5:5, thick,samples=50, green] {2};
  127. \addplot[domain=-5:5, thick,samples=50, blue] {3};
  128. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  129. \addplot[blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 3)};
  130. \addplot[green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates {(2, 2)};
  131. \addplot[red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 1)};
  132. \draw[thick, dashed] (axis cs:2,0) -- (axis cs:2,3);
  133. \addlegendentry{$f(x)=1$}
  134. \addlegendentry{$g(x)=2$}
  135. \addlegendentry{$h(x)=3$}
  136. \end{axis}
  137. \end{tikzpicture}
  138. \caption{Three constant functions and their points with minimal distance}
  139. \label{fig:constant-min-distance}
  140. \end{figure}
  141. Then $(x_P,f(x_P))$ has
  142. minimal distance to $P$. Every other point has higher distance.
  143. See Figure~\ref{fig:constant-min-distance}.
  144. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  145. % Linear functions %
  146. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  147. \section{Minimal distance to a linear function}
  148. Let $f(x) = m \cdot x + t$ with $m \in \mdr \setminus \Set{0}$ and
  149. $t \in \mdr$ be a linear function.
  150. \begin{figure}[htp]
  151. \centering
  152. \begin{tikzpicture}
  153. \begin{axis}[
  154. legend pos=north east,
  155. axis x line=middle,
  156. axis y line=middle,
  157. grid = major,
  158. width=0.8\linewidth,
  159. height=8cm,
  160. grid style={dashed, gray!30},
  161. xmin= 0, % start the diagram at this x-coordinate
  162. xmax= 5, % end the diagram at this x-coordinate
  163. ymin= 0, % start the diagram at this y-coordinate
  164. ymax= 3, % end the diagram at this y-coordinate
  165. axis background/.style={fill=white},
  166. xlabel=$x$,
  167. ylabel=$y$,
  168. tick align=outside,
  169. minor tick num=-3,
  170. enlargelimits=true,
  171. tension=0.08]
  172. \addplot[domain=-5:5, thick,samples=50, red] {0.5*x};
  173. \addplot[domain=-5:5, thick,samples=50, blue] {-2*x+6};
  174. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  175. \addlegendentry{$f(x)=\frac{1}{2}x$}
  176. \addlegendentry{$g(x)=-2x+6$}
  177. \end{axis}
  178. \end{tikzpicture}
  179. \caption{The shortest distance of $P$ to $f$ can be calculated by using the perpendicular}
  180. \label{fig:linear-min-distance}
  181. \end{figure}
  182. Now you can drop a perpendicular $f_\bot$ through $P$ on $f(x)$. The slope of $f_\bot$
  183. is $- \frac{1}{m}$. Now you can calculate $f_\bot$:\nobreak
  184. \begin{align}
  185. f_\bot(x) &= - \frac{1}{m} \cdot x + t_\bot\\
  186. \Rightarrow y_P &= - \frac{1}{m} \cdot x_P + t_\bot\\
  187. \Leftrightarrow t_\bot &= y_P + \frac{1}{m} \cdot x_P
  188. \end{align}
  189. Now find the point $(x, f(x))$ where the perpendicular crosses the function:
  190. \begin{align}
  191. f(x) &= f_\bot(x)\\
  192. \Leftrightarrow m \cdot x + t &= - \frac{1}{m} \cdot x + \left(y_P + \frac{1}{m} \cdot x_P \right)\\
  193. \Leftrightarrow \left (m + \frac{1}{m} \right ) \cdot x &= y_P + \frac{1}{m} \cdot x_P - t\\
  194. \Leftrightarrow x &= \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )
  195. \end{align}
  196. There is only one point with minimal distance. See Figure~\ref{fig:linear-min-distance}.
  197. \clearpage
  198. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  199. % Quadratic functions %
  200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201. \section{Minimal distance to a quadratic function}
  202. Let $f(x) = a \cdot x^2 + b \cdot x + c$ with $a \in \mdr \setminus \Set{0}$ and
  203. $b, c \in \mdr$ be a quadratic function.
  204. \begin{figure}[htp]
  205. \centering
  206. \begin{tikzpicture}
  207. \begin{axis}[
  208. legend pos=north west,
  209. axis x line=middle,
  210. axis y line=middle,
  211. grid = major,
  212. width=0.8\linewidth,
  213. height=8cm,
  214. grid style={dashed, gray!30},
  215. xmin=-3, % start the diagram at this x-coordinate
  216. xmax= 3, % end the diagram at this x-coordinate
  217. ymin=-0.25, % start the diagram at this y-coordinate
  218. ymax= 9, % end the diagram at this y-coordinate
  219. axis background/.style={fill=white},
  220. xlabel=$x$,
  221. ylabel=$y$,
  222. %xticklabels={-2,-1.6,...,7},
  223. %yticklabels={-8,-7,...,8},
  224. tick align=outside,
  225. minor tick num=-3,
  226. enlargelimits=true,
  227. tension=0.08]
  228. \addplot[domain=-3:3, thick,samples=50, red] {0.5*x*x};
  229. \addplot[domain=-3:3, thick,samples=50, green] {x*x};
  230. \addplot[domain=-3:3, thick,samples=50, blue] {x*x + x};
  231. \addplot[domain=-3:3, thick,samples=50, orange] {x*x + 2*x};
  232. \addplot[domain=-3:3, thick,samples=50, black] {-x*x + 6};
  233. \addlegendentry{$f_1(x)=\frac{1}{2}x^2$}
  234. \addlegendentry{$f_2(x)=x^2$}
  235. \addlegendentry{$f_3(x)=x^2+x$}
  236. \addlegendentry{$f_4(x)=x^2+2x$}
  237. \addlegendentry{$f_5(x)=-x^2+6$}
  238. \end{axis}
  239. \end{tikzpicture}
  240. \caption{Quadratic functions}
  241. \end{figure}
  242. \subsection{Calculate points with minimal distance}
  243. In this case, $d_{P,f}^2$ is polynomial of degree 4.
  244. We use Theorem~\ref{thm:required-extremum-property}:\nobreak
  245. \begin{align}
  246. 0 &\overset{!}{=} (d_{P,f}^2)'\\
  247. &= -2 x_p + 2x -2y_p f'(x) + \left (f(x)^2 \right )'\\
  248. &= -2 x_p + 2x -2y_p f'(x) + 2 f(x) \cdot f'(x) \rlap{\hspace*{3em}(chain rule)}\label{eq:minimizingFirstDerivative}\\
  249. \Leftrightarrow 0 &\overset{!}{=} -x_p + x -y_p f'(x) + f(x) \cdot f'(x) \rlap{\hspace*{3em}(divide by 2)}\label{eq:minimizingFirstDerivative}\\
  250. &= -x_p + x -y_p (2ax+b) + (ax^2+bx+c)(2ax+b)\\
  251. &= -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)\\
  252. &= -x_p + x -2y_p ax- y_p b + (2a^2 x^3 + 3 ab x^2 + 2acx + b^2 x + bc)\\
  253. &= 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}
  254. \end{align}
  255. %\begin{align}
  256. % 0 &\overset{!}{=}(d_{P,f}^2)''\\
  257. % &= 2 - 2y_p f''(x) + \left ( 2 f(x) \cdot f'(x) \right )' \rlap{\hspace*{3em}(Eq. \ref{eq:minimizingFirstDerivative})}\\
  258. % &= 2 - 2y_p f''(x) + 2 \cdot \left ( f'(x) \cdot f'(x) + f(x) \cdot f''(x) \right ) \rlap{\hspace*{3em}(product rule)}\\
  259. % &= 2 - 2y_p f''(x) + 2 \cdot \left ( f'(x)^2 + f(x) \cdot f''(x) \right )\\
  260. % &= 12a^2 x^2 + 12abx + 2(1 -2y_p a+ 2ac + b^2)
  261. %\end{align}
  262. This is an algebraic equation of degree 3.
  263. There can be up to 3 solutions in such an equation. Those solutions
  264. can be found with a closed formula.
  265. \todo[inline]{Where are those closed formulas?}
  266. \begin{example}
  267. Let $a = 1, b = 0, c= 1, x_p= 0, y_p = 1$.
  268. So $f(x) = x^2 + 1$ and $P(0, 1)$.
  269. \begin{align}
  270. 0 &\stackrel{!}{=} 4 x^3 - 2x\\
  271. &=2x(2x^2 - 1)\\
  272. \Rightarrow x_1 &= 0 \;\;\; x_{2,3} = \pm \frac{1}{\sqrt{2}}
  273. \end{align}
  274. As you can easily verify, only $x_1$ is a minimum of $d_{P,f}$.
  275. \end{example}
  276. \subsection{Number of points with minimal distance}
  277. \begin{theorem}
  278. A point $P$ has either one or two points on the graph of a
  279. quadratic function $f$ that are closest to $P$.
  280. \end{theorem}
  281. In the following, I will do some transformations with $f = f_0$ and
  282. $P = P_0$ .
  283. Moving $f_0$ and $P_0$ simultaneously in $x$ or $y$ direction does
  284. not change the minimum distance. Furthermore, we can find the
  285. points with minimum distance on the moved situation and calculate
  286. the minimum points in the original situation.
  287. First of all, we move $f_0$ and $P_0$ by $\frac{b}{2a}$ in $x$ direction, so
  288. \[f_1(x) = ax^2 - \frac{b^2}{4a} + c \;\;\;\text{ and }\;\;\; P_1 = \left (x_p+\frac{b}{2a},\;\; y_p \right )\]
  289. Because:
  290. \begin{align}
  291. f(x-\nicefrac{b}{2a}) &= a (x-\nicefrac{b}{2a})^2 + b (x-\nicefrac{b}{2a}) + c\\
  292. &= a (x^2 - \nicefrac{b}{a} x + \nicefrac{b^2}{4a^2}) + bx - \nicefrac{b^2}{2a} + c\\
  293. &= ax^2 - bx + \nicefrac{b^2}{4a} + bx - \nicefrac{b^2}{2a} + c\\
  294. &= ax^2 -\nicefrac{b^2}{4a} + c
  295. \end{align}
  296. Then move $f_1$ and $P_1$ by $\frac{b^2}{4a}-c$ in $y$ direction. You get:
  297. \[f_2(x) = ax^2\;\;\;\text{ and }\;\;\; P_2 = \left (x_p+\frac{b}{2a},\;\; y_p+\frac{b^2}{4a}-c \right )\]
  298. \textbf{Case 1:} As $f_2(x) = ax^2$ is symmetric to the $y$ axis, only points
  299. $P = (0, w)$ could possilby have three minima.
  300. Then compute:
  301. \begin{align}
  302. d_{P,{f_2}}(x) &= \sqrt{(x-x_{P})^2 + (f(x)-w)^2}\\
  303. &= \sqrt{x^2 + (ax^2-w)^2}\\
  304. &= \sqrt{x^2 + a^2 x^4-2aw x^2+w^2}\\
  305. &= \sqrt{a^2 x^4 + (1-2aw) x^2 + w^2}\\
  306. &= \sqrt{\left (a^2 x^2 + \frac{1-2 a w}{2} \right )^2 + w^2 - (1-2 a w)^2}\\
  307. &= \sqrt{\left (a^2 x^2 + \nicefrac{1}{2}-a w \right )^2 + (w^2 - (1-2 a w)^2)}\\
  308. \end{align}
  309. The term
  310. \[a^2 x^2 + (\nicefrac{1}{2}-a w)\]
  311. should get as close to $0$ as possilbe when we want to minimize
  312. $d_{P,{f_2}}$. For $w \leq \nicefrac{1}{2a}$ you only have $x = 0$ as a minimum.
  313. For all other points $P = (0, w)$, there are exactly two minima $x_{1,2} = \pm \sqrt{aw - \nicefrac{1}{2}}$.
  314. \textbf{Case 2:} $P = (z, w)$ is not on the symmetry axis, so $z \neq 0$. Then you compute:
  315. \begin{align}
  316. d_{P,{f_2}}(x) &= \sqrt{(x-z)^2 + (f(x)-w)^2}\\
  317. &= \sqrt{(x^2 - 2zx + z^2) + ((ax^2)^2 - 2 awx^2 - w^2)}\\
  318. &= \sqrt{a^2x^4 + (1- 2 aw)x^2 +(- 2z)x + z^2 - w^2}\\
  319. 0 &\stackrel{!}{=} d_{P, {f_2}}'(x) \\
  320. &= 4a^2x^3 + 2(1- 2 aw)x +(- 2z)\\
  321. &= 2 \left (2a^2x^2 + (1- 2 aw) \right )x - 2z\\
  322. \Leftrightarrow z &\stackrel{!}{=} (2a^2x^2 + (1- 2 aw)) x\\
  323. \Leftrightarrow 0 &\stackrel{!}{=} 2 a^2 x^3 + (1- 2 aw)x - z\label{line:solution-equation}
  324. \end{align}
  325. The solution for this equation was computated with Wolfram|Alpha.
  326. I will only verify that the solution is correct. As there is only
  327. one solution in this case, we only have to check this one.
  328. \begin{align}
  329. t &:= \sqrt[3]{108a^4z + \sqrt{\num{11664}a^8 z^2 + 864 a^6 (1-2aw)^3}}\\
  330. x &= \frac{t}{6 \sqrt[3]{2} a^2} - \frac{\sqrt[3]{2}(1-2aw)}{t}\\
  331. \xRightarrow{x \text{ in line }\ref{line:solution-equation}} 0 &\stackrel{!}{=} 2a^2 \left ( \frac{t}{6 \sqrt[3]{2} a^2} - \frac{\sqrt[3]{2}(1-2aw)}{t} \right )^3 + (1-2aw) (\frac{t}{6 \sqrt[3]{2} a^2} - \frac{\sqrt[3]{2}(1-2aw)}{t}) - z\\
  332. &= 2a^2 \underbrace{\left ( \frac{t}{6 \sqrt[3]{2} a^2} - \frac{\sqrt[3]{2}(1-2aw)}{t} \right )^3}_{=: \alpha} + \frac{t \cdot (1-2aw)}{6 \sqrt[3]{2} a^2} - \frac{\sqrt[3]{2} (1-2aw)^2}{t} - z
  333. \end{align}
  334. Now compute $\alpha$. We know that $(a-b)^3 = a^3 - 3a^2 b +3ab^2 - b^3$:
  335. \begin{align}
  336. \alpha &= \left (\frac{t}{6 \sqrt[3]{2} a^2} \right )^3 - 3 \left (\frac{t}{6 \sqrt[3]{2} a^2} \right )^2 \left (\frac{\sqrt[3]{2}(1-2aw)}{t} \right ) + 3 \left (\frac{t}{6 \sqrt[3]{2} a^2} \right ) \left (\frac{\sqrt[3]{2}(1-2aw)}{t} \right )^2 - \left (\frac{\sqrt[3]{2}(1-2aw)}{t} \right )^3\\
  337. &= \frac{t^3}{216 \cdot 2 \cdot a^6} - \frac{3t^2}{36 \sqrt[3]{4} a^4} \cdot \frac{\sqrt[3]{2}(1-2aw)}{t}
  338. + \frac{3t}{6 \sqrt[3]{2} a^2} \cdot \frac{\sqrt[3]{4} (1-2aw)^2}{t^2} - \frac{2 (1-2aw)^3}{t^3}\\
  339. &= \frac{t^3}{432a^6} - \frac{t (1-2aw)}{12\sqrt[3]{2} a^4} + \frac{\sqrt[3]{2} (1-2aw)^2}{2ta^2} - \frac{2 (1-2aw)^3}{t^3}\\
  340. &= \frac{t^3}{432a^6} + \frac{t^2 (2aw-1) + 6 \sqrt[3]{4} a^2 (1-2aw)^2}{12\sqrt[3]{2} a^4} - \frac{2 (1-2aw)^3}{t^3}\\
  341. &= \frac{t^3- 2 (1-2aw)^3 \cdot 432a^6}{432a^6 t^3} + \frac{t^2 (2aw-1) + 6 \sqrt[3]{4} a^2 (1-2aw)^2}{12\sqrt[3]{2} a^4}\\
  342. &= \frac{\sqrt[3]{2}(t^3- 2 (1-2aw)^3 \cdot 432a^6) + 36 a^2 t^3 (t^2 (2aw-1) + 6 \sqrt[3]{4} a^2 (1-2aw)^2)}{432 \sqrt[3]{2} a^6 t^3}\\
  343. &= \frac{\sqrt[3]{2} t^3 - \sqrt[3]{2} (1-2aw)^3 \cdot 864a^6 + 36 a^2 t^3 (2awt^1-t^2 + 6 \sqrt[3]{4} a^2 (1-2aw)^2)}{6 \sqrt[3]{2} a^2 t \cdot 2a^2 \cdot 36 a^2 t^2}
  344. \end{align}
  345. \goodbreak
  346. So the solution is given by
  347. \begin{align*}
  348. x_S &:= - \frac{b}{2a} \;\;\;\;\; \text{(the symmetry axis)}\\
  349. \underset{x\in\mdr}{\arg \min d_{P,f}(x)} &= \begin{cases}
  350. 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} \\
  351. x_2 = -\sqrt{a (y_p + \frac{b^2}{4a} - c) - \frac{1}{2}} + x_S\\
  352. x_1 = x_S &\text{if } x_P = x_S \text{ and } y_p + \frac{b^2}{4a} - c \leq \frac{1}{2a} \\
  353. x_1 = todo &\text{if } x_P \neq x_S
  354. \end{cases}
  355. \end{align*}
  356. \clearpage
  357. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  358. % Cubic %
  359. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  360. \section{Minimal distance to a cubic function}
  361. Let $f(x) = a \cdot x^3 + b \cdot x^2 + c \cdot x + d$ be a cubic function
  362. with $a \in \mdr \setminus \Set{0}$ and
  363. $b, c, d \in \mdr$ be a function.
  364. \begin{figure}[htp]
  365. \centering
  366. \begin{tikzpicture}
  367. \begin{axis}[
  368. legend pos=south east,
  369. axis x line=middle,
  370. axis y line=middle,
  371. grid = major,
  372. width=0.8\linewidth,
  373. height=8cm,
  374. grid style={dashed, gray!30},
  375. xmin=-3, % start the diagram at this x-coordinate
  376. xmax= 3, % end the diagram at this x-coordinate
  377. ymin=-3, % start the diagram at this y-coordinate
  378. ymax= 3, % end the diagram at this y-coordinate
  379. axis background/.style={fill=white},
  380. xlabel=$x$,
  381. ylabel=$y$,
  382. %xticklabels={-2,-1.6,...,7},
  383. %yticklabels={-8,-7,...,8},
  384. tick align=outside,
  385. minor tick num=-3,
  386. enlargelimits=true,
  387. tension=0.08]
  388. \addplot[domain=-3:3, thick,samples=50, red] {x*x*x};
  389. \addplot[domain=-3:3, thick,samples=50, green] {x*x*x+x*x};
  390. \addplot[domain=-3:3, thick,samples=50, blue] {x*x*x+2*x*x};
  391. \addplot[domain=-3:3, thick,samples=50, orange] {x*x*x+x};
  392. \addlegendentry{$f_1(x)=x^3$}
  393. \addlegendentry{$f_2(x)=x^3 + x^2$}
  394. \addlegendentry{$f_2(x)=x^3 + 2 \cdot x^2$}
  395. \addlegendentry{$f_1(x)=x^3 + x$}
  396. \end{axis}
  397. \end{tikzpicture}
  398. \caption{Cubic functions}
  399. \end{figure}
  400. %
  401. %\subsection{Special points}
  402. %\todo[inline]{Write this}
  403. %
  404. %\subsection{Voronoi}
  405. %
  406. %For $b^2 \geq 3ac$
  407. %
  408. %\todo[inline]{Write this}
  409. \subsection{Calculate points with minimal distance}
  410. When you want to calculate points with minimal distance, you can
  411. take the same approach as in Equation \ref{eq:minimizingFirstDerivative}:
  412. \begin{align}
  413. 0 &\stackrel{!}{=} -2 x_p + 2x -2y_p(f(x))' + (f(x)^2)'\\
  414. &= 2 f(x) \cdot f'(x) - 2 y_p f'(x) + 2x - 2 x_p\\
  415. &= f(x) \cdot f'(x) - y_p f'(x) + x - x_p\\
  416. &= \underbrace{f'(x) \cdot \left (f(x) - y_p \right )}_{\text{Polynomial of degree 5}} + x - x_p
  417. \end{align}
  418. General algebraic equations of degree 5 don't have a solution formula.\footnote{TODO: Quelle}
  419. Although here seems to be more structure, the resulting algebraic
  420. 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}
  421. \begin{align}
  422. 0 &\stackrel{!}{=} f'(x) \cdot \left (f(x) - y_p \right ) + (x - x_p)\\
  423. &= \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 \\
  424. & &+& \underbrace{(2 b d+c^2+1-2 b y_p)}_{=: \tilde{e}}x+\underbrace{c d-c y_p-x_p}_{=: \tilde{f}}\\
  425. 0 &\stackrel{!}{=} \tilde{a}x^5 + \tilde{b}x^4 + \tilde{c}x^3 + \tilde{d}x^2 + \tilde{e}x + \tilde{f}
  426. \end{align}
  427. \begin{enumerate}
  428. \item With $a$, we can get any value of $\tilde{a} \in \mdr \setminus \Set{0}$.
  429. \item With $b$, we can get any value of $\tilde{b} \in \mdr \setminus \Set{0}$.
  430. \item With $c$, we can get any value of $\tilde{c} \in \mdr$.
  431. \item With $d$, we can get any value of $\tilde{d} \in \mdr$.
  432. \item With $y_p$, we can get any value of $\tilde{e} \in \mdr$.
  433. \item With $x_p$, we can get any value of $\tilde{f} \in \mdr$.
  434. \end{enumerate}
  435. The first restriction only guaratees that we have a polynomial of
  436. degree 5. The second one is necessary, to get a high range of
  437. $\tilde{e}$.
  438. This means, that there is no solution formula for the problem of
  439. finding the closest points on a cubic function to a given point.
  440. \subsection{Another approach}
  441. Just like we moved the function $f$ and the point to get in a
  442. nicer situation, we can apply this approach for cubic functions.
  443. \begin{figure}[htp]
  444. \centering
  445. \begin{tikzpicture}
  446. \begin{axis}[
  447. legend pos=south east,
  448. axis x line=middle,
  449. axis y line=middle,
  450. grid = major,
  451. width=0.8\linewidth,
  452. height=8cm,
  453. grid style={dashed, gray!30},
  454. xmin=-3, % start the diagram at this x-coordinate
  455. xmax= 3, % end the diagram at this x-coordinate
  456. ymin=-3, % start the diagram at this y-coordinate
  457. ymax= 3, % end the diagram at this y-coordinate
  458. axis background/.style={fill=white},
  459. xlabel=$x$,
  460. ylabel=$y$,
  461. %xticklabels={-2,-1.6,...,7},
  462. %yticklabels={-8,-7,...,8},
  463. tick align=outside,
  464. minor tick num=-3,
  465. enlargelimits=true,
  466. tension=0.08]
  467. \addplot[domain=-3:3, thick,samples=50, red] {x*x*x};
  468. \addplot[domain=-3:3, thick,samples=50, green] {x*x*x+x};
  469. \addplot[domain=-3:3, thick,samples=50, orange] {x*x*x-x};
  470. \addplot[domain=-3:3, thick,samples=50, blue, dotted] {x*x*x+2*x};
  471. \addplot[domain=-3:3, thick,samples=50, lime, dashed] {x*x*x+3*x};
  472. \addlegendentry{$f_1(x)=x^3$}
  473. \addlegendentry{$f_2(x)=x^3 + x$}
  474. \addlegendentry{$f_1(x)=x^3 - x$}
  475. \addlegendentry{$f_2(x)=x^3 + 2 \cdot x$}
  476. \addlegendentry{$f_2(x)=x^3 + 3 \cdot x$}
  477. \end{axis}
  478. \end{tikzpicture}
  479. \caption{Cubic functions with $b = d = 0$}
  480. \end{figure}
  481. First, we move $f_0$ by $\frac{b}{3a}$ to the right, so
  482. \[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)\]
  483. because
  484. \begin{align}
  485. 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\\
  486. &= a \left (x^3 - 3 \frac{b}{3a}x^2 + 3 (\frac{b}{3a})^2 x - \frac{b^3}{27a^3} \right )
  487. +b \left (x^2 - \frac{2b}{3a} x + \frac{b^2}{9a^2} \right )
  488. +c x - \frac{bc}{3a} + d\\
  489. &= ax^3 - bx^2 + \frac{b^2}{3a}x - \frac{b^3}{27 a^2}\\
  490. & \;\;\;\;\;\;+ bx^2 - \frac{2b^2}{3a}x + \frac{b^3}{9a^2}\\
  491. & \;\;\;\;\;\;\;\;\;\;\;\; + c x - \frac{bc}{3a} + d\\
  492. &= 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
  493. \end{align}
  494. \todo[inline]{Which way to move might be clever?}
  495. \subsection{Number of points with minimal distance}
  496. As there is an algebraic equation of degree 5, there cannot be more
  497. than 5 solutions.
  498. \todo[inline]{Can there be 3, 4 or even 5 solutions? Examples!
  499. After looking at function graphs of cubic functions, I'm pretty
  500. sure that there cannot be 4 or 5 solutions, no matter how you
  501. chose the cubic function $f$ and $P$.
  502. I'm also pretty sure that there is no polynomial (no matter what degree)
  503. that has more than 3 solutions.}
  504. \todo[inline]{If there is no closed form solution, I want to
  505. describe a numerical solution. I guess Newtons method might be good.}
  506. \end{document}