This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Session 7: Working with Polynomials


Objectives

In this session we will learn the following:

  • Defining a polynomial
  • Performing operations on polynomials
  • Finding roots of a polynomial

Defining a Polynomial

A polynomial in x, of order n can be defined as:

p(x) = a0 + a1 x + a2 x2 + ... + an xn

Such a polynomial has n roots and n+1 coefficients, namely, a0, a1, a2, ..., an.

Polynomial is a built-in data type in Scilab. A polynomial can be defined in one of several ways.

Defining a Polynomial in terms of its Roots

If we know the roots of the polynomial we wish to define, then

-->p = poly([2 3], 'x')
 p  =
             2
   6 - 5x + x

or, alternately

-->p = poly([2 3], 'x', 'r')

Here we define p as a polynomial in x, having the roots specified in the vector [2 3]. This means p is a quadratic polynomial in x, and therefore will have three coefficients. Note that the coefficients are arranged in the order coefficient of x^0 (constant, x^1, x^2 and so on.

Defining a Polynomial in terms of its Coefficients

If we know the coefficients of the polynomial, then

-->p1 = poly([6  -5  1], 'x', 'c')
 p1  =
             2
    6 -5x + x

This is the same polynomial we defined above, having the roots [2 3].

Shortcut for p(x) = x

-->p = poly([0 1], 'x', 'c')
 p  =
   x

This can be created in another way.

-->p = poly(0, 'x')

This can be used to define a polynomial, much the same way as we write a polynomial on paper:

-->x = poly(0, 'x')
-->p = 6 - 5*x + x^2
 p  =
            2
   6 -5x + x
-->roots(p)
 ans  =
    2
    3
-->coeff(p)
 ans  =
    6    -5    1

Operations on Polynomials

Operations on polynomials include addition, subtraction, multiplication with a scalar, multiplication with a polynomial, division by a scalar, division by a polynomial.

Let us try the following:

-->p1 = poly([2 3], 'x'); p2 = poly([6 -5 1], 'x', 'c');
-->p3 = p1 + p2, p4 = p1 - p2
 p3  =
                 2
    12 - 10x + 2x
-->p1 - p2
 ans  =
    0
-->2 * p1
 ans  =
                 2
    12 - 10x + 2x
-->p1 * p2
 ans  =
                  2      3    4
    36 - 60x + 37x  - 10x  + x
-->p1 / 2
 ans  =
                   2
    6 - 2.5x + 0.5x
-->p = p1 / p2
 ans  =
     1
     -
     1
-->typeof(p)
 ans  =
rational

Note that dividing a polynomial by another polynomial results in a rational. Here in this case the numerator and denominator are identical and hence result is 1. If the polynomials are of different order, the result will contain x.

-->poly([2 3 5], 'x') / poly([4 3], 'x')
 ans  =
             2
  10 - 7x + x
  -----------
     -4 + x

It is an error to attempt polynomial operations on two polynomials of different variables. For example, if p1 is a polynomial in x and p2 is a polynomial in y, adding the two polynomials results in an error.

-->poly([2 3], 'x') + poly([2 3], 'y')
                                    !--error 144
Undefined operaion for the given operand

Boolean operaions are also be used

-->p1 == p2 // equal to
 ans =
  T
-->p1 ~= p2 // not equal to
 ans  =
  F

Coefficients, Roots and Derivatives

Other operations on a polynomials include extracting its coefficients, computing its roots, determining its derivative.

  1. coeff(p) returns a row vector containing the coefficients of the polynomial.
  2. roots(p) returns a column vector containing the roots of the polynomial.
  3. companion(p) returns the companion matrix (a matrix whose characteristic equation is the given polynomial) of the polynomial
  4. derivat(p) returns a polynomial which is the derivative of p

If c is the companion matrix of the given polynomial, spec(c) are the eigenvalues of the companion matrix c, which are the same as the roots of the characteristic equation (which is the given polynomial p).

New Functions used in this Session

poly(), coeff(), roots(), companion(), derivat(), spec()

Task Discussion