Python Forum
Wrong result of polyroots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrong result of polyroots
#1
I'm using the following code as part of a recursively called process.
It processes a proportional feedback loop provided by a polynom, that might use complex coefficients.

import numpy.polynomial.polynomial as poly
import math
import cmath

def coefficients(k, ts, t1):
    z1 = complex(1-k*t1,0)
    z2 = complex(k*(ts+2*t1)-1-math.exp(-ts/t1),0)
    z3 = complex(math.exp(-ts/t1)-k*(t1+ts*math.exp(-ts/t1)),0)
    coeff = (z1,z2,z3)
    return coeff

coeff = coefficients(0, 0.3, 0.4)
# coeff = ((1+0j), (-1.4723665527410148+0j), (0.47236655274101474+0j))
roots = poly.polyroots(coeff)
# roots = [ 1.00000000+0.j  2.11700002+0.j]
# ... 
# The first root is ok, the second is not a root:
>>> coeff[0]*roots[0]**2+coeff[1]*roots[0]+coeff[2]
-2.7755575615628914e-16
>>> coeff[0]*roots[1]**2+coeff[1]*roots[1]+coeff[2]
1.8370556064664063
# The correct value for the 2nd root would have been 0.4723665524
>>> coeff[0]*0.4723665524**2+coeff[1]*0.4723665524+coeff[2]
1.799307924876814e-10
What are I'm doing wrong? Or is there another method to calculate the root locus in NumPy?

By the way, I can't use the usual formula for quadratic equations because is swapped by an other function of higher degree for PI and PID-feedbacks. Thank you for any hints
Thomas
Reply
#2
Check reverse coefficients order instead:

coeff[2]*roots[0]**2+coeff[1]*roots[0]+coeff[0]
coeff[2]*roots[1]**2+coeff[1]*roots[1]+coeff[0]
Note, that polyroot function uses np.linalg.eigs to get roots of a polynomial; In case of higher dimension precise computing eigenvalues
of a non-symmetric matrix might be problematic due to their high sensitivity to numerical errors, even to errors of representing floating point
numbers in a computer memory.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nelder-Mead gives wrong result Alex009988 0 1,465 Sep-02-2019, 07:39 PM
Last Post: Alex009988

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020