find roots in matlab

Matlab provides several built-in functions to find the roots of a given polynomial equation.

One of the functions that can be used to find the roots of a polynomial is roots(). Here is the general syntax:

main.m
roots(polyVec)
15 chars
2 lines

where polyVec is a vector containing the coefficients of the polynomial in decreasing order. For example,

main.m
p = [3 4 0 -1];
16 chars
2 lines

represents the polynomial 3x^3 + 4x^2 - 1.

To find the roots of this polynomial, we can use the roots() function as follows:

main.m
r = roots(p)
13 chars
2 lines

The result would be:

main.m
r =

    0.6438
   -1.0000
   -0.6438
38 chars
6 lines

These values represent the roots of the polynomial.

Another function that can be used to find the roots of a polynomial is polyval(). This function evaluates the polynomial for a given input and can be used to confirm whether a particular value is a root of the polynomial.

Here is an example usage of polyval():

main.m
x = 1;
y = polyval(p, x)
25 chars
3 lines

Output:

main.m
y =

     6
12 chars
4 lines

Since the value of y is not zero, we can determine that x=1 is not a root of the polynomial.

related categories

gistlibby LogSnag