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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. \title{Minimal distance to a cubic function}
  16. \author{Martin Thoma}
  17. \hypersetup{
  18. pdfauthor = {Martin Thoma},
  19. pdfkeywords = {},
  20. pdftitle = {Minimal Distance}
  21. }
  22. \def\mdr{\ensuremath{\mathbb{R}}}
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. % Begin document %
  25. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  26. \begin{document}
  27. \maketitle
  28. \begin{abstract}
  29. When you have a selfdriving car, you have to plan which path you
  30. want to take. A reasonable choice for the representation of this
  31. path is a cubic spline. But you also have to be able to calculate
  32. how to steer to get or to remain on this path. A way to do this
  33. is applying the \href{https://en.wikipedia.org/wiki/PID_algorithm}{PID algorithm}.
  34. But this algorithm needs to know the current error. So you need to
  35. be able to get the minimal distance of a point to a cubic spline.
  36. As you need to get the signed error (and one steering direction might
  37. be prefered), it is not only necessary to
  38. get the minimal absolute distance, but also to get all points
  39. on the spline with minimal distance.
  40. In this paper I want to discuss how to find all points on a cubic
  41. function with minimal distance to a given point.
  42. As other representations of paths might be easier to understand and
  43. to implement, I will also cover the problem of finding the minimal
  44. distance of a point to a polynomial of degree 0, 1 and 2.
  45. \end{abstract}
  46. \section{Description of the Problem}
  47. Let $f: \mdr \rightarrow \mdr$ be a polynomial function and $P \in \mdr^2$
  48. be a point. Let $d: \mdr^2 \times \mdr^2 \rightarrow \mdr_0^+$
  49. be the Euklidean distance of two points:
  50. \[d \left ((x_1, y_1), (x_2, y_2) \right) := \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\]
  51. Now there is \todo{Should I proof this?}{finite set} $x_1, \dots, x_n$ such that
  52. \[\forall \tilde x \in \mathbb{R} \setminus \{x_1, \dots, x_n\}: d(P, (x_1, f(x_1))) = \dots = d(P, (x_n, f(x_n))) < d(P, (\tilde x, f(\tilde x)))\]
  53. The task is now to find those $x_1, \dots, x_n$ for given $f, P$.
  54. \section{Minimal distance to a constant function}
  55. Let $f(x) = c$ with $c \in \mdr$ be a constant function.
  56. \begin{figure}[htp]
  57. \centering
  58. \begin{tikzpicture}
  59. \begin{axis}[
  60. legend pos=north west,
  61. axis x line=middle,
  62. axis y line=middle,
  63. grid = major,
  64. width=0.8\linewidth,
  65. height=8cm,
  66. grid style={dashed, gray!30},
  67. xmin=-5, % start the diagram at this x-coordinate
  68. xmax= 5, % end the diagram at this x-coordinate
  69. ymin= 0, % start the diagram at this y-coordinate
  70. ymax= 3, % end the diagram at this y-coordinate
  71. axis background/.style={fill=white},
  72. xlabel=$x$,
  73. ylabel=$y$,
  74. tick align=outside,
  75. minor tick num=-3,
  76. enlargelimits=true,
  77. tension=0.08]
  78. \addplot[domain=-5:5, thick,samples=50, red] {1};
  79. \addplot[domain=-5:5, thick,samples=50, green] {2};
  80. \addplot[domain=-5:5, thick,samples=50, blue] {3};
  81. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  82. \addplot[blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 3)};
  83. \addplot[green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates {(2, 2)};
  84. \addplot[red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates {(2, 1)};
  85. \draw[thick, dashed] (axis cs:2,0) -- (axis cs:2,3);
  86. \addlegendentry{$f(x)=1$}
  87. \addlegendentry{$g(x)=2$}
  88. \addlegendentry{$h(x)=3$}
  89. \end{axis}
  90. \end{tikzpicture}
  91. \caption{3 constant functions and their points with minimal distance}
  92. \label{fig:constant-min-distance}
  93. \end{figure}
  94. Then $(x_P,f(x_P))$ has
  95. minimal distance to $P$. Every other point has higher distance.
  96. See Figure~\ref{fig:constant-min-distance}.
  97. \section{Minimal distance to a linear function}
  98. Let $f(x) = m \cdot x + t$ with $m \in \mdr \setminus \Set{0}$ and
  99. $t \in \mdr$ be a linear function.
  100. \begin{figure}[htp]
  101. \centering
  102. \begin{tikzpicture}
  103. \begin{axis}[
  104. legend pos=north east,
  105. axis x line=middle,
  106. axis y line=middle,
  107. grid = major,
  108. width=0.8\linewidth,
  109. height=8cm,
  110. grid style={dashed, gray!30},
  111. xmin= 0, % start the diagram at this x-coordinate
  112. xmax= 5, % end the diagram at this x-coordinate
  113. ymin= 0, % start the diagram at this y-coordinate
  114. ymax= 3, % end the diagram at this y-coordinate
  115. axis background/.style={fill=white},
  116. xlabel=$x$,
  117. ylabel=$y$,
  118. tick align=outside,
  119. minor tick num=-3,
  120. enlargelimits=true,
  121. tension=0.08]
  122. \addplot[domain=-5:5, thick,samples=50, red] {0.5*x};
  123. \addplot[domain=-5:5, thick,samples=50, blue] {-2*x+6};
  124. \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
  125. \addlegendentry{$f(x)=\frac{1}{2}x$}
  126. \addlegendentry{$g(x)=-2x+6$}
  127. \end{axis}
  128. \end{tikzpicture}
  129. \caption{The shortest distance of $P$ to $f$ can be calculated by using the perpendicular}
  130. \label{fig:linear-min-distance}
  131. \end{figure}
  132. Now you can drop a perpendicular $f_\bot$ through $P$ on $f(x)$. The slope of $f_\bot$
  133. is $- \frac{1}{m}$. Now you can calculate $f_\bot$:\nobreak
  134. \begin{align}
  135. f_\bot(x) &= - \frac{1}{m} \cdot x + t_\bot\\
  136. \Rightarrow y_P &= - \frac{1}{m} \cdot x_P + t_\bot\\
  137. \Leftrightarrow t_\bot &= y_P + \frac{1}{m} \cdot x_P
  138. \end{align}
  139. Now find the point $(x, f(x))$ where the perpendicular crosses the function:
  140. \begin{align}
  141. f(x) &= f_\bot(x)\\
  142. \Leftrightarrow m \cdot x + t &= - \frac{1}{m} \cdot x + \left(y_P + \frac{1}{m} \cdot x_P \right)\\
  143. \Leftrightarrow \left (m + \frac{1}{m} \right ) \cdot x &= y_P + \frac{1}{m} \cdot x_P - t\\
  144. \Leftrightarrow x &= \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )
  145. \end{align}
  146. There is only one point with minimal distance. See Figure~\ref{fig:linear-min-distance}.
  147. \clearpage
  148. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  149. % Quadratic functions %
  150. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  151. \section{Minimal distance to a quadratic function}
  152. Let $f(x) = a \cdot x^2 + b \cdot x + c$ with $a \in \mdr \setminus \Set{0}$ and
  153. $b, c \in \mdr$ be a quadratic function.
  154. \begin{figure}[htp]
  155. \centering
  156. \begin{tikzpicture}
  157. \begin{axis}[
  158. legend pos=north west,
  159. axis x line=middle,
  160. axis y line=middle,
  161. grid = major,
  162. width=0.8\linewidth,
  163. height=8cm,
  164. grid style={dashed, gray!30},
  165. xmin=-3, % start the diagram at this x-coordinate
  166. xmax= 3, % end the diagram at this x-coordinate
  167. ymin=-0.25, % start the diagram at this y-coordinate
  168. ymax= 9, % end the diagram at this y-coordinate
  169. axis background/.style={fill=white},
  170. xlabel=$x$,
  171. ylabel=$y$,
  172. %xticklabels={-2,-1.6,...,7},
  173. %yticklabels={-8,-7,...,8},
  174. tick align=outside,
  175. minor tick num=-3,
  176. enlargelimits=true,
  177. tension=0.08]
  178. \addplot[domain=-3:3, thick,samples=50, red] {0.5*x*x};
  179. \addplot[domain=-3:3, thick,samples=50, green] {x*x};
  180. \addplot[domain=-3:3, thick,samples=50, blue] {x*x + x};
  181. \addplot[domain=-3:3, thick,samples=50, orange] {x*x + 2*x};
  182. \addplot[domain=-3:3, thick,samples=50, black] {-x*x + 6};
  183. \addlegendentry{$f_1(x)=\frac{1}{2}x^2$}
  184. \addlegendentry{$f_2(x)=x^2$}
  185. \addlegendentry{$f_3(x)=x^2+x$}
  186. \addlegendentry{$f_4(x)=x^2+2x$}
  187. \addlegendentry{$f_5(x)=-x^2+6$}
  188. \end{axis}
  189. \end{tikzpicture}
  190. \caption{Quadratic functions}
  191. \end{figure}
  192. \subsection{Number of points with minimal distance}
  193. It is obvious that a quadratic function can have two points with
  194. minimal distance.
  195. For example, let $f(x) = x^2$ and $P = (0,5)$. Then $P_{f,1} \approx (2.179, 2.179^2)$
  196. has minimal distance to $P$, but also $P_{f,2}\approx (-2.179, 2.179^2)$.
  197. Obviously, there cannot be more than three points with minimal distance.
  198. But can there be three points?
  199. \begin{figure}[htp]
  200. \centering
  201. \begin{tikzpicture}
  202. \begin{axis}[
  203. legend pos=north west,
  204. axis x line=middle,
  205. axis y line=middle,
  206. grid = major,
  207. width=0.8\linewidth,
  208. height=8cm,
  209. grid style={dashed, gray!30},
  210. xmin=-0.7, % start the diagram at this x-coordinate
  211. xmax= 0.7, % end the diagram at this x-coordinate
  212. ymin=-0.25, % start the diagram at this y-coordinate
  213. ymax= 0.5, % end the diagram at this y-coordinate
  214. axis background/.style={fill=white},
  215. xlabel=$x$,
  216. ylabel=$y$,
  217. %xticklabels={-2,-1.6,...,7},
  218. %yticklabels={-8,-7,...,8},
  219. tick align=outside,
  220. minor tick num=-3,
  221. enlargelimits=true,
  222. tension=0.08]
  223. \addplot[domain=-0.7:0.7, thick,samples=50, orange] {x*x};
  224. \draw (axis cs:0,0.5) circle[radius=0.5];
  225. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:0.101,0.0102);
  226. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:-0.101,0.0102);
  227. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:0,0);
  228. \addlegendentry{$f(x)=x^2$}
  229. \end{axis}
  230. \end{tikzpicture}
  231. \caption{3 points with minimal distance?}
  232. \todo[inline]{Is this possible? http://math.stackexchange.com/q/553097/6876}
  233. \end{figure}
  234. As the point is already given, you want to minimize the following
  235. function:
  236. \begin{align}
  237. d: &\mdr \rightarrow \mdr^+_0\\
  238. d(x) &= \sqrt{(x_p,y_p),(x,f(x))}\\
  239. &= \sqrt{(x_p-x)^2 + (y_p - f(x))^2}\\
  240. &= \sqrt{x_p^2 - 2x_p x + x^2 + y_p^2 - 2y_p f(x) + f(x)^2}
  241. \end{align}
  242. Minimizing $d$ is the same as minimizing $d^2$:
  243. \begin{align}
  244. d(x)^2 &= x_p^2 - 2x_p x + x^2 + y_p^2 - 2y_p f(x) + f(x)^2\\
  245. (d(x)^2)' &= -2 x_p + 2x -2y_p(f(x))' + (f(x)^2)'\\
  246. 0 &\stackrel{!}{=} -2 x_p + 2x -2y_p(f(x))' + (f(x)^2)' \label{eq:minimizing}
  247. \end{align}
  248. Now we use thet $f(x) = ax^2 + bx + c$:
  249. \begin{align}
  250. 0 &\stackrel{!}{=} -2 x_p + 2x -2y_p(2ax+b) + ((ax^2+bx+c)^2)'\\
  251. &= -2 x_p + 2x -2y_p \cdot 2ax-2 y_p b + (a^2 x^4+2 a b x^3+2 a c x^2+b^2 x^2+2 b c x+c^2)'\\
  252. &= -2 x_p + 2x -4y_p ax-2 y_p b + (4a^2 x^3 + 6 ab x^2 + 4acx + 2b^2 x + 2bc)\\
  253. &= 4a^2 x^3 + 6 ab x^2 + 2(1 -2y_p a+ 2ac + b^2)x +2(bc-by_p-x_p)\\
  254. \end{align}
  255. \subsubsection{Solutions}
  256. As the problem stated above is a cubic equation, you can solved it
  257. analytically. But the solutions are not very nice, so I've entered
  258. \texttt{$0=4*a^2 *x^3 + 6 *a*b *x^2 + 2*(1 -2*e *a+ 2*a*c + b^2)*x +2*(b*c-b*e-d)$}
  259. with $d := x_p$ and $e := y_p$.
  260. to \href{http://www.wolframalpha.com/input/?i=0%3D4*a%5E2+*x%5E3+%2B+6+*a*b+*x%5E2+%2B+2*%281+-2*e+*a%2B+2*a*c+%2B+b%5E2%29*x+%2B2*%28b*c-b*e-d%29}{WolframAlpha} to let it solve. The solutions are:
  261. \textbf{First solution}
  262. \begin{align*}
  263. x = &\frac{1}{6 \sqrt[3]{2} a^2} \sqrt[3]{(108 a^4 d+54 a^3 b+\sqrt{(108 a^4 d+54 a^3 b)^2+4 (12 a^3 c-12 a^3 e-3 a^2 b^2+6 a^2)^3})}\\
  264. &-\frac{12 a^3 c-12 a^3 e-3 a^2 b^2+6 a^2}
  265. {3 (2^{\frac{2}{3}}) a^2 \sqrt[3]{108 a^4 d+54 a^3 b+\sqrt{(108 a^4 d+54 a^3 b)^2+4 (12 a^3 c-12 a^3 e-3 a^2 b^2+6 a^2)^3}} }-b/(2 a)
  266. \end{align*}
  267. So the minimum for $a=1, b=c=d=0$ is:
  268. \subsection{Calculate points with minimal distance}
  269. \todo[inline]{Write this}
  270. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  271. % Cubic %
  272. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  273. \section{Minimal distance to a cubic function}
  274. Let $f(x) = a \cdot x^3 + b \cdot x^2 + c \cdot x + d$ be a cubic function
  275. with $a \in \mdr \setminus \Set{0}$ and
  276. $b, c, d \in \mdr$ be a function.
  277. \subsection{Number of points with minimal distance}
  278. \todo[inline]{Write this}
  279. \subsection{Special points}
  280. \todo[inline]{Write this}
  281. \subsection{Voronoi}
  282. For $b^2 \geq 3ac$
  283. \todo[inline]{Write this}
  284. \subsection{Calculate points with minimal distance}
  285. When you want to calculate points with minimal distance, you can
  286. take the same approach as in Equation \ref{eq:minimizing}:
  287. \begin{align}
  288. 0 &\stackrel{!}{=} -2 x_p + 2x -2y_p(f(x))' + (f(x)^2)'\\
  289. \Leftrightarrow 0 &\stackrel{!}{=} 2 f(x) \cdot f'(x) - 2 y_p f'(x) + 2x - 2 x_p\\
  290. \Leftrightarrow 0 &\stackrel{!}{=} \underbrace{\left (2 f(x) - 2 y_p \right ) \cdot f'(x)}_{\text{Polynomial of degree 5}} + \underbrace{2x - 2 x_p}_{\text{:-(}}
  291. \end{align}
  292. \todo[inline]{Write this}
  293. \end{document}