MATLAB Answers

Give a range of Z axis instead of X and Y when graphing a surface

13 views (last 30 days)
A
A on 25 Mar 2016
Edited: A on 27 Mar 2016
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)

Accepted Answer

Image Analyst
Image Analyst on 25 Mar 2016
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')
  3 Comments
A
A on 27 Mar 2016
Sorry, I'll accept it now.
I can't seem to work out figure 3 below where I perform the subtraction. It gives me a matrix error and I can't get my head around how to fix the x and y of the grid to graph the difference between these:
x = 0:500;
y = 0:500;
surface1 = @(x,y) x + y;
surface2 = @(x,y) 2.*x + y;
[X,Y] = meshgrid(x,y);
S1a = surface1(X,Y);
S2a = surface2(X,Y);
figure(1)
% Find out where S1a is between 20 and 30
binaryMap = S1a >= 20 & S1a <= 30;
[rows, columns] = find(binaryMap);
xLeft = min(columns)
xRight = max(columns)
yTop = min(rows)
yBottom = max(rows)
S1b = S1a; % Initialize
% Set to NaN where outside range so it will be invisible.
S1b(~binaryMap) = nan;
% Crop to desired region;
S1b = S1b(yTop:yBottom, xLeft:xRight);
surf(x(xLeft:xRight), y(yTop:yBottom), S1b);
figure(2)
% Find out where S2a is between 20 and 30
binaryMap = S2a >= 20 & S2a <= 30;
[rows, columns] = find(binaryMap);
xLeft = min(columns)
xRight = max(columns)
yTop = min(rows)
yBottom = max(rows)
S2b = S2a; % Initialize
% Set to NaN where outside range so it will be invisible.
S2b(~binaryMap) = nan;
% Crop to desired region;
S2b = S2b(yTop:yBottom, xLeft:xRight);
surf(x(xLeft:xRight), y(yTop:yBottom), S2b);
figure(3)
surf(x(xLeft:xRight), y(yTop:yBottom), S2b-S1b);

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 25 Mar 2016
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 Comments
Walter Roberson
Walter Roberson on 26 Mar 2016
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.

Sign in to comment.


Mike Garrity
Mike Garrity on 25 Mar 2016
Edited: Mike Garrity on 25 Mar 2016
The new fsurf function, introduced in R2016a, is probably a good choice here.
surface1 = @(x,y) x + y;
fsurf(surface1)
zlim([0 5])
  1 Comment
Walter Roberson
Walter Roberson on 25 Mar 2016
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!