/Advanced Level/Polynomials/

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Polynomials in Scilab

Author: Anuradha Amrutkar

Keywords: scilab, polynomials


Visual Clue
Narration
1st Slide Welcome to the spoken tutorial on Polynomials in Scilab.

By using Scilab you can create polynomials, find their roots and perform operations on them such as addition, subtraction, multiplication, division, simplification, etc.

Scilab Console Please open Scilab Console window to practise this tutorial
Scilab Console and type
-->x = poly(0, 'x')
 x  =
   x
Let us create a polynomial in x with one root at zero. It can be done using the following command
Type
-->p = 1+x+2*x^2
  p  =
             2  
   1 + x + 2x   
This variable x can now be used to define another polynomial in x using
Now type
-->p1 = poly([3 2], 'x')
or

type

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

6 - 5x + x

We can directly define a polynomial by specifying all its roots. For example Let us create a polynomial p1 having roots 3 and 2, and x being the symbolic variable. The polynomial is p1 = 6−5x+x^2.In the above polynomial p1 it is observed that the third parameter, when supplied, may be 'r' or 'roots' in which case the first parameter is a vector containing the roots of the polynomial.
Now type
-->p2 = poly([6 -5 1], 'x', 'c') 
 p2  =
             2  

6 - 5x + x

Now we will create a polynomial p2 having coefficients 6,−5,1 assuming the symbolic variable as x again. The polynomial is p2 = 6−5x+x^2.

In the above polynomial p2 which is same as p1, it is observed that, the third parameter, when supplied, may be 'c' or 'coeff' in which case the first parameter is a vector containing the coefficients of the polynomial, starting from the constant as the first element and power of the symbolic variable increasing by one for each element in the vector.

Switch Back to 2nd Slide Thus, the polynomial with two roots is a polynomial of order two. Similarly, a polynomial with three coefficients is also a polynomial of order two.

When the third parameter is not supplied, it defaults to 'r' or 'roots'.

Type
-->p1 
 p1  =
             2  

6 - 5x + x

It is possible to perform a number of operations on polynomials, such as, find its roots, add, subtract, multiply, divide and simplify.

Recall p1 by typing

-->roots(p1)
 ans  =
   2.  

3.

the roots of p1 can be obtained as:
type
-->p2
 p2  =
             2  
   6 - 5x + x  
Also recall p2
type
-->p3 = p1 + p2  
 p3  =
                2  

12 - 10x + 2x

Now add the two polynomials p1 and p2 and store the result in the polynomial p3.

Subtraction can be performed in a similar way.

type
-->p4 = p1 * p2 
p4  =
                2     3   4  

36 - 60x + 37x - 10x + x

Product of two polynomials is also a polynomial, and is calculated using the multiplication operator (*).



type
-->q1 = 1+x
  q1  =

1 + x

We will define another polynomial say q1 as
type
-->q2 = 1+2*x+x^2 
  q2  = 
              2  
   1 + 2x + x 
and q2 as
type
-->q = q1 / q2 
  q  =
     1     
   -----   

1 + x

Dividing a polynomial with another polynomial is done as shown



type
-->q3 = 1+3*x+2*x^2
  q3  =
              2  

1 + 3x + 2x


also let us define another polynomial say q3 as
type
-->q1/q3
  ans  =
     0.5     
   -------   

0.5 + x

and divide q1 by q3 as



type
-->derivat(p1)
 ans  =

- 5 + 2x

The derivative of the polynomial p1 can be obtained using the derivat command



type
-->derivat(p1*p2)
  ans  =
  − 60 + 74x − 30x^2 + 4x^3


Also we can find derivative of p1*p2



type
-->coeff(p1)
 ans  =

6. - 5. 1.

The coefficients of the polynomial p1 can be found out using the coeff command as
type
-->horner(p1,0)
 ans  =
   6.  

type

-->horner(p1,[0 1 2])
 ans  =

6. 2. 0.

To evaluate a polynomial p1 at ' 0 ' or at set of values ' [0 1 2] ' the horner command is used.



