argument in meshgrid - confused
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tatte Berklee
el 31 de Ag. de 2022
Editada: Bruno Luong
el 31 de Ag. de 2022
Hey folks!
I am trying to use -meshgrid- and use -contourf- to plot a contourp map.
The two-variable function I am interested in studying is:
for
I tried this code: Version 1
%Meshgrid Ver 1
a = -1:.2:1;
b = 0:.2:1;
[X,Y]= meshgrid(a,b);
Z = X./(Y.*(1-X)-1);
contourf(X,Y,Z);
Next, I tried a similar code, except I changed the -meshgrid- argument:
%Meshgrid Ver 2
[X,Y]= meshgrid(-1:1);
Z = X./(Y.*(1-X)-1);
contourf(X,Y,Z);
I looked at the MATLAB document here: https://www.mathworks.com/help/matlab/ref/meshgrid.html
My questions:
(1) Why are they giving different contour plots?
(2) Version 1 is the correct contour plot corresponding to the origina equation Z, right?
Thanks, guys!
0 comentarios
Respuesta aceptada
Bruno Luong
el 31 de Ag. de 2022
You need to make finer grid, you meshgrid is too coarse.
The version 1 is the top half of version 2.
%Meshgrid Ver 1
a = -1:.01:1;
b = 0:.01:1;
[X,Y]= meshgrid(a,b);
Z = X./(Y.*(1-X)-1);
ax1 = subplot(2,1,1);
contourf(X,Y,Z);
title('Version 1')
%Meshgrid Ver 2
[X,Y]= meshgrid(-1:0.01:1);
Z = X./(Y.*(1-X)-1);
ax2 = subplot(2,1,2);
contourf(X,Y,Z);
title('Version 2')
linkaxes([ax1 ax2])
3 comentarios
Bruno Luong
el 31 de Ag. de 2022
Editada: Bruno Luong
el 31 de Ag. de 2022
Because your grid is too coarse. Matlab draws the contour line as sequence of straight lines that cut the grid. The real function and contour lines are smooth.
Más respuestas (1)
David Hill
el 31 de Ag. de 2022
Your domain for X is -1:1 and your domain for Y is 0:1. The first code choses the correct domains, the second assigns the same domain to both X and Y (-1:1). If you want more data points:
a = -1:.001:1;
b = 0:.001:1;
[X,Y]= meshgrid(a,b);
Z = X./(Y.*(1-X)-1);
contourf(X,Y,Z);
Ver también
Categorías
Más información sobre Surface and Mesh 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!