Thursday, November 10, 2011

Polynomials in Scilab

Creating polynomials in Scilab and exploring operations on them.

A polynomial equation can be written as follows:

a0 + a1 x + a2 x2 + ... + an-1 xn-1 + anxn = 0

On the left hand side is the polynomial of order 'n', in terms of the independent variable 'x'. A polynomial of order 'n' requires 'n+1' coefficients ai, i = 0 to n. When the polynomial is equated to zero, it becomes a polynomial equation. A polynomial equation of order 'n' has exactly 'n' roots. The roots may be real or complex numbers. Polynomial evaluates to zero at the roots.

Polynomials are a built-in data type in Scilab. These are the operations that you are likely to perform on polynomials:
  1. Create a polynomial and query its attributes such as coefficients and independent variable
  2. Find the roots of a polynomial equation
  3. Evaluate a polynomial for a given value of the independent variable
Creating a Polynomial - Using Roots of the Polynomial Equation
Let the roots of the polynomial equation be 2 and 3. That is, the equation is of order 2. It can be created with the following statement:
-->p = poly([2, 3], 'x')
p  =
    6 - 5x + x
Input in the above example are:
  1. Array containing the roots of the polynomial equation
  2. String containing the name of the independent variable
The statement above can also be written as:
-->p = poly([2, 3], 'x', 'r')
ans  =
    6 - 5x + x
The third argument is a string with the character 'r' which means the first argument is an array containing the roots of the polynomial equation. If the third argument is not present, it defaults to 'r', as in the first statement.

Creating a Polynomial - Using Coefficients of the Polynomial Equation
From the above example, we se that the coefficients of the polynomial equation with the roots 2 and 3 are 6, -5 and 1 arranged in increasing order of the power of 'x', starting with 0. Let us now create another polynomial for the same polynomial equation:
-->p1 = poly([6, -5, 1], 'x', 'c')
p1  =
    6 - 5x + x

Creating a Polynomial - From a Seed
This is a third way to create a polynomial seed and then using it to construct the polynomial equation:
-->p2 = poly(0, 'x')
p1  =
Here 'p2' is a polynomial in 'x'. It can be used to construct the same polynomial as in the above two examples:
-->p3 = 6 - 5*p2 + p2**2
p3  =
    6 - 5x + x

Attributes of a Polynomial - Coefficients and Independent Variable
Functions coeff() and varn() return the coefficients and the variable of a polynomial, respectively.
ans  =
   6.   -5.   1.
ans  =

Operations on a Polynomial - Finding the Roots
Function roots() finds the roots of a polynomial equation, as follows:
ans  =
Finding the roots of 'p1' and 'p3' results in an identical answers.

Operations on a Polynomial - Evaluating the Polynomial
Function horner() evaluates the polynomial for the given value/values of the independent variable. If the input is a single number, polynomial is evaluated for that value of the independent variable. If the input is a vector, polynomial is evaluated at each value in the vector, and returns a vector as the result.
-->horner(p, 2)
ans  =
-->horner(p, [2, 3])
ans  =
   0.   0.
-->horner(p, 4)
ans  =

Operations on a Polynomial - Addition, Subtraction, Multiplication, Division
All the above operations are defined for polynomials and work as expected. Check out these examples:
-->p + p2
ans  =
  12 - 10x  + 2x
-->p - p1
ans  =
-->p * p1
ans  =
                   2      3    4
    36 - 60x  + 37x  - 10x  + x
--> p4 = p / p1
p4  =
ans  =
The division operation results in a rational while the rest result in polynomials.

Some other operations on polynomials not discussed above include finding the companion matrix of a polynomial. The companion matrix A of a given polynomial p is a matrix with characteristic equation equal to the the given polynomial p.

To learn more. buy my document on Lulu Support independent publishing: Buy this e-book on Lulu.

No comments:

Post a Comment

Your comments are ciritical input for the sustainance and improvement of this blog.