# Using numerical solution from system of equations

 1 I want to take the numerical solution of a variable in a system of equations and use it later in the program. But all I can get is the symbolic definition. Here's a simplified example of the problem. sage: var('x y z') sage: eq1 = x + y + z == 6 sage: eq2 = 2*x - y + 2*z == 6 sage: eq3 = 3*x + 3*y - z == 6 sage: solve([eq1, eq2, eq3], x, y, z) sage: v = x sage: print v  Output: [ [x == 1, y == 2, z == 3] ] x  I assume the syntax for solving the system is correct because I get the right answers but I want v = 1, not v = x. Thanks. asked Feb 16 '11 RAC 11 ● 1 ● 3 DSM 4892 ● 12 ● 65 ● 105

 3 I see what you are doing. Remember, Sage is Python too! So x is still x. Just because you showed a solution doesn't mean that x was actually assigned to 1. If you want that, you'll have to explicitly say so. There are a few ways to do this, but sage: sols = solve([eq1, eq2, eq3], x, y, z,solution_dict=True) sage: v = x.subs(sols[0]) sage: v 1 sage: x x  is one way. Notice that here x is still x and v has your solution. Let us know if this answers the question you have. posted Feb 16 '11 kcrisman 7812 ● 20 ● 78 ● 170 oh, you just beat me! ;) niles (Feb 16 '11) Yeah, but yours is more comprehensive, though you don't mention how to substitute. We need a union of the answers... kcrisman (Feb 16 '11)
 3 Hi RAC, You're almost there -- you just need to save the output of the solve command: sage: solutions = solve([eq1, eq2, eq3], x, y, z) sage: print solutions [ [x == 1, y == 2, z == 3] ] sage: print solutions[0] [x == 1, y == 2, z == 3] sage: soln = solutions[0] sage: print soln[0] x == 1  The output is a list, whose contents are solutions and multiplicities. In this case the solution has multiplicity 1, so the list contains a single item, which is another list -- that list is the list of expressions which constitutes a solution. solutions[0] is the first item of solutions -- that is, the solution. soln[0] is the first item of that solution -- that is, the expression which gives the value of x: sage: type(soln[0])  Now to get the value itself, you need to just take the right hand side of that expression: sage: expr = soln[0] sage: expr.rhs() 1 sage: expr.lhs() x  Note that you don't have to define all these intermediate variables if you don't want to -- I just did it to make the explanation a little clearer: sage: solve([eq1, eq2, eq3], x, y, z)[0][0].rhs() 1  Using the other entries in the soln list will give the values of the other variables: sage: soln[1].rhs() 2 sage: soln[2].rhs() 3  posted Feb 16 '11 niles 3725 ● 7 ● 45 ● 104 http://nilesjohnson.net/
 0 Great, thanks for the help. I'm a social scientist who's trying to teach himself sage and python (and to use it for my work). The last time I had a computer science course Pascal was the in thing ;) RAC posted Feb 16 '11 RAC 11 ● 1 ● 3 you're welcome -- good luck :) niles (Feb 16 '11) If you like the answers, or they answer your question, feel free to click them up/check them. kcrisman (Feb 16 '11)

## Stats:

Seen: 489 times

Last updated: Feb 16 '11