ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Feb 2011 21:05:47 +0100Using numerical solution from system of equationshttps://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/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.
Wed, 16 Feb 2011 13:35:22 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/Answer by kcrisman for <p>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.</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>[
[x == 1, y == 2, z == 3]
]
x
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12110#post-id-12110I 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.Wed, 16 Feb 2011 14:15:33 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12110#post-id-12110Comment by niles for <p>I see what you are doing. Remember, Sage is Python too! So <code>x</code> is still <code>x</code>. Just because you showed a solution doesn't mean that <code>x</code> was actually assigned to <code>1</code>. If you want that, you'll have to explicitly say so. There are a few ways to do this, but </p>
<pre><code>sage: sols = solve([eq1, eq2, eq3], x, y, z,solution_dict=True)
sage: v = x.subs(sols[0])
sage: v
1
sage: x
x
</code></pre>
<p>is one way. Notice that here <code>x</code> is still <code>x</code> and <code>v</code> has your solution. </p>
<p>Let us know if this answers the question you have.</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22114#post-id-22114oh, you just beat me! ;)Wed, 16 Feb 2011 14:18:48 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22114#post-id-22114Comment by kcrisman for <p>I see what you are doing. Remember, Sage is Python too! So <code>x</code> is still <code>x</code>. Just because you showed a solution doesn't mean that <code>x</code> was actually assigned to <code>1</code>. If you want that, you'll have to explicitly say so. There are a few ways to do this, but </p>
<pre><code>sage: sols = solve([eq1, eq2, eq3], x, y, z,solution_dict=True)
sage: v = x.subs(sols[0])
sage: v
1
sage: x
x
</code></pre>
<p>is one way. Notice that here <code>x</code> is still <code>x</code> and <code>v</code> has your solution. </p>
<p>Let us know if this answers the question you have.</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22113#post-id-22113Yeah, but yours is more comprehensive, though you don't mention how to substitute. We need a union of the answers...Wed, 16 Feb 2011 15:11:21 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22113#post-id-22113Answer by RAC for <p>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.</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>[
[x == 1, y == 2, z == 3]
]
x
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12113#post-id-12113Great, 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 ;)
RACWed, 16 Feb 2011 15:57:56 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12113#post-id-12113Comment by niles for <p>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</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22110#post-id-22110you're welcome -- good luck :)Wed, 16 Feb 2011 16:23:18 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22110#post-id-22110Comment by kcrisman for <p>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</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22106#post-id-22106If you like the answers, or they answer your question, feel free to click them up/check them. Wed, 16 Feb 2011 21:05:47 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?comment=22106#post-id-22106Answer by niles for <p>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.</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>[
[x == 1, y == 2, z == 3]
]
x
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12111#post-id-12111Hi 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
Wed, 16 Feb 2011 14:17:41 +0100https://ask.sagemath.org/question/7948/using-numerical-solution-from-system-of-equations/?answer=12111#post-id-12111