Adding feasible region on a Contour plot
57 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am trying to plot a contour of an optimization problem and also determine the feasible region for the function.
I have been able to generate the contout plot but I I don't know how to plot the feasible region when the constraint is X > 0.
Example of my code is shown below.
Assuming the constraints is given by:

Thank you for your help
x = -5:0.005:5;
y = -5:0.005:5;
[X,Y] = meshgrid(x,y);
F = (X.^2+Y-11).^2 + (X + Y.^2 - 7).^2;
v = [0:2:10 10:10:100 100:20:200]
[c,h]=contour(X,Y,F,v,'linewidth',2);
2 件のコメント
DGM
2021 年 4 月 10 日
It depends how you want to represent it. Do you want the whole plot from -5:5 with some sort of lines or mask to delineate the feasible region, or do you just want to plot the feasibile region alone?
採用された回答
DGM
2021 年 4 月 10 日
編集済み: DGM
2021 年 4 月 10 日
Like I mentioned, it depends how you want to represent the region. Let's say you want to represent the feasibility region within the context of the function over its entire domain. I suspect this, since you're solving for F over x,y=[-5 5], even though your constraints exclude negative inputs. There are probably a lot of things you could do. You could delineate the area with boundary lines, or you could de-emphasize the overall function representation using a masking approach. Let me just try something as an example:
clf
x = -5:0.05:5;
y = -5:0.05:5;
[X,Y] = meshgrid(x,y);
F = (X.^2+Y-11).^2 + (X + Y.^2 - 7).^2;
v = [0:2:10 10:10:100 100:20:200]
% plot the whole thing
[c,h]=contour(X,Y,F,v,'linewidth',2); hold on; axis equal
% but subdue it with an overlay
hf=fill([1 1 -1 -1]*5,[-1 1 1 -1]*5,'w','facealpha',0.8);
% plot just the ROI
feasible = (X>=0) & (Y>=0) & (X+2*Y<=2);
F(~feasible) = NaN;
contour(X,Y,F,v,'linewidth',2);
% maybe include lines if you want?
line([5 -5],[-1.5 3.5],'linestyle','--','color','k');
line([0 0],[-5 5],'linestyle','--','color','k');
line([-5 5],[0 0],'linestyle','--','color','k');

Alternatively, maybe you could find some way to represent the set of constraints using overlays (though maybe these colors are a big garish)
x = -5:0.005:5;
y = -5:0.005:5;
[X,Y] = meshgrid(x,y);
F = (X.^2+Y-11).^2 + (X + Y.^2 - 7).^2;
v = [0:2:10 10:10:100 100:20:200]
% plot the whole thing
[c,h]=contour(X,Y,F,v,'linewidth',2); hold on; axis equal
% do something to represent the constraint areas individually
hf1=fill([-1 0 0 -1]*5,[1 1 -1 -1]*5,[1 0.5 1],'facealpha',0.6,'edgecolor','none')
hf2=fill([1 1 -1 -1]*5,[-1 0 0 -1]*5,[0.5 1 1],'facealpha',0.6,'edgecolor','none')
hf3=fill([5 -5 -5 5 5],[-1.5 3.5 5 5 -1.5],[1 1 0.5],'facealpha',0.6,'edgecolor','none')

On the other hand, maybe you want to just plot the ROI by itself.
x = 0:0.005:2;
y = 0:0.005:1;
[X,Y] = meshgrid(x,y);
F = (X.^2+Y-11).^2 + (X + Y.^2 - 7).^2;
% you'll want to adjust this to better fit the ROI
%v = [0:2:10 10:10:100 100:20:200]
% plot just the ROI
feasible = (X>=0) & (Y>=0) & (X+2*Y<=2);
F(~feasible) = NaN;
contour(X,Y,F,20,'linewidth',2); axis equal

Alternatively, this is often recommended, but I honestly have never used it and don't know how well it's suited for a 2D contour:
2 件のコメント
DGM
2021 年 4 月 10 日
編集済み: DGM
2021 年 4 月 10 日
the function line() plots a polyline between x and y points specified in two vectors. So
line([5 -5],[-1.5 3.5],'linestyle','--','color','k');
plots a diagonal line from (5,-1.5) to (-5,3.5). Similarly,
fill([1 1 -1 -1]*5,[-1 1 1 -1]*5,'w','facealpha',0.8);
plots a filled polygon whose vertices are coincident with the corners of the plot region. (a rectangle in this case)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Contour Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!