If f1(x,y)≤z≤f2(x,y), how to draw the range of z?

5 views (last 30 days)
xin
xin on 12 Sep 2024
Commented: xin on 12 Sep 2024
x=-5:0.1:5;
y=-5:0.1:5;
for m=1:101
for n=1:101
z1(m,n)=x(m)^2+y(n)^2;
z2(m,n)=3*x(m)^3+y(n)^2+1;
end
end
surf(x,y,z1,"EdgeColor","none")
hold on
surf(x,y,z2,"EdgeColor","none")
If the upper and lower limits of z are described by separate functions, how to plot the range of the z function in the direction of the z-axis instead of two surfaces?

Answers (3)

KSSV
KSSV on 12 Sep 2024
x=-5:0.1:5;
y=-5:0.1:5;
[m,n] = meshgrid(x,y) ;
z1 = m.^2+n.^2;
z2 = 3*m.^3+n.^2+1 ;
surf(x,y,z1,"EdgeColor","none")
hold on
surf(x,y,z2,"EdgeColor","none")

Shivam
Shivam on 12 Sep 2024
Hi @xin,
As per my understanding, you are given an inequality f1(x,y) <= z <= f2(x,y) describing the upper and lower limit for z on defined arrays x and y and you want to see the region of z satisfiying the inequality given.
In the workaround provided below, the points z1 and z2, corresponding to f1(x,y) and f2(x,y) respectively, are identified where z1 <= z2. The points that satisfy this condition are plotted in different colors using surface plot.
x = -5:0.1:5;
y = -5:0.1:5;
% Create meshgrid for x and y
[m, n] = meshgrid(x, y);
% Calculate z1 and z2
z1 = m.^2 + n.^2;
z2 = 3*m.^3 + n.^2 + 1;
% Logical mask for the region where z1 <= z2
mask = z2 >= z1;
% Set the values of z1 and z2 to NaN where the condition is not met
% This effectively removes these points from the plot
z1(~mask) = NaN;
z2(~mask) = NaN;
% Plot the region where z1 <= z2
figure;
surf(x, y, z2, 'EdgeColor', 'none', 'FaceAlpha', 0.7, 'FaceColor', 'r');
hold on;
surf(x, y, z1, 'EdgeColor', 'none', 'FaceAlpha', 0.7, 'FaceColor', 'b');
% Add labels and title
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Region where z1 <= z2');
grid on;
hold off;
The region between red and blue surface is technically the preferred region for z as per the given equality.
I hope it helps.

Image Analyst
Image Analyst on 12 Sep 2024
Do you want just the magnitude of the range, without visualizing the starting and stopping points? If so you can just subtract the functions:
x=-5:0.1:5;
y=-5:0.1:5;
for m=1:101
for n=1:101
z1(m,n)=x(m)^2+y(n)^2;
z2(m,n)=3*x(m)^3+y(n)^2+1;
end
end
rangeMagnitude = abs(z1-z2);
imshow(rangeMagnitude, []);
axis('on', 'image')
colorbar
  1 Comment
xin
xin on 12 Sep 2024
No, I want to fill the area between two surfaces. Is there a function that does something similar to fill or patch?

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!