0
0
MATLABdata~5 mins

Polynomial evaluation and roots in MATLAB

Choose your learning style9 modes available
Introduction

We use polynomial evaluation to find the value of a polynomial at a certain point. Finding roots helps us know where the polynomial equals zero.

To check the value of a polynomial for a specific input, like calculating speed or growth.
To find where a curve crosses the x-axis in a graph.
To solve equations that can be written as polynomials.
To analyze signals or data trends that follow polynomial patterns.
Syntax
MATLAB
value = polyval(p, x)
roots_of_p = roots(p)

p is a vector of polynomial coefficients starting with the highest power.

x is the point or points where you want to evaluate the polynomial.

Examples
Evaluates the polynomial 2x² + 3x - 5 at x = 4.
MATLAB
p = [2 3 -5];
value = polyval(p, 4);
Finds the roots of the polynomial x³ - 6x² + 11x - 6.
MATLAB
p = [1 -6 11 -6];
r = roots(p);
Evaluates x² - 4 at x = 0, 1, and 2.
MATLAB
p = [1 0 -4];
values = polyval(p, [0 1 2]);
Sample Program

This program evaluates the polynomial x² - 3x + 2 at x = 5 and finds its roots.

MATLAB
p = [1 -3 2]; % Polynomial x^2 - 3x + 2
x = 5;
value = polyval(p, x);
roots_of_p = roots(p);

fprintf('Value of polynomial at x = %d is %d\n', x, value);
fprintf('Roots of the polynomial are:\n');
fprintf('%f\n', roots_of_p);
OutputSuccess
Important Notes

The roots function returns complex roots if they exist.

Polynomial coefficients must be in descending order of power.

Summary

Use polyval to find polynomial values at given points.

Use roots to find where the polynomial equals zero.