Open In App

polyval() in MATLAB

Last Updated : 18 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

polyval is a built-in function in MATLAB that allows you to evaluate a polynomial at a specific point. It evaluates the polynomial let’s p at the points in x and it returns the corresponding function values in y . The syntax for polyval is as follows:

Syntax:

y = polyval(p,x)

%returns the value of a polynomial of degree n evaluated at x 

Where is a vector of coefficients representing a polynomial in descending order, and x is the point at which the polynomial is to be evaluated. The output, y, is the value of the polynomial at that point.

In this article, we will explore the polyval function in MATLAB and how it can be used to evaluate a polynomial at a given point.

Suppose we have the following polynomial:

3x^2 + 2x + 1

We can represent this polynomial as a vector of coefficients [3 2 1] and use polyval to evaluate it at a specific point x. For example:

Example 1:

Matlab




p = [3 2 1];
x = 2;
y = polyval(p,x)


Output:

17

 

Explanation:

This means that the value of the polynomial at x = 2 is 17.

Multiple Points:

polyval can also be used to evaluate a polynomial at multiple points at once. When multiple points are given as input, the function evaluates the polynomial at each point and returns a vector containing the results. 

Example 2:

Matlab




p = [3 2 1];
x = [1 2 3];
y = polyval(p,x)
y = 6    17    34    
% answer vector.
y = [6 11 18]        
% you can also change the vector.


Output:

6    17    34

 

Explanation:

In this case, the polynomial is evaluated at x = 1, x = 2, and x = 3, and the resulting values are stored in the y vector.

Conclusion:

In conclusion, polyval is a useful function in MATLAB for evaluating a polynomial at a specific point or multiple points. It is simple to use and can save time when working with polynomials in your code.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads