Find point in the image of a linear function that is closest to some other point I have a linear function $f: \mathbb{R}^m\rightarrow\mathbb{R}^n$ with $n>m$ of the form:
$$
y \equiv f(x) = M\cdot x
$$
where $M$ is some coefficient matrix of shape $(n,m)$. For example, for $m=2, n=3$ the image would be a 2d plane embedded in 3d space.
Now I have another point $p\in\mathbb{R}^n$ and I want to find the value of $x$ that corresponds to the point in the image of $f$ which has the smallest Euclidean distance to $p$.
So I figured out, since $m<n$, there must be a line in $\mathbb{R}^n$, described by $y_l = m\cdot t + b \;\; (m,b\in\mathbb{R}^n, t\in\mathbb{R}$), that is perpendicular to the image of $f$ in a sense that:
$$
(mt + b)^T \cdot (Mx)  = 0 \quad \forall x\in\mathbb{R}^m, t\in\mathbb{R}
$$
i.e. the dot product between any two vectors corresponding to, respectively, the line and the image of the function $f$, is zero.
Secondly, the line must pass through the point $p$, i.e.
$$
mt_p + b = p
$$
for some value of $t_p$.
Using these relations together gives
$$
(m(t-t_p) + p)^T \cdot Mx = 0
$$
which is equivalent to
$$
(mt + p)^T \cdot Mx = 0
$$
since the equation must hold for all $t\in\mathbb{R}$.
At this point, however, I'm stuck in deriving the value of $m$, in dependence on $x$ and $p$, so that the equation is satisfied. It seems I could first compute the product $(mt + p)^T \cdot M$ which gives me some vector in $\mathbb{R}^m$ depending on the choice of $m$, but that can't be perpendicular to all vectors $x\in\mathbb{R}^m$; this seems to contradict the initial assumption, but I can't see why it wouldn't be valid (consider for example the $m=2,n=3$ case).
Once the line equation is known, the point $x$ can be derived via
$$
\begin{aligned}
Mx &= mt + b \\
Mx - mt &= b
\end{aligned}
$$
which can be solved by constructing a new matrix by augmenting $M$ with a new column $m$:
$$
\begin{pmatrix} M & | & m \end{pmatrix}
\begin{pmatrix} x \\ t \end{pmatrix}
= b
$$
However, as mentioned above, I'm stuck with deriving the parameters of the line equation, so any help is greatly appreciated.
 A: The vector $x$ that you are looking for is known as the least squares solution to the equation $M \cdot x = p$. It turns out that there is a very straightforward solution to this problem. A vector $x$ is a least solution if and only if we have
$$
M^TMx = M^Tp.
$$
Thus, if the columns of $M$ are linearly independent, the (unique) solution is
$$
x = (M^TM)^{-1}M^Tp.
$$
If $M$ has linearly dependent columns, then the solution is no longer unique. If we want the vector $x$ among these solutions for which the Euclidean norm $\|x\|$ is minimized, then we could use the MP pseudoinverse $M^+$ of $M$ to get
$$
x = M^+p.
$$

Claim: If $x$ is a least squares solution to $Mx = p$, then $M^TMx = M^Tp$.
Proof: Let $x$ denote the least squares solution. As you note, the line connecting $Mx$ to $p$ must be perpendicular to the image of $f$. On the other hand, for $z \in \Bbb R^n$
$$
z^T My = 0 \quad \forall y \in \Bbb R^m \iff z^TM = 0 \iff (z^TM)^T = M^Tz = 0.
$$
That is, a vector $z$ is orthogonal to the image of $f$ iff $M^Tz = 0$.  Now, the vector $p - Mx$ points from $p$ to the closest point within the image of $f$, so this vector is orthogonal to the image of $f$. That is, we have
$$
0 = M^T(p - Mx) \implies 0 = M^Tp - M^TMx \implies M^TMx = M^Tp,
$$
which was what we wanted.
