You are on page 1of 3

31/10/2019 Polynomial roots - MATLAB roots

roots
Polynomial roots

Syntax

r = roots(p)

Description
r = roots(p) returns the roots of the polynomial represented by p as a column vector. Input p is a example
vector containing n+1 polynomial coefficients, starting with the coefficient of xn. A coefficient of 0
indicates an intermediate power that is not present in the equation. For example, p = [3 2 -2]
represents the polynomial 3x + 2x − 2 . 2

The roots function solves polynomial equations of the form p1 x


n
+ ... + pn x + pn + 1 = 0 . Polynomial
equations contain a single variable with nonnegative exponents.

Examples collapse all

 Roots of Quadratic Polynomial

Solve the equation 3x


2
− 2x − 4 = 0 .
Try it in MATLAB
Create a vector to represent the polynomial, then find the roots.

p = [3 -2 -4];
r = roots(p)

r = 2×1

1.5352
-0.8685

 Roots of Quartic Polynomial

Solve the equation x


4
− 1 = 0 .
Try it in MATLAB
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
collapse all

https://www.mathworks.com/help/matlab/ref/roots.html 1/3
31/10/2019 Polynomial roots - MATLAB roots

p — Polynomial coefficients
 vector

Polynomial coefficients, specified as a vector. For example, the vector [1 0 1] represents the polynomial
x + 1 , and the vector [3.13 -2.21 5.99] represents the polynomial 3.13x − 2.21x + 5.99 .
2 2

For more information, see Create and Evaluate Polynomials.

Data Types: single | double


Complex Number Support: Yes

Tips
• Use the poly function to obtain a polynomial from its roots: p = poly(r). The poly function is the inverse of the
roots function.
• Use the fzero function to find the roots of nonlinear equations. While the roots function works only with
polynomials, the fzero 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 nth 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

 C/C++ Code Generation


Generate C and C++ code using MATLAB® Coder™.

 GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing
Toolbox™.

See Also
fzero | poly | polyval | residue

Topics
Roots of Polynomials
Create and Evaluate Polynomials

Introduced before R2006a

https://www.mathworks.com/help/matlab/ref/roots.html 2/3
31/10/2019 Polynomial roots - MATLAB roots

https://www.mathworks.com/help/matlab/ref/roots.html 3/3

You might also like