Matlab is an interactive computational platform which has the capability of handling very large data. In a previous article I have described how to plot multiple graphs in Matlab and in this article I will discuss different kinds of polynomial operations that can be performed here using different inbuilt functions. Polynomials can be multivariate but here I will consider polynomials of a single variable only.
Polynomial Declaration
A polynomial of degree N has the general form
P(x) = aN.x^N + … + a3.x^3 + a2.x^2 + a1.x + a0
where x is the independent variable and N is an positive integer. Now, in Matlab the polynomials are simply represented by their coefficients. Suppose we want to write a 3^{rd} degree polynomial
x^3 + 4x^2 – 3x -2
then the declaration will be
$$ p = [ 1 4 -3 -2 ] $$
So, we need to write the coefficients from highest degree of the variable x to the lowest inserting a space between the numbers. Remember that if some power of x does not appear in a polynomial we must put a zero in place of it. For example,
x^3 – 1 => $$ q = [ 1 0 0 -1 ] $$
Addition and Subtraction
Addition and subtraction is extremely easy. We just need to keep in mind that the two polynomials must be of same degree. For example, for the previously defined polynomials $$ p+q $$ and $$ p-q $$ are valid operations.
Multiplication
Multiplication can be performed between any two polynomials using the function “conv” which can take only two arguments. So to multiply more than two polynomials we need to do that one by one.
$$ p = [ 1 4 -3 -2 ];
q = [ 2 -3 5 ];
conv(p,q)
conv(p, conv(q,p) ) $$
Division
Two polynomials can be divided using “deconv” which returns the quotient polynomial coefficients q and remainder polynomial coefficients r. Let us denote the numerator and denominator polynomials by n and d respectively.
$$ n = [ 2 3 6 -4 -1 ];
d = [ 1 -2 1 ];
[ q r ] = deconv(n,d ) $$
You can crosscheck the result by writing $$ conv(q,d ) + r $$ to get back the original polynomial n.
Derivative
Differentiation of a polynomial can be easily done by “polyder” as shown below.
$$ p = [ 3 -4 5 10 ];
polyder(p) $$
Polynomial Evaluation
Suppose we want to evaluate the polynomial x^3 – 3x^2 + 4x – 2 at points -1, 0 and 1. For that we will create a row vector x and use the function “polyval”.
$$ x = -1:1
p = [ 1 -3 4 -2 ];
polyval(p,x ) $$
Finding Roots of a Polynomial
In many physical problems we often need to find the roots of a polynomial which is basically the values of x for which the polynomial vanishes. This can be done using “roots” which is capable of determining both real and imaginary roots.
$$ p = [ 1 -3 4 -2 ];
roots(p) $$
Thus we have learned different kinds of polynomial operations in Matlab.
Note that here in each case a pair of “$$” sign is used for separating the commands from general texts.