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.Sun, 04 Jul 2021 03:13:36 +0200Implicit_plot with degenerate conicshttps://ask.sagemath.org/question/57860/implicit_plot-with-degenerate-conics/I'm plotting some (possibly degenerate) conics via implicit_plot.
y = var('y')
implicit_plot((y*sqrt(sqrt(2) + 2) - x*sqrt(-sqrt(2) + 2))^2 == 0, (x,-2,2),(y,-2,2))
yields a blank plot for me. Yet this should be equivalent to
implicit_plot((y*sqrt(sqrt(2) + 2) - x*sqrt(-sqrt(2) + 2)) == 0, (x,-2,2),(y,-2,2))
which yields a line, as it should. On the other hand,
implicit_plot(x^2 == 0, (x,-2,2),(y,-2,2))
does give a vertical line. Why is this different? Is this a bug?
I'm running this on the cloud, using a free account on cocalc.com. Perhaps this has something to do with the problem?yoleo531Sun, 04 Jul 2021 03:13:36 +0200https://ask.sagemath.org/question/57860/Implicit plot with complex functionhttps://ask.sagemath.org/question/54765/implicit-plot-with-complex-function/I have a complex function $f:\mathbb{C}\to\mathbb{C}$ and
want to draw the locus where $f$ is in the interval $[0,1]$.
I made the following (where $f$ is my complex function):
x = var('x')
assume(x, 'real')
y = var('y')
assume(y, 'real')
F = f.subs(z == x + I*y)
P = lambda x, y: F.subs(x=x, y=y).real()
Q = lambda x, y: F.subs(x=x, y=y).imag()
region_plot([P(x, y) <= 1, P(x, y) >= 0, Q(x, y) == 0], (x, 0, 5), (y, -1, 1))
That works well for small functions (low degree)
but for big functions it's too long.
For example with
$$ f(z)=-\frac{{\left(z^{4} - 6 z^{3} + 12 z^{2} - 8 \, z\right)} {\left(z - 1\right)}^{3} {\left(z - 3\right)}}{{\left(2z - 3\right)} {\left(z - 2\right)}^{3} z} $$
there is no problem (a few second), but with
$$ f(z)=-\frac{{\left(z^{8} - 16 z^{7} + 108 z^{6} - 400 z^{5} + 886 z^{4} - 1200 z^{3} + 972 z^{2} - 432 z + 81\right)} {\left(z - 2\right)}^{6} {\left(z - 4\right)} z}{{\left(6 z^{4} - 48 z^{3} + 140 z^{2} - 176 z + 81\right)} {\left(z - 1\right)}^{4} {\left(z - 3\right)}^{4}} $$
that's too long.
I think there should be something better. I've seen for example
the function `complex_plot` works well even for big functions
and seems to do something more complex.
Any idea?Gabriel SoranzoSat, 19 Dec 2020 23:44:26 +0100https://ask.sagemath.org/question/54765/Problem with implicit_plothttps://ask.sagemath.org/question/51443/problem-with-implicit_plot/I have three huge degree 31 bivariate polynomials (20,000 characters long each) I want to plot, but I keep getting a lot of noise in my plot. I can't upload it, but the point is that in some regions I just get colorful noise. I've tried defining the polynomials over RealField(n) and increasing the number of plot_points, but neither of these approaches work. Any ideas on how to work around this? Thanks.
**Edit:** Tried using sympy's plot_implicit and it's so (SO!) slow. Then used numpy's contour_plot and it's fast, but has the same problem as sage.
Here's the code that produces the polynomials and plot. Be patient as it could be a bit slow (depending on your machine).
plot = Graphics()
m = 32-1
D = [(i,j) for i in range(0,m+1) for j in range(0,m+1) if i+j<m+1]
#Polygon
P = Polyhedron( vertices= [(0, 0), (32, 44), (23, 0), (10, 14), (2, 3)] )
points = P.integral_points()
plot_pts = point(points, rgbcolor=(0, 0, 0), size = 20).plot()
plot_np = P.plot(fill = False, point=False, line='black')
M = matrix(ZZ, len(points), len(D), 0)
for row_num, row in enumerate(points):
for col_num, column in enumerate(D):
i, j = row
a, b= column
#Matrix for interpolation:
M[row_num, col_num] = (i^a)*(j^b)
R = PolynomialRing(QQ, 2, 'xy')
S = PolynomialRing(RealField(500), 2, 'uv')
x, y = R.gens()
u, v = S.gens()
K = M.right_kernel()
Kdim = K.dimension()
print(Kdim)
if Kdim > 0:
for l in range(Kdim):
K_basis = K.basis()[l]
#Writing the interpolating polynomial
f=0
for order, bidegree in enumerate(D):
a, b = bidegree
f += list(K_basis)[order] * u^a * v^b
F = f.factor()
f = F[6][0]
cols = ['red', 'blue', 'green']
interpolation = implicit_plot(f, (u,-1,34), (v,-1,45), plot_points=100, color=cols[l])
plot += interpolation
plot += plot_pts + plot_np
plot.show(figsize=10)
**Edit 2:** Using the mpmath library in Python and with the aid of Sébastien's code below I wrote a routine that allows us to control the root finding method and precision of our computations. I tried several methods, secant (default), newton, hailley, mnewton, etc. without success. Changing the precision and tolerance of the root finding function from mpmath didn't help either. I think this polynomial just behaves too wildly in the region of the plot.
Here's the code and relevant documentation for the "mpmath.findroot" function:
http://mpmath.org/doc/current/calculus/optimization.html :
from sympy import *
import matplotlib.pyplot as plt
import mpmath as mp
mp.dps = 100
stepx = 0.1
stepy = 0.5
xrange = np.arange(7.5,12.5, stepx)
yrange = np.arange(0, 5, stepy)
def plot_roots_of_f(f):
L = []
for u in xrange:
for v in yrange:
Root = mp.findroot(f.eval({x:u}),v, method = 'mnewton', tol = E-60, verbose=False,verify=False)
L.append([u,Root])
return plt.plot(*zip(*L),linestyle='None', marker='.')
plot_roots_of_f(f)
plt.show()jgaMon, 18 May 2020 09:30:23 +0200https://ask.sagemath.org/question/51443/Using implicit_plot with non-rectangular regionshttps://ask.sagemath.org/question/50148/using-implicit_plot-with-non-rectangular-regions/I'd like to use implicit_plot on a function that involves a square root. When I do, I get an error message saying that for some values of x and y the number inside the square root is negative. Well, sure. Is there a way to restrict implicit_plot to the region where the function has real values? In this the thing in the square root is 1-u^2-v^2, so I'd like to restrict u and v to the unit circle.Fernando256Tue, 03 Mar 2020 20:19:51 +0100https://ask.sagemath.org/question/50148/implicit_plot leaves out pointshttps://ask.sagemath.org/question/50130/implicit_plot-leaves-out-points/I'm trying to use `implicit_plot` to graph nullclines for a competition model, but only some parts of the nullclines are plotted. I have:
var("N,P")
Nprime(N,P) = N*(3-N-2*P)
implicit_plot(Nprime, (N,0,3), (P,0,3))
Clearly, this function is 0 at all points where N=0, but the graph only shows them up to P=3/2. Is there anything I can do to avoid this or is it a bug in `implicit_plot`?
jaiaSun, 01 Mar 2020 08:36:16 +0100https://ask.sagemath.org/question/50130/Restriction of domain of vector fieldhttps://ask.sagemath.org/question/44207/restriction-of-domain-of-vector-field/I was trying to plot a field of normal vectors to a given implicit graph. What I got so far is:
x, y = var('x y')
f(x,y) = 2*x*y^3
g(x,y) = x^2*3*y^2
d = plot_vector_field((f(x,y)/sqrt(f(x,y)^2+g(x,y)^2),g(x,y)/sqrt(f(x,y)^2+g(x,y)^2)), (x,-5,5), (y,-5,5))
s = implicit_plot(x^2*y^3-1 , (x,-5,5), (y,-5,5))
show(s+d)
And it works like a charm, however, is there a way to "restrict" a domain of plot_vector_field so it doesn't plot all the vectors in a given range, but only for point (x,y) lying on my graph? That is such points that x^2*y^3=1. I tried to just put it instead (y,-5,5) (in a y=(1/x^2)^(1/3) form), but it obviously doesn't work.
Thx for any help as I'm new to Sage.Michał_FabisiakTue, 06 Nov 2018 21:04:40 +0100https://ask.sagemath.org/question/44207/implicit plot with different scales on x and y axeshttps://ask.sagemath.org/question/41110/implicit-plot-with-different-scales-on-x-and-y-axes/ Say I want to plot `implicit_plot(x^2 + 10000*y^2 - 2, (x,-3,3), (y,-0.03,0.03))`, or anything else where the interesting values of x and y varies over different orders of magnitude. At least on my machine, the result is a completely illegible png, squished into nothingness.
http: // i187.photobucket.com/albums/x207/pratarpersilja/scaling_unusable_zpsspbvecof.png
(I wasn't allowed to add clickable links).
Of course, in this particular case I could rewrite the expression manually until the x and y ranges are the same. In the case that inspired these experiments in the first place, that rewrite is not realistic to do by hand.
How do I force an implicit plot to not have the same number of pixels per numerical unit in x and y directions?pratarpersiljaWed, 14 Feb 2018 05:03:13 +0100https://ask.sagemath.org/question/41110/Using Sage plotting capability on data from PARI/GP (1)https://ask.sagemath.org/question/41048/using-sage-plotting-capability-on-data-from-parigp-1/ This is a short follow-up question from [this one](https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/).
I would like to produce an implicit plot (a contour plot where the real or imaginary values of a function are zero):
var('x,y,s')
g=real(zeta(s))
implicit_plot(lambda x,y:g(x+y*I),(x,-3,3),(y,-3,3))
This works fine, however I want to use GP/Pari to evaluate the zeta function and therefore wrote:
var('x,y')
g=gp("H(s)=zeta(s)")
implicit_plot(lambda x,y:real_part(g(x+y*I)),(x,-3,3),(y,-3,3))
but then keep getting an error message:
**PARI/GP ERROR:
*** at top-level: sage[45020]=sage[16][1]
*** ^---
*** incorrect type in _[_] OCcompo1 [not a vector] (t_REAL).**
For 'normal' plots like this one:
var('x')
g=gp("H(s)=real(zeta(s))")
plot(lambda x:(g(x)),(x,3,6))
the interface with GP works fine, so I probably do something wrong using multiple variables or complex numbers? It doesn't seem to be zeta-function specific (like the pole at s=1), since it also fails for e.g. the cos-function.
Grateful for any advice on how to make this work.
Thanks!RuudHFri, 09 Feb 2018 16:20:43 +0100https://ask.sagemath.org/question/41048/Add legend to implicit_plot for different parameter valueshttps://ask.sagemath.org/question/40277/add-legend-to-implicit_plot-for-different-parameter-values/Hi,
I want to add a legend to an implicit plot, to plot the same function for different parameter values. I can plot the functions but the legend is not displayed. Is it possible to do it?
var('x y z')
xrange = (x,0,2)
yrange = (y,0,2)
z=1
g1 = implicit_plot(x**3-2*x+y**2==z,xrange,yrange,color='red',legend_label='g1')
z=2
g2 = implicit_plot(x**3-2*x+y**2==z,xrange,yrange,color='blue',legend_label='g2')
g = g1+g2
g.legend(True)
g.show(legend_loc='upper right',frame=True)flucoeFri, 22 Dec 2017 00:54:19 +0100https://ask.sagemath.org/question/40277/Implicit plot with squaring and absolute value produces empty plothttps://ask.sagemath.org/question/38563/implicit-plot-with-squaring-and-absolute-value-produces-empty-plot/ The following code:
sage: x,y = var('x y')
sage: implicit_plot(x, (x,-2,2), (y,-2,2))
produces a plot of a straight line along the coordinate y-axis as expected (it should plot the curve $x = 0$).
However, this very similar code:
sage: implicit_plot(x^2, (x,-2,2), (y,-2,2))
produces a plot with nothing on it, even though it *should* plot the curve $x^2 = 0$ which is clearly the same as the above curve.
This problem seems to persist with any function $f(x, y)$, i.e., in the following code,
f(x, y) = [some expression involving x and y]
sage: implicit_plot(f(x, y), [x scale], [y scale])
sage: implicit_plot(f(x, y)^2, [x scale], [y scale])
the second line produces a nonempty plot while the third line produces an empty plot. Replacing `f(x,y)^2` with `abs(f(x,y))` or even `abs_symbolic(f(x,y))` has the same effect: they all produce empty plots.
The problem persists when one replaces `implicit_plot` with `implicit_plot3d`.
What's going on? Is this a bug?DoekeFri, 18 Aug 2017 00:36:31 +0200https://ask.sagemath.org/question/38563/X and Y values of plot points using implicit_plothttps://ask.sagemath.org/question/27249/x-and-y-values-of-plot-points-using-implicit_plot/ Is there a way to get Sage to generate a list of the x and y values computed in generating a 2D implicit plot using the implicit_plot command? Thanks.cltho2Fri, 03 Jul 2015 00:32:33 +0200https://ask.sagemath.org/question/27249/Plotting an element in a polynomial idealhttps://ask.sagemath.org/question/26931/plotting-an-element-in-a-polynomial-ideal/I have a polynomial ring in three variables:
P.<x,y,z> = PolynomialRing(QQ)
and I've eliminated the variable `t` in an ideal generated by several polynomials to obtain an implicit function in just `x` and `y`. I'd like to plot this function. But the `plot` method, applied to an ideal in just two variables, produces the error:
ValueError: Variable 't' not found
So either I need somehow to introduce this variable into the plot, or pull the function out of the polynomial ring so that it can be plotted as a function of real variables with `implicit_plot`. But I'm not sure how to do either...any advice would be most helpful!AlasdairMon, 25 May 2015 03:57:20 +0200https://ask.sagemath.org/question/26931/Implicit plot in tachyon scenehttps://ask.sagemath.org/question/26639/implicit-plot-in-tachyon-scene/Is there a way to get some implicit plot, or some other graphic object in general into a tachyon scene?
Something like:
t = Tachyon(xres=400,yres=400, camera_center=(4,30,30), look_at=(0,0,0))
t.ImplicitPlot3d( ... )
Or can I modify the camera_center parameter when using the tachyon viewer in the show() method?
ThorstenFri, 24 Apr 2015 19:37:52 +0200https://ask.sagemath.org/question/26639/convert function to variablehttps://ask.sagemath.org/question/23984/convert-function-to-variable/ Aloha,
I am solving differential equations using desolve, which requires that I declare my dependent variable, say y, as a function, but if I want to plot the result using implicit_plot or plot the slope field using plot_slope_field, then I need y to be a variable.
I have been able to do this by converting my function to a string, replacing "y(x)" by "y" and then converting the result back to a sage symbolic expression.
For example,
x = var('x')
y = function('y', x)
f=(4-2*x)/(3*y^2-5)
print(f)
y = var('y')
f=sage_eval(str(f).replace('y(x)','y'), locals=vars())
print(f)
prints
-2*(x - 2)/(3*y(x)^2 - 5)
-2*(x - 2)/(3*y^2 - 5)
An example with dsolve and implicit_plot:
# Define f(x,y)
x = var('x')
y = function('y', x)
f=(4-2*x)/(3*y^2-5)
# Define dy/dx=f(x,y)
de=diff(y,x)==f
# Solve dy/dx=f(x,y)
sol=desolve(de,y,ics=(1,3))
show(sol)
# Convert our solution to something that implicit_plot can handle,
# i.e., change y fom a function to a variable
y = var('y')
sol=sage_eval(str(sol).replace('y(x)','y'), locals=vars())
implicit_plot(sol, (x,-6,8), (y,-6,6))
I then wrote a function that does this:
def convert_function_to_var(f) :
x,y = var('x,y')
f=sage_eval(str(f).replace('y(x)','y'), locals=vars())
return f
which I test this way
x = var('x')
y = function('y', x)
f=(4-2*x)/(3*y^2-5)
print(f)
f=convert_function_to_var(f)
print(f)
Now, my questions are
1. is there a better way to do
this?
2. when I define
`convert_function_to_var` why do I
need to declare `x,y = var('x,y')`
in the procedure?
3. `convert_function_to_var` requires
that I have two, and only two,
variables, named x and y, is there
some more generic and robust way of
doing this?
Mahalo,
RamónRamón Figueroa-CentenoFri, 29 Aug 2014 17:39:49 +0200https://ask.sagemath.org/question/23984/Sage plot inner structurehttps://ask.sagemath.org/question/8168/sage-plot-inner-structure/Hi, I would like to manipulate the raw data of a plot. More specifically, I'd like to get the set of points produced by implicit_plot in order to use it for another task.
Is that possible?jllbThu, 12 Dec 2013 03:45:49 +0100https://ask.sagemath.org/question/8168/Implicit plotting accuracyhttps://ask.sagemath.org/question/10770/implicit-plotting-accuracy/I am trying to plot this implicit function
![image description](http://www.xamuel.com/blog/wp-content/uploads/5.jpg)
What I get is
![image description](http://www.sagenb.org/home/aspras/1/cells/20/sage0.png)
Is there a way to make the curves thinner ?asprasSat, 23 Nov 2013 12:01:36 +0100https://ask.sagemath.org/question/10770/get_minmax_data on implicit_plothttps://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/This is a sequel of [my question about plotting level set](http://ask.sagemath.org/question/358/plot-the-level-sets-of-a-function).
In the following, G is a circle :
sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}
The "correct" get_minmax_data sould be
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :
xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in g],dtype=float)
in ../plot/contour_plot.py
My questions are :
1. can I retrieve that xy_data_array ?
2. If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?
Laurent ClaessensFri, 04 Feb 2011 13:21:09 +0100https://ask.sagemath.org/question/7920/Plot the level sets of a functionhttps://ask.sagemath.org/question/7919/plot-the-level-sets-of-a-function/I'm trying to draw level set of a function f:R^2->R, that is the set of solutions of
f(x,y)=h for a given h.
For that purpose I wrote the following
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def level_curve(f,h):
solutions_list = solve(f==h,y)
return [sol.rhs() for sol in solutions_list]
var('x,y')
f=x+y+2
for g in level_curve(f,3):
print g
print "-----"
f=x**2+y**2
for g in level_curve(f,3):
print g
This works, but I'm not satisfied principally because I got the level sets under the form of a list of functions. Moreover it will not work if the level set is vertical.
Thus I would prefer to get the solution under the form of a parametric curve.
Does Sage provides something for that ?
Laurent ClaessensFri, 04 Feb 2011 06:20:26 +0100https://ask.sagemath.org/question/7919/