how to plot contour for arbitrary shape (not rectangular) in matlab?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (2)
Walter Roberson
el 28 de Abr. de 2024
Use a rectangular array of Z, but set it to NaN outside of the area of interest.
William Rose
el 28 de Abr. de 2024
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 comentarios
William Rose
el 28 de Abr. de 2024
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
Ver también
Categorías
Más información sobre Contour Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!