Forcing positive values when solving system of linear equations
17 views (last 30 days)
Show older comments
For finding the least squared error solution for X for the equation:
Ax = B
I am currently doing:
x = pinv(A) * B
However, in my case, there are negative values for X in the results, which is non desireable.
Is there a way to force X to be all non-negative values when solving the equation Ax = B using preferably SVD?
0 Comments
Accepted Answer
John D'Errico
on 15 Dec 2021
Edited: John D'Errico
on 15 Dec 2021
Sorry. PINV does not allow you to constrain the sign of your estimates.
You can use LSQNONNEG, which does allow you to do that, or you can use a tool like LSQLIN (from the optimization toolbox.)
For eample here, A is a 100x7 matri, with rank 5 by design.
A = randn(100,5)*randn(5,7);
rank(A)
b = randn(100,1);
As you can see, A\b gets upset at you, because A is singular.
A\b
However lsqnonneg does not yell at you about a singular matrix A, so you may prefer that. But does it explicitly use the SVD? No. It uses an active set strategy, choosing a subset of the unknowns to be non-negative.
lsqnonneg(A,b)
0 Comments
More Answers (0)
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!