## How do I plot a 3D surface bounded over the first octant?

### Jett Marcos (view profile)

on 27 Jan 2018 at 14:48
Latest activity Edited by John D'Errico

### John D'Errico (view profile)

on 28 Jan 2018 at 17:17

### John D'Errico (view profile)

I'm trying to plot the surface X+Y+Z=a bounded in the first octant, where a is a constant. My code so far is as follows, however, I have no idea how to bound it within the first octant and I always end up with a square, and not the expected result of a triangle.

a=2; [X,Y]=meshgrid(-3:0.5:3,-3:0.5:3); Z=a-X-Y; surf(X,Y,Z)

### John D'Errico (view profile)

on 28 Jan 2018 at 14:23
Edited by John D'Errico

### John D'Errico (view profile)

on 28 Jan 2018 at 14:31

It fails to do what you asked to do, because what you did has nothing to do with constraining it to the first octant. You generated a list of (x,y) pairs, but provided no understanding to MATLAB of the real question, which is what is the surface in terms of where thing lives in the first octant!

If you recognize the result will be a planar triangle, embedded in R^3, then you could simply compute the coordinates of the three vertices of that triangle. Then use patch.

So when x=y=0, what is z? z=a of course. Likewise, when x=z=0, what is y? Finally, when y=z=0, what is x?

You should see that this results in three vertices of a triangle, thus the rows of

```[0 0 a;0 a 0;a 0 0]
```

Patch is now simple to use.

```help patch
```

If your goal is somewhat less understanding of the result, so you hope that MATLAB can simply be told to plot some general relation when constrained to the first octant, that will take a completely different approach, because then you are effectively asking MATLAB to do your thinking for you.

Jett Marcos

### Jett Marcos (view profile)

on 28 Jan 2018 at 16:22

While this particular surface is rather simple, more complicated surfaces might not be calculated as easily, so I'm trying to get the correct plot before any actual calculations are made. If I just want to plot the surface, is it possible to bound it using limits instead? Would the ff code work better?

a=2;

[X,Y]=meshgrid(-3:0.5:3,-3:0.5:3);

Z=a-X-Y;

xlim([0 inf]); ylim([0 inf]); zlim([0 inf]);

surf(X,Y,Z)

John D'Errico

### John D'Errico (view profile)

on 28 Jan 2018 at 17:13

Yes, but more complex surfaces will potentially require their own methods, depending upon the complexity of those surfaces. If you are asking me how to solve a problem where you have not bothered to tell me what is the problem, how can I know the best way to solve it?

If the problem is trivial enough that you can isolate z, as you can on that problem, then one method you can employ is to use meshgrid as you did, then replacing the elements with NaN that fail the test of falling in the 1st octant. But that will leave holes along the edge of the boundary, where the mesh is insufficiently fine. Really, you need to define the boundary where combinations of x and y fail to result in a value that stays in the first octant. So then I might start with a TRIANGULATION of the domain in (x,y) that yields a solution, then only work with those combinations of x and y to genrate a z value. Then plot the result using trisurf. On a COMPLETELY general surface, the boundary can be quite nasty itself. So simply starting with a meshgrid in x and y cannot suffice.

The point is, the code you ask about using meshgrid and axis limits cannot be sufficient for any general relationship. For example, suppose the surface in question is some general conic form, an ellipsoid or general hyperbolic surface that lies partially in the first octant? For any combination of x and y, there may be multiple solutions, with disjoint segments of surfaces that just happen to wander into the first octant.

For example, is the surface some completely implicit thing, some nasty, convoluted mess? You might decide to solve the problem in terms of an iso-surface. So now start with a meshgrid of the domain of interest in THREE dimensions, then call isosurface. Since meshgrid only extends as far out as you are willing to look in 3-d, this iso-surface may be insufficient. Or it may be too coarse, etc.

Different problems will require different solutions, solutions that are generated by understanding the problem and what form the solution may involve. Simply trying to solve the problem using only axis limits will generally be a waste of time though.

If your question is how do you solve this problem for ANY completely general relationship, then you might need to understand a wide variety of tools, ranging from graphical tools, to triangulation & tessellation tools, rootfinding tools, domain meshing tools, etc.