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

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