How to plot 2D Linear Inequality?

How to plot cond1 and cond2 with the cost in one figure?
[x1,x2]=meshgrid(0:.1:10,0:.1:10);
cond1=x1-x2<=-2;
cond2=-0.3*x1-x2<=-8;
cost=0.4*x1.^2-5*x1+x2.^2-6*x2+50;
figure (1)
contour(x1,x2,cost,10,'linewidth',2)
xlabel('x_{1}');
ylabel('x_{2}');
hold on
...

1 件のコメント

Omar B.
Omar B. 2021 年 9 月 17 日
How to get this pic?

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

 採用された回答

KSSV
KSSV 2021 年 9 月 17 日

0 投票

[x1,x2]=meshgrid(0:.1:10,0:.1:10);
cond1=x1-x2<=-2;
cond2=-0.3*x1-x2<=-8;
cost=0.4*x1.^2-5*x1+x2.^2-6*x2+50;
figure (1)
contour(x1,x2,cost,10,'linewidth',2)
hold on
contour(x1,x2,cond1,'color','k','linewidth',2)
contour(x1,x2,cond2,'color','r','linewidth',2)
xlabel('x_{1}');
ylabel('x_{2}');
hold on

6 件のコメント

Omar B.
Omar B. 2021 年 9 月 17 日
編集済み: Omar B. 2021 年 9 月 17 日
Thank you. Why don't they look linear? can we use plot?
KSSV
KSSV 2021 年 9 月 17 日
clc; clear all ;
[x1,x2]=meshgrid(0:.1:10,0:.1:10);
cond1=x1-x2<=-2;
cond2=-0.3*x1-x2<=-8;
cost=0.4*x1.^2-5*x1+x2.^2-6*x2+50;
%% Get cond1 and cond2
c1 = contour(x1,x2,cond1,[1 1],'color','k') ;
idx = c1(2,:)>max(x2(:)) ;
c1(:,idx) = [] ;
p = polyfit(c1(1,:),c1(2,:),1) ;
c1(2,:) = polyval(p,c1(1,:)) ;
%
c2 = contour(x1,x2,cond2,[1 1],'color','k') ;
idx = c2(2,:)>max(x2(:)) ;
c2(:,idx) = [] ;
p = polyfit(c2(1,:),c2(2,:),1) ;
c2(2,:) = polyval(p,c2(1,:)) ;
figure (1)
contour(x1,x2,cost,10,'linewidth',2)
hold on
plot(c1(1,:),c1(2,:),'k','Linewidth',2)
plot(c2(1,:),c2(2,:),'k','LineWidth',2)
xlabel('x_{1}');
ylabel('x_{2}');
Omar B.
Omar B. 2021 年 9 月 17 日
Thank you so much.
Omar B.
Omar B. 2021 年 9 月 17 日
could you please explain what do you mean by c1(2,:) and c1(1,:)?
KSSV
KSSV 2021 年 9 月 17 日
Coordinates of contour lines are obtained. Those are c1, c2. These coordinates are row arrays. c1(1,:) means first row which is x -coordinate and c1(2,:) is second row which is y-coordinate.
Omar B.
Omar B. 2021 年 9 月 17 日
I got it. Thank you so much.

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2021 年 9 月 17 日

コメント済み:

2021 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by