Polynomials are functions that have the form
The coefficients are often real numbers and which is a nonnegative integer, is the degree or order of the polynomial.
MATLAB uses a row vector to represent a polynomial, by the elements in the vector are the coefficients . The first element is the coefficient of the x with the highest power. The vector has to include all the coefficients, including the ones that are equal to 0. For example, the polynomial
can be represented in MATLAB by typing:
>> p = [5 0 0 6 -3 10];
The value of a polynomial at a point x can be calculated with the function polyval which has the form:
polyval(p,x)
where p is a vector with the coefficients of the polynomial and x is a value to be evaluated. x can also be a vector or a matrix. In these cases, the polynomial is calculated element by element. For example, with the polynomial p(x) given above, the value of p(0) can be computed by:
>> polyval(p,0)
ans =
10
or if we want to evaluate p(x) at x = 0,1,2,3 at once, we can type:
>> polyval(p,[0 1 2 3])
ans =
10 18 188 1270
The command polyval returns a vector with the corresonding values of the polynomial.
Hence, this make us an easier way to plot a polynomial function. Suppose we want to plot
We can use polyval for this purpose by typing:
>> p = [1 -2 5 -10];
>> x = linspace(-3,5,100);
>> px = polyval(p,x);
>> plot(x,px)
Roots of a polynomial
are the values of for which . For example, the roots of are -1 and -2. There are n roots of a polynomial with degree n.
The command roots determines the roots of a polynomial. The usage of the function is:
r = roots(p)
where r is a column vector with the roots and p is a row vector with the coefficients of the polynomial. For example, the roots of
can be found as follows:
>> p = [3 15 0 -10 0 4 0]
p =
3 15 0 -10 0 4 0
>> roots(p)
ans =
0
-4.8613
-0.6925 + 0.3093i
-0.6925 - 0.3093i
0.6232 + 0.2975i
0.6232 - 0.2975i
Two polynomials can be added or subtracted by adding the vectors of the coefficients. If the two polynomials do not have the same order (which mean the coefficient vectors are not of the same length), the shorter vectors has to be modified to have the same length as the longer one by adding zeros in front. For example, to compute
we first use p and q to represent the two polynomials respectively
>> p = [3 -2 10 -1 0 0 7];
>> q = [1 0 -2 -4]
The polynomial q has degree 3, we need to add 3 zeros since p has order 6:
>> p+[0 0 0 q]
ans =
3 -2 10 0 0 -2 3
Then we read that the summation is
Two polynomials can be multiplied by using the command conv which has the form:
r = conv(p,q)
where p,q are the coefficient vectors that are being multiplied and r is the coefficient vector of the product. For example, the multiplication of and is done by:
>> conv([3 -2],[5 0 -1])
ans =
15 -10 -3 2
The result is the polynomial . Note that the two polynomials that are being multiplied do not have to be the same order. Multiplication of three or more polynomials is done by using conv command repeatedly.
A polynomial can be divided by another polynomial with the command deconv that has the form:
[q,r] = deconv(u,v)
For example, dividing by is done by:
>> [q,r] = deconv([2 7 -5 -2],[1 3])
q =
2 1 -8
r =
0 0 0 22
The answer is and the remainder is 22.
The command polyder calculates the derivative of a single polynomial, a product of two polynomials, and a quotient of two polynomials.
The only difference between the last two commands is the number of the output arguments. With two output arguments MATLAB calculates the derivative of quotient of two polynomials. With one output argument, MATLAB computes the derivative of the product.
For example,
The derivatives of , and, can be determined by:
>> p = [3 -2 4]; q = [1 0 3];
>> k = polyder(p)
k =
6 -2
>> d = polyder(p,q)
d =
12 -6 26 -6
>> [n d] = polyder(p,q)
n =
2 10 -6
d =
1 0 6 0 9