Fill a specific region of fcontour without generating a mesh

4 views (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 14 Dec 2023
Edited: Matt J on 14 Dec 2023
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 Comments
Matt J
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
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!