Ask Your Question
1

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

asked 2019-12-12 02:38:30 -0600

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)
print(fexplicit)

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
2

answered 2019-12-12 03:41:02 -0600

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
0

answered 2019-12-13 05:16:17 -0600

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
sage:
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

Stats

Asked: 2019-12-12 02:38:30 -0600

Seen: 41 times

Last updated: Dec 13 '19