On Fri, 29 May 2020 at 09:53, David Bailey <[email protected]> wrote:
>
> On 28/05/2020 22:13, Rainer Dorsch wrote:
>
> Hello,
>
> when solving a cubic equation
>
> fk:  95.9271531456121*x - 1.7e-18/(x - 6.0e-7)**2
>
> I get a strange result:
>
> [6.10000000000001e-8 - 0.e-30*I, 3.85627081928849e-7 + 0.e-27*I,
> 7.53372918071151e-7 - 0.e-28*I]
>
> What does the 0.e-30*I mean? I do not think that there should be an imaginary
> part...
>
> Even worse, if I decleare x as real, I do get no solution at all.
>
>
> In addition to Aaron's comments I would like to point  out that if you have a 
> non-linear equation which is either intrinsically inexact because one or more 
> of the coefficients is a floating point number (as in this case) or where the 
> intention is simply to convert the answer as a floating point number, it is 
> far better to use nsolve in the real domain:
>
> nsolve( 95.9271531456121*x - 1.7e-18/(x - 6.0e-7)**2,x,(-50,50))
>
> The result is generally more accurate, spurious imaginary solutions don't 
> appear, and the calculation is probably more efficient (only relevant if the 
> intention is to do a lot of such calculations).

There are also specific numerical algorithms for finding approximate
roots of univariate polynomials the can be used through the roots
function or the nroots method of Poly:

>>> from sympy import *
>>> x = Symbol('x')
>>> eq = 95.9271531456121*x - 1.7e-18/(x - 6.0e-7)**2
>>> eq2 = eq.as_numer_denom()[0]
>>> eq2
95.9271531456121*x*(x - 6.0e-7)**2 - 1.7e-18
>>> roots(eq2)
{6.10000000000001e-8: 1, 3.85627081928849e-7: 1, 7.53372918071150e-7: 1}
>>> p = Poly(eq2, x)
>>> p
Poly(95.9271531456121*x**3 - 0.000115112583774735*x**2 +
3.45337751324203e-11*x - 1.7e-18, x, domain='RR')
>>> p.nroots()
[6.10000000000001e-8, 3.85627081928849e-7, 7.53372918071150e-7]

Because these are tailored to polynomials they can have explicit
knowledge about which roots are real and which are non-real:

>>> Poly(x**3 + x**2 + x + 1, x).nroots()
[-1.00000000000000, -1.0*I, 1.0*I]

-- 
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxS9h9gNYWD7L11WgYZPn2hT86tZ%3DiECtGT%3DFQY%3D%2BvtyLQ%40mail.gmail.com.

Reply via email to