argument in meshgrid - confused
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bruno Luong
2022-8-31
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 个评论
Bruno Luong
2022-8-31
编辑:Bruno Luong
2022-8-31
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.
更多回答(1 个)
David Hill
2022-8-31
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!