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.Thu, 26 Nov 2020 19:40:24 +0100Sage solver : approximate solutionhttps://ask.sagemath.org/question/54399/sage-solver-approximate-solution/ I have the simple following equation :
res = solve([1.00000000000000*x8 == 0.923879532511287,0.414213562373096*x8 == 0.382683432365089],x8,solution_dict=True)
However, the solver will return an empty solution because the value do not quite exactly match (there is a 1e-15 difference that is due to numerical errors of the 53 bits mantissa precision). Is there a way to ask the solver to get an approximate solution instead?Thu, 26 Nov 2020 18:50:37 +0100https://ask.sagemath.org/question/54399/sage-solver-approximate-solution/Answer by Emmanuel Charpentier for <p>I have the simple following equation :</p>
<pre><code>res = solve([1.00000000000000*x8 == 0.923879532511287,0.414213562373096*x8 == 0.382683432365089],x8,solution_dict=True)
</code></pre>
<p>However, the solver will return an empty solution because the value do not quite exactly match (there is a 1e-15 difference that is due to numerical errors of the 53 bits mantissa precision). Is there a way to ask the solver to get an approximate solution instead?</p>
https://ask.sagemath.org/question/54399/sage-solver-approximate-solution/?answer=54403#post-id-54403Your first equation *is* an approximate solution (the coefficient of `x8` is indistinguishable from 1) .
It turns out that your two equations are *not* equivalent :
sage: var("x8")
x8
sage: Sys=[1.00000000000000*x8 == 0.923879532511287,0.414213562373096*x8 == 0.382683432365089]
sage: Sys[1].solve(x8)[0].rhs()-Sys[0].solve(x8)[0].rhs()
-9631419/1809844328933473637045
sage: (Sys[1].solve(x8)[0].rhs()-Sys[0].solve(x8)[0].rhs()).n()
-5.32168366418327e-15
And Sage is *right* in telling you that your system has no solution.
Workarounds :
- Obtain exact values for your systems' coefficients ;
- solve for one equation, use the second to estimate the error, and decide if you want to accept this as an approximation. ;
- treat your system as a system of redundant equations and search for the least-squares solution (left as an exercise for the reader...).
Note, by default, Sage (9.3.beta2) converts your equations' coefficients to rational approximations :
sage: Sys[0].solve(x8)[0].rhs()
15965419/17280845
sage: Sys[1].solve(x8)[0].rhs()
96759049255592/104731240221961
You can obtain floating-point solutions :
sage: (Sys[0].lhs()-Sys[0].rhs()).roots(ring=RR, multiplicities=False)
[0.923879532511287]
sage: (Sys[1].lhs()-Sys[1].rhs()).roots(ring=RR, multiplicities=False)
[0.923879532511283]
And it turns out that, even in the inexact ring `RR`, your equations are *not* equivalent.Thu, 26 Nov 2020 19:40:24 +0100https://ask.sagemath.org/question/54399/sage-solver-approximate-solution/?answer=54403#post-id-54403