![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173831/image.png)
Give a range of Z axis instead of X and Y when graphing a surface
3 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I am trying to graph a surface below, but instead of giving it x and y restrictions like how I have given it, can I graph in a certain range of Z values and see what X and Y values it comes up with? For example, with the surface below I want it to graph the surface between Z values of 20 and 30. Is this possible? Thanks!
x = [0:500];
y = [0:500];
[X,Y] = meshgrid(x,y);
surface1 = @(x,y) x + y;
S1 = surface1(X,Y);
surf(X,Y,S1)
0 个评论
采纳的回答
Image Analyst
2016-3-25
Perhaps this is what you want:
x = 0:500;
y = 0:500;
[X,Y] = meshgrid(x,y);
surface1 = @(x,y) x + y;
subplot(2,1,1);
S1 = surface1(X,Y);
surf(X,Y,S1, 'edgecolor', 'none')
title('S1', 'FontSize', 40);
% Find out where S1 is between 20 and 30
binaryMap = S1 >= 20 & S1 <= 30;
[rows, columns] = find(binaryMap);
xLeft = min(columns)
xRight = max(columns)
yTop = min(rows)
yBottom = max(rows)
S2 = S1; % Initialize
% Set to NaN where outside range so it will be invisible.
S2(~binaryMap) = nan;
% Crop to desired region;
S2 = S2(yTop:yBottom, xLeft:xRight);
subplot(2,1,2);
surf(x(xLeft:xRight), y(yTop:yBottom), S2);
title('S2', 'FontSize', 40);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.5 0 0.3 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173831/image.png)
3 个评论
Image Analyst
2016-3-27
You should construct surface2 with capital X and Y. Then you can do
surface3 = surface2 - S1; % or whatever surfaces you want to subtract.
then do the same thing with this new subtraction surface. By they way, you didn't Accept the answer (yet).
更多回答(2 个)
Walter Roberson
2016-3-25
Not in general, no.
If you have the formula for the function available in analytic form, it is sometimes possible to symbolically find the boundary conditions by inverting the function, after which you would plot within the boundary, setting zlim to the desired portion.
If you do not have the formula for the function available in analytic form, then you would have to apply human reasoning to find the boundaries.
But if the function has to be treated as a "black box" (you call it, it computes a value through some unspecified method) then you cannot know where the function will produce certain values. Consider for example,
f = @(x,y) x + y + 20 * (x == 7.3193432343)
which produces unexpected values in-range at x == 7.31934323429999977861371007747948169708251953125 exactly ... and also pushes some values that would normally be in-range to be out of range. As humans looking at the calculation we can determine that, but any sampling of the surface that you do would be likely to miss that exact x.
2 个评论
Walter Roberson
2016-3-26
I think ImageAnalyst's solutions does not meet the requirements you set out in your question. It might satisfy your actual requirements, rather than what you asked for.
Note that your question requires an infinite plotting domain. For example at x = -1E8 then y = 20 - 1E8 to y = 30 - 1E8 satisfies your stated requirements and so must be plotted. For every finite real x, there is a range of y that is on the surface, and for every finite y, there is a range of x that is on the surface. Therefore if you take a rectangular bounding box aligned with the x and y axes, it is going to be the same size as the rectangle you sampled over.
Mike Garrity
2016-3-25
编辑:Mike Garrity
2016-3-25
surface1 = @(x,y) x + y;
fsurf(surface1)
zlim([0 5])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173829/image.png)
1 个评论
Walter Roberson
2016-3-25
Interesting, but not the solution here. The user wants to be able to give a function handle and a z range, and have the program figure out what x and y range is needed for the results to be in the given z range.
fsurf() uses a default x and y range, not unlike the older ezplot.
In order for MATLAB to possibly be able to figure out what x and y range to use, the function would have to be invertable from the user-supplied function handle. Something along the lines of solve(f(x,y)>=zmin & f(x,y)<=zmax, [x, y]) and plot all resulting x, y domains.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!