# Skew lines/R^3/Distance/Extremal problem/Example

Let
    $G=P+\mathbb {R} v\,$
and
    $H=Q+\mathbb {R} w\,$
be skew lines. We want to understand the distance problem between the two lines as an extremal problem in the sense of higher-dimensional analysis. Let
    $P={\begin{pmatrix}a_{1}\\a_{2}\\a_{3}\end{pmatrix}}\,$
and
    $Q={\begin{pmatrix}b_{1}\\b_{2}\\b_{3}\end{pmatrix}}\,.$
The square of the distance between the two points
    $P'={\begin{pmatrix}a_{1}\\a_{2}\\a_{3}\end{pmatrix}}+s{\begin{pmatrix}v_{1}\\v_{2}\\v_{3}\end{pmatrix}}\,$
and
    $Q'={\begin{pmatrix}b_{1}\\b_{2}\\b_{3}\end{pmatrix}}+t{\begin{pmatrix}w_{1}\\w_{2}\\w_{3}\end{pmatrix}}\,$
is (setting $c_{i}=a_{i}-b_{i}$)
    ${\begin{aligned}d(P',Q')^{2}&={\left(c_{1}+sv_{1}-tw_{1}\right)}^{2}+{\left(c_{2}+sv_{2}-tw_{2}\right)}^{2}+{\left(c_{3}+sv_{3}-tw_{3}\right)}^{2}\\&=c_{1}^{2}+s^{2}v_{1}^{2}+t^{2}w_{1}^{2}+2sc_{1}v_{1}-2tc_{1}w_{1}-2stv_{1}w_{1}+c_{2}^{2}+s^{2}v_{2}^{2}+t^{2}w_{2}^{2}+2sa_{2}v_{2}-2tc_{2}w_{2}-2stv_{2}w_{2}+c_{3}^{2}+s^{2}v_{3}^{2}+t^{2}w_{3}^{2}+2sc_{3}v_{3}-2tc_{3}w_{3}-2stv_{3}w_{3}\\&=c_{1}^{2}+c_{2}^{2}+c_{3}^{2}+2s{\left(c_{1}v_{1}+c_{2}v_{2}+c_{3}v_{3}\right)}-2t{\left(c_{1}w_{1}+c_{2}w_{2}+c_{3}w_{3}\right)}+s^{2}{\left(v_{1}^{2}+v_{2}^{2}+v_{3}^{2}\right)}+t^{2}{\left(w_{1}^{2}+w_{2}^{2}+w_{3}^{2}\right)}-2st{\left(v_{1}w_{1}+v_{2}w_{2}+v_{3}w_{3}\right)}.\end{aligned}}$
We interpret this expression with methods of Analysis 2. We consider the data given by the lines as fixed parameters, so that we have an real-valued expression $f(s,t)$ in the two real variables $s$ and $t$, and we want to determine its extrema. The partial derivatives are
    ${\frac {\partial f}{\partial s}}=2{\left(c_{1}v_{1}+c_{2}v_{2}+c_{3}v_{3}\right)}+2s{\left(v_{1}^{2}+v_{2}^{2}+v_{3}^{2}\right)}-2t{\left(v_{1}w_{1}+v_{2}w_{2}+v_{3}w_{3}\right)}\,$
and
    ${\frac {\partial f}{\partial t}}=2{\left(c_{1}w_{1}+c_{2}w_{2}+c_{3}w_{3}\right)}+2t{\left(w_{1}^{2}+w_{2}^{2}+w_{3}^{2}\right)}-2s{\left(v_{1}w_{1}+v_{2}w_{2}+v_{3}w_{3}\right)}\,.$
If we equate this with $0$, then we obtain an inhomogeneous linear system of equations with two equations in the variables $s$ and $t$. Using Cramer's rule, we get
    $s={\frac {\det {\begin{pmatrix}-c_{1}v_{1}-c_{2}v_{2}-c_{3}v_{3}&-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}\\-c_{1}w_{1}-c_{2}w_{2}-c_{3}w_{3}&w_{1}^{2}+w_{2}^{2}+w_{3}^{2}\end{pmatrix}}}{\det {\begin{pmatrix}v_{1}^{2}+v_{2}^{2}+v_{3}^{2}&-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}\\-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}&w_{1}^{2}+w_{2}^{2}+w_{3}^{2}\end{pmatrix}}}}\,$
and
    $t={\frac {\det {\begin{pmatrix}v_{1}^{2}+v_{2}^{2}+v_{3}^{2}&-c_{1}v_{1}-c_{2}v_{2}-c_{3}v_{3}\\-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}&-c_{1}w_{1}-c_{2}w_{2}-c_{3}w_{3}\end{pmatrix}}}{\det {\begin{pmatrix}v_{1}^{2}+v_{2}^{2}+v_{3}^{2}&-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}\\-v_{1}w_{1}-v_{2}w_{2}-v_{3}w_{3}&w_{1}^{2}+w_{2}^{2}+w_{3}^{2}\end{pmatrix}}}}\,.$
If $v$ and $w$ are normed, then these expressions can be simplified to
    $s={\frac {-\left\langle P-Q,v\right\rangle -\left\langle P-Q,w\right\rangle \left\langle v,w\right\rangle }{1-\left\langle v,w\right\rangle ^{2}}}\,$
and
    $t={\frac {-\left\langle P-Q,w\right\rangle -\left\langle P-Q,v\right\rangle \left\langle v,w\right\rangle }{1-\left\langle v,w\right\rangle ^{2}}}\,.$