Contour not taking into account edge of domain

2 views (last 30 days)
Hello eveyone,
I have an issue using contour (contourf or contourc as well) with convhull in order to get the convex polygon that includes the domain where Z=1.
When i try to retrieve the contour matrix, only the coordinates contained are those of strictly inside the domain, and the edges of the domain do not appear, then the polygon I get is way smaller than I expect.
I hope this is rather clear. Thank you in advance!
Here is my code
X = 1:0.2:2;
Y = 2:0.2:3;
[X_mesh,Y_mesh]=meshgrid(X,Y);
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
figure,
[M,h]=contourf(X_mesh,Y_mesh,Z,[1 1]);
M
M = 2×9
1.0000 1.4000 1.6000 1.6000 1.6000 1.4000 1.6000 1.8000 2.0000 8.0000 2.0000 2.2000 2.2000 2.2000 2.4000 2.6000 2.6000 2.6000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
coords_x=M(1,2:end);
coords_y=M(2,2:end);
K=convhull(coords_x,coords_y);
figure,hold on,
contourf(X_mesh,Y_mesh,Z,[1 1]);
patch(coords_x(K),coords_y(K),'green', 'FaceAlpha', 0.5);

Accepted Answer

lepolonais03
lepolonais03 on 24 Jan 2025
I found a workaround. I added zeros all around the contour I wanted in order to virtually 'erase' the edges.
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
Z_extended = [zeros(1,size(Z,2)+2);
[zeros(size(Z,1),1),Z,zeros(size(Z,1),1)];
zeros(1,size(Z,2)+2)]
  1 Comment
Paul
Paul on 24 Jan 2025
Edited: Paul on 24 Jan 2025
Doesn't this approach yiled the same polygon as in the original code?
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
Z_extended = [zeros(1,size(Z,2)+2);
[zeros(size(Z,1),1),Z,zeros(size(Z,1),1)];
zeros(1,size(Z,2)+2)];
X = [0.8 , 1:0.2:2 , 2.2];
Y = [1.8 , 2:0.2:3 , 3.2];
[X_mesh,Y_mesh]=meshgrid(X,Y);
figure,
[M,h]=contourf(X_mesh,Y_mesh,Z_extended,[1 1]);
I confess that I don't understand why M has so many points, and some vertices are repeated.
M.'
ans = 28×2
1.0000 27.0000 1.0000 2.0000 1.0000 2.0000 1.2000 2.0000 1.4000 2.0000 1.4000 2.0000 1.6000 2.2000 1.6000 2.2000 1.6000 2.2000 1.4000 2.4000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!