Ask Your Question
0

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

asked 2012-05-31 17:37:29 -0500

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
3

answered 2012-05-31 19:03:55 -0500

ndomes gravatar image

updated 2012-05-31 19:18:18 -0500

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()
h(x0)

Further you can use python dictionaries:

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

Comments

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-05-31 20:56:58 -0500 )edit
2

answered 2012-05-31 18:18:17 -0500

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
h.subs(sols[0])
edit flag offensive delete link more

Comments

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-05-31 21:01:31 -0500 )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

Stats

Asked: 2012-05-31 17:37:29 -0500

Seen: 575 times

Last updated: May 31 '12