roots - Polynomial roots - MATLAB (original) (raw)
Main Content
Syntax
Description
r = roots([p](#buo5d7q%5Fsep%5Fshared-p))
returns the roots of the polynomial represented by the coefficients in p
as a column vectorr
. Input p
is a vector containingn+1
polynomial coefficients, starting with the coefficient of_x_n. For example, p = [3 2 -2]
represents the polynomial 3x2+2x−2. A coefficient of 0
indicates an intermediate power that is not present in the equation.
The roots
function solves polynomial equations of the form p1xn+...+pnx+pn+1=0. Polynomial equations contain a single variable with nonnegative exponents.
Examples
Solve the equation 3x2-2x-4=0.
Create a vector to represent the polynomial, then find the roots.
p = [3 -2 -4]; r = roots(p)
Solve the equation x4-1=0.
Create a vector to represent the polynomial, then find the roots.
p = [1 0 0 0 -1]; r = roots(p)
r = 4×1 complex
-1.0000 + 0.0000i 0.0000 + 1.0000i 0.0000 - 1.0000i 1.0000 + 0.0000i
Input Arguments
Data Types: single
| double
Complex Number Support: Yes
Tips
- Use the poly function to obtain a polynomial from its roots:
p = poly(r)
. Thepoly
function is the inverse of theroots
function. - Use the fzero function to find the roots of nonlinear equations. While the
roots
function works only with polynomials, thefzero
function is more broadly applicable to different types of equations.
Algorithms
The roots
function considers p
to be a vector with n+1
elements representing the n
th degree characteristic polynomial of an n
-by-n
matrix, A
. The roots of the polynomial are calculated by computing the eigenvalues of the companion matrix, A
.
A = diag(ones(n-1,1),-1); A(1,:) = -p(2:n+1)./p(1); r = eig(A)
The results produced are the exact eigenvalues of a matrix within roundoff error of the companion matrix, A
. However, this does not mean that they are the exact roots of a polynomial whose coefficients are within roundoff error of those in p
.
Extended Capabilities
Usage notes and limitations:
- In the generated code, the output of the
roots
function is always complex. - The order of the roots in output vector
r
might differ between the generated code and MATLAB®. - The roots returned by the generated code might not be exactly equal to those returned by MATLAB.
- For input argument
p
:- You must specify the input vector as a fixed-size or variable-length vector at code generation time. Either the first or the second dimension of the vector can be variable size. All other dimensions must have a fixed size of 1.
The roots
function supports GPU array input with these usage notes and limitations:
- The output
r
is always complex even if all the imaginary parts are zero.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a