0% found this document useful (0 votes)
151 views

How To Define and Solve Polynomials in Scilab: P (X) A X +a X +a X + +a X

1. Scilab uses the poly() function to define polynomials based on their coefficients or roots. 2. Roots of a polynomial can be found using the roots() function, which returns complex numbers that may need to be converted to real numbers. 3. Various mathematical operations like addition, subtraction, multiplication can be performed on polynomials, while division results in a rational function.

Uploaded by

Charlyn Flores
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

How To Define and Solve Polynomials in Scilab: P (X) A X +a X +a X + +a X

1. Scilab uses the poly() function to define polynomials based on their coefficients or roots. 2. Roots of a polynomial can be found using the roots() function, which returns complex numbers that may need to be converted to real numbers. 3. Various mathematical operations like addition, subtraction, multiplication can be performed on polynomials, while division results in a rational function.

Uploaded by

Charlyn Flores
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

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−8x +7x 2 3

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
--> 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
-->
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.

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)
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.

ACTIVITY (INDIVIDUAL) (1hour)


Given the following polynomials:

p1 = x4+3x2-2x+a

p2 = bx5 + 2x3 + x + 1

p3 = p1 + p2

p4 = p1 * p2

p5 = p2/p1

Where a is the 2nd to the last letter of your id number

Where b is the number of letters of your first name

Make a table to summarize properties of each polynomial

Polynomial Degree Roots Type of ans


p1 = …
p2 = …
And so on…

You might also like