I want to use SageMath to plot in 3D a function of two variables z=f(x,y) that is bound by a number of inequalities. As a simple example, say:

f(x,y) = x+2y

This is just a tilted plane; doesn't look too exciting. But now... Restrict it to only points that satisfy both of these inequalities:

y<x (so, boundary is line with slope 1)

y>-x (so, boundary is line with slope -1)

Note that these boundaries intersect at (x,y)=(0,0) I would like a 3D rotatable view of a plane with the edges at the boundary lines, and a corner at the origin.

Later I would like to add more inequalities, maybe a few other bondaries so that it is limited to a triangular or other polygonal region, or perhaps $x^2 + y^2 < 1$ so it would be a tilted stretch quarter-circle.

How do I set these boundaries? When I looked up plotting bondaries, my web search only found how to set min,max for (x,y) for the purposes of displaying the plot, and not how to confine the region itself.

Thanks for any help you can give.

  • 1
    $\begingroup$ Implicit plots let you specify a region, but unfortunately the boundary looks jagged, although you can disguise that by using a huge number for plot_points. doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/… $\endgroup$
    – PM 2Ring
    Commented Sep 25, 2022 at 15:27
  • 1
    $\begingroup$ Right, in particular implicit_plot3d(x+2*y-z==0, (x, -3, 3), (y, -3, 3), (z, -3, 3), region=lambda x,y,z: x-y > 0 and y + x > 0, plot_points=100). $\endgroup$ Commented Sep 25, 2022 at 16:20
  • $\begingroup$ sagecell.sagemath.org/… $\endgroup$
    – PM 2Ring
    Commented Sep 25, 2022 at 16:47
  • $\begingroup$ PM 2Ring, thank you for telling me about the "region" argument -- from reading the docs, I couldn't tell if it was the first argument, or the second, or the third ... turns out it's none of the above, and I should just add an argument prefixed by "region=". John Palmieri, thank you for giving an example. This works! To clarify for other readers: before executing the above implicit_plot3d() command, you need to type var('x,y,z') PM 2Ring and John Palmieri, if you put your response as a formal answer rather than just a comment to my question, I can upvote it and give you points. $\endgroup$
    – kwantum
    Commented Sep 25, 2022 at 17:05
  • $\begingroup$ I'm hoping that someone will post a better solution. ;) You can often work around this problem by using a parametric plot, if you can figure out how to incorporate the desired restrictions into the parameterisation. There's a trick using NaN that lets you specify a region in a normal plot3d, but it also gives a jagged boundary. I'll put a demo in the next comment. $\endgroup$
    – PM 2Ring
    Commented Sep 25, 2022 at 23:22


You must log in to answer this question.

Browse other questions tagged .