vyi
vyi

Reputation: 1092

Plot x=y plane in MATLAB

I fail to think how should I proceed for plotting x=y plane withing a 2x2x2 space.

I create a 2x2 meshgrid

[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);

then I come to define Z - I get stuck

My thoughts go like this

  1. I need to create a 21x21 array for Z ( I use surf function.. and the dimensions of X, Y, Z must match. Right?).
  2. I need to populate only those values of Z that follow x==y
  3. Now for each such point i.e. x==y Z will vary -1:0.1:1 .. Does This require that I iterate again and again on the x==y and keep drawing Z with values from -1:0.1:1?

Am I going the right way about plotting this plane? Kindly help.

Upvotes: 3

Views: 1284

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

You are actually trying to do something two dimensional in a 3 dimensional setting.

A bit unintuitive, but that does not mean it can't be done, for example:

[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
Z = zeros(size(X)); % Perhaps you want NaN rather than zeros
idx = X==Y;
Z(idx)=X(idx).^2+Y(idx)  % Of course identical to X(idx).^2+X(idx)
surf(Z)

Upvotes: 1

Shai
Shai

Reputation: 114806

You simply need to define X and Z, Y is equal to X by definition:

[X Z] = meshgrid(-1:.1:1,-1:.1:1);
figure;
surf(X,X,Z);xlabel('x');ylabel('y');zlabel('z');

Results with
enter image description here

Upvotes: 9

Related Questions