# 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:

 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]) 

edit retag close merge delete

Sort by » oldest newest most voted It 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])

more

Thanks. I tried that and here's what I get:  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]) 

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,

Seems like replacing x with x1 helped a little, but it's not solving the system of equations.

Right. 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.

It is not letting me upvote. I will accept the answer though.