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.Mon, 09 Sep 2019 08:44:56 +0200solving simultaneous equations with solve()https://ask.sagemath.org/question/47784/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[0][x],' y=',s[0][y]Sun, 08 Sep 2019 02:15:09 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/Comment by dazedANDconfused for <p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47785#post-id-47785I 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.Sun, 08 Sep 2019 04:34:01 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47785#post-id-47785Answer by rburing for <p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?answer=47788#post-id-47788Looks 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.Sun, 08 Sep 2019 11:49:04 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?answer=47788#post-id-47788Answer by Emmanuel Charpentier for <p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?answer=47801#post-id-47801A 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...Mon, 09 Sep 2019 03:22:29 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?answer=47801#post-id-47801Comment by rburing for <p>A bug in <code>sympy</code>, indeed. <strong>EDIT :</strong> No, it's a (non-) feature of Sage. From the docstring :</p>
<blockquote>
<p>There are a few optional keywords if you are trying to solve a single equation. They may only be used in that context.</p>
</blockquote>
<p>And below (i. e. in the "single equation" context :</p>
<blockquote>
<p>"algorithm" - string (default: 'maxima'); to use SymPy's solvers set this to 'sympy'. Note that SymPy is always
used for diophantine equations.</p>
</blockquote>
<p>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...</p>
<p>Another workaround, using Sage's default solver (i. e. <code>maxima</code>'s) and filtering afterwards :</p>
<pre><code>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}]
</code></pre>
<p>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...</p>
https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47802#post-id-47802The 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.Mon, 09 Sep 2019 08:34:48 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47802#post-id-47802Comment by rburing for <p>A bug in <code>sympy</code>, indeed. <strong>EDIT :</strong> No, it's a (non-) feature of Sage. From the docstring :</p>
<blockquote>
<p>There are a few optional keywords if you are trying to solve a single equation. They may only be used in that context.</p>
</blockquote>
<p>And below (i. e. in the "single equation" context :</p>
<blockquote>
<p>"algorithm" - string (default: 'maxima'); to use SymPy's solvers set this to 'sympy'. Note that SymPy is always
used for diophantine equations.</p>
</blockquote>
<p>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...</p>
<p>Another workaround, using Sage's default solver (i. e. <code>maxima</code>'s) and filtering afterwards :</p>
<pre><code>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}]
</code></pre>
<p>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...</p>
https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47803#post-id-47803Also `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 `.Mon, 09 Sep 2019 08:44:56 +0200https://ask.sagemath.org/question/47784/solving-simultaneous-equations-with-solve/?comment=47803#post-id-47803