Plotting multiple inequalities in 3D Space
16 views (last 30 days)
Show older comments
Patrick Dotson
on 23 Nov 2021
Answered: John D'Errico
on 23 Nov 2021
Hi, I wanted to plot a set of points in 3d space in Matlab subject to multiple inequalities. I tried doing this:
[x,y,x] = meshgrid(-10:0.1:10);
ineq = [0<=y, y<=3, 9-y.^2<=x, x<=9, 0<=z, z<=9-x]
x(~ineq) = NaN;
y(~ineq) = NaN;
z(~ineq) = NaN;
surf(x,y,z)
But I am told that I "Attempted to grow array along ambiguous dimension." I was wondering what the correct way to do this would be.
0 Comments
Accepted Answer
John D'Errico
on 23 Nov 2021
First,
[x,y,x] = meshgrid(-10:0.1:10);
Correct code is important in MATLAB. Else it will not know that you really intended to write [x,y,z] and not [x,y,x].
Next, what do you think this does?
ineq = [0<=y, y<=3, 9-y.^2<=x, x<=9, 0<=z, z<=9-x]
It does not create something that is "and"ed across those inequaities. Instead, it creates a larger array.
Next, is the result of that operation actually a surface, or just something that you want to interpret as a surface?
surf(x,y,z)
So how can surf possibly interpret what it will see as a set of scattered points, some of which are NaNs, and know what you intend?
You MIGHT try this:
[x,y,z] = meshgrid(-10:0.1:10,-10:0.1:10,-10:0.1:10);
ineq = (0<=y) & (y<=3) & (9-y.^2<=x) & (x<=9) & (0<=z) & (z<=9-x);
x(~ineq) = NaN;
y(~ineq) = NaN;
z(~ineq) = NaN;
plot3(x(:),y(:),z(:),'.')
view(-15,65)
box on
grid on
But all that does is show a non-convex domain, represented by scattered data points that lie inside the domain. Surf is not designed to do what you want.
0 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots 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!