Making a 2D plot of an equation against 2 datasets
2 views (last 30 days)
Show older comments
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,
0 Comments
Answers (2)
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))
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')
0 Comments
See Also
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!