2

I am trying to plot the following function in MATLAB:

1

The goal is to stitch all of the different conditions into one graph to make an overall CDF graph. What I have tried so far is the following:

x=linspace(0,1,20);
y=linspace(0,1,20);
Z=x.^y;
plot3(x,y,Z)
hold on
plot3(x,0,0);
plot3(0,y,0);

I am unsure of how to graph 1 for x>1, y>1 and also if there is a way to make the plot a solid surface for this set of conditions. I have tried using the rand() generator to produce 20+ numbers between 0 and 1, which shows the area that the variables could be located. However, it looks messy when it's a scatter of lines in a given area. I would rather it be a solid surface.

Is there a specific command I can use for this? I have seen in some examples using Mesh() to make the graph solid but am not sure if this would work for the set of data.

gnovice
  • 125,304
  • 15
  • 256
  • 359
James
  • 127
  • 2
  • 12
  • You appear to be missing 2 conditions: (`x < 0` and `y > 1`) and (`y < 0` and `x > 1`). Perhaps some of those "and"s should be "or"s? – gnovice Nov 06 '18 at 18:39
  • Judging the function, are you after something like a 2D colour plot, or a `surface()` plot for true 3D? – Adriaan Nov 06 '18 at 18:39

1 Answers1

6

You can accomplish this by generating a regular grid of x and y values with meshgrid, modifying values in Z with logical indexing, and displaying the result with surf:

% Grid points spanning from -1 to 2 for x and y:
[x, y] = meshgrid(linspace(-1, 2, 91));

% Fill Z with zeroes to start (satisfies condition 1 by default):
Z = zeros(size(x));

% Condition 2:
index = (x >= 0) & (x <= 1) & (y >= 0) & (y <= 1);
Z(index) = x(index).*y(index);

% Condition 3:
index = (x >= 0) & (x <= 1) & (y > 1);
Z(index) = x(index);

% Condition 4:
index = (y >= 0) & (y <= 1) & (x > 1);
Z(index) = y(index);

% Condition 5:
Z((x > 1) & (y > 1)) = 1;

% Plot surface:
surf(x, y, Z, 'EdgeColor', 'none');

enter image description here

Note: This assumes that unspecified conditions, like (x < 0 and y > 1) and (y < 0 and x > 1), should be zero as well.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Ever play Civilization IV? I'm totally getting a flashback: https://www.civfanatics.com/images/civ4/pyramids-sm.jpg. – rayryeng Nov 07 '18 at 18:14