Making a 2D plot of an equation against 2 datasets

2 views (last 30 days)
Hello,
I am trying to make a 2D plot of x and y based on a certain relation between them
Let:
x= 1:1:20
y= 1:0.5:8
Now, I want to plot z= x*y against x and y in the xy plane. Also, I only want to see the values of z such that z <10 or any other range .
Can someone please tell me how to do this? It seems simple, but I’m not a MATLAB Wiz.
Regards,

Answers (2)

jonas
jonas on 19 Sep 2018
Edited: jonas on 19 Sep 2018
x = 1:1:20;
y = 1:0.5:8;
[X,Y] = meshgrid(x,y);
Z = X.*Y;
surf(X,Y,Z)
zlim([0 10]);
You could also replace the last two lines by something like
scatter3(X(Z<10),Y(Z<10),Z(Z<10),[],Z(Z<10))
  2 Comments
Kyle
Kyle on 19 Sep 2018
Thank you sir. But this is a 3D plot. I want it in 2D. Regards,
jonas
jonas on 19 Sep 2018
Edited: jonas on 19 Sep 2018
Like this?
scatter(X(Z<10),Y(Z<10),[],Z(Z<10))
or
Z(Z>=10)=NaN;
imagesc(X(:),Y(:),Z)
or
Z(Z>=10)=NaN;
contourf(X,Y,Z)

Sign in to comment.


Star Strider
Star Strider on 19 Sep 2018
Edited: Star Strider on 19 Sep 2018
For a 2D version, try this:
x = 1:1:20;
y = 1:0.5:8;
z = bsxfun(@times, x, y');
z(z >= 10) = NaN;
figure
plot(z)
grid
Using contour with specific labeled levels:
x = 1:1:20;
y = 1:0.5:8;
[X,Y] = ndgrid(x,y);
Z = X.*Y;
figure
[C,h] = contour(X, Y, Z, 1:0.5:10);
Lvls = h.LevelList;
h.LevelList = Lvls(Lvls < 10);
clabel(C, h, Lvls(1:2:end))
xlabel('X')
ylabel('Y')

Categories

Find more on Graphics Object Programming 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!