Ask Your Question

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

asked 2019-12-12 09:38:30 +0200

dan101 gravatar image

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)

I ran it and saw this:

y == -(x - 1)/x

Next, I tried to plot the solution:

plotme = fexplicit[0]
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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-12-12 10:41:02 +0200

eric_g gravatar image

Replace your definition of plotme by

plotme = fexplicit[0].rhs()

Indeed, we have

sage: fexplicit[0]
y == -(x - 1)/x
sage: fexplicit[0].rhs()
-(x - 1)/x
edit flag offensive delete link more

answered 2019-12-13 12:16:17 +0200

dan_fulea gravatar image

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-12-12 09:38:30 +0200

Seen: 207 times

Last updated: Dec 13 '19