How to Get Roots of Polynomial in Matlab
-
Get the Roots of the Polynomial Using the
roots()
Function in MATLAB -
Get the Roots of the Polynomial Using the
solve()
Function in MATLAB
This tutorial will introduce how to find the roots of the polynomial using the roots()
and solve()
functions in MATLAB.
Get the Roots of the Polynomial Using the roots()
Function in MATLAB
If you want to find the roots of a polynomial, you can use the roots()
function in MATLAB. This input of this function is a vector that contains the coefficients of the polynomial. If a power is not present in the polynomial, then 0 will be used as its coefficient. The output of this function is a column vector that contains the real and imaginary roots of the given polynomial. For example, let’s find the roots of a quadratic polynomial: 2x^2 - 3x + 6 = 0. We have to define the polynomial coefficients starting from the highest power, and if a power is not present, we will use 0 as its coefficient. See the code below.
poly = [2 -3 6];
p_roots = roots(poly)
Output:
p_roots =
0.7500 + 1.5612i
0.7500 - 1.5612i
In the above code, we only used the coefficients of the polynomial starting from the highest power. You can change the coefficients of the polynomial according to the given polynomial. Know, let’s find the roots of a quartic polynomial: 2x^4 + 1 = 0. See the code below.
poly = [2 0 0 0 1];
p_roots = roots(poly)
Output:
p_roots =
-0.5946 + 0.5946i
-0.5946 - 0.5946i
0.5946 + 0.5946i
0.5946 - 0.5946i
We used three 0 between the two polynomials in the above code because the three powers are missing. Check this link for more information about the roots()
function.
Get the Roots of the Polynomial Using the solve()
Function in MATLAB
If you want to find the roots of a polynomial, you can use the solve()
function in MATLAB. This input of this function is a polynomial. The output of this function is a column vector that contains the real and imaginary roots of the given polynomial. For example, let’s find the roots of a quadratic polynomial: 2x^2 - 3x + 6 = 0. We have to define the polynomial. See the code below.
syms x
poly = 2*x^2 -3*x +6 == 0;
p_roots = solve(poly,x)
p_roots = vpa(p_roots,2)
Output:
p_roots =
0.75 - 1.6i
0.75 + 1.6i
In the above code, we defined the whole polynomial, and we used the vpa()
function to change the precision of the result. You can change the polynomial according to the given polynomial and the precision according to your requirements. Know, let’s find the roots of a quartic polynomial: 2x^4 + 1 = 0. See the code below.
syms x
poly = 2*x^4 +1 == 0;
p_roots = solve(poly,x);
p_roots = vpa(p_roots,2)
Output:
p_roots =
- 0.59 - 0.59i
- 0.59 + 0.59i
0.59 - 0.59i
0.59 + 0.59i
In the above code, we defined the whole polynomial and used the vpa()
function to change the result’s precision. You can change the polynomial according to the given polynomial and the precision according to your requirements.