argument in meshgrid - confused

5 ビュー (過去 30 日間)
Tatte Berklee
Tatte Berklee 2022 年 8 月 31 日
編集済み: Bruno Luong 2022 年 8 月 31 日
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!

採用された回答

Bruno Luong
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
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.
Tatte Berklee
Tatte Berklee 2022 年 8 月 31 日
Gocha! Thanks, Bruno!!

サインインしてコメントする。

その他の回答 (1 件)

David Hill
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);
  1 件のコメント
Tatte Berklee
Tatte Berklee 2022 年 8 月 31 日
Thanks for the response, David!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by