You are on page 1of 6

COMPUTER SERVICES CENTRE,IIT DELHI

SCILAB Exercise- 3

Polynomials

How to define and solve polynomials in Scilab


Scilab comes with a built-in function to define polynomials. The Scilab
function for polynomials definition is poly(). Depending on the options of
the function, the polynomial can be defined based on its coefficients or
its roots.
The generic definition of a polynomial is:

p(x)=a0x0+a1x1+a2x2+…+anxn
where:

an – real numbers (an ∈ R), representing the coefficients of the polynomial
x – symbolic variable of the polynomial
n – natural numbers (an ∈ N), representing the exponents of the
polynomial
The syntax of the Scilab poly() function is:
p = poly(data, 'var', 'options')
where:

data – vector or real number representing the coefficients or the roots of


the polynomial
'var' – string representing the symbolic variable name of the polynomial;
the maximum length of the string is 4 characters
'options' – string representing the type of the polynomial definition; the
possible values are predefined as:'roots' (short 'r'), default value, for
the definition of the polynomial based on its roots or 'coeff' (short'c'), for
the definition of the polynomial based on its coefficients
p – variable defined as a polynomial
Example 1. Define the polynomial which has the following roots: x1 = -
1 and x2 = 2.
--> p=poly([-1 2],'x','r')
p = 
        2
-2 -x +x 
-->
The result is the polynomial:
1
p(x)=−2−x+x2
Example 2. Define the polynomial which has the following
coefficients: a0 = 3, a1=-3, a2=-8 and a3=7.
p(x)=3−3x−8x2+7x3
The Scilab instruction is:

--> p=poly([3 -3 -8 7],'x','c')


p = 
         2   3
3 -3x -8x +7x
-->
There is also an alternative way of defining a polynomial in Scilab. We
can define in a first instance the variable of the polynomial, and
second, the polynomial, as a symbolic expression.
Variable definition:

--> x=poly(0,'x')
x =
x
-->
Polynomial definition:

--> p=-2-x+x^2
p =
        2
-2 -x +x
-->
To check if a variable is a polynomial or not, we can use the Scilab
function typeof(). If true, the return of the function will be the
string 'polynomial'.

On polynomials we can perform several mathematical operations, like


addition, subtraction, multiplication and division. The results of these
operation will be also polynomials with the exception of the division
operation, where we’ll get a rational variable.
Let’s define two polynomials, p1 and p2:
p1=poly([-1 2],'x','r');
p2=poly([3 -3 -8 7],'x','c');
Polynomial addition
2
--> p1+p2
ans =
         2   3
1 -4x -7x +7x
--> typeof(ans)
ans =
polynomial
-->
Polynomial subtraction
--> p1-p2
ans =
          2   3
-5 +2x +9x -7x
--> typeof(ans)
ans =
polynomial
-->
Polynomial multiplication
--> p1*p2
ans =
           2   3    4   5
-6 +3x +22x -9x -15x +7x
--> typeof(ans)
ans =
polynomial
-->
Polynomial division
--> p1/p2
ans =
          2
-2 - x + x
-----------------
           2    3
3 - 3x - 8x + 7x
--> typeof(ans)
ans =
rational
-->

3
The poly() function is also used to define transfer functions for dynamic
systems. The approach is to define first the symbolic variable and second
the rational function, which represents the transfer function.

In the example below we are going to define the variable s as a


polynomial, the variable H as a rational function and sys as a continuous
linear system (defined by the transfer function H).
s=poly(0,'s');
H=[1/(2*s^2+s+2)];
sys=syslin('c',H);
Finding the roots of a polynomial
In most of the cases, in mathematics, we have the polynomial defined and
we need to find its roots. The roots of a polynomial are calculated using
the Scilab function roots().
r=roots(p,'method')
where:

p – the polynomial for which we want to find the roots


'method' – a string variable defining the numerical method for finding the
roots; the default value is 'e', which means that the eigenvalues of the
companion matrix are returned; setting it to 'f', the Jenkins-Traub
method is used
r – a vector containing the roots of the polynomial, defined as complex
numbers
As example, let’s take the polynomial p2 defined above and plot
it between [-0.8 1.4]. The roots of the polynomial are the x-coordinates
where the plot crosses the horizontal axis. As you can see, our polynomial
has 3 roots, depicted in the graphical window with blue circles.

4
Image: Scilab plot for a 3rd order polynomial
To find the roots of the polynomial p2, we use the following Scilab
instruction:
--> r=roots(p2)
r =
-0.6276878 
1.2029662 
0.5675787
-->
The roots are stored in the vector r but as complex numbers, which have
the imaginary part equal to zero. To check the type of numbers of the
roots we can use the Scilab function isreal().
--> isreal(r)
ans =
F
-->
As you can see, the return of the check is False which means that the
vector is made up of complex numbers. To convert them into real
numbers we can use the Scilab function real().
--> r=real(r)

5
r =
-0.6276878
1.2029662
0.5675787
-->
If we apply the same type check, this time we’ll get a different result:

--> isreal(r)
ans =
T
-->
As you can see, the roots returned with the function roots() fit with the x-
coordinates where the plot crosses the horizontal axis. This mean that the
roots where correctly calculated.

You might also like