Solving Linear Least Squares with Linear Inequality Constraints Given:
$$
\begin{aligned}
A \in \mathbb{R}^{m \times n} \\
y \in \mathbb{R}^{m \times 1} \\
c, d \in \mathbb{R}^{m \times 1} \\
d \geq c \geq 0 \\
y \geq 0
\end{aligned}
$$
I seek $\hat{x}$:
$$ \hat{x} = \arg\min_{x} \|Ax - y\|_2 \\
s.t. \; \hat{x} \in \mathbb{R}^{m \times 1} , \; d \geq A \hat{x} \geq c$$
How do I compute $\hat{x}$?
 A: Royi's answer shows how to solve it with lsqlin.
You could also use alglib under the GPL license, using a form of an active-set algorithm.
Active set resources:


*

*http//www.alglib.net/optimization/boundandlinearlyconstrained.php#header1

*http//en.wikipedia.org/wiki/Active_set

*http//research.harkegard.se/papers/cdc2002_ca.pdf

A: By defining the problem as following:
$$
\begin{aligned}
A \in \mathbb{R}^{m \times n} \\
y \in \mathbb{R}^{m \times 1} \\
c, d \in \mathbb{R}^{m \times 1} \\
d \geq c \geq 0
\end{aligned}
$$
We're after:
$$ \hat{x} = \arg\min_{x} \|Ax - y\|_2 \\
s.t. \; \hat{x} \in \mathbb{R}^{m \times 1} , \; A \hat{x} \geq c , \; A \hat{x} \leq d $$
Since $ c $ is positive, we can also write the following:
$$ A x \geq c \Leftrightarrow -A x \leq -c $$
Now, by defining $ \tilde{A} = \begin{bmatrix}
-A \\ 
A
\end{bmatrix} $ we can rewrite:
$$ Ax \geq c, Ax \leq d \Leftrightarrow \tilde{A} x = \begin{bmatrix}
-A \\ 
A
\end{bmatrix} x \leq e = 
\begin{bmatrix}
-c \\ 
d
\end{bmatrix} $$
Namely the problem has become:
$$ \hat{x} = \arg\min_{x} \|Ax - y\|_2 \\
s.t. \; \tilde{A} \hat{x} \leq e $$
Which is now in the regular form of Quadratic Programming and can be solved using lsqlin.
