# solving simultaneous equations with solve()

I am trying to solve a set of (fairly simple) nonlinear simultaneous algebraic equations with real solutions as shown below. I know the equations have real solutions (they are in the comment line), but I cannot get sage to produce them. If I remove the 'sympy' and 'real' it produces complex solutions. Is there a better method for solving equations like these?

x,y,z=var('x y z')
s=solve([x*x*x-y*y==10.5,3.0*x*y+y==4.6],x,y,algorithm='sympy',domain='real',solution_dict=True)
#solutions are x=2.215, y=0.6018
print 'second problem:',' x=',s[x],' y=',s[y]

edit retag close merge delete

1

I don't know what's going wrong here. I'll leave that to the experts. I did find that not specifying the algorithm gives many solutions, including the solution you list.

x,y=var('x,y')
s=solve([x*x*x-y*y==10.5,3.0*x*y+y==4.6],x,y,domain='real',solution_dict=True)
print s


I tried assume(x>0,y>0) but that didn't work.

Sort by » oldest newest most voted

A bug in sympy, indeed. EDIT : No, it's a (non-) feature of Sage. From the docstring :

There are a few optional keywords if you are trying to solve a single equation. They may only be used in that context.

And below (i. e. in the "single equation" context :

"algorithm" - string (default: 'maxima'); to use SymPy's solvers set this to 'sympy'. Note that SymPy is always used for diophantine equations.

One may try to use directly the sympy solvers ; the point is to sympify the arguments (correctly...). And to convert the result back to Sage...

Another workaround, using Sage's default solver (i. e. maxima's) and filtering afterwards :

sage: [s for s in solve([x^3-y^2==21/2, 3*x*y+y==23/5],[x,y], solution_dict=True) if s[x].is_real() and s[y].is_real()]
[{x: 2.21465033180194, y: 0.6017830609212481}]


Note: unless you have compelling reasons (e. g. speed) to use numerical approximations, taking advantage of Sage's ability to use exact representations of quantities defined in exact terms is usually a good idea...

more

The documentation is incorrect, the source shows that algorithm is used in case of a list (and only has a special case for sympy). Also, it seems like a bug that no error is raised about the unused extra arguments.

Also solution_dict is used, as you show yourself. The strategy of trying maxima is already included in solve, so it could be improved to work with domain. Looks like a bug in sympy. Here is an alternative method, for polynomial equations with a finite number of solutions:

sage: R.<x,y> = PolynomialRing(QQ)
sage: I = R.ideal([x*x*x-y*y-10.5, 3.0*x*y+y-4.6])
sage: I.variety(RR)
[{y: 0.601783026716651, x: 2.21465035058553}]


You might also like AA (the field of real algebraic numbers) instead of RR, for exact computations.

more