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!