Search code examples
pythonnumerical-analysis

finding number of root determined interval


I have n degree polynomial system ,just I want to learn number of root between previously determined interval .But I do not want to find root.I need number of root.I need to write python code. For example : x^8+2x^6+5x^4+x^2+45x+1=0 How many root have we between 3-5? emphasize=I do not want to find root,just I want to learn how many root I have.


Solution

  • You can do this with numpy

    import numpy as np
    coeff = [1,0,2,0,5,0,1,45,1] #for the polynomial  x^8+2x^6+5x^4+x^2+45x+1=0
    np.roots(coeff) 
     # array([ 1.37234708+0.91495949j,  1.37234708-0.91495949j,
     #   0.43013459+1.75225934j,  0.43013459-1.75225934j,
     #  -1.06175643+1.53395567j, -1.06175643-1.53395567j,
     #  -1.45921726+0.j        , -0.02223323+0.j        ])
    

    Thus, as you can see, this one has zero real roots.

    Edit: If you want to find the number of roots between an interval without finding the roots explicitly, you can use sturm's theorem. Using sympy,

    from sympy import Sturm,Symbol
    from sympy.abc import x
    sturm_seq = sturm(x**6-3*x**5+2*x**4)
    limits = [2,4]; x = Symbol('x')
    values_at_start = [polynomial.subs(x,limits[0]).evalf() for polynomial in sturm_seq]
    values_at_end = [polynomial.subs(x,limits[1]).evalf() for polynomial in sturm_seq]
    import itertools
    # Count number of sign changes in values_at_start
    count_start = len(list(itertools.groupby(values_at_start, lambda values_at_start: values_at_start > 0)))
    count_end = len(list(itertools.groupby(values_at_end, lambda values_at_end: values_at_end > 0)))
    ans = count_start - count_end
    print ans # ans = 1