r = Vectors.Utilities.roots(p);
This function computes the roots of a polynomial P of x
P = p[1]*x^n + p[2]*x^(n-1) + ... + p[n-1]*x + p[n+1];
with the coefficient vector p. It is assumed that the first element of p is not zero, i.e., that the polynomial is of order size(p,1)-1.
To compute the roots, the eigenvalues of the corresponding companion matrix C
|-p[2]/p[1] -p[3]/p[1] ... -p[n-2]/p[1] -p[n-1]/p[1] -p[n]/p[1] | | 1 0 0 0 0 | | 0 1 ... 0 0 0 | C = | . . ... . . . | | . . ... . . . | | 0 0 ... 0 1 0 |
are calculated. These are the roots of the polynomial.
Since the companion matrix has already Hessenberg form, the
transformation to Hessenberg form has not to be performed. Function
eigenvaluesHessenberg
provides efficient eigenvalue computation for those matrices.
r = roots({1,2,3}); // r = [-1.0, 1.41421356237309; // -1.0, -1.41421356237309] // which corresponds to the roots: -1.0 +/- j*1.41421356237309
encapsulated function roots import Modelica.Math.Matrices; import Modelica; extends Modelica.Icons.Function; input Real p[:] "Vector with polynomial coefficients p[1]*x^n + p[2]*x^(n-1) + p[n]*x +p[n-1]"; output Real roots[max(0, size(p, 1) - 1), 2] = fill(0, max(0, size(p, 1) - 1), 2) "roots[:,1] and roots[:,2] are the real and imaginary parts of the roots of polynomial p"; end roots;