Polynomials

Contents

Working with polynomials

A polynomial in MATLAB is represented by a row vector of coefficients. The polynomial:

(x^4)-(7x^3)+(11x^2)+(7x)-12

is represented in MATLAB as:

p = [ 1 -7 11 7 -12 ];

Back to top

Find roots of the polynomial

The MATLAB command "roots" will provide the roots of a polynomials:

roots(p)
ans =

    4.0000
    3.0000
   -1.0000
    1.0000

Back to top

Define a polynomial by its roots

We can build up a polynomial based on its roots:

r = [1 -1 4 3]
poly(r)
r =

     1    -1     4     3


ans =

     1    -7    11     7   -12

Back to top

Multiply polynomials

Polynomial multiplication corresponds to Matrix convolution, and so is done using the “conv” command in MATLAB. For example, to compute:

((x^2)-(3x)+1)((x^5)-(x^3)-1)

Enter the MATLAB commands:

p1 = [1 -3 1]
p2 = [1 0 -1 0 0 -1]
conv(p1,p2)
p1 =

     1    -3     1


p2 =

     1     0    -1     0     0    -1


ans =

     1    -3     0     3    -1    -1     3    -1

Back to top

Polynomial division

Polynomial division corresponds to matrix deconvolution and is performed with the "deconv" command:

((x^5)-(x^3)+1))/((x^2)-1)

num = [1 0 -1 0 0 1]
dem = [1 0 -1]
deconv(num, dem)
num =

     1     0    -1     0     0     1


dem =

     1     0    -1


ans =

     1     0     0     0

Back to top

Deconvolution ("deconv" command) can return both quotient and remainder

[q, r] = deconv(num, dem)
q =

     1     0     0     0


r =

     0     0     0     0     0     1

Back to top

Evaluate a polynomial at a point

The MATLAB function "polyval" will evaluate a polynomial at a point.

p
p =

     1    -7    11     7   -12

polyval(p, 4)
ans =

     0

polyval(p, 5)
ans =

    48

polyval(p, [1 2 3 4 5])
ans =

     0     6     0     0    48

Back to top

Plot

x = -2:.1:5;
plot(x, polyval(p,x))

Plot example

Back to top

Exercises

  1. Use 'polyder' to compute the derivative of a polynomial.
  2. Use 'polyint' to integrate a polynomial.
  3. Plot a polynomial and its derivative on the same axis.
  4. Find the critical points (i.e., roots of the derivative).
  5. Format plots of the polynomial, its derivative, and/or the critical points.

Back to top


Published with MATLAB® 7.4