Contour not taking into account edge of domain

조회 수: 2 (최근 30일)
lepolonais03
lepolonais03 2025년 1월 22일
편집: Paul 2025년 1월 24일
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);

채택된 답변

lepolonais03
lepolonais03 2025년 1월 24일
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
Paul
Paul 2025년 1월 24일
편집: Paul 2025년 1월 24일
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>

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by