Adding feasible region on a Contour plot

조회 수: 48 (최근 30일)
Telema Harry
Telema Harry 2021년 4월 10일
편집: DGM 2021년 4월 10일
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
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?
Telema Harry
Telema Harry 2021년 4월 10일
Thank you for the response. I want to plot only the feasible region.
I have mananced to plot the constraint line.. but it is not starting and ending at the point I want.
In the attached diagram, I have shaded the feasible region.
My other challenge is how to plot the straigth lines when

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

채택된 답변

DGM
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
Telema Harry
Telema Harry 2021년 4월 10일
Thank you for the detailed response.
Please I have one more trival question. I have several contour plots and I would like to understand it so I can easiliy plot them. Please what does this line of code mean: [1 1 -1 -1]*5,[-1 1 1 -1]*5, [5 -5],[-1.5 3.5]. I suspect, it is related to the axis.
DGM
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 CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by