I need help for drawing a constraint in Matlab in 2D

조회 수: 4 (최근 30일)
nadia nadi
nadia nadi 2025년 3월 7일
댓글: nadia nadi 2025년 3월 9일
Dear All,
I'm trying to draw a set of constraints but I got a wrong line for the constraint 2x1<=3 !!!! I need a vertical line !! I approciate any help from you !!!
Many Thanks,
Nadia
clear;clc;close;
%max 5x1+7x2;
%3x1+8x2<=12;
%x1+x2<=2;
%2x1<=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')
  댓글 수: 3
Sam Chak
Sam Chak 2025년 3월 7일

Woukd you also like to apply a colorful gradient translucent patch on the bounded feasible region as well?

nadia nadi
nadia nadi 2025년 3월 9일

I’m ok thank you very much I’m appreciate your help.

Best

댓글을 달려면 로그인하십시오.

채택된 답변

Sam Chak
Sam Chak 2025년 3월 7일
편집: Sam Chak 2025년 3월 7일
You should replace the p3 line with this:
p3 = 3/2*ones(1, numel(x1(1,:)));
...
plot(p3, x1(1,:))
clear;clc;close;
%max 5x1+7x2;
%3x1+8x2<=12;
%x1+x2<=2;
%2x1<=3;
%x2<=4;
%x1,x2>=0;
%% plot the feasible region %Generate data
[x1,x2] = meshgrid(0:0.01: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,:);
p3 = 3/2*ones(1, numel(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(p3,x1(1,:),'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')

추가 답변 (1개)

Rik
Rik 2025년 3월 7일
I have no clue what you mean mathematically with what you wrote, but if you want to plot a vertical line, you can use xline():
x=linspace(0,2*pi);
plot(x,sin(x))
xline(pi)
yline(-0.5*sqrt(2))
legend({'data','xline','yline'})

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by