Polynomials in Matlab

Contents

1. Representation of polynomials

2. Derivatives

3. Roots

1. Representation of polynomialsIn Matlab, a polynomial is represented by a row vector of its coefficients. If the polynomial has degree n, the corresponding representing vector has length n+1 and contains the coefficients associated with decreasing powers from left to right. Zero coefficients must be marked as zero entries. For example, [3 2 1] represents P(r) = 3r

^{2}+ 2r + 1 whereas [1 0 0] represents P(r) = r^{2}.The command polyval(vector,arg) interprets vector as polynomial and evaluates it at arg, which can be a number, a vector or even a matrix (pointwise evaluation - like sin(x) if x is an array).

Example:» p=[1 2 1];polyval(p,1),polyval(p,[1 2])

ans =

4

ans =

4 9

This allows to plot polynomials in the usual way using plot. For example, with the commands

» r=linspace(-2,0,100);plot(r,polyval([1 2 1],r))

a plot of P(r) = r

^{2}+2r+1 in the range -2 £ r £ 0 is created using 100 supporting points.

2. DerivativesAnother useful command is polyder(vector). Here again the vector argument is interpreted as polynomial and the output is the vector representing the derivative of this polynomial. For example, the derivative of P(r) = r

^{2}+2r+1 (vector [1 2 1]) is P'(r)=2r+2 (vector [2 2]) which you can find by executing» polyder([1 2 1])

ans =

Root command.The most important command is root(vector)which finds numerically all roots of the polynomial associated with the vector argument. Let's define a polynomial of degree 6 with random coefficients and compute its roots:» p=rand([1 7]),roots(p)

p =

0.9169 0.4103 0.8936 0.0579 0.3529 0.8132 0.0099

ans =

-0.4043 + 1.0987i

-0.4043 - 1.0987i

0.5809 + 0.6921i

0.5809 - 0.6921i

-0.7883

-0.0122The first output shows 7 random numbers between 0 and 1 to which a polynomial of degree 6 is associated. The second output contains numerical approximations of the 6 roots of this poynomial which are stored in a column vector. Let's confirm that the third number of the last answer is indeed a root:

» polyval(p,ans(3))

ans =

9.1073e-016 +7.8063e-017i

Multiple roots.There are some complications with multiple roots. For second degree polynomials these are usually recognized, but not necessarily for polynomials of higher degree. The polynomials r^{2}+2r+1 and r^{3}+3r^{2}+3r+1 have just one root r = -1, but» roots([1 2 1]),roots([1 3 3 1])

ans =

-1

-1

ans =

-1.00000913968880

-0.99999543015560 + 0.00000791513186i

-0.99999543015560 - 0.00000791513186iin the cubic case three different (though close) values are returned.