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.Sun, 18 Nov 2018 20:15:54 +0100solve() is not giving the right solutionshttps://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/Hey there,
i need your help on this one. The following sage code is giving me a headache:
- f(x) = x^3 + x^2 - 0.1
- solve(f==0, x)
- -->
- [x == -1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(I*sqrt(3) + 1) - 1/18*(-I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(-I*sqrt(3) + 1) - 1/18*(I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) + 1/9/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3]
So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be.
Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?
Regards,
BenThu, 15 Nov 2018 15:17:54 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/Comment by Emmanuel Charpentier for <p>Hey there,</p>
<p>i need your help on this one. The following sage code is giving me a headache:</p>
<ul>
<li>f(x) = x^3 + x^2 - 0.1</li>
<li>solve(f==0, x)</li>
<li>--></li>
<li>[x == -1/2<em>(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3)<em>(I</em>sqrt(3) + 1) - 1/18<em>(-I</em>sqrt(3) + 1)/(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2</em>(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3)</em>(-I<em>sqrt(3) + 1) - 1/18</em>(I<em>sqrt(3) + 1)/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) + 1/9/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3]</li>
</ul>
<p>So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be.
Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?</p>
<p>Regards,
Ben</p>
https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44295#post-id-44295See [casus irreducibilis](https://en.wikipedia.org/wiki/Casus_irreducibilis)...
You *can* get "ostensibly real" solutions by expressing the quantities in trigonometric form. For example :
sage: map(lambda s:s.rhs().imag_part().trig_expand(), (x^3+x^2-1/10).solve(x))
[0, 0, 0]Thu, 15 Nov 2018 18:39:11 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44295#post-id-44295Answer by tmonteil for <p>Hey there,</p>
<p>i need your help on this one. The following sage code is giving me a headache:</p>
<ul>
<li>f(x) = x^3 + x^2 - 0.1</li>
<li>solve(f==0, x)</li>
<li>--></li>
<li>[x == -1/2<em>(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3)<em>(I</em>sqrt(3) + 1) - 1/18<em>(-I</em>sqrt(3) + 1)/(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2</em>(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3)</em>(-I<em>sqrt(3) + 1) - 1/18</em>(I<em>sqrt(3) + 1)/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) + 1/9/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3]</li>
</ul>
<p>So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be.
Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?</p>
<p>Regards,
Ben</p>
https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?answer=44293#post-id-44293Note that even if `I` appears in the solutions, the solutons are actually real, we can simplify the right-hand side of each solution, and see that `I` cancel eachother:
sage: s = solve(f==0, x)
sage: [i.rhs().full_simplify() for i in s]
[-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) - 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) + 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
2/3*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) - 1/3]
Note that 0.1 is interpreted as 1/10 in the symbolic ring, so that the solutions are symbolic and not numerical.
Thu, 15 Nov 2018 15:32:41 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?answer=44293#post-id-44293Comment by bentheteacher for <p>Note that even if <code>I</code> appears in the solutions, the solutons are actually real, we can simplify the right-hand side of each solution, and see that <code>I</code> cancel eachother:</p>
<pre><code>sage: s = solve(f==0, x)
sage: [i.rhs().full_simplify() for i in s]
[-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) - 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) + 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
2/3*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) - 1/3]
</code></pre>
<p>Note that 0.1 is interpreted as 1/10 in the symbolic ring, so that the solutions are symbolic and not numerical.</p>
https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44294#post-id-44294I figured that it would cancel each other, but i wasnt aware of the simplify-method. Thank you, im going to try this! :)
e: im still wondering if it should be this way. If i test this with "in RR" i get false which is not true as u said yourself... thats kinda oddThu, 15 Nov 2018 18:15:10 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44294#post-id-44294Comment by Emmanuel Charpentier for <p>Note that even if <code>I</code> appears in the solutions, the solutons are actually real, we can simplify the right-hand side of each solution, and see that <code>I</code> cancel eachother:</p>
<pre><code>sage: s = solve(f==0, x)
sage: [i.rhs().full_simplify() for i in s]
[-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) - 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) + 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
2/3*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) - 1/3]
</code></pre>
<p>Note that 0.1 is interpreted as 1/10 in the symbolic ring, so that the solutions are symbolic and not numerical.</p>
https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44297#post-id-44297sage: RR
Real Field with 53 bits of precision
Working in RR entails rounding errors, i. e. imperfect representation of quantities with no finite binary representation, hence inequalities where equalities are expected...Thu, 15 Nov 2018 18:48:04 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?comment=44297#post-id-44297Answer by slelievre for <p>Hey there,</p>
<p>i need your help on this one. The following sage code is giving me a headache:</p>
<ul>
<li>f(x) = x^3 + x^2 - 0.1</li>
<li>solve(f==0, x)</li>
<li>--></li>
<li>[x == -1/2<em>(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3)<em>(I</em>sqrt(3) + 1) - 1/18<em>(-I</em>sqrt(3) + 1)/(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2</em>(1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3)</em>(-I<em>sqrt(3) + 1) - 1/18</em>(I<em>sqrt(3) + 1)/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180<em>I</em>sqrt(13)<em>sqrt(3) + 7/540)^(1/3) + 1/9/(1/180</em>I<em>sqrt(13)</em>sqrt(3) + 7/540)^(1/3) - 1/3]</li>
</ul>
<p>So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be.
Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?</p>
<p>Regards,
Ben</p>
https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?answer=44332#post-id-44332Instead of defining `f` as a sympolic expression and using `solve`,
one can define `f` as a polynomial over the rationals, and get
its roots over the algebraic numbers.
Define ring of polynomials in `x` over the rationals, then `f`:
sage: R.<x> = QQ[]
sage: f = x^3 + x^2 - 1/10
Roots of `f` in `QQbar` (they are real: no imaginary part is shown):
sage: rr = f.roots(QQbar, multiplicities=False)
sage: print(rr)
[-0.8669513175959773?, -0.4126055722546906?, 0.2795568898506678?]
Further check that all roots are real:
sage: [r.imag() == 0 for r in rr]
[True, True, True]
Get radical expressions (involving `I` even though all roots are real).
sage: [r.radical_expression() for r in rr]
[-1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(-I*sqrt(3) + 1)
- 1/18*(I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3,
-1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(I*sqrt(3) + 1)
- 1/18*(-I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3,
(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)
+ 1/9/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3]
See link provided by @Emmanuel_Charpentier for the mathematics of that.Sun, 18 Nov 2018 20:15:54 +0100https://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/?answer=44332#post-id-44332