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.Sat, 07 Nov 2015 19:13:45 +0100Having trouble solving simultaneous questionshttps://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/I'm trying to use sagemath cloud to solve simultaneous equations, but I'm having trouble. Can you please look at the code below and see what I'm doing wrong. sagemath cloud gives the following error:
Error in lines 7-7
Traceback (most recent call last):
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
KeyError: x
My code:
<code>
x, y, x2, y2 = var('x y x2 y2')
eq1 = (x-1)*(x-2) + (y-3)*(y-4) == 0
eq2 = (x2-1)*(x2-2) + (y2-3)*(y2-4) == 0
eq3 = x+x2 == 3
eq4 = y+y2 == 7
sols = solve([eq1,eq2,eq3,eq4],x,y,x2,y2,solution_dict=True)
for soln in sols: print "x: %s, y: %s x2: %s y2: %s"%(soln[x], soln[y],soln[x2],soln[y2])
</code>Sat, 07 Nov 2015 14:19:22 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/Answer by fidbc for <p>I'm trying to use sagemath cloud to solve simultaneous equations, but I'm having trouble. Can you please look at the code below and see what I'm doing wrong. sagemath cloud gives the following error:</p>
<p>Error in lines 7-7
Traceback (most recent call last):
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
KeyError: x</p>
<p>My code:</p>
<p><code>
x, y, x2, y2 = var('x y x2 y2')</code></p><code>
<p>eq1 = (x-1)<em>(x-2) + (y-3)</em>(y-4) == 0</p>
<p>eq2 = (x2-1)<em>(x2-2) + (y2-3)</em>(y2-4) == 0</p>
<p>eq3 = x+x2 == 3</p>
<p>eq4 = y+y2 == 7</p>
<p>sols = solve([eq1,eq2,eq3,eq4],x,y,x2,y2,solution_dict=True)</p>
</code><p><code>for soln in sols: print "x: %s, y: %s x2: %s y2: %s"%(soln[x], soln[y],soln[x2],soln[y2])
</code></p>
https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?answer=30529#post-id-30529It seems like the solution dictionary does not contain an entry for `x`. If you inspect the solution dictionary before printing, you can see that the keys are `x + x2`, `(x - 1)*(x - 2) + (y - 3)*(y - 4)`, `(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4)` and `y + y2`. To print the solutions (independently of what is returned in the dictionaries) you can try
for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])Sat, 07 Nov 2015 15:24:36 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?answer=30529#post-id-30529Comment by ameetnsharma for <p>It seems like the solution dictionary does not contain an entry for <code>x</code>. If you inspect the solution dictionary before printing, you can see that the keys are <code>x + x2</code>, <code>(x - 1)*(x - 2) + (y - 3)*(y - 4)</code>, <code>(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4)</code> and <code>y + y2</code>. To print the solutions (independently of what is returned in the dictionaries) you can try</p>
<pre><code>for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])
</code></pre>
https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30531#post-id-30531Thanks. I tried that and here's what I get:
<code>
x1, y1, x2, y2 = var('x1 y1 x2 y2')
eq1 = (x1-1)*(x1-2) + (y1-3)*(y1-4) == 0
eq2 = (x2-1)*(x2-2) + (y2-3)*(y2-4) == 0
eq3 = x1+x2 == 3
eq4 = y1+y2 == 7
sols = solve([eq1,eq2,eq3,eq4],x1,y1,x2,y2,solution_dict=True)
for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])
</code>
HEre's the output:
x1 + x2 : 3,
(x1 - 1)*(x1 - 2) + (y1 - 3)*(y1 - 4) : 0,
(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4) : 0,
y1 + y2 : 7,Sat, 07 Nov 2015 17:06:11 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30531#post-id-30531Comment by ameetnsharma for <p>It seems like the solution dictionary does not contain an entry for <code>x</code>. If you inspect the solution dictionary before printing, you can see that the keys are <code>x + x2</code>, <code>(x - 1)*(x - 2) + (y - 3)*(y - 4)</code>, <code>(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4)</code> and <code>y + y2</code>. To print the solutions (independently of what is returned in the dictionaries) you can try</p>
<pre><code>for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])
</code></pre>
https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30532#post-id-30532Seems like replacing x with x1 helped a little, but it's not solving the system of equations.Sat, 07 Nov 2015 17:07:58 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30532#post-id-30532Comment by fidbc for <p>It seems like the solution dictionary does not contain an entry for <code>x</code>. If you inspect the solution dictionary before printing, you can see that the keys are <code>x + x2</code>, <code>(x - 1)*(x - 2) + (y - 3)*(y - 4)</code>, <code>(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4)</code> and <code>y + y2</code>. To print the solutions (independently of what is returned in the dictionaries) you can try</p>
<pre><code>for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])
</code></pre>
https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30534#post-id-30534Right. However that seems like another question. Please feel free to create a new question for that. You may want to include details on whether you are looking for an exact solution or a numerical approximation. Also, if you found this answer helpful you can up/check it.Sat, 07 Nov 2015 18:06:54 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30534#post-id-30534Comment by ameetnsharma for <p>It seems like the solution dictionary does not contain an entry for <code>x</code>. If you inspect the solution dictionary before printing, you can see that the keys are <code>x + x2</code>, <code>(x - 1)*(x - 2) + (y - 3)*(y - 4)</code>, <code>(x2 - 1)*(x2 - 2) + (y2 - 3)*(y2 - 4)</code> and <code>y + y2</code>. To print the solutions (independently of what is returned in the dictionaries) you can try</p>
<pre><code>for soln in sols:
for varbl in soln:
print "{0} : {1},".format(varbl, soln[varbl])
</code></pre>
https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30535#post-id-30535It is not letting me upvote. I will accept the answer though.Sat, 07 Nov 2015 19:13:45 +0100https://ask.sagemath.org/question/30528/having-trouble-solving-simultaneous-questions/?comment=30535#post-id-30535