I need help for drawing a constraint in Matlab in 2D
Dear All,
I’m trying to draw a set of constraints but I got a wrong line for the constraint 2×1<=3 !!!! I need a vertical line !! I approciate any help from you !!!
Many Thanks,
Nadia
clear;clc;close;
%max 5×1+7×2;
%3×1+8×2<=12;
%x1+x2<=2;
%2×1<=3;
%x2<=4;
%x1,x2>=0;
%% plot the feasible region %Generate data
[x1,x2] = meshgrid(0:0.1:10);% i changed 0.1 to 0.01 to plot the fourth constraint
NB=5*x1+7*x2;
% Get True where condition applies, false where not.
cond1=3*x1+8*x2<=12; cond2=x1+x2<=2; cond3=2*x1<=3; cond4=x2<=2;
% Get boundaries of the condition
p1=(12-3*x1(1,:))/8;
p2=2-x1(1,:);
p3=3/2-x1(1,:);
p4=2-x2(1,:);
%Delete Areas whereCondition does not apply;
NB(~cond1)=NaN; NB(~cond2)=NaN; NB(~cond3)=NaN; NB(~cond4)=NaN;
%% Plot
[x2,h]=contourf(x1,x2,NB,0); % command contourf for filling the area between the contour levels
hold on
plot(x1(1,:),p1,’r’,’LineWidth’,2); text(x1(1,20),p1(20), ‘leftarrow Cond1’); %arbitrary location
plot(x1(1,:),p2,’k’,’LineWidth’,2); text(x1(1,15),p2(15), ‘leftarrow Cond2’); %arbitrary location
plot(x1(1,:),p3,’b’,’LineWidth’,2); text(x1(1,20),p3(20), ‘leftarrow Cond3’); %arbitrary location
plot(x1(1,:),p4,’b’,’LineWidth’,2); text(x1(1,20),p4(20), ‘leftarrow Cond3’); %arbitrary location
axis([0 5 0 5])
xlabel(‘x1’)
ylabel(‘x2’)Dear All,
I’m trying to draw a set of constraints but I got a wrong line for the constraint 2×1<=3 !!!! I need a vertical line !! I approciate any help from you !!!
Many Thanks,
Nadia
clear;clc;close;
%max 5×1+7×2;
%3×1+8×2<=12;
%x1+x2<=2;
%2×1<=3;
%x2<=4;
%x1,x2>=0;
%% plot the feasible region %Generate data
[x1,x2] = meshgrid(0:0.1:10);% i changed 0.1 to 0.01 to plot the fourth constraint
NB=5*x1+7*x2;
% Get True where condition applies, false where not.
cond1=3*x1+8*x2<=12; cond2=x1+x2<=2; cond3=2*x1<=3; cond4=x2<=2;
% Get boundaries of the condition
p1=(12-3*x1(1,:))/8;
p2=2-x1(1,:);
p3=3/2-x1(1,:);
p4=2-x2(1,:);
%Delete Areas whereCondition does not apply;
NB(~cond1)=NaN; NB(~cond2)=NaN; NB(~cond3)=NaN; NB(~cond4)=NaN;
%% Plot
[x2,h]=contourf(x1,x2,NB,0); % command contourf for filling the area between the contour levels
hold on
plot(x1(1,:),p1,’r’,’LineWidth’,2); text(x1(1,20),p1(20), ‘leftarrow Cond1’); %arbitrary location
plot(x1(1,:),p2,’k’,’LineWidth’,2); text(x1(1,15),p2(15), ‘leftarrow Cond2’); %arbitrary location
plot(x1(1,:),p3,’b’,’LineWidth’,2); text(x1(1,20),p3(20), ‘leftarrow Cond3’); %arbitrary location
plot(x1(1,:),p4,’b’,’LineWidth’,2); text(x1(1,20),p4(20), ‘leftarrow Cond3’); %arbitrary location
axis([0 5 0 5])
xlabel(‘x1’)
ylabel(‘x2’) Dear All,
I’m trying to draw a set of constraints but I got a wrong line for the constraint 2×1<=3 !!!! I need a vertical line !! I approciate any help from you !!!
Many Thanks,
Nadia
clear;clc;close;
%max 5×1+7×2;
%3×1+8×2<=12;
%x1+x2<=2;
%2×1<=3;
%x2<=4;
%x1,x2>=0;
%% plot the feasible region %Generate data
[x1,x2] = meshgrid(0:0.1:10);% i changed 0.1 to 0.01 to plot the fourth constraint
NB=5*x1+7*x2;
% Get True where condition applies, false where not.
cond1=3*x1+8*x2<=12; cond2=x1+x2<=2; cond3=2*x1<=3; cond4=x2<=2;
% Get boundaries of the condition
p1=(12-3*x1(1,:))/8;
p2=2-x1(1,:);
p3=3/2-x1(1,:);
p4=2-x2(1,:);
%Delete Areas whereCondition does not apply;
NB(~cond1)=NaN; NB(~cond2)=NaN; NB(~cond3)=NaN; NB(~cond4)=NaN;
%% Plot
[x2,h]=contourf(x1,x2,NB,0); % command contourf for filling the area between the contour levels
hold on
plot(x1(1,:),p1,’r’,’LineWidth’,2); text(x1(1,20),p1(20), ‘leftarrow Cond1’); %arbitrary location
plot(x1(1,:),p2,’k’,’LineWidth’,2); text(x1(1,15),p2(15), ‘leftarrow Cond2’); %arbitrary location
plot(x1(1,:),p3,’b’,’LineWidth’,2); text(x1(1,20),p3(20), ‘leftarrow Cond3’); %arbitrary location
plot(x1(1,:),p4,’b’,’LineWidth’,2); text(x1(1,20),p4(20), ‘leftarrow Cond3’); %arbitrary location
axis([0 5 0 5])
xlabel(‘x1’)
ylabel(‘x2’) drawing a constraint, vertical line, matlab MATLAB Answers — New Questions