user2291324
user2291324

Reputation: 13

Mesh function seems to swap x and y values

I am trying to plot a very simple function in the 3d plane.

f=zeros(101,101);
xs=0:0.1:10;
ys=0:0.1:10;

for j=1:101
    f(1,j)=ys(j);
end

Here are 3 plots:

enter image description here enter image description here enter image description here

The first is a plot of f vs x at ymin:

figure; plot(xs,f(:,1),'*r')
xlabel('x')
ylabel('f')

The second is a plot of f vs y at xmin:

figure; plot(ys,f(1,:),'*r')
xlabel('y')
ylabel('f')

And finally the third is a 3d mesh:

figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')

However the mesh plot seems to contradict the 2 2d plots, it seems to have the x and y switched around if you get me. Can anyone help? Should it be mesh(ys,xs,f) for some reason? Thanks!

Upvotes: 1

Views: 2055

Answers (1)

Dan
Dan

Reputation: 45752

mesh didn't switch your x and y, it's a matter of definition. Don't forget that a matrix doesn't have an x axis or a y axis but rather a row dimension and a column dimension. The row dimension is normally stated first in a pair just like the x dimension but if you are equating it too an image then you would normally have the x axis going across which is actually along the column axis!

Try like this rather

for j=1:101
    f(j,1)=ys(j);
end

figure; plot(xs,f(1,:),'*r')
xlabel('x')
ylabel('f')

figure; plot(ys,f(:,1),'*r')
xlabel('y')
ylabel('f')

figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')

Upvotes: 3

Related Questions