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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. % Define theorems %
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. \theoremstyle{break}
  21. \setlength\theoremindent{0.7cm}
  22. \theoremheaderfont{\kern-0.7cm\normalfont\bfseries}
  23. \theorembodyfont{\normalfont} % nicht mehr kursiv
  24. \newframedtheorem{theorem}{Theorem}[section]
  25. \newframedtheorem{lemma}[theorem]{Lemma}
  26. \newtheorem{plaindefinition}{Definition}
  27. \newenvironment{definition}{\begin{plaindefinition}}{\end{plaindefinition}}
  28. \newenvironment{definition*}{\begin{plaindefinition*}}{\end{plaindefinition*}}
  29. \newtheorem{example}{Example}
  30. \theoremstyle{nonumberplain}
  31. \newtheorem{proof}{Proof:}
  32. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  33. \title{Minimal distance to a cubic function}
  34. \author{Martin Thoma}
  35. \hypersetup{
  36. pdfauthor = {Martin Thoma},
  37. pdfkeywords = {},
  38. pdftitle = {Minimal Distance}
  39. }
  40. \def\mdr{\ensuremath{\mathbb{R}}}
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. % Begin document %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. \begin{document}
  45. \maketitle
  46. \begin{abstract}
  47. When you have a selfdriving car, you have to plan which path you
  48. want to take. A reasonable choice for the representation of this
  49. path is a cubic spline. But you also have to be able to calculate
  50. how to steer to get or to remain on this path. A way to do this
  51. is applying the \href{https://en.wikipedia.org/wiki/PID_algorithm}{PID algorithm}.
  52. But this algorithm needs to know the current error. So you need to
  53. be able to get the minimal distance of a point to a cubic spline.
  54. As you need to get the signed error (and one steering direction might
  55. be prefered), it is not only necessary to
  56. get the minimal absolute distance, but also to get all points
  57. on the spline with minimal distance.
  58. In this paper I want to discuss how to find all points on a cubic
  59. function with minimal distance to a given point.
  60. As other representations of paths might be easier to understand and
  61. to implement, I will also cover the problem of finding the minimal
  62. distance of a point to a polynomial of degree 0, 1 and 2.
  63. \end{abstract}
  64. \section{Description of the Problem}
  65. Let $f: \mdr \rightarrow \mdr$ be a polynomial function and $P \in \mdr^2$
  66. be a point. Let $d_{P,f}: \mdr^2 \rightarrow \mdr_0^+$
  67. be the Euklidean distance $d_{P,f}$ of a point $P$ and a point $\left (x, f(x) \right )$:
  68. \[d_{P,f} (x) := \sqrt{(x_P - x)^2 + (y_P - f(x))^2}\]
  69. Now there is \todo{Should I proof this?}{finite set} $x_1, \dots, x_n$ such that
  70. \[\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)\]
  71. Essentially, you want to find the minima $x_1, \dots, x_n$ for given
  72. $f$ and $P$.
  73. But minimizing $d_{P,f}$ is the same as minimizing $d_{P,f}^2$:
  74. \begin{align}
  75. d_{P,f}(x)^2 &= \sqrt{(x_P - x)^2 + (y_P - f(x))^2}^2\\
  76. &= x_p^2 - 2x_p x + x^2 + y_p^2 - 2y_p f(x) + f(x)^2
  77. \end{align}
  78. \todo[inline]{hat dieser Satz einen Namen? kann ich den irgendwo zitieren? Ich habe ihn aus \href{https://github.com/MartinThoma/LaTeX-examples/tree/master/documents/Analysis I}{meinem Analysis I Skript} (Satz 22.3).}
  79. \begin{theorem}[Minima of polynomial functions]\label{thm:minima-of-polynomials}
  80. Let $n \in \mathbb{N}, n \geq 2$, $f$ polynomial function of
  81. degree $n$, $x_0 \in \mathbb{R}$, \\
  82. $f'(x_0) = f''(x_0) = \dots = f^{(n-1)} (x_0) = 0$
  83. and $f^{(n)} > 0$.
  84. Then $x_0$ is a local minimum of $f$.
  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] {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 slope of $f_\bot$
  172. is $- \frac{1}{m}$. Now you can calculate $f_\bot$:\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. Now find the point $(x, f(x))$ where the perpendicular crosses the function:
  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. %xticklabels={-2,-1.6,...,7},
  212. %yticklabels={-8,-7,...,8},
  213. tick align=outside,
  214. minor tick num=-3,
  215. enlargelimits=true,
  216. tension=0.08]
  217. \addplot[domain=-3:3, thick,samples=50, red] {0.5*x*x};
  218. \addplot[domain=-3:3, thick,samples=50, green] {x*x};
  219. \addplot[domain=-3:3, thick,samples=50, blue] {x*x + x};
  220. \addplot[domain=-3:3, thick,samples=50, orange] {x*x + 2*x};
  221. \addplot[domain=-3:3, thick,samples=50, black] {-x*x + 6};
  222. \addlegendentry{$f_1(x)=\frac{1}{2}x^2$}
  223. \addlegendentry{$f_2(x)=x^2$}
  224. \addlegendentry{$f_3(x)=x^2+x$}
  225. \addlegendentry{$f_4(x)=x^2+2x$}
  226. \addlegendentry{$f_5(x)=-x^2+6$}
  227. \end{axis}
  228. \end{tikzpicture}
  229. \caption{Quadratic functions}
  230. \end{figure}
  231. \subsection{Calculate points with minimal distance}
  232. We use Theorem~\ref{thm:minima-of-polynomials}:\nobreak
  233. \begin{align}
  234. 0 &\stackrel{!}{=} (d_{P,f}^2)'\\
  235. &= -2 x_p + 2x -2y_p f'(x) + \left (f(x)^2 \right )'\label{eq:minimizingFirstDerivative}\\
  236. &= -2 x_p + 2x -2y_p (2ax+b) + ((ax^2+bx+c)^2)'\\
  237. &= -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)'\\
  238. &= -2 x_p + 2x -4y_p ax-2 y_p b + (4a^2 x^3 + 6 ab x^2 + 4acx + 2b^2 x + 2bc)\\
  239. &= 4a^2 x^3 + 6 ab x^2 + 2(1 -2y_p a+ 2ac + b^2)x +2(bc-by_p-x_p)\\
  240. 0 &\stackrel{!}{=}(d_{P,f}^2)''\\
  241. &= 12a^2 x^2 + 12abx + 2(1 -2y_p a+ 2ac + b^2)
  242. \end{align}
  243. This is an algebraic equation of degree 3.
  244. There can be up to 3 solutions in such an equation. An example is
  245. \begin{align*}
  246. a &= 1 & b &= 0 & c &= 1 & x_p &= 0 & y_p &= 1
  247. \end{align*}
  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. \subsection{Number of points with minimal distance}
  256. It is obvious that a quadratic function can have two points with
  257. minimal distance.
  258. For example, let $f(x) = x^2$ and $P = (0,5)$. Then $P_{f,1} \approx (2.179, 2.179^2)$
  259. has minimal distance to $P$, but also $P_{f,2}\approx (-2.179, 2.179^2)$.
  260. Obviously, there cannot be more than three points with minimal distance.
  261. But can there be three points?
  262. \begin{figure}[htp]
  263. \centering
  264. \begin{tikzpicture}
  265. \begin{axis}[
  266. legend pos=north west,
  267. axis x line=middle,
  268. axis y line=middle,
  269. grid = major,
  270. width=0.8\linewidth,
  271. height=8cm,
  272. grid style={dashed, gray!30},
  273. xmin=-0.7, % start the diagram at this x-coordinate
  274. xmax= 0.7, % end the diagram at this x-coordinate
  275. ymin=-0.25, % start the diagram at this y-coordinate
  276. ymax= 0.5, % end the diagram at this y-coordinate
  277. axis background/.style={fill=white},
  278. xlabel=$x$,
  279. ylabel=$y$,
  280. %xticklabels={-2,-1.6,...,7},
  281. %yticklabels={-8,-7,...,8},
  282. tick align=outside,
  283. minor tick num=-3,
  284. enlargelimits=true,
  285. tension=0.08]
  286. \addplot[domain=-0.7:0.7, thick,samples=50, orange] {x*x};
  287. \draw (axis cs:0,0.5) circle[radius=0.5];
  288. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:0.101,0.0102);
  289. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:-0.101,0.0102);
  290. \draw[red, thick] (axis cs:0,0.5) -- (axis cs:0,0);
  291. \addlegendentry{$f(x)=x^2$}
  292. \end{axis}
  293. \end{tikzpicture}
  294. \caption{3 points with minimal distance?}
  295. \end{figure}
  296. \todo[inline]{write this}
  297. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  298. % Cubic %
  299. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  300. \section{Minimal distance to a cubic function}
  301. Let $f(x) = a \cdot x^3 + b \cdot x^2 + c \cdot x + d$ be a cubic function
  302. with $a \in \mdr \setminus \Set{0}$ and
  303. $b, c, d \in \mdr$ be a function.
  304. \subsection{Number of points with minimal distance}
  305. \todo[inline]{Write this}
  306. \subsection{Special points}
  307. \todo[inline]{Write this}
  308. \subsection{Voronoi}
  309. For $b^2 \geq 3ac$
  310. \todo[inline]{Write this}
  311. \subsection{Calculate points with minimal distance}
  312. When you want to calculate points with minimal distance, you can
  313. take the same approach as in Equation \ref{eq:minimizingFirstDerivative}:
  314. \begin{align}
  315. 0 &\stackrel{!}{=} -2 x_p + 2x -2y_p(f(x))' + (f(x)^2)'\\
  316. \Leftrightarrow 0 &\stackrel{!}{=} 2 f(x) \cdot f'(x) - 2 y_p f'(x) + 2x - 2 x_p\\
  317. \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{:-(}}
  318. \end{align}
  319. \todo[inline]{Write this}
  320. \end{document}