argument in meshgrid - confused

5 views (last 30 days)
Tatte Berklee
Tatte Berklee on 31 Aug 2022
Edited: Bruno Luong on 31 Aug 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!

Accepted Answer

Bruno Luong
Bruno Luong on 31 Aug 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 Comments
Bruno Luong
Bruno Luong on 31 Aug 2022
Edited: Bruno Luong on 31 Aug 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.
Tatte Berklee
Tatte Berklee on 31 Aug 2022
Gocha! Thanks, Bruno!!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 31 Aug 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);

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!