-2
$\begingroup$

I'm having trouble using the mesh or surf functions in matlab to make a 3d plot. I have a defined range of x values x=0:.5:5 and y values y=-5:.5:5 These both come out to 21 values. I also have a 21x21 matrix C with rows corresponding to x values and columns to y values.

I've been trying

figure();

x=0:.5:5;

z=-5:.5:5;

mesh(z,x,C);

but all I get is graph of a plane when the matrix actually has a very diverse set of elements. Any thoughts as to what I'm doing wrong? Thanks

1 Answers 1

2

Plot-commands usually wants the dimensions of all arguments to be the same. By this, it means, that it takes a value at any place of the first argument and can take the values of the corresponding places in the other arguments. If the dimensions don't agree, it assumes that they are the same and takes the last argument as "multiple plot"-commands.

What you have to do, is to create a meshgrid by using this command. This will take two vectors $x$ and $y$ and returns two rank-1 matrices with that one collumn repeated to fit the length of the other vector.

  • 0
    Could you go into detail about what kind of syntax I would have to use to get this to work? I'm just so baffled by why the original code isn't working. Other students computing the same exact problem have used surf(z,x,C) where x and y are vectors with the same size and Z is an nxn matrix where n is the size of x and y. My graph always turns out as a plane. How could I use meshgrid to fix this? [Z,X]=meshgrid(z,x) -> mesh(Z,X,C) didn't work for me.2017-01-18
  • 0
    Did you check the sizes of $X$ and $Y$? They should be the same size as $C$. I worked on my machine! Also: Vector-input is (I just learned that) perfectly legal for surf-commands and seems to work with mesh too. There has to be something wrong elsewhere.2017-01-18
  • 0
    Yeah laray I'm worried I'm missing some obscure setting that could be causing this. I seem to get a flat plane no matter what I do.2017-01-18
  • 0
    Did you try unsing other values than your $C$-Matrix? "C2=rand(size(C))" could give you a meaningful result.2017-01-18