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.Wed, 08 Dec 2021 07:40:47 +0100A parametric plot with branches for which restricting the range with xmin, xmax doesn't workhttps://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/Hi
The following plot
var('t')
parametric_plot( (8*(6+t)/(t^2-16), 2*(8+ 3 *t)/(t^2-16)), (t, -6, 6),xmin=-3,xmax=3,detect_poles=True)
has three branches. 1) For some reason, the plot is not correct. Also
var('t')
parametric_plot( (8*(6+t)/(t^2-16), 2*(8+ 3 *t)/(t^2-16)), (t, -6, 6),xmin=-3,xmax=3,ymin=-3,ymax=3)
fails. 2)How to plot the branches in different colors? 3) Can I add a point moved by a cursor when t increases, like with the Mathematica command Manipulate? Or make an animation to show how a point traverses the three branches?Sat, 04 Dec 2021 20:12:17 +0100https://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/Answer by Emmanuel Charpentier for <p>Hi
The following plot</p>
<pre><code>var('t')
parametric_plot( (8*(6+t)/(t^2-16), 2*(8+ 3 *t)/(t^2-16)), (t, -6, 6),xmin=-3,xmax=3,detect_poles=True)
</code></pre>
<p>has three branches. 1) For some reason, the plot is not correct. Also</p>
<pre><code>var('t')
parametric_plot( (8*(6+t)/(t^2-16), 2*(8+ 3 *t)/(t^2-16)), (t, -6, 6),xmin=-3,xmax=3,ymin=-3,ymax=3)
</code></pre>
<p>fails. 2)How to plot the branches in different colors? 3) Can I add a point moved by a cursor when t increases, like with the Mathematica command Manipulate? Or make an animation to show how a point traverses the three branches?</p>
https://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/?answer=60098#post-id-60098> For some reason, the plot is not correct.
You mean that `xmin` and `xmax` are not honored ? See [Trac#16686](https://trac.sagemath.org/ticket/16686) and the ticket-chain referenced...
Workaround :
sage: def ininterval(x,l,u):return float(x) if x>l and x<u else float("NaN")
sage: plot([lambda t:ininterval(8*(6+t)/(t^2-16),-3,3),lambda t:ininterval(2*(8+ 3 *t)/(t^2-16),-3,3)], (t,-6,6), axes_labels=["x","y"], parametric=True)
verbose 0 (3839: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 130 points.
verbose 0 (3839: plot.py, generate_plot_points) Last error message: 'Unable to compute f(6.0)'
Launched png viewer for Graphics object consisting of 2 graphics primitives
![image description](/upfiles/16386949594178996.png)
> has three branches.
Nope.
What are the possible values of `x` and `y` for $t\in[-6~6]$ ? An analytical solution is possible by solving `x` and `y` for `t` then solving the inequations for $t\in|-5~6]$, but is somewhat painful... A graphical solution is easier to grasp :
sage: plot([lambda t:ininterval(8*(6+t)/(t^2-16),-30,30),lambda t:ininterval(2*(8+ 3 *t)/(t^2-16),-30,30)], (t,-6,6), axes_labels=["t",""], legend_label=["x","y"])+plot([-3, 3],(-6,6),color=["green","red"])
verbose 0 (3839: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 14 points.
verbose 0 (3839: plot.py, generate_plot_points) Last error message: 'Unable to compute f(4.325615967306306)'
Launched png viewer for Graphics object consisting of 8 graphics primitives
![image description](/upfiles/16386959221236952.png)
The most limiting variable is `x`, which is positive and <3 for `t`>-6, `t`> -9/2 approximately, and slightly superior to -3 for `t`>-3, `t`<0 approximately.
> How to plot the branches in different colors ?
The painful analytical solution I studiously avoided above is necessary to define the different regions; the `color=` argument can then be built around this definition (left tothe reader as an exercise;-)...
**EDIT :** after addition of another question...
A static version of your chronogram could be :
var("t")
fx(t)=8*(6+t)/(t^2-16)
fy(t)=2*(8+ 3 *t)/(t^2-16)
def ininterval(x,l,u):return float(x) if x>l and x<u else float("NaN")
P=plot([lambda t:ininterval(fx(t),-3,3), lambda t:ininterval(fy(t),-3,3)],
(t,-6,6), axes_labels=["x","y"], parametric=True)
Pts=[]
T=[]
xmin=P.xmin()
xmax=P.xmax()
ymin=P.ymin()
ymax=P.ymax()
for t in srange(-6,6.1,0.1):
px=fx(t)
py=fy(t)
if px>=xmin and px<=xmax and py>=xmin and py<=ymax:
Pts+=[(px,py)]
T+=[text("%4.1f"%t,(px,py),
vertical_alignment="bottom",
horizontal_alignment="left",
color="black")]
sage: show(P+points(Pts)+sum(T))
Launched png viewer for Graphics object consisting of 43 graphics primitives
![image description](/upfiles/16389126893713255.png)
An animated/interactive version is left to the reader of the documentation as an exercise...
HTH,Sun, 05 Dec 2021 10:33:32 +0100https://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/?answer=60098#post-id-60098Comment by florin for <blockquote>
<p>For some reason, the plot is not correct.</p>
</blockquote>
<p>You mean that <code>xmin</code> and <code>xmax</code> are not honored ? See <a href="https://trac.sagemath.org/ticket/16686">Trac#16686</a> and the ticket-chain referenced...</p>
<p>Workaround :</p>
<pre><code>sage: def ininterval(x,l,u):return float(x) if x>l and x<u else float("NaN")
sage: plot([lambda t:ininterval(8*(6+t)/(t^2-16),-3,3),lambda t:ininterval(2*(8+ 3 *t)/(t^2-16),-3,3)], (t,-6,6), axes_labels=["x","y"], parametric=True)
verbose 0 (3839: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 130 points.
verbose 0 (3839: plot.py, generate_plot_points) Last error message: 'Unable to compute f(6.0)'
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
<p><img src="/upfiles/16386949594178996.png" alt="image description"></p>
<blockquote>
<p>has three branches. </p>
</blockquote>
<p>Nope.</p>
<p>What are the possible values of <code>x</code> and <code>y</code> for $t\in[-6~6]$ ? An analytical solution is possible by solving <code>x</code> and <code>y</code> for <code>t</code> then solving the inequations for $t\in|-5~6]$, but is somewhat painful... A graphical solution is easier to grasp :</p>
<pre><code>sage: plot([lambda t:ininterval(8*(6+t)/(t^2-16),-30,30),lambda t:ininterval(2*(8+ 3 *t)/(t^2-16),-30,30)], (t,-6,6), axes_labels=["t",""], legend_label=["x","y"])+plot([-3, 3],(-6,6),color=["green","red"])
verbose 0 (3839: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 14 points.
verbose 0 (3839: plot.py, generate_plot_points) Last error message: 'Unable to compute f(4.325615967306306)'
Launched png viewer for Graphics object consisting of 8 graphics primitives
</code></pre>
<p><img src="/upfiles/16386959221236952.png" alt="image description"></p>
<p>The most limiting variable is <code>x</code>, which is positive and <3 for <code>t</code>>-6, <code>t</code>> -9/2 approximately, and slightly superior to -3 for <code>t</code>>-3, <code>t</code><0 approximately.</p>
<blockquote>
<p>How to plot the branches in different colors ?</p>
</blockquote>
<p>The painful analytical solution I studiously avoided above is necessary to define the different regions; the <code>color=</code> argument can then be built around this definition (left tothe reader as an exercise;-)...</p>
<p><strong>EDIT :</strong> after addition of another question...</p>
<p>A static version of your chronogram could be :</p>
<pre><code>var("t")
fx(t)=8*(6+t)/(t^2-16)
fy(t)=2*(8+ 3 *t)/(t^2-16)
def ininterval(x,l,u):return float(x) if x>l and x<u else float("NaN")
P=plot([lambda t:ininterval(fx(t),-3,3), lambda t:ininterval(fy(t),-3,3)],
(t,-6,6), axes_labels=["x","y"], parametric=True)
Pts=[]
T=[]
xmin=P.xmin()
xmax=P.xmax()
ymin=P.ymin()
ymax=P.ymax()
for t in srange(-6,6.1,0.1):
px=fx(t)
py=fy(t)
if px>=xmin and px<=xmax and py>=xmin and py<=ymax:
Pts+=[(px,py)]
T+=[text("%4.1f"%t,(px,py),
vertical_alignment="bottom",
horizontal_alignment="left",
color="black")]
sage: show(P+points(Pts)+sum(T))
Launched png viewer for Graphics object consisting of 43 graphics primitives
</code></pre>
<p><img src="/upfiles/16389126893713255.png" alt="image description"></p>
<p>An animated/interactive version is left to the reader of the documentation as an exercise...</p>
<p>HTH,</p>
https://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/?comment=60158#post-id-60158Thanks for the fixup of parametricplot !
indeed the three branches appear only after enlarging the t range to say -8,8,
and indeed, before doing animations, I should start with static version :)
One more question : why doesn't just composing ``ininterval(fx(t),-3,3)" , without lambda t: , work? I know what lambda does, but is there also a way to use the syntax of the composition of functions?Wed, 08 Dec 2021 07:40:47 +0100https://ask.sagemath.org/question/60087/a-parametric-plot-with-branches-for-which-restricting-the-range-with-xmin-xmax-doesnt-work/?comment=60158#post-id-60158