Ask Your Question

use output of solve() without 'var == '

asked 2012-06-01 00:37:29 +0200

daniel.e2718 gravatar image

Is there a way to use the values of solve() without having that pesky 'var == ' in front of it?

in   h = x^2-9; h
out     x |--> x^2 - 9
in   sols = solve(h == 0, x); sols
out     [x == -3, x == 3]
in   sols[0]
out     x == -3
in   h(sols[0])
out     (x == -3)^2 - 9

What the heck is that last line?!

Is there a way to extract the value of sols[0] for use without needing to copy/paste the value?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-06-01 02:03:55 +0200

ndomes gravatar image

updated 2012-06-01 02:18:18 +0200

An other way:

solve returns a list of equations.

You can get the value with .rhs() ( right hand side )

h(x) = x^2-9
sols = solve(h == 0, x); print sols
x0 = sols[0].rhs()

Further you can use python dictionaries:

h(x) = x^2-9
sols = solve(h(x), x,solution_dict=True); print sols
edit flag offensive delete link more


rhs() is exactly what I was lookin for - extracting the value of the solution out of the equation. Now I don't have to hardcode numbers :D

daniel.e2718 gravatar imagedaniel.e2718 ( 2012-06-01 03:56:58 +0200 )edit

answered 2012-06-01 01:18:17 +0200

Shashank gravatar image

sols[0] is x==3 not a substitution command.

Use this instead.

h = x^2-9; h
sols = solve(h == 0, x); sols
edit flag offensive delete link more


Conceptually, not quite what I had in mind. But a useful command to know, regardless. EDIT: Actually, I came across a problem at GeoGebra forums where someone wanted to make a list of values of functions evaluated at a number. For 'for i in functionlist', where i became the function, i(x) gave a deprecation warning, whereas i.subs(x == evalnum) goes without warning.

daniel.e2718 gravatar imagedaniel.e2718 ( 2012-06-01 04:01:31 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-06-01 00:37:29 +0200

Seen: 1,648 times

Last updated: Jun 01 '12