how to plot contour for arbitrary shape (not rectangular) in matlab?

조회 수: 16 (최근 30일)
sajad sajad
sajad sajad 2024년 4월 28일
댓글: sajad sajad 2024년 4월 29일
Hello guys, I have a question for you
In relation to drawing the contour of a non-rectangular shape, what method should be adopted?
For example, how can you draw the contour of the following figure?
If our data is the coordinates of each point (x,y) and the value of Z at each point.
Thank you all.

답변 (2개)

Walter Roberson
Walter Roberson 2024년 4월 28일
Use a rectangular array of Z, but set it to NaN outside of the area of interest.
  댓글 수: 1
sajad sajad
sajad sajad 2024년 4월 29일
Hi Mr. Roberson, Oh right, thanks
But my mesh are not regular, so that the matrix can't be used!!

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


William Rose
William Rose 2024년 4월 28일
You can make a MxN grid of points that cover the X-Y extent of your shape, and define the Z values at those points,. Then you can use contour or surf.
Example:
M=10; N=20;
[X,Y]=meshgrid(0:N,0:M);
Z=exp(-((X-10).^2+(Y-5).^2)/8);
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
Now adjust the grid x and y coordinates (only adjust the Y values in this example):
for j=1:5,Y(:,j)=Y(:,j)/2; end
for j=6:10,Y(:,j)=Y(:,j)*j/10; end
for j=11:15,Y(:,j)=Y(:,j)*(21-j)/10; end
for j=16:21,Y(:,j)=Y(:,j)/2; end
Make another figure with the adjusted grid coordinates:
figure
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
Good luck!
  댓글 수: 2
William Rose
William Rose 2024년 4월 28일
You could also reverse the order in the example above: first adjust the X,Y coordinates of the grid, then deifne the Z values on the adjusted X,Y grid.
M=10; N=20;
[X,Y]=meshgrid(0:N,0:M);
for j=1:5,Y(:,j)=Y(:,j)/2; end
for j=6:10,Y(:,j)=Y(:,j)*j/10; end
for j=11:15,Y(:,j)=Y(:,j)*(21-j)/10; end
for j=16:21,Y(:,j)=Y(:,j)/2; end
Z=exp(-((X-10).^2+(Y-4).^2)/20); % define Z values on adjusted grid
Make figure:
figure
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
OK
sajad sajad
sajad sajad 2024년 4월 29일
Hi Mr. Rose, Thanks a lot.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by