XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Rpqr79()

Sub Rpqr79 ( N As  Long,
A() As  Double,
Z() As  Complex,
Info As  Long 
)

Roots of a polynomial (real coefficients) (Companion matrix method)

Purpose
This routine computes all roots of a polynomial p(z) with real coefficients by computing the eigenvalues of the companion matrix.
p(z) = a0*z^n + a1*z^(n-1) + ... + an
Parameters
[in]NDegree of polynomial. (N >= 1)
[in]A()Array A(LA - 1) (LA >= N + 1)
Real coefficient vector of p(z) (a0 to an).
[out]Z()Array Z(LZ - 1) (LZ >= N)
The obtained zeros.
[out]Info= 0: Successful exit.
= -1: The argument N had an illegal value. (N <= 0)
= -2: The argument A() is invalid.
= -3: The argument Z() is invalid.
= 1: Failed to converge within maximum number (30) of QR iterations on some eigenvalue of the companion matrix.
Reference
SLATEC
Example Program
Solve the following algebraic equation.
x^5 + 2*x^3 + 2*x^2 - 15*x + 10 = 0
The exact solutions are 1(double root), -2 and ±√5.
Sub Ex_Rpqr79()
Const N As Long = 5
Dim A(N) As Double, Z(N - 1) As Complex, S(N - 1) As Double
Dim Info As Long, I As Long
A(0) = 1: A(1) = 0: A(2) = 2: A(3) = 2: A(4) = -15: A(5) = 10
Call Rpqr79(N, A(), Z(), Info)
For I = 0 To N - 1
Debug.Print Creal(Z(I)), Cimag(Z(I))
Next
Debug.Print "Info =", Info
End Sub
Function Cimag(A As Complex) As Double
Imaginary part of complex number
Function Creal(A As Complex) As Double
Real part of complex number
Sub Rpqr79(N As Long, A() As Double, Z() As Complex, Info As Long)
Roots of a polynomial (real coefficients) (Companion matrix method)
Example Results
-2 0
-2.1094237467878E-15 2.23606797749979
-2.1094237467878E-15 -2.23606797749979
0.999999999999999 2.75985349470833E-08
0.999999999999999 -2.75985349470833E-08
Info = 0