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)
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.
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.