Minimization with linear contrainsts

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)

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 18 日

0 投票

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 件のコメント

April
April 2021 年 2 月 18 日
Thank you for your response!
Below is the question and the result that I got from the code
Walter Roberson
Walter Roberson 2021 年 2 月 18 日
Looks ok. All of the constraints must hold simultaneously so if x1 must be at least 15 according to the second constraint then it must be at least 15 and so 3*x1 must be at least 45, which is the solution you got.
April
April 2021 年 2 月 18 日
Thank you so much!
I did another way by using the graphical method. But I got the difference answer by using the above code. Could you please help me out with it?
Walter Roberson
Walter Roberson 2021 年 2 月 20 日
I cannot read the image showing the alternative method. I certainly cannot get matlab to execute images.
April
April 2021 年 2 月 20 日
編集済み: Walter Roberson 2021 年 2 月 20 日
Here is the code. Sorry about that!
x=0:20;
y1= max((-20 - 0*x)/-5,0); %Let y1 = -5x1, y1 <= 20
y2= max((-30-0*x)/-2,0); %Let y2 = -2x1, y2 <=30
y3= max((-6-0*x)/-1,0); %Let y3 = -x1, y3 <=6
y4= max((-24-0*x)/-3,0); %Let y4 = x2, y4 <= 8
ytop=min([y1;y2;y3;y4]);
area(x,ytop)
hold on;
[v,u] = meshgrid(0:0.5:20,0:0.5:20);
Z= 3*u ;
contour(v,u,Z,'r','ShowText','on'); % 'r' =red color, 'ShowText','on' = show value of Z
hold off;
title('Problem 5') %Title of the graph is "Problem 5"
xlabel('x2') %The x-axis label is "x2"
ylabel('x1') %The y-axis label is "x1"
legend('Feasible region','Value of Z') %Legend is "Feasible region" for the shaded region,
%"Value of Z" is the lines
Walter Roberson
Walter Roberson 2021 年 2 月 20 日
x=0:20;
y1= max((-20 - 0*x)/-5,0); %Let y1 = -5x1, y1 <= 20
min(y1), max(y1)
ans = 4
ans = 4
y2= max((-30-0*x)/-2,0); %Let y2 = -2x1, y2 <=30
min(y2), max(y2)
ans = 15
ans = 15
y3= max((-6-0*x)/-1,0); %Let y3 = -x1, y3 <=6
min(y3), max(y3)
ans = 6
ans = 6
y4= max((-24-0*x)/-3,0); %Let y4 = x2, y4 <= 8
min(y4), max(y4)
ans = 8
ans = 8
Notice those are all constants, because in each case you have 0*x so you are ignoring the x.
ytop=min([y1;y2;y3;y4]);
That will of course be the 4
area(x,ytop)
hold on;
[v,u] = meshgrid(0:0.5:20,0:0.5:20);
Z= 3*u ;
contour(v,u,Z,'r','ShowText','on'); % 'r' =red color, 'ShowText','on' = show value of Z
hold off;
title('Problem 5') %Title of the graph is "Problem 5"
xlabel('x2') %The x-axis label is "x2"
ylabel('x1') %The y-axis label is "x1"
legend('Feasible region','Value of Z') %Legend is "Feasible region" for the shaded region,
%"Value of Z" is the lines
Walter Roberson
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
April
April 2021 年 2 月 24 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with Optimization Toolbox についてさらに検索

タグ

質問済み:

2021 年 2 月 18 日

コメント済み:

2021 年 2 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by