ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Sep 2015 17:49:42 +0200Graph or plot a system of inequalitieshttps://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/Disclaimer: I'm somewhat new to Sage.
Is it possible to graph a system of inequalities in Sage? I'd like to reproduce the following system (top of image) and its graph (bottom of image). Based on (http://www.sagemath.org/tour-graphics.html), it seems like I need a region plot, but I'm not sure.
![image description](https://dl.dropboxusercontent.com/u/22824199/forums/sagemath/Systems%20of%20inequalities.png)Thu, 24 Sep 2015 14:34:10 +0200https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/Answer by calc314 for <p>Disclaimer: I'm somewhat new to Sage.</p>
<p>Is it possible to graph a system of inequalities in Sage? I'd like to reproduce the following system (top of image) and its graph (bottom of image). Based on (<a href="http://www.sagemath.org/tour-graphics.html">http://www.sagemath.org/tour-graphics...</a>), it seems like I need a region plot, but I'm not sure.</p>
<p><img alt="image description" src="https://dl.dropboxusercontent.com/u/22824199/forums/sagemath/Systems%20of%20inequalities.png"/></p>
https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?answer=29564#post-id-29564Here is another option:
var('y')
p=region_plot([y>=x-3,x>=y^2+2*y-3],(x,-10,20),(y,-8,10))
p+=implicit_plot(x==y^2+2*y-3,(x,-10,20),(y,-8,10),color='black')
p+=plot(x-3,(x,-10,20),color='black')
show(p,axes=True,frame=False )
Thu, 24 Sep 2015 16:25:08 +0200https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?answer=29564#post-id-29564Comment by mellow_yellow for <p>Here is another option:</p>
<pre><code>var('y')
p=region_plot([y>=x-3,x>=y^2+2*y-3],(x,-10,20),(y,-8,10))
p+=implicit_plot(x==y^2+2*y-3,(x,-10,20),(y,-8,10),color='black')
p+=plot(x-3,(x,-10,20),color='black')
show(p,axes=True,frame=False )
</code></pre>
https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?comment=29565#post-id-29565You beat me to it. I was going to suggest:
var("x y")
f1 = x >= y^2 + 2*y -3
f2 = y >= x-3
f3 = x == y^2 + 2*y -3
f4 = y == x-3
rx = (-10,15)
ry = rx
i = [f1, f2] # i meaning inequalities
p = region_plot(i, rx, ry)
p+= implicit_plot(f3, rx, ry, color="black")
p+= implicit_plot(f4, rx, ry, color="black")
show(p, axes="true", frame=False, aspect_ratio=1)Thu, 24 Sep 2015 17:47:48 +0200https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?comment=29565#post-id-29565Answer by ndomes for <p>Disclaimer: I'm somewhat new to Sage.</p>
<p>Is it possible to graph a system of inequalities in Sage? I'd like to reproduce the following system (top of image) and its graph (bottom of image). Based on (<a href="http://www.sagemath.org/tour-graphics.html">http://www.sagemath.org/tour-graphics...</a>), it seems like I need a region plot, but I'm not sure.</p>
<p><img alt="image description" src="https://dl.dropboxusercontent.com/u/22824199/forums/sagemath/Systems%20of%20inequalities.png"/></p>
https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?answer=29563#post-id-29563To get a plot in your case:
g(x) = x-3
y = var('y')
eq = x == y^2 + 2*y -3
sol = solve(eq,y)
h1(x) = sol[0].rhs()
h2(x) = sol[1].rhs()
G = Graphics()
G += plot(g,-5,5, fill=g(5),fillcolor='gold')
G += plot(h1,-4,5)
G += plot(h2,-4,5,fill=h1,fillcolor='blue',fillalpha=0.2)
G.show() Thu, 24 Sep 2015 16:20:05 +0200https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?answer=29563#post-id-29563Comment by mellow_yellow for <p>To get a plot in your case:</p>
<pre><code>g(x) = x-3
y = var('y')
eq = x == y^2 + 2*y -3
sol = solve(eq,y)
h1(x) = sol[0].rhs()
h2(x) = sol[1].rhs()
G = Graphics()
G += plot(g,-5,5, fill=g(5),fillcolor='gold')
G += plot(h1,-4,5)
G += plot(h2,-4,5,fill=h1,fillcolor='blue',fillalpha=0.2)
G.show()
</code></pre>
https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?comment=29566#post-id-29566This is a clever solution. I didn't know you could use the solver with rhs and plot to graph relations. The problem is that it doesn't shade the inequality's region like region_plot does.Thu, 24 Sep 2015 17:49:42 +0200https://ask.sagemath.org/question/29560/graph-or-plot-a-system-of-inequalities/?comment=29566#post-id-29566