How can I manually fill in colors (or shade) a part of a feasible region?

3 次查看(过去 30 天)
How to shade part of a graph with colors?
I have tried using something like this (look at the very last figure):
[A B] = meshgrid(-2:0.01:2.5,-2:0.01:2.5);
F = 100*(B-A.^2).^2+(1-A)^2;
surf(A,B,F,'EdgeColor','none')
shading flat,view(2)
C1 = A.*B>=1;
F1=F;
F1(~C1)=NaN;
figure, surf(A,B,F1,'EdgeColor','none')
shading flat,view(2)
C2 = A+B.^2 >=0;
F2=F1;
F2(~C2)=NaN;
figure, surf(A,B,F2,'EdgeColor','none')
shading flat,view(2)
C3 = A<=4.5;
F3=F2;
F3(~C3)=NaN;
figure, p1=surf(A,B,F3,'EdgeColor','none')
shading flat,view(2)
hold on
f = @(x1,x2) 100*(x2-(x1).^2).^2+(1-x1).^2;
g1 = @(x1,x2) 1-(x1.*x2);g2 = @(x1,x2) -x1-(x2).^2;g3=@(x1,x2) x1-4.5;
circle_x=0:0.00001:2;
t = (-1:0.00001:1)'*pi;
%circle_y=sqrt(1-(circle_x -2).^2 );
X_double=[X{:}];
ezplot(g1,[-2,2.5,-2,2.5])
hold on
ezplot(g2,[-2,2.5,-2,2.5])
hold on
p2=ezcontour(f,[-2,2.5,-2,2.5])
p3=plot(1,1,'ro');
hold on
p4=plot(X_double(1,:),X_double(2,:),'-k.','MarkerSize',10)
hold on
% CIRCLES OF RAD radius AND CENTERED AT (1,1)
for l=1:5
radius=1/10^(l-1);
x = radius*cos(t) +1;
y = radius*sin(t) +1;
plot(x,y,':k','MarkerSize',100)
hold on
end
ezplot(g3,[-2,2.5,-2,2.5])
hold on
legend([p1 p2 p3 p4],'Feasible Region','f contours','minimum','sequence approaching x*');
title('Feasible Region, contour map and minimizer for Test 2');
hold off
, however, the options for shading (flat, faceted or interp) are hiding some of the the circles and points .I believe it is because the colors are too heavy maybe?, so that is why I wanted to see if there was another way of coloring or shading a specific part of a graph (for instance, I would chose a light color, that would allow me to see all the circles and lines).
I also tried using just this:
f = @(x1,x2) 100*(x2-(x1).^2).^2+(1-x1).^2;
g1 = @(x1,x2) 1-(x1.*x2);g2 = @(x1,x2) -x1-(x2).^2;g3=@(x1,x2) x1-4.5;
circle_x=0:0.00001:2;
t = (-1:0.00001:1)'*pi;
%circle_y=sqrt(1-(circle_x -2).^2 );
X_double=[X{:}];
ezplot(g1,[-2,2.5,-2,2.5])
hold on
ezplot(g2,[-2,2.5,-2,2.5])
hold on
p2=ezcontour(f,[-2,2.5,-2,2.5])
p3=plot(1,1,'ro');
hold on
p4=plot(X_double(1,:),X_double(2,:),'-k.','MarkerSize',10)
hold on
% CIRCLES OF RAD RADIUS AND CENTERED AT (1,1)
for l=1:5
radius=1/10^(l-1);
x = radius*cos(t) +1;
y = radius*sin(t) +1;
plot(x,y,':k','MarkerSize',100)
hold on
end
ezplot(g3,[-2,2.5,-2,2.5])
hold on
legend([p1 p2 p3 p4],'Feasible Region','f contours','minimum','sequence approaching x*');
title('Feasible Region, contour map and minimizer for Test 2');
hold off
But in this case, I do not know how to shade or add color to the feasible region.
  3 个评论
Del
Del 2013-3-16
Sorry, here is X:
X={[1.70142552153801;1.71274275705554] [0.941167364374744;1.08086235827247] [1.00999488741642;0.999680272720687] [0.999815053130831;1.00098274852103] [1.00005366180435;1.00008438252635] [1.00000626810729;1.00000779171554] [1.00000056985569;1.00000082174478] [1.00000007290727;0.999999931556373] [0.999999996514253;1.00000000937281] [1.00000000063503;1.00000000077249] [1.00000000009447;0.999999999967202] [1.00000000000716;0.999999999993020] [1.00000000000081;0.999999999999415] [1.00000000000008;0.999999999999937] [1.00000000000001;0.999999999999998] [1.00000000000000;1.00000000000000] [1;1] [1;1] [1;1] [1;1]}
And for the second code, replace the legend by:
legend([p2 p3 p4],'f contours','minimum','sequence approaching x*');
Del
Del 2013-3-17
编辑:Del 2013-3-17
I guess my main problem is, how to add/overlay curves on an existing plot that was done by a surf() function. I read somewhere that I could use "plot3()" BEFORE using surf(), however, my plots are in 2-D (I am using view(2), because I just want to obtain the Feasible region), and I am not quite sure how to use plot3() in this case. Any idea?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by