How To Define and Solve Polynomials in Scilab: P (X) A X +a X +a X + +a X
How To Define and Solve Polynomials in Scilab: P (X) A X +a X +a X + +a X
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
--> 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:
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.
p1 = x4+3x2-2x+a
p2 = bx5 + 2x3 + x + 1
p3 = p1 + p2
p4 = p1 * p2
p5 = p2/p1