Minimization with linear contrainsts
조회 수: 3 (최근 30일)
이전 댓글 표시
I tried to get the minimum value of the objective function z=3*x1, but the result I got was z=45, x1=15 which I believe that are the value of maximum. Could someone help me out with the code?
Thank you in advance!
x = optimvar('x',1,'lowerbound',0);
prob = optimproblem('Objective',3*x(1),'ObjectiveSense','min');
cons1 = -5*x(1) <= -20; %Constraint 1: -5x1 <= -20
cons2 = -2*x(1) <= -30; %Constraint 2: -2x1 <= -30
cons3 = -x(1) <= -6; %Constraint 3: -x1 <= -6
cons4 = -3*x(1) <= -24; %Constraint 4: -3x1 <= -24
%Now set the cons# as the constraints of the optimization problem
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
problem = prob2struct(prob);
show(prob)
[sol,fval,exitflag,output] = linprog(problem)
댓글 수: 0
채택된 답변
Walter Roberson
2021년 2월 18일
cons2 = -2*x(1) <= -30; %Constraint 2: -2x1 <= -30
Divide by the -2 and adjust the sense of the test appropriately to see that is equivalent to x1 >= 15, which is the value actually used. So it did return the minimum given those constraints.
댓글 수: 8
Walter Roberson
2021년 2월 20일
I don't know what you think you are calculating there.
%Constraint 1: 5x1 >= 20
%Constraint 2: 2x1 >= 30
%Constraint 3: x1 >= 6
%Constraint 4: 3x1 >= 24
x1 = linspace(0,20);
c1 = 5*x1 >= 20;
c2 = 2*x1 >= 30;
c3 = 1*x1 >= 6;
c4 = 3*x1 >= 24;
overall = c1 & c2 & c3 & c4;
bad = find(~overall);
good = find(overall);
fill(x1([bad(1), bad(end), bad(end), bad(1)]), [0 0 1 1], 'r');
hold on
fill(x1([good(1), good(end), good(end), good(1)]), [0 0 1 1], 'g');
hold off
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Stress and Strain에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!