Ask Your Question
2

solving simultaneous equations with solve()

asked 2019-09-07 19:15:09 -0500

Wayne gravatar image

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[0][x],' y=',s[0][y]
edit retag flag offensive close merge delete

Comments

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.

dazedANDconfused gravatar imagedazedANDconfused ( 2019-09-07 21:34:01 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-09-08 20:22:29 -0500

Emmanuel Charpentier gravatar image

updated 2019-09-08 20:49:09 -0500

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

edit flag offensive delete link more

Comments

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.

rburing gravatar imagerburing ( 2019-09-09 01:34:48 -0500 )edit

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.

rburing gravatar imagerburing ( 2019-09-09 01:44:56 -0500 )edit
1

answered 2019-09-08 04:49:04 -0500

rburing gravatar image

updated 2019-09-08 05:12:00 -0500

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.

edit flag offensive delete link more

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: 2019-09-07 19:15:09 -0500

Seen: 52 times

Last updated: Sep 08