Open In App

Polynomials in MATLAB

A polynomial is an expression that is made up of variables, constants, and exponents, that are combined using mathematical operations such as addition, subtraction, multiplication, and division (No division operation by a variable). Polynomials in MATLAB are represented as row of a vector containing coefficients ordered by descending powers. 

For example, the equation G(x) = 2x4 + 3x3 – 4x + 1 could be represented as gfg = [2 3 -4 1]. For evaluating Polynomials we use function polyval( ), It evaluates the polynomial gfg at each point in x.



Example 1:




% MATLAB code for example:
% to evaluate our polynomial gfg =
% 2x4 + 3x3 - 4x + 1, at x = 4,
gfg = [2 3 0  -4 1];
GFG= polyval(gfg,4)

Output:



 

Example 2:




% MATLAB code for evaluating a polynomial with
% matrices as variables, we use polyvalm( ) function.
gfg = [2 3 0  -4 1];
X = [1 2 -3 4; 2 -5 6 3; 3 1 0 2; 5 -7 3 8];
GFG= polyvalm(gfg, X)

Output:  

 

Example 3:




% MATLAB code for finding roots
% of polynomial : gfg= 2x4 + 3x3 - 4x + 1,
gfg = [2 3 0  -4 1];
GFG = roots(gfg)

Output:

 

 


Article Tags :