Fill a specific region of fcontour without generating a mesh
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Matt J
on 14 Dec 2023
Edited: Matt J
on 14 Dec 2023
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 Comments
Matt J
on 15 Dec 2023
Edited: Matt J
on 15 Dec 2023
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
More Answers (0)
See Also
Categories
Find more on Polygons in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!