Matlab

How to Use roots Function in MATLAB

Finding the roots of a polynomial is a widely used mathematical operation that becomes difficult and time-consuming for polynomials having degree n = 3 or above. In the world of high-performance computing tools like MATLAB, it is an impractical approach to manually find the roots of polynomials. MATLAB provides us with a built-in roots() function that computes the roots of a polynomial having any degree n where n represents a natural number.

This guide is going to explore the functionality of the roots() function with its syntax and examples.

Why Do We Need to Find Roots?

Finding roots in mathematics is an important task since it allows you to solve equations and understand the behavior of the functions. It is also useful in computer science and other fields for finding the optimal solutions.

How to Use roots() Function in MATLAB

MATLAB has a huge library of built-in functions to perform many tasks. One such function is the roots() function which is responsible for finding the roots of the given input polynomial. This function accepts a polynomial as an input and provides its roots as an output.

Syntax

The roots() function can be implemented in MATLAB through the following syntax:

r = roots(p)

Here,

The function roots(p) is responsible for computing the roots of the given polynomial represented by variable p which is a row vector containing n+1 coefficients of the polynomial having degree n.

Example 1: How to Find Roots of a Cubic Polynomial in MATLAB?

This MATLAB code creates a polynomial of degree n=3 and computes its roots using the roots() function.

p = [7 -6 0 5];

r = roots(p)

Example 2: How to Compute Roots of a Polynomial Having Degree N=10?

In this example, we use the roots() function to find the roots of a polynomial having degree n = 10.

p = [1 7 -6 0 0 0 0 1 -2 5 9];

r = roots(p)

Conclusion

Manually calculating the roots of a polynomial having degree 3 or above is a complicated and time-consuming task. However, with the inclusion of the roots() function in MATLAB, we can easily calculate the roots of a polynomial of any nth-degree. This guide has provided the syntax and a few examples to help us understand the workings of the roots() function.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.