Z must be a matrix, not scalar or vector

3 views (last 30 days)
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!

Accepted Answer

Cris LaPierre
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)
  1 Comment
Daniel Walker
Daniel Walker on 4 Aug 2020
Your response was very helpfuland resulted in me getting this far. The eqn for the intersection seems correct. I'm still figuring out how to include all three equations (the eqn for the ellipsoid cylinder, the plane and the curve intersection between them) in the same frame. x^2/2 +2z^2 = 2 can be simplified to x^2/4 +z^2 = 1. In this format 2cost can be substituted for x with sint replacing z with the objective to make the eqn into 4cos^2(t)/4 + sin^2(t)=1.

Sign in to comment.

More Answers (1)

Walter Roberson
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.
  1 Comment
Daniel Walker
Daniel Walker on 4 Aug 2020
Thanks for your response. x^2/2 +2z^2 = 2 can be simplified to x^2/4 +z^2 = 1. In this format 2cost can be substituted for x with sint replacing z with the objective to make the eqn into 4cos^2(t)/4 + sin^2(t)=1. The graph is a parabaloid cylinder along the y axis which I feel very confident about. Just graphing the cylinder results in below:
However, I'm still trying to figure out how to neatly highlight the intersection of the curve and the plane and how that relates to the function which defines the intersection.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!