# Non-negative solution to matrix equation

I want to solve $Ax = b$ subject to the constraint that all of the elements of $x$ are non-negative. If such a solution does not exist, I want to find non-negative $x$ such that the quadratic form $(Ax - b)^T (Ax-b)$ is minimized. What kind of algorithm will let me do this?

-
what are dimensions of matrix $A$? Is it square, more columns than rows or vice versa? – mpiktas Apr 16 '11 at 18:14

Put differently, you want to solve the following optimization problem $$\min_x \|Ax-b\|^2\ \text{s. t.}\ x\geq 0.$$ This is a quadratic and convexly constrained minimization problem which can be solved in many ways. There are several software packages around to do the task, for example the quadprog routine of the Matlab Optimization toolbox should work here. However, the choice should at least depend on the size and the type of the matrix.