Matlab

# How to Use poly() Function in MATLAB?

Polynomial functions are the building blocks of mathematics and they assist us in solving many mathematical problems such as finding x-intercepts of a function.

We can create a polynomial if its roots are given also a characteristic polynomial of a matrix can be determined in terms of its eigenvalues. In the era of high-performance tools, it has become an impractical approach to construct a polynomial manually. So, we can efficiently construct a polynomial in MATLAB using the poly() function.

This tutorial is going to make a useful discussion about:

## What is a Polynomial?

Polynomials are the mathematical uni or multivariate functions driven by the coefficients and variables. Usually, it is denoted by the term p(x). The standard form of a polynomial of degree n is given as:

Where,

• ai ’s represent the polynomial coefficients.
• n represents the highest degree of polynomial.
• xi ’s represent the indeterminents of the polynomial.

## Why do we Use Polynomials?

The polynomial functions have many applications. Some of them are given below.

Polynomials are used:

• In the various fields of science and engineering such as chemistry, physics, computer science, electronics, and many other fields.
• To solve many mathematical problems.
• In construction.
• To find zeros of a function.
• In Roller coaster design to describe curves.

## How to Use the poly() Function in MATLAB?

The poly() is a built-in function in MATLAB that is used for constructing a polynomial from its roots or for creating characteristic polynomials of a matrix from its eigenvalues. This function accepts a row vector containing roots or eigenvalues or accepts an n-by-n square matrix as a parameter and returns a row vector p containing coefficients of the created polynomial in the order of decreasing powers.

## Syntax

The poly() can be implemented through the given syntaxes in MATLAB.

p = poly(r)
p = poly(A)

Here,

The function p = poly(r) is responsible for creating a polynomial p of degree n from the roots of the polynomial contained by the row vector r. This function returns a row vector p that contains n+1 coefficients of the polynomial in such a way the coefficient of the highest degree of the polynomial is located at the first index of the vector p and so on for all other polynomial coefficients. Remember that the higher degree of the polynomial p will be equal to the length(r).

The function p = poly(A) is responsible for creating a characteristic polynomial p of the given A matrix having degree n from the eigenvalues of A. This function returns a row vector p that contains the n+1 co-efficient of the created polynomial of degree n in such a way the coefficient of the higher degree of the polynomial is placed at the first index of the vector p and so on or all other polynomial coefficients. Remember that the highest degree n of the polynomial p will be equal to the size(A).

The mathematical expression to calculate the characteristic polynomial of a square matrix is given as:

## Example 1: How to Create a Polynomial from its Roots Using the poly() Function?

This MATLAB code uses the poly() function to create a polynomial. To perform this task, it creates a row vector r that contains two complex roots of the polynomial p which we want to create. After that, it constructs a polynomial p of degree 2 having its coefficients in the row vector p.

r = [-i i];
p = poly(r)

The mathematical form of the polynomial p is x2 + 1.

## Example 2: How to Create a Characteristic Polynomial of the Given Matrix from its Eigenvalues Using the poly() Function?

In this example, we implement the poly() function to construct the characteristic polynomial p of the given matrix A using its eigenvalues. For this, first, we create a 5-by-5 matrix of positive integers using the magic() function. After that, we use the eig() function to calculate the eigenvalues of the matrix A and store them in the row vector e. At the end, we use the poly() function to create a characteristic polynomial having degree 5 of the matrix A from its eigenvalues. The coefficients of the created polynomial are stored in the row vector p.

A = magic(5);
e = eig(A);
p = poly(e)

## Example 3: How to Create a Characteristic Polynomial of the Given Matrix Using the poly() Function?

The below-given example creates a characteristic polynomial of the given matrix A. Here, we do not need to calculate the eigenvalues of the matrix A. The poly() function simply accepts the matrix A as an argument and returns a row vector p containing coefficients of the polynomial of degree 5.

A = magic(5);
p = poly(A)

## Conclusion

Polynomials are uni or multivariate functions that can be expressed in terms of their coefficients and variables. These functions have many applications in the different areas of science and engineering. Constructing a polynomial from its roots or a characteristic polynomial of a matrix from the eigenvalues of that matrix is a common problem in linear algebra that can be solved efficiently using MATLAB’s built-in poly() function. This article has presented the implementation of the poly() function in three different ways to create a polynomial.