Why can't I generate a 3-d surface when using surf? (file)
Show older comments
My version is 2015b. I use Mac.
x=linspace(-2,2,20);
y=x';
z=y*x;
surf(x,y,z)
And the output has nothing, shownd in picture.

Answers (1)
Z must be a matrix. In your code, it is only a vector. Follow the eamples on the surf documentation page. You will find meshgrid helpful for this purpose.
x=linspace(-2,2,20);
y=x';
[X,Y] = meshgrid(x,y);
Z = X.*Y;
surf(x,y,Z)
3 Comments
Actally, in the code shown by @WEN SHIN LU, z is a matrix, because it was created as an outer product of the vectors y*x, where y is a column vector, and x a row vector. So the code shown does work.
x=linspace(-2,2,20);
y=x';
z=y*x;
surf(x,y,z)
Cris LaPierre
on 22 Jan 2022
Good point. I wonder what the issue is then.
WEN SHIN LU
on 23 Jan 2022
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
