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 Center および File ExchangePolygons についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by