How do you impose a constraint that all values in a vector you are trying to optimize for are greater than zero, using fmincon()?
According to the documentation, I need some parameters A and b, where A*x ≤ b, but I think if I make A a vector of -1’s and b 0, then I will have optimized for the sum of x>0, instead of each value of x greater than 0.
Just in case you need it, here is my code. I am trying to optimize over a vector (x) such that the (componentwise) product of x and a matrix (called multiplierMatrix) makes a matrix for which the sum of the columns is x.
function [sse] = myfun(x) % this is a nested function
bigMatrix = repmat(x,1,120) .* multiplierMatrix;
answer = sum(bigMatrix,1)';
sse = sum((expectedAnswer - answer).^2);
end
xGuess = ones(1:120,1);
[sse xVals] = fmincon(@myfun,xGuess,???);
Let me know if I need to explain my problem better. Thanks for your help in advance!
You can use the lower bound:
note that
xValsandsseshould probably be swapped (if their name means anything).The lower bound
lbmeans that elements in your decision variablexwill never fall below the corresponding element inlb, which is what you are after here.The empties (
[]) indicate you’re not using linear constraints (e.g.,A,b,Aeq,beq), only the lower boundslb.Some advice:
fminconis a pretty advanced function. You’d better memorize the documentation on it, and play with it for a few hours, using many different example problems.