linear optimization with changing constraints(Solved)

1 view (last 30 days)
Hey everyone, I have a problem with the inequality constraints.
in my problem, the b is changing accoring to the previous b's value, so I wrote a for structure but no idea if it's the right way.
here is my code:
price=randi([-10,50],24,1);
x=ones(24,1);
k=0.2;
T=[15 25];
A=k*ones(24);
C=20*ones(24,1);
A=[A;-A];
for i=1:24
C(i+1)=C(i)+k*x(i);
B(i)=T(2)-C(i);
D(i)=C(i)-T(1);
end
c=D.';
b=B.';
b=[b;c];
lb=0*ones(24,1);
ub=20*ones(24,1);
x=linprog(-price,A,b,[],[],lb,ub);
everytime I run it, my x array is full of 0 with only one random value '2'
would you kindly tell me what's going wrong? does the inequality constraints function properly? many thanks!
Thank you very much for your help! the problem is solved now

Answers (1)

Madhav Thakker
Madhav Thakker on 18 Aug 2020
Hi
As per your code, the equality constraints are working fine with different[MT1] values of ub, b, price. To get a deeper understanding, refer this documentation of linprog . So basically, the code is minimizing price'*x such that A.x <= b and lb <= x <= ub. The 'random' value 2 is always associated with the index where priceis largest because we are trying to minimize price'*x and the argument passed in linprog is -price.
Another thing you can do to verify the correctness of the function, is by trying to set ub = 1.2 or by increasing b. You'll see non-zero values in index corresponding to next price value.
Hope this helps.
  1 Comment
Yingjie Wang
Yingjie Wang on 18 Aug 2020
thank you very much I've solved it, I think that problem in a wrong way, lol

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!