I want to plot the function The function is defined for . With
f = @(x,y)sqrt(y-x.^2);
fmesh(f,[-3 3 0 9])
I get a mesh plot of the function in the rectangle [-3,3]X[0,9]. I want to plot the function using meshgrid. The following code results to complex values error message.
x = -3:1:3
y = 0:1:9
[X, Y] = meshgrid(x,y)
Z = sqrt(Y-X.^2)
mesh(X,Y,Z)
The problem is that Z is evaluated for not suitable pairs of (X,Y). What am I doing wrong?

 채택된 답변

Star Strider
Star Strider 2021년 4월 6일
편집: Star Strider 2021년 4월 6일

1 개 추천

Try this:
x = -3:1:3;
y = 0:1:9;
[X, Y] = meshgrid(x,y);
Y = Y.*(Y>=X.^2); % Select Y ≥ X^2
Z = sqrt(Y-X.^2);
Zre = Z.*(imag(Z)==0); % Select Only Real Values Of ‘Z’
figure
mesh(X,Y,Zre)
EDIT — (5 Apr 2021 at 15:56)
Added:
Y = Y.*(Y>=X.^2); % Select Y ≥ X^2
to accord with the edited Question.

댓글 수: 5

Thanks for your answer. Still I do not think that this solves my issue. Compare the output of your instructions with the output of
clear x,y
f = @(x,y)sqrt(y-x.^2);
fmesh(f,[-3 3 0 9])
The function is defined for . You solution gives also Z=0 for which violates the domain of definition.
With your edited answer. It is almost there with a smaller increment.
x = -3:0.2:3;
y = 0:0.2:9;
[X, Y] = meshgrid(x,y);
Y = Y.*(Y>=X.^2); % Select Y ≥ X^2
Z = sqrt(Y-X.^2);
Zre = Z.*(imag(Z)==0); % Select Only Real Values Of ‘Z’
figure
mesh(X,Y,Zre)
Still I am puzzled why the graph is not so smooth.
I am teaching Matlab. I worked with meshgrid and I avoid fmesh. I told to my students that the latter (fmesh) does not provide full control. I am wondering how to explain to my students why your code and code
clear x,y
f = @(x,y)sqrt(y-x.^2);
fmesh(f,[-3 3 0 9])
produces such different plots.
The two plots (using fmesh and mesh) will not appear to be the same because fmesh uses an adaptive algorithm to create the plot, while mesh just uses what it is given. They are not the same functions.
It is straightforward to get the data from fmesh if you prefer those results:
figure
f = @(x,y)sqrt(y-x.^2);
hfm = fmesh(f,[-3 3 0 9]);
Xmtx = hfm.XData;
Ymtx = hfm.YData;
Zmtx = hfm.ZData;
However, the results are vectors, not matrices, and it is not possible to reshape the vectors into matrices to use with mesh.
The best option is likely:
figure
scatter3(Xmtx, Ymtx, Zmtx, 5, Zmtx, 'filled')
Make appropriate changes to get the result youi want.
So, I guess, the easiest solution is to use fmesh here:-)! Thank your very much for your feedback. I was puzzled today during a lesson how to have mesh return a similar graph with fmesh.
Star Strider
Star Strider 2021년 4월 6일
As always, my pleasure!
The easiest solution is to use fmesh. It is possible to examine the fmesh code using edit or type (however that is not very revealing, since it calls fsurf, that it is posible to examine in some detail) to see exactly how it works. The fsurf function then calls a number of other funcitons that I have not taken the time to examine closely. My information on the ‘f’ plotting functions derives from other discussions in MATLAB Central that I have read over the years.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2021년 4월 6일

0 개 추천

Here is the surface in meshgrid form
s = -1:0.01:1;
y = 0:0.1:9;
[S, Y] = meshgrid(s,y);
X = S.*sqrt(Y);
Z = sqrt(max(Y-X.^2,0));
figure
mesh(X,Y,Z)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by