Fill a specific region of fcontour without generating a mesh

조회 수: 4 (최근 30일)
Paul
Paul 2023년 12월 14일
댓글: Paul 2023년 12월 15일
I have the following code:
%% Parameters:
a = 0.36;
b = 0.26;
c = 0.15;
d = 0.5;
e = 0.2;
f = -0.14;
%% Function
fun = @(alpha,beta) f + 2*c.^2.*cos(beta) + 2*c.*sin(beta).*(d.*sin(alpha) + e.*cos(alpha)) + 2*b.*(a.^2-c.^2.*(1-cos(beta)).^2).^(1/2);
I would like to fill only the region where the function is negative. Specifically, I would like to find an intermediate solution between the two options provided by Star Strider in this link.That is, I would like to fill the region corresponding to the negative values of the function with one specific color, while the positive regions remain transparent (NO white color).
Thus, if possible, I would like an intermediate solutions between these two plots:
OPTION 1: Use Fill "on" option. (The problem is that you cannot specify only the color for the negative region, and, therefore, the positive region is not transparent).
figure
fcontour(fun,[-pi pi -pi pi],'LevelList',[-0.1,0],'Fill','on');
colormap([0 1 0; 1 1 1]) % HERE I WOULD LIKE ONY TO CHOOSE THE COLOR FOR ONE REGION (the other one transparent)
grid on
xlabel(' alpha (rad) ');
ylabel(' beta (rad) ');
OPTION 2: Use LevelList with negative contours (This way, the positive region is transparent, but to plot the negative region several levels are needed).
figure
fcontour(fun,[-pi pi -pi pi],'LevelList',linspace(-0.5,0,1000));
colormap([0 1 0])
grid on
xlabel(' alpha (rad) ');
ylabel(' beta (rad) ');
I know I can use set(gca,'Layer','top') to make it look like the white is transparent...But, is there any other way?
Also, if possible, I would like to find a solution that does not use 'meshgrid', cause I'm trying to figure out a way that does not involve generating a mesh.
Thank you in advance for your help.

채택된 답변

Matt J
Matt J 2023년 12월 14일
편집: Matt J 2023년 12월 14일
Using the FEX download getContourLineCoordinates,
fun=@(x,y)x.^2+y.^2-1;
M=fcontour(fun,[-pi pi -pi pi],'LevelList',[0,1,2,3]).ContourMatrix; hold on
[~,xy]=getContourLineCoordinates(M);
p=polyshape(xy{1});
plot(p); hold off
  댓글 수: 6
Matt J
Matt J 2023년 12월 15일
편집: Matt J 2023년 12월 15일
Indeed,
%% Parameters:
a = 0.36;
b = 0.26;
c = 0.15;
d = 0.5;
e = 0.2;
f = -0.14;
%% Function
fun = @(alpha,beta) f + 2*c.^2.*cos(beta) + 2*c.*sin(beta).*(d.*sin(alpha) + e.*cos(alpha)) + 2*b.*(a.^2-c.^2.*(1-cos(beta)).^2).^(1/2);
M=fcontour(fun,[-pi pi -pi pi],'LevelList',[-inf,0]).ContourMatrix; hold on
[~,xy]=getContourLineCoordinates(M);
p=polyshape(cell2mat(xy(1:2)'));
V=combinations(xlim,ylim);
plot(subtract( convhull(polyshape(V{:,:})),p )); axis([-pi pi -pi pi]); hold off
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
Paul
Paul 2023년 12월 15일
Solved! Thanks to both of you :)

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by