ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 15 Jan 2014 03:11:22 -0600Isolines of piecewise linear functionhttps://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/Hi,
I'm trying to find out how to plot isolines (in 3D) of the following function :
z = 0.2 x + 0.8 y if x < y and z = 0.6 x+0.4 y if x >= y
Thanks in advance for any help,
PatrickWed, 15 Jan 2014 00:47:34 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/Answer by paterijk for <p>Hi, </p>
<p>I'm trying to find out how to plot isolines (in 3D) of the following function : </p>
<p>z = 0.2 x + 0.8 y if x < y and z = 0.6 x+0.4 y if x >= y</p>
<p>Thanks in advance for any help, </p>
<p>Patrick</p>
https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?answer=15921#post-id-15921Thanx for the quick answer.
In fact, I need a mix of both plot3d and contour_plot : in the 3d plot, I would like to see a sample of lines having the same "z" values, instead of the surface.
Best regards,
PatrickWed, 15 Jan 2014 02:01:08 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?answer=15921#post-id-15921Answer by tmonteil for <p>Hi, </p>
<p>I'm trying to find out how to plot isolines (in 3D) of the following function : </p>
<p>z = 0.2 x + 0.8 y if x < y and z = 0.6 x+0.4 y if x >= y</p>
<p>Thanks in advance for any help, </p>
<p>Patrick</p>
https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?answer=15920#post-id-15920First, you can define your function as follows:
sage: f = lambda x,y : 0.2*x + 0.8*y if x < y else 0.6*x + 0.4
Then, your question is not clear to me. If you want a 3D plot of the function, you can do:
sage: plot3d(f, [-10,10], [-10,10])
But if you want the isolines, then it is a 2D object not a 3D one, which you can get by:
sage: contour_plot(f, [-10,10], [-10,10])
You can get some fancy style output options by typing
sage: contour_plot?
If you want to immerse the contour plot in 3D along the graph of the function (as suggested in your comment), you can try something like:
sage: sum([implicit_plot3d(lambda x,y,z : f(x,y), [-10,10], [-10,10], [c,c+0.01], contour=c) for c in range(-10,10)])
Wed, 15 Jan 2014 01:39:05 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?answer=15920#post-id-15920Comment by tmonteil for <p>First, you can define your function as follows:</p>
<pre><code>sage: f = lambda x,y : 0.2*x + 0.8*y if x < y else 0.6*x + 0.4
</code></pre>
<p>Then, your question is not clear to me. If you want a 3D plot of the function, you can do:</p>
<pre><code>sage: plot3d(f, [-10,10], [-10,10])
</code></pre>
<p>But if you want the isolines, then it is a 2D object not a 3D one, which you can get by:</p>
<pre><code>sage: contour_plot(f, [-10,10], [-10,10])
</code></pre>
<p>You can get some fancy style output options by typing </p>
<pre><code>sage: contour_plot?
</code></pre>
<p>If you want to immerse the contour plot in 3D along the graph of the function (as suggested in your comment), you can try something like:</p>
<pre><code>sage: sum([implicit_plot3d(lambda x,y,z : f(x,y), [-10,10], [-10,10], [c,c+0.01], contour=c) for c in range(-10,10)])
</code></pre>
https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16440#post-id-16440I updated my answer to try to answer your comment.
Wed, 15 Jan 2014 03:06:04 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16440#post-id-16440Comment by paterijk for <p>First, you can define your function as follows:</p>
<pre><code>sage: f = lambda x,y : 0.2*x + 0.8*y if x < y else 0.6*x + 0.4
</code></pre>
<p>Then, your question is not clear to me. If you want a 3D plot of the function, you can do:</p>
<pre><code>sage: plot3d(f, [-10,10], [-10,10])
</code></pre>
<p>But if you want the isolines, then it is a 2D object not a 3D one, which you can get by:</p>
<pre><code>sage: contour_plot(f, [-10,10], [-10,10])
</code></pre>
<p>You can get some fancy style output options by typing </p>
<pre><code>sage: contour_plot?
</code></pre>
<p>If you want to immerse the contour plot in 3D along the graph of the function (as suggested in your comment), you can try something like:</p>
<pre><code>sage: sum([implicit_plot3d(lambda x,y,z : f(x,y), [-10,10], [-10,10], [c,c+0.01], contour=c) for c in range(-10,10)])
</code></pre>
https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16439#post-id-16439That's exactly what I need, thank you ;-)Wed, 15 Jan 2014 03:11:22 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16439#post-id-16439Comment by paterijk for <p>First, you can define your function as follows:</p>
<pre><code>sage: f = lambda x,y : 0.2*x + 0.8*y if x < y else 0.6*x + 0.4
</code></pre>
<p>Then, your question is not clear to me. If you want a 3D plot of the function, you can do:</p>
<pre><code>sage: plot3d(f, [-10,10], [-10,10])
</code></pre>
<p>But if you want the isolines, then it is a 2D object not a 3D one, which you can get by:</p>
<pre><code>sage: contour_plot(f, [-10,10], [-10,10])
</code></pre>
<p>You can get some fancy style output options by typing </p>
<pre><code>sage: contour_plot?
</code></pre>
<p>If you want to immerse the contour plot in 3D along the graph of the function (as suggested in your comment), you can try something like:</p>
<pre><code>sage: sum([implicit_plot3d(lambda x,y,z : f(x,y), [-10,10], [-10,10], [c,c+0.01], contour=c) for c in range(-10,10)])
</code></pre>
https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16442#post-id-16442Thanx for the quick answer.
In fact, I need a mix of both plot3d and contour_plot : in the 3d plot, I would like to see a sample of lines having the same "z" values, instead of the surface.
Best regards,
PatrickWed, 15 Jan 2014 02:01:24 -0600https://ask.sagemath.org/question/10914/isolines-of-piecewise-linear-function/?comment=16442#post-id-16442