First time here? Check out the FAQ!

Ask Your Question
0

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

asked 12 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 12 years ago

ndomes gravatar image

updated 12 years ago

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])
Preview: (hide)
link

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 ( 12 years ago )
2

answered 12 years ago

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])
Preview: (hide)
link

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 ( 12 years ago )

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: 12 years ago

Seen: 1,819 times

Last updated: Jun 01 '12