# Solving constrained system of linear equations with R

In R I would like to solve a system of linear equations with constraints to preserve monotonicity. I can do this easily with no constraints on the coefficients. Here is an example:

       [,1]      [,2]        [,3]      [,4]       [,5]      [,6]      [,7]
[1,] 0.6945405 0.1157702 0.004973632 0.0000000 0.00000000 0.0000000 0.1625076
[2,] 0.9212828 0.3055870 0.026655560 0.0000000 0.00000000 0.0000000 0.3916894
[3,] 1.0000000 0.9987081 0.835572186 0.1767705 0.00000000 0.0000000 6.6993305
[4,] 1.0000000 1.0000000 0.992828243 0.5758778 0.02530867 0.0000000 7.6371723
[5,] 1.0000000 1.0000000 0.997171672 0.6412910 0.04668548 0.0000000 7.6628770
[6,] 1.0000000 1.0000000 1.000000000 0.9970624 0.90614523 0.4305434 7.6796152


Columns 1-6 are the coefficients and column 7 is the right side of the linear system. This can easily be solved by:

solve(mat[,-ncol(mat)],mat[,ncol(mat)])


However if I want to put constraints where all coefficients must be greater than 0 is there an easy way to do this in R? If the solve function returns a negative coefficient does this indicate no solution exists where all the coefficients are positive?

-
Usually such constrains occur when you solve some linear programming problem, thus you have the gradient of the objective function to maximize (or minimize). For an invertible $A$ in $Ax = b$ the Karl's point goes the solution is unique for the system of linear equations with $n$ unknowns and $n$ equations. Probably ieeexplore.ieee.org/Xplore/… could help. –  Dmitrij Celov Sep 29 '11 at 22:30
If your 6x6 matrix is invertible, then there is a unique solution to the equations, which is what solve would give you.