Surf Plot error code with Z having to be defined as a matrix, not scalar or vector

I'm trying to graph a 3D function z=(x-y)/(1+y) as a challenge.
Here is what I have so far:
>> x=-1:1:1;
>> y=-1:1:1;
>> [X,Y]=meshgrid(x,y);
>> Z=[(x-y)/(1+y)];
>> surf(X,Y,Z)
It is returning:
Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
I do not know what the problem is with my code, can someone help? Thank you!

 Accepted Answer

There are a couple of errs. Here is the corrected code
x=-1:1:1;
y=-1:1:1;
[X,Y]=meshgrid(x,y);
% Ver 1.
Z=(x(:)-y)./(1+y);
surf(X,Y,Z')
% or Ver 2.
Z=(X-Y)./(1+Y);
surf(X,Y,Z)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!