# Finding the distance from a point to a $R^m$ restricted area in $R^n$

The problem is described below:

When m=2 and n=3, it is basically finding the distance between a point and a line segment in $$R^3$$.

But when both m and n are larger, do I have to use a generic optimizer to solve this, or this problem can be precisely solved with mathematics, like the case when m=2 and n=3?

What I have done for now:

Approach A:

I tried to solve it with Gram–Schmidt process and projection but got stuck.

For example, the following R code:

P <- c(1,1,1,1)

m <- rbind(c(1,-1,1,2)*1/3,c(1,2,1,1)*1/5)

m2 <- qr.Q(qr(t(m)))

P2 <- P%*%m2[,1]*m2[,1]+P%*%m2[,2]*m2[,2]


It does not take into account the restriction $$w_1+w_2=1$$

Approach B:

Tried to solve it with lagrangian optimization, but also got stuck there.

• Don't use images as main content of your post. This makes your question impossible to search and inaccessible to the visually impaired; we don't like that. Please transcribe text and mathematics. You can use LaTeX. Don't forget to give proper attribution to your sources! – D.W. Jun 18 at 2:49

Given a matrix $$M$$ and vector $$y$$, you are trying to find a vector $$x$$ that minimizes $$\|Mx-y\|_2$$, subject to $$x \ge 0$$ and $$\|x\|_1=1$$.
Without the additional constraints on $$x$$, this would be a least squares problem, which could be solved through linear algebra (see https://en.wikipedia.org/wiki/Linear_least_squares), or with gradient descent.