Distance between affine spaces using least squares method Let's represent an $n$-dimensional affine space $L$ as an affine frame, so an origin point $p$ and linear basis $\{ e_1, e_2, ..., e_n \}$.
How to compute the minimum Euclidean distance between affine spaces $L_1$ and $L_2$ using the least squares method?
 A: Here is how I would try: let the two spaces be ${\cal A}$ and ${\cal B}$
and let $A\in {\cal A}$ and $B \in {\cal B}$ be two points that minimize $d(A, B)^2$. A necessary and sufficient condition is that the line $AB$ is orthogonal to the affine manifolds ${\cal A}$ and ${\cal B}$. Let the coordinates of $A$ and $B$ in the frame $(p, e_1,\ldots,e_n)$ be
$$A(a_1,\ldots, a_n),\quad B(b_1,\ldots, b_n)$$
Let ${\cal A}$ be defined by affine conditions
$$\varphi_i(a_1,\ldots, a_n) = \alpha_i$$
Let ${\cal B}$ be defined by affine conditions
$$\psi_j(b_1,\ldots, b_n) = \beta_j$$
Assuming $e_1,\ldots,e_n$ is an orthonormal basis, the orthogonality condition leads to the linear system
$$
\left\{
\begin{array}\\
\varphi_i(a_1,\ldots, a_n) = \alpha_i\\
\psi_j(b_1,\ldots, b_n) = \beta_j\\
B - A = \sum_i {\lambda_i\varphi_i}\\
B - A = \sum_j \mu_j\psi_j
\end{array}
\right.
$$
Solve this in the unknowns $a_k$, $b_k$, $\lambda_k$, $\mu_k$. The distance between the manifolds is then $d(A, B)$.
Numerical example: take
the two straight lines in ${\mathbb{R}}^{3}$ defined respectively
by the equations
$$\left\{\begin{array}{rcl}x+y+z&=&1\\
2 x-y+3 z&=&2
\end{array}\right.$$
and
$$\left\{\begin{array}{rcl}x-2 y-z&=&{-1}\\
3 x-4 y+z&=&1
\end{array}\right.$$
The above procedure leads to the system
$$\left\{\begin{array}{rcllllllllll}1&=&{a}_{1}&{+{a}_{2}}&{+{a}_{3}}&&&&&&&\\
2&=&2 {a}_{1}&{-{a}_{2}}&{+3} {a}_{3}&&&&&&&\\
{-1}&=&&&&{b}_{1}&{-2} {b}_{2}&{-{b}_{3}}&&&&\\
1&=&&&&3 {b}_{1}&{-4} {b}_{2}&{+{b}_{3}}&&&&\\
0&=&{a}_{1}&&&{-{b}_{1}}&&&{-{{\lambda}}_{1}}&{-2} {{\lambda}}_{2}&&\\
0&=&&{a}_{2}&&&{-{b}_{2}}&&{-{{\lambda}}_{1}}&{+{{\lambda}}_{2}}&&\\
0&=&&&{a}_{3}&&&{-{b}_{3}}&{-{{\lambda}}_{1}}&{-3} {{\lambda}}_{2}&&\\
0&=&{a}_{1}&&&{-{b}_{1}}&&&&&{-{{\mu}}_{1}}&{-3} {{\mu}}_{2}\\
0&=&&{a}_{2}&&&{-{b}_{2}}&&&&{+2} {{\mu}}_{1}&{+4} {{\mu}}_{2}\\
0&=&&&{a}_{3}&&&{-{b}_{3}}&&&{+{{\mu}}_{1}}&{-{{\mu}}_{2}}
\end{array}\right.$$
The unique solution is
$$A = \left(\frac{11}{195} , \frac{46}{195} , \frac{46}{65}\right) \qquad  B = \left(\frac{1}{5} , \frac{2}{15} , \frac{14}{15}\right)$$
$$\left({{\lambda}}_{1} , {{\lambda}}_{2}\right) = \left(\frac{4}{195} , \frac{{-16}}{195}\right) \qquad  \left({{\mu}}_{1} , {{\mu}}_{2}\right) = \left(\frac{4}{30} , \frac{{-6}}{65}\right)$$
The distance between the two lines is
$$d \left(A , B\right) = \sqrt{\frac{16}{195}}$$
