2013-11-05 20:30:34 +01:00
\documentclass [a4paper] { scrartcl}
\usepackage { amssymb, amsmath} % needed for math
2013-11-13 08:50:04 +01:00
\usepackage { mathtools} % \xRightarrow
2013-11-05 20:30:34 +01:00
\usepackage [utf8] { inputenc} % this is needed for umlauts
\usepackage [english] { babel} % this is needed for umlauts
\usepackage [T1] { fontenc} % this is needed for correct output of umlauts in pdf
\usepackage [margin=2.5cm] { geometry} %layout
\usepackage { hyperref} % links im text
\usepackage { braket} % needed for \Set
\usepackage { parskip}
\usepackage [colorinlistoftodos] { todonotes}
\usepackage { pgfplots}
\pgfplotsset { compat=1.7,compat/path replacement=1.5.1}
\usepackage { tikz}
\title { Minimal distance to a cubic function}
\author { Martin Thoma}
\hypersetup {
pdfauthor = { Martin Thoma} ,
pdfkeywords = { } ,
pdftitle = { Minimal Distance}
}
\def \mdr { \ensuremath { \mathbb { R} } }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Begin document %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin { document}
\maketitle
\begin { abstract}
2013-11-22 21:40:15 +01:00
When you have a selfdriving car, you have to plan which path you
want to take. A reasonable choice for the representation of this
path is a cubic spline. But you also have to be able to calculate
how to steer to get or to remain on this path. A way to do this
is applying the \href { https://en.wikipedia.org/wiki/PID_ algorithm} { PID algorithm} .
But this algorithm needs to know the current error. So you need to
be able to get the minimal distance of a point to a cubic spline.
As you need to get the signed error (and one steering direction might
be prefered), it is not only necessary to
get the minimal absolute distance, but also to get all points
on the spline with minimal distance.
In this paper I want to discuss how to find all points on a cubic
2013-11-05 20:30:34 +01:00
function with minimal distance to a given point.
2013-11-22 21:40:15 +01:00
As other representations of paths might be easier to understand and
to implement, I will also cover the problem of finding the minimal
distance of a point to a polynomial of degree 0, 1 and 2.
2013-11-05 20:30:34 +01:00
\end { abstract}
\section { Description of the Problem}
Let $ f: \mdr \rightarrow \mdr $ be a polynomial function and $ P \in \mdr ^ 2 $
be a point. Let $ d: \mdr ^ 2 \times \mdr ^ 2 \rightarrow \mdr _ 0 ^ + $
2013-11-22 21:40:15 +01:00
be the Euklidean distance of two points:
2013-11-05 20:30:34 +01:00
\[ d \left ( ( x _ 1 , y _ 1 ) , ( x _ 2 , y _ 2 ) \right ) : = \sqrt { ( x _ 1 - x _ 2 ) ^ 2 + ( y _ 1 - y _ 2 ) ^ 2 } \]
2013-11-22 21:40:15 +01:00
Now there is \todo { Should I proof this?} { finite set} $ x _ 1 , \dots , x _ n $ such that
2013-11-05 20:30:34 +01:00
\[ \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 ) ) ) \]
2013-11-22 21:40:15 +01:00
The task is now to find those $ x _ 1 , \dots , x _ n $ for given $ f, P $ .
2013-11-05 20:30:34 +01:00
\section { Minimal distance to a constant function}
2013-11-22 21:40:15 +01:00
Let $ f ( x ) = c $ with $ c \in \mdr $ be a constant function.
2013-11-05 20:30:34 +01:00
2013-11-05 20:47:41 +01:00
\begin { figure} [htp]
\centering
\begin { tikzpicture}
\begin { axis} [
legend pos=north west,
axis x line=middle,
axis y line=middle,
grid = major,
width=0.8\linewidth ,
height=8cm,
grid style={ dashed, gray!30} ,
xmin=-5, % start the diagram at this x-coordinate
xmax= 5, % end the diagram at this x-coordinate
ymin= 0, % start the diagram at this y-coordinate
ymax= 3, % end the diagram at this y-coordinate
axis background/.style={ fill=white} ,
xlabel=$ x $ ,
ylabel=$ y $ ,
tick align=outside,
minor tick num=-3,
enlargelimits=true,
tension=0.08]
\addplot [domain=-5:5, thick,samples=50, red] { 1} ;
\addplot [domain=-5:5, thick,samples=50, green] { 2} ;
\addplot [domain=-5:5, thick,samples=50, blue] { 3} ;
\addplot [black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates { (2, 2)} ;
2013-11-22 21:40:15 +01:00
\addplot [blue, mark = *, nodes near coords=$P_{h,\text{min}}$,every node near coord/.style={anchor=225}] coordinates { (2, 3)} ;
\addplot [green, mark = x, nodes near coords=$P_{g,\text{min}}$,every node near coord/.style={anchor=120}] coordinates { (2, 2)} ;
\addplot [red, mark = *, nodes near coords=$P_{f,\text{min}}$,every node near coord/.style={anchor=225}] coordinates { (2, 1)} ;
2013-11-05 20:47:41 +01:00
\draw [thick, dashed] (axis cs:2,0) -- (axis cs:2,3);
\addlegendentry { $ f ( x ) = 1 $ }
\addlegendentry { $ g ( x ) = 2 $ }
\addlegendentry { $ h ( x ) = 3 $ }
\end { axis}
\end { tikzpicture}
2013-11-22 21:40:15 +01:00
\caption { 3 constant functions and their points with minimal distance}
\label { fig:constant-min-distance}
2013-11-05 20:47:41 +01:00
\end { figure}
2013-11-05 20:30:34 +01:00
Then $ ( x _ P,f ( x _ P ) ) $ has
minimal distance to $ P $ . Every other point has higher distance.
2013-11-22 21:40:15 +01:00
See Figure~\ref { fig:constant-min-distance} .
2013-11-05 20:30:34 +01:00
\section { Minimal distance to a linear function}
Let $ f ( x ) = m \cdot x + t $ with $ m \in \mdr \setminus \Set { 0 } $ and
2013-11-22 21:40:15 +01:00
$ t \in \mdr $ be a linear function.
2013-11-05 20:30:34 +01:00
2013-11-05 20:47:41 +01:00
\begin { figure} [htp]
\centering
\begin { tikzpicture}
\begin { axis} [
legend pos=north east,
axis x line=middle,
axis y line=middle,
grid = major,
width=0.8\linewidth ,
height=8cm,
grid style={ dashed, gray!30} ,
xmin= 0, % start the diagram at this x-coordinate
xmax= 5, % end the diagram at this x-coordinate
ymin= 0, % start the diagram at this y-coordinate
ymax= 3, % end the diagram at this y-coordinate
axis background/.style={ fill=white} ,
xlabel=$ x $ ,
ylabel=$ y $ ,
tick align=outside,
minor tick num=-3,
enlargelimits=true,
tension=0.08]
\addplot [domain=-5:5, thick,samples=50, red] { 0.5*x} ;
\addplot [domain=-5:5, thick,samples=50, blue] { -2*x+6} ;
\addplot [black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates { (2, 2)} ;
\addlegendentry { $ f ( x ) = \frac { 1 } { 2 } x $ }
\addlegendentry { $ g ( x ) = - 2 x + 6 $ }
\end { axis}
\end { tikzpicture}
\caption { The shortest distance of $ P $ to $ f $ can be calculated by using the perpendicular}
2013-11-22 21:40:15 +01:00
\label { fig:linear-min-distance}
2013-11-05 20:47:41 +01:00
\end { figure}
2013-11-05 20:30:34 +01:00
2013-11-22 21:40:15 +01:00
Now you can drop a perpendicular $ f _ \bot $ through $ P $ on $ f ( x ) $ . The slope of $ f _ \bot $
is $ - \frac { 1 } { m } $ . Now you can calculate $ f _ \bot $ :\nobreak
2013-11-05 20:30:34 +01:00
\begin { align}
f_ \bot (x) & = - \frac { 1} { m} \cdot x + t_ \bot \\
\Rightarrow y_ P & = - \frac { 1} { m} \cdot x_ P + t_ \bot \\
2013-11-22 21:40:15 +01:00
\Leftrightarrow t_ \bot & = y_ P + \frac { 1} { m} \cdot x_ P
\end { align}
Now find the point $ ( x, f ( x ) ) $ where the perpendicular crosses the function:
\begin { align}
2013-11-05 20:30:34 +01:00
f(x) & = f_ \bot (x)\\
\Leftrightarrow m \cdot x + t & = - \frac { 1} { m} \cdot x + \left (y_ P + \frac { 1} { m} \cdot x_ P \right )\\
\Leftrightarrow \left (m + \frac { 1} { m} \right ) \cdot x & = y_ P + \frac { 1} { m} \cdot x_ P - t\\
\Leftrightarrow x & = \frac { m} { m^ 2+1} \left ( y_ P + \frac { 1} { m} \cdot x_ P - t \right )
\end { align}
2013-11-22 21:40:15 +01:00
There is only one point with minimal distance. See Figure~\ref { fig:linear-min-distance} .
2013-11-05 20:47:41 +01:00
\clearpage
2013-11-22 21:40:15 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quadratic functions %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2013-11-05 20:30:34 +01:00
\section { Minimal distance to a quadratic function}
Let $ f ( x ) = a \cdot x ^ 2 + b \cdot x + c $ with $ a \in \mdr \setminus \Set { 0 } $ and
2013-11-22 21:40:15 +01:00
$ b, c \in \mdr $ be a quadratic function.
2013-11-05 20:30:34 +01:00
\begin { figure} [htp]
\centering
\begin { tikzpicture}
\begin { axis} [
legend pos=north west,
axis x line=middle,
axis y line=middle,
grid = major,
width=0.8\linewidth ,
height=8cm,
grid style={ dashed, gray!30} ,
xmin=-3, % start the diagram at this x-coordinate
xmax= 3, % end the diagram at this x-coordinate
ymin=-0.25, % start the diagram at this y-coordinate
ymax= 9, % end the diagram at this y-coordinate
axis background/.style={ fill=white} ,
xlabel=$ x $ ,
ylabel=$ y $ ,
%xticklabels={-2,-1.6,...,7},
%yticklabels={-8,-7,...,8},
tick align=outside,
minor tick num=-3,
enlargelimits=true,
tension=0.08]
\addplot [domain=-3:3, thick,samples=50, red] { 0.5*x*x} ;
\addplot [domain=-3:3, thick,samples=50, green] { x*x} ;
\addplot [domain=-3:3, thick,samples=50, blue] { x*x + x} ;
\addplot [domain=-3:3, thick,samples=50, orange] { x*x + 2*x} ;
\addplot [domain=-3:3, thick,samples=50, black] { -x*x + 6} ;
\addlegendentry { $ f _ 1 ( x ) = \frac { 1 } { 2 } x ^ 2 $ }
\addlegendentry { $ f _ 2 ( x ) = x ^ 2 $ }
\addlegendentry { $ f _ 3 ( x ) = x ^ 2 + x $ }
\addlegendentry { $ f _ 4 ( x ) = x ^ 2 + 2 x $ }
\addlegendentry { $ f _ 5 ( x ) = - x ^ 2 + 6 $ }
\end { axis}
\end { tikzpicture}
\caption { Quadratic functions}
\end { figure}
\subsection { Number of points with minimal distance}
It is obvious that a quadratic function can have two points with
minimal distance.
For example, let $ f ( x ) = x ^ 2 $ and $ P = ( 0 , 5 ) $ . Then $ P _ { f, 1 } \approx ( 2 . 179 , 2 . 179 ^ 2 ) $
has minimal distance to $ P $ , but also $ P _ { f, 2 } \approx ( - 2 . 179 , 2 . 179 ^ 2 ) $ .
Obviously, there cannot be more than three points with minimal distance.
But can there be three points?
\begin { figure} [htp]
\centering
2013-11-05 20:47:41 +01:00
\begin { tikzpicture}
\begin { axis} [
legend pos=north west,
axis x line=middle,
axis y line=middle,
grid = major,
width=0.8\linewidth ,
height=8cm,
grid style={ dashed, gray!30} ,
xmin=-0.7, % start the diagram at this x-coordinate
xmax= 0.7, % end the diagram at this x-coordinate
ymin=-0.25, % start the diagram at this y-coordinate
ymax= 0.5, % end the diagram at this y-coordinate
axis background/.style={ fill=white} ,
xlabel=$ x $ ,
ylabel=$ y $ ,
%xticklabels={-2,-1.6,...,7},
%yticklabels={-8,-7,...,8},
tick align=outside,
minor tick num=-3,
enlargelimits=true,
tension=0.08]
\addplot [domain=-0.7:0.7, thick,samples=50, orange] { x*x} ;
\draw (axis cs:0,0.5) circle[radius=0.5];
\draw [red, thick] (axis cs:0,0.5) -- (axis cs:0.101,0.0102);
\draw [red, thick] (axis cs:0,0.5) -- (axis cs:-0.101,0.0102);
\draw [red, thick] (axis cs:0,0.5) -- (axis cs:0,0);
\addlegendentry { $ f ( x ) = x ^ 2 $ }
\end { axis}
\end { tikzpicture}
2013-11-05 20:30:34 +01:00
\caption { 3 points with minimal distance?}
2013-11-05 20:47:41 +01:00
\todo [inline] { Is this possible? http://math.stackexchange.com/q/553097/6876}
2013-11-05 20:30:34 +01:00
\end { figure}
2013-11-13 08:50:04 +01:00
As the point is already given, you want to minimize the following
function:
\begin { align}
d: & \mdr \rightarrow \mdr ^ +_ 0\\
d(x) & = \sqrt { (x_ p,y_ p),(x,f(x))} \\
& = \sqrt { (x_ p-x)^ 2 + (y_ p - f(x))^ 2} \\
& = \sqrt { x_ p^ 2 - 2x_ p x + x^ 2 + y_ p^ 2 - 2y_ p f(x) + f(x)^ 2}
\end { align}
Minimizing $ d $ is the same as minimizing $ d ^ 2 $ :
\begin { align}
d(x)^ 2 & = x_ p^ 2 - 2x_ p x + x^ 2 + y_ p^ 2 - 2y_ p f(x) + f(x)^ 2\\
(d(x)^ 2)' & = -2 x_ p + 2x -2y_ p(f(x))' + (f(x)^ 2)'\\
2013-11-22 21:40:15 +01:00
0 & \stackrel { !} { =} -2 x_ p + 2x -2y_ p(f(x))' + (f(x)^ 2)' \label { eq:minimizing}
2013-11-13 08:50:04 +01:00
\end { align}
Now we use thet $ f ( x ) = ax ^ 2 + bx + c $ :
\begin { align}
0 & \stackrel { !} { =} -2 x_ p + 2x -2y_ p(2ax+b) + ((ax^ 2+bx+c)^ 2)'\\
& = -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)'\\
& = -2 x_ p + 2x -4y_ p ax-2 y_ p b + (4a^ 2 x^ 3 + 6 ab x^ 2 + 4acx + 2b^ 2 x + 2bc)\\
& = 4a^ 2 x^ 3 + 6 ab x^ 2 + 2(1 -2y_ p a+ 2ac + b^ 2)x +2(bc-by_ p-x_ p)\\
\end { align}
\subsubsection { Solutions}
As the problem stated above is a cubic equation, you can solved it
analytically. But the solutions are not very nice, so I've entered
\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 ) $ }
with $ d : = x _ p $ and $ e : = y _ p $ .
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:
\textbf { First solution}
\begin { align*}
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} )} \\
& -\frac { 12 a^ 3 c-12 a^ 3 e-3 a^ 2 b^ 2+6 a^ 2}
{ 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)
\end { align*}
So the minimum for $ a = 1 , b = c = d = 0 $ is:
2013-11-05 20:30:34 +01:00
\subsection { Calculate points with minimal distance}
\todo [inline] { Write this}
2013-11-22 21:40:15 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Cubic %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2013-11-05 20:30:34 +01:00
\section { Minimal distance to a cubic function}
2013-11-22 21:40:15 +01:00
Let $ f ( x ) = a \cdot x ^ 3 + b \cdot x ^ 2 + c \cdot x + d $ be a cubic function
with $ a \in \mdr \setminus \Set { 0 } $ and
2013-11-05 20:47:41 +01:00
$ b, c, d \in \mdr $ be a function.
2013-11-05 20:30:34 +01:00
\subsection { Number of points with minimal distance}
2013-11-13 08:50:04 +01:00
2013-11-05 20:30:34 +01:00
\todo [inline] { Write this}
2013-11-05 20:47:41 +01:00
\subsection { Special points}
\todo [inline] { Write this}
\subsection { Voronoi}
For $ b ^ 2 \geq 3 ac $
\todo [inline] { Write this}
2013-11-05 20:30:34 +01:00
\subsection { Calculate points with minimal distance}
2013-11-22 21:40:15 +01:00
When you want to calculate points with minimal distance, you can
take the same approach as in Equation \ref { eq:minimizing} :
\begin { align}
0 & \stackrel { !} { =} -2 x_ p + 2x -2y_ p(f(x))' + (f(x)^ 2)'\\
\Leftrightarrow 0 & \stackrel { !} { =} 2 f(x) \cdot f'(x) - 2 y_ p f'(x) + 2x - 2 x_ p\\
\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 { :-(} }
\end { align}
2013-11-05 20:30:34 +01:00
\todo [inline] { Write this}
\end { document}