type
-->p3
p3  =
                2  

12 - 10x + 2x

We will now see how to find factors of a polynomial

For this purpose the factors() command is used.

This command performs numeric real factorization.

Let us see an example to illustrate this concept.

Let us Recall p3 which was p1+p2 and

type
-->factors(p3)
ans  =
      ans(1)
 - 2 + x   
      ans(2)
 - 3 + x   
find its factors using the factors() command
Type
-->q3
  q3  =
              2  

1 + 3x + 2x

Also let us recall q3 and find its factors
type
-->factors(q3)
  ans  =
      ans(1)
   0.5 + x   
      ans(2)
   1 + x   
type

--> s = %s;

Another important feature that scilab provides is partial fraction decomposition of the linear system. Let us see an example


Let us declare 's' as symbolic variable with the command

Slide 3 Please note that defining symbolic variable using the above command works only for variable s and z.

But the command 'poly' used at the start of the tutorial to define a polynomial in x, works for any variable.

Scilab Console and type


-->num =1;
-->den =s^2+3*s+2;
Now we will define a transfer function with numerator and denominator abbreviated as 'num' and 'den' respectively.
Type

-->tf=num/den

 tf  =
       1        
   ---------    
             2  
   2 + 3s + s  
Let us now define a variable say 'tf' in the form of fraction

and

type
-->pfe=pfss(tf)
 pfe  =
      pfe(1)
     1     
   -----   
   1 + s   
pfe(2)
   - 1     
   -----   
   2 + s   
perform the partial fraction decomposition operation on 'tf' using the 'pfss() command as
type
-->orig = pfe(1)+pfe(2)
  orig  =
       1        
   ---------    
             2  
   2 + 3s + s   
Let us retrieve the original transfer function by adding the output obtained from the pfss() command


which is exactly same as the transfer function we defined using the num and den variables.

Slide 4 Let us now discuss how to find poles and zeros of a transfer function.

Consider a Second Order Transfer Function with no zeros of the form

<math> g(s)=\frac{n_0}{d_2s^2 + d_1s + d_0} </math>

dividing numerator and denominator by d2 gives

<math> g(s)=\frac{\frac{n_0}{d_2}}{s^2 + \frac{d_1}{d_2}s + \frac{d_0}{d_2}} </math>

Equating the RHS of the above equation to the standard Second Order System

<math> g(s)=\frac{k}{s^2 + 2\zeta\omega_n s + \omega_n^2} </math>

Slide 5 We obtain

<math> \omega_n^2 = \frac{d_0}{d_2}</math>

and

<math> 2\zeta\omega_n = \frac{d_1}{d_2}</math>

which help to solve for <math> \omega_n </math> and <math> \zeta </math>

In Scilab 'trfmod()' with option 'f' gives

Numerator <math> k=\frac{n_0}{d_2} </math>

and Denominator <math> \omega_n </math> and <math> \zeta </math>

Slide 6 Consider an example

<math> g(s) = \frac{5}{2s^2+3s+8} </math>

we get this equal to

<math> g(s) = \frac{\frac{5}{2}}{2s^2+\frac{3}{2}s+4} </math>

This gives the natural frequency

<math> \omega_n = 2 </math>

and damping ratio

<math> \zeta = \frac{3}{8}= 0.375 </math>

Scilab Console type

-->g = 5/(2*s^2+3*s+8)


This brings us to the end of spoken tutorial on Polynomials in Scilab.

In this tutorial we have learnt how to create polynomials in scilab and how to perform various operations on those polynomials. There are many other functions in Scilab which will be covered in other spoken tutorials. Keep watching the Scilab links.

Slide 7 The Spoken Tutorials are part of the Talk to a Teacher project, supported by the National Mission on Education through ICT abbreviated as NMEICT given by MHRD government of India. More information on the same is available at this websitehttp://spoken-tutorial.org/NMEICT-Intro. Thanks for joining us. This is Anuradha Amrutkar signing off. Goodbye.

Contributors and Content Editors

Gyan