Using numerical solution from system of equations

i like this post (click again to cancel)
1
i dont like this post (click again to cancel)

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 gravatar image RAC
11 1 3

updated Feb 17 '11

DSM gravatar image DSM flag of Canada
4892 12 65 105

3 Answers:

i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel)

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.

link

posted Feb 16 '11

kcrisman gravatar image 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)
i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel)

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])
<type 'sage.symbolic.expression.Expression'>

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
link

posted Feb 16 '11

niles gravatar image niles
3725 7 45 104
http://nilesjohnson.net/
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

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

link

posted Feb 16 '11

RAC gravatar image 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)

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
Login/Signup to Post

Question tools

Tags:

Stats:

Asked: Feb 16 '11

Seen: 489 times

Last updated: Feb 16 '11

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.