Z must be a matrix, not scalar or vector
3 views (last 30 days)
Show older comments
Daniel Walker
on 4 Aug 2020
Commented: Daniel Walker
on 4 Aug 2020
I've seen a bunch of other people ask similar questions, my code is a bit different.
The following were my originial commands before I tried to code it:
>> t=linspace(0, 2*pi);
>> x=2*cos(t);
>> y=sin(t)+cos(t)-1;
>> z=sin(t);
>> surf(x,y,z)
Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
I am trying to graph the equation of the curve of intersection of curve (x^2)/2 + 2z^2=2 and plane x-y+z-1=0
Thanks for any help I can get!
0 Comments
Accepted Answer
Cris LaPierre
on 4 Aug 2020
z needs to be the same size as X and Y. Perhaps you mean to do something like this?
t=linspace(0, 2*pi);
T = meshgrid(t);
x=2*cos(T);
y=sin(T)+cos(T)-1;
z=sin(T)
surf(x,y,z)
More Answers (1)
Walter Roberson
on 4 Aug 2020
t=linspace(0, 2*pi);
That is a row vector.
x=2*cos(t);
Row Vector.
y=sin(t)+cos(t)-1;
Row vector.
z=sin(t);
Row vector that does not depend upon x or y.
surf(x,y,z)
x, y, z are all row vectors.
In some cases it is valid for x and y to be vectors for surf, provided that length(x) = size(z,2) and length(y) = size(z,1). However, with your z being a row vector, then size(z,1) = 1 so your y would have to be length 1 to match... and even that would not be permitted as it does not give enough input values to form a surface out of.
In the code you show the image of, you do
[X,Y] = meshgrid(x,y);
and after that X and Y would be 2D matrices. However, that code still creates z from the row vector t.
curve of intersection of curve (x^2)/2 + 2z^2=2 and plane x-y+z-1=0
It is not at all obvious that you are plotting the intersection in what you posted. It looks to me more as if you are trying to plot the first curve in parameteric form. You could use more comments.
See Also
Categories
Find more on Surface and Mesh Plots 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!