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.Tue, 27 Nov 2012 13:00:49 +0100How do I assign variables from the solution of an equation?https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/I was pleased to see how easily Sage solves systems of equations. For example, evaluating:
var('A,B')
f(x) = A*x + B
solve([f(3) == 5, f(7) == -3], A, B)
yields:
[[A == -2, B == 11]]
With a little text editing, I can use that solution to define f, which I can then plot or use to solve other equations:
A = -2; B = 11
f(x) = A*x + B
...
Is there a way to make the assignments of A and B (or more directly, f ) from the solution of the linear equations, without the copy+paste+edit step?Tue, 27 Nov 2012 09:15:24 +0100https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/Answer by William H. Hooper for <p>I was pleased to see how easily Sage solves systems of equations. For example, evaluating:</p>
<pre><code>var('A,B')
f(x) = A*x + B
solve([f(3) == 5, f(7) == -3], A, B)
</code></pre>
<p>yields:</p>
<pre><code>[[A == -2, B == 11]]
</code></pre>
<p>With a little text editing, I can use that solution to define f, which I can then plot or use to solve other equations:</p>
<pre><code>A = -2; B = 11
f(x) = A*x + B
...
</code></pre>
<p>Is there a way to make the assignments of A and B (or more directly, f ) from the solution of the linear equations, without the copy+paste+edit step?</p>
https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/?answer=14305#post-id-14305Thanks to kcrisman (http://ask.sagemath.org/question/347) I found a method using loops:
var('A,B')
f(x) = A*x + B
sol = solve([f(3) == 5, f(7) == -3], A, B)
for s in sol[0]:
f = f.subs(s)
...
Now I can plot f, or use it to solve other equations. That's still a little cumbersome, when I have multiple equations to solve and substitutions to apply. If you have a loopless method, please post.Tue, 27 Nov 2012 10:01:28 +0100https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/?answer=14305#post-id-14305Answer by ndomes for <p>I was pleased to see how easily Sage solves systems of equations. For example, evaluating:</p>
<pre><code>var('A,B')
f(x) = A*x + B
solve([f(3) == 5, f(7) == -3], A, B)
</code></pre>
<p>yields:</p>
<pre><code>[[A == -2, B == 11]]
</code></pre>
<p>With a little text editing, I can use that solution to define f, which I can then plot or use to solve other equations:</p>
<pre><code>A = -2; B = 11
f(x) = A*x + B
...
</code></pre>
<p>Is there a way to make the assignments of A and B (or more directly, f ) from the solution of the linear equations, without the copy+paste+edit step?</p>
https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/?answer=14307#post-id-14307I recommend the option **solution_dict=True**:
**solve** returns the solutions as list of python dictionaries and this makes substitution very easy.
var('A,B')
f(x) = A*x + B
sol = solve([f(3) == 5, f(7) == -3], A, B,solution_dict=True)
f.subs(sol[0])Tue, 27 Nov 2012 13:00:49 +0100https://ask.sagemath.org/question/9572/how-do-i-assign-variables-from-the-solution-of-an-equation/?answer=14307#post-id-14307