# How to transform fxy.solve(y) into function I can plot?

hello world,

I am running SageMath 8.9 on Ubuntu.

I wrote a simple script:

var('x y')
fxy = y -1/x + 1

fexplicit = fxy.solve(y)
print(fexplicit)


I ran it and saw this:

[
y == -(x - 1)/x
]


Next, I tried to plot the solution:

plotme = fexplicit
plot(plotme, (x, -0.5, 2))


SageMath gave me an error:

ValueError: Variable 'y' not found


Question: How to transform fxy.solve(y) into function I can plot?

edit retag close merge delete

Sort by » oldest newest most voted

Replace your definition of plotme by

plotme = fexplicit.rhs()


Indeed, we have

sage: fexplicit
y == -(x - 1)/x
sage: fexplicit.rhs()
-(x - 1)/x

more

Alternatively one can use the optional parameter solution_dict=True as below, so the result of the solve function is rather a list of "solutions", where each solution is a dictionary. The keys of the dictionary being the unknowns. Here we have only one unknown variable, y. (It would be better to use the right name for the right object, so fexplicit is misleading, rather solutions or solutions_list should be used.)

sage: var('x y');
....: fxy = y - 1/x + 1
....:
....: fexplicit = fxy.solve(y, solution_dict=True)
....: # we have only one solution, but in general...
....: for sol in fexplicit:
....:     print "Solution:", sol[y]
....:     sol[y].plot(ymin=-10, ymax=10)
....:
(x, y)
Solution: -(x - 1)/x
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage:

more