7. Polynomials

MATLAB has a number of commands to make manipulating and evaluating polynomials very simple. In the Help Window, in the \mat\polyfun topic you will see the following commands:

roots - Find polynomial roots

poly - Convert roots to polynomial

polyval - Evaluate polynomial

polyvalm - Evaluate polynomial with matrix argument

residue - Partial-fraction expansion (residues)

polyfit - Fit polynomial to data

polyder - Differentiate polynomial

conv - Multiply polynomials

deconv - Divide polynomials

In MATLAB (but not in the “Maple” Symbolic Math Toolbox) a polynomial is represented by an array (row vector) of its coefficients in descending order. For example, enter the polynomial p(x) = 3x5−4x4+7x2 −9x+3 as p=[3 -4 0 7 -9 3].

What polynomial q(x) does q=[4 5 -6] represent?

MATLAB does not provide a special function for adding or subtracting polynomials other than using arrays of the same length. Hence you would need to pad out the shorter array with leading zeros in this circumstance. Note that when poly forms a polynomial with given roots, it makes the leading coefficient equal to one. A few of these concepts are illustrated by executing the following M-file. Examine carefully the output.

clear all % file available in M-files folder as file4.m

p=[3 -4 0 7 -9 3]

q=[4 5 -6], qpadded=[0 0 0 4 5 -6];

newp=p+2*qpadded

proots=roots(p)

qroots=roots(q)

p1=poly(proots)       % check that p=3*p1, as expected

q1=poly(qroots)       % check that q=4*q1, as expected

y=polyval(p,2.31)     % evaluate p(2.31)

pder=polyder(p)       % check answer is dp/dx

qder=polyder(q)       % check answer is dq/dx=8*x+5

pq=conv(p,q) % multiply p(x)*q(x)

[Q,R]=deconv(p,q) % Q=quotient, R=remainder for p/q

Exercise: Add the commands x=linspace(-1.7,2,300); plot(x,polyval(p,x)) to the previous M-file to see a graph of y = p(x) for −1.7 ≤ x ≤ 2.