Ask Your Question
0

Having trouble solving simultaneous questions

asked 2015-11-07 14:19:22 +0100

ameetnsharma gravatar image

updated 2015-11-07 14:34:13 +0100

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-11-07 15:24:36 +0100

fidbc gravatar image

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])
edit flag offensive delete link more

Comments

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,

ameetnsharma gravatar imageameetnsharma ( 2015-11-07 17:06:11 +0100 )edit

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

ameetnsharma gravatar imageameetnsharma ( 2015-11-07 17:07:58 +0100 )edit

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.

fidbc gravatar imagefidbc ( 2015-11-07 18:06:54 +0100 )edit

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

ameetnsharma gravatar imageameetnsharma ( 2015-11-07 19:13:45 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-11-07 14:19:22 +0100

Seen: 642 times

Last updated: Nov 07 '15