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.Tue, 13 Apr 2021 23:27:02 +0200Parametric plot doesn't work as expectedhttps://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/I'm working on some visualizations. In fact, I'd like to vizualize lines
and curves on the projective plane. I started with something very easy:
draw a line on sphere. I figured out the following.
Compute intersection of a plane with the unit sphere:
sage: x, y, z = var('x y z')
sage: sol = solve([x + y + z == 0.0, x^2 + y^2 + z^2 == 1.0], y, z)
sage: sol
[[y == -1/2*x - 1/2*sqrt(-3*x^2 + 2), z == -1/2*x + 1/2*sqrt(-3*x^2 + 2)],
[y == -1/2*x + 1/2*sqrt(-3*x^2 + 2), z == -1/2*x - 1/2*sqrt(-3*x^2 + 2)]]
Draw the plane, the sphere, and their intersection:
sage: p = implicit_plot3d(x + y + z, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="blue", opacity=0.2)
sage: p += implicit_plot3d(x^2 + y^2 + z^2 - 1, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="red", opacity=0.4)
sage: for s in sol:
....: p += parametric_plot3d([x, y.subs(s[0]), z.subs(s[1])], (x, -5.0, 5.0), color="green")
sage: show(p)
![Plane, sphere and their intersection](https://zielony-backlog.pl/wp-content/uploads/2021/04/screenshot1.png)
I believe the computation is correct.
However I'm not satisfied with the resulting plot.
Instead of a smooth line (like a tilted equator)
I have strange "triangles" at opposite points of
the sphere.
What can I do about it?Sat, 10 Apr 2021 16:59:16 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/Comment by pawel.bogdan for <p>I'm working on some visualizations. In fact, I'd like to vizualize lines
and curves on the projective plane. I started with something very easy:
draw a line on sphere. I figured out the following.</p>
<p>Compute intersection of a plane with the unit sphere:</p>
<pre><code>sage: x, y, z = var('x y z')
sage: sol = solve([x + y + z == 0.0, x^2 + y^2 + z^2 == 1.0], y, z)
sage: sol
[[y == -1/2*x - 1/2*sqrt(-3*x^2 + 2), z == -1/2*x + 1/2*sqrt(-3*x^2 + 2)],
[y == -1/2*x + 1/2*sqrt(-3*x^2 + 2), z == -1/2*x - 1/2*sqrt(-3*x^2 + 2)]]
</code></pre>
<p>Draw the plane, the sphere, and their intersection:</p>
<pre><code>sage: p = implicit_plot3d(x + y + z, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="blue", opacity=0.2)
sage: p += implicit_plot3d(x^2 + y^2 + z^2 - 1, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="red", opacity=0.4)
sage: for s in sol:
....: p += parametric_plot3d([x, y.subs(s[0]), z.subs(s[1])], (x, -5.0, 5.0), color="green")
sage: show(p)
</code></pre>
<p><img src="https://zielony-backlog.pl/wp-content/uploads/2021/04/screenshot1.png" alt="Plane, sphere and their intersection"></p>
<p>I believe the computation is correct.</p>
<p>However I'm not satisfied with the resulting plot.
Instead of a smooth line (like a tilted equator)
I have strange "triangles" at opposite points of
the sphere.</p>
<p>What can I do about it?</p>
https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56592#post-id-56592Here is the result I was writing about: [pict](https://zielony-backlog.pl/wp-content/uploads/2021/04/screenshot1.png)Sat, 10 Apr 2021 16:59:51 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56592#post-id-56592Answer by vdelecroix for <p>I'm working on some visualizations. In fact, I'd like to vizualize lines
and curves on the projective plane. I started with something very easy:
draw a line on sphere. I figured out the following.</p>
<p>Compute intersection of a plane with the unit sphere:</p>
<pre><code>sage: x, y, z = var('x y z')
sage: sol = solve([x + y + z == 0.0, x^2 + y^2 + z^2 == 1.0], y, z)
sage: sol
[[y == -1/2*x - 1/2*sqrt(-3*x^2 + 2), z == -1/2*x + 1/2*sqrt(-3*x^2 + 2)],
[y == -1/2*x + 1/2*sqrt(-3*x^2 + 2), z == -1/2*x - 1/2*sqrt(-3*x^2 + 2)]]
</code></pre>
<p>Draw the plane, the sphere, and their intersection:</p>
<pre><code>sage: p = implicit_plot3d(x + y + z, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="blue", opacity=0.2)
sage: p += implicit_plot3d(x^2 + y^2 + z^2 - 1, (x, -5, 5), (y, -5, 5), (z, -5, 5), color="red", opacity=0.4)
sage: for s in sol:
....: p += parametric_plot3d([x, y.subs(s[0]), z.subs(s[1])], (x, -5.0, 5.0), color="green")
sage: show(p)
</code></pre>
<p><img src="https://zielony-backlog.pl/wp-content/uploads/2021/04/screenshot1.png" alt="Plane, sphere and their intersection"></p>
<p>I believe the computation is correct.</p>
<p>However I'm not satisfied with the resulting plot.
Instead of a smooth line (like a tilted equator)
I have strange "triangles" at opposite points of
the sphere.</p>
<p>What can I do about it?</p>
https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?answer=56598#post-id-56598Your computation is somehow wrong. It comes from the formula `sqrt(-3*x^2 + 2)` that is valid only in a certain range of `x`. Here you let `x` run from `-5` to `5` and you will have negative numbers inside the `sqrt` such as
sage: sqrt(-3*5**2 + 2)
sqrt(-73)
I find it curious that you still get a picture.Sat, 10 Apr 2021 19:53:34 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?answer=56598#post-id-56598Comment by slelievre for <p>Your computation is somehow wrong. It comes from the formula <code>sqrt(-3*x^2 + 2)</code> that is valid only in a certain range of <code>x</code>. Here you let <code>x</code> run from <code>-5</code> to <code>5</code> and you will have negative numbers inside the <code>sqrt</code> such as</p>
<pre><code>sage: sqrt(-3*5**2 + 2)
sqrt(-73)
</code></pre>
<p>I find it curious that you still get a picture.</p>
https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56641#post-id-56641> I'm wondering if there is a way to automate this kind of calculations
One might walk the expression tree for each solved variable,
list things things that must be nonnegative or positive by detecting
arguments of square roots and logarithms, and find the correct
plotting intervals that way.Tue, 13 Apr 2021 23:27:02 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56641#post-id-56641Comment by slelievre for <p>Your computation is somehow wrong. It comes from the formula <code>sqrt(-3*x^2 + 2)</code> that is valid only in a certain range of <code>x</code>. Here you let <code>x</code> run from <code>-5</code> to <code>5</code> and you will have negative numbers inside the <code>sqrt</code> such as</p>
<pre><code>sage: sqrt(-3*5**2 + 2)
sqrt(-73)
</code></pre>
<p>I find it curious that you still get a picture.</p>
https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56637#post-id-56637> negative numbers inside the [square root] [...]
> I find it curious that you still get a picture
I wonder whether this might be related to:
- [Sage Trac ticket 30793: Sage may ignore the imaginary part of variables not explicitly declared complex](https://trac.sagemath.org/ticket/30793)Tue, 13 Apr 2021 21:15:53 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56637#post-id-56637Comment by pawel.bogdan for <p>Your computation is somehow wrong. It comes from the formula <code>sqrt(-3*x^2 + 2)</code> that is valid only in a certain range of <code>x</code>. Here you let <code>x</code> run from <code>-5</code> to <code>5</code> and you will have negative numbers inside the <code>sqrt</code> such as</p>
<pre><code>sage: sqrt(-3*5**2 + 2)
sqrt(-73)
</code></pre>
<p>I find it curious that you still get a picture.</p>
https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56604#post-id-56604Thank you very much, for your suggestion! I fixed my example code:
x,y,z=var('x y z')
sol = solve([x+y+z==0.0, x^2+y^2+z^2 == 1.0], y, z);
print(sol)
# p = implicit_plot3d(x+y+z, (x,-5,5), (y,-5,5), (z,-5,5), color="blue", opacity=0.2)
p = implicit_plot3d(x^2+y^2+z^2 - 1,(x,-5,5), (y,-5,5), (z,-5,5), color="red", opacity=0.4)
for s in sol:
p += parametric_plot3d([x,y.subs(s[0]), z.subs(s[1])], (x,-1/3*sqrt(3)*sqrt(2), 1/3*sqrt(3)*sqrt(2)), color="green")
show(p)
And now the result is satisfying. I'm wondering if there is a way to automatize this kind of calculationsSat, 10 Apr 2021 23:48:08 +0200https://ask.sagemath.org/question/56591/parametric-plot-doesnt-work-as-expected/?comment=56604#post-id-56604