Scilab Variables – complex numbers, strings, polynomials and matrices

In this article we will learn how to define other types of Scilab variables:

  • complex numbers
  • strings
  • matrices
  • polynomials

How to install Scilab

Complex numbers

A complex number is a number which contains a pair of real numbers and it is written in the following manner:

\[ \begin{equation*} \begin{split}
c = a + b \cdot i
\end{split} \end{equation*} \]

where:
c – complex number
a – real number
b – real number
i – imaginary unit

The complex number c can be written also as a pair of real numbers like this:

\[ \begin{equation*} \begin{split}
c \left (a, b \right)
\end{split} \end{equation*} \]

Complex number extend the one-dimensional (line) domain of real numbers into a two-dimensional domain, where the complex number has on the horizontal axis the real part and on the vertical axis the imaginary part.

Graphical representation of complex numbers

Image: Graphical representation of complex numbers

Re – real axis
Im – imaginary axis

If the real number (a) is zero, the complex number is purely imaginary. Also, if the real number (b) is zero, the complex number becomes a real number.

In Scilab we define the complex numbers by using the special constant %i, in the following manner:

-->c = 2 + 3*%i
 c =
 
 2. + 3.i 
 
-->

This way we’ve defined a complex number c which has the real part 2 and the imaginary part 3i. A purelly imaginary complex number is defined like this:

-->c = 5*%i
 c =
 
 5.i 
 
-->

Polynomials

A polynomial is defined as an mathematical expression of determined length which contains operations between variables and constants (coefficients). The allowed operations within a polynomial are additions, subtractions, multiplications and positive integer exponents.

Here is an example of a polynomial:

\[ \begin{equation*} \begin{split}
P(x) = x^3 – 4x^2 + 5x^1 – 2
\end{split} \end{equation*} \]

where:
P – polynomial
x – variable (unknown or indeterminate)
4, 5, 2 – constants (coefficients)
3, 2, 1 – positive integer exponents

A variable form a mathematical polynomial is not the same thing as a Scilab variable. In fact a Scilab variable defined as a polynomial contains variables (unknowns or indeterminate) and constant numbers.

In Scilab, in order to define a polynomial, we have to use the poly(arg1,arg2,arg3) function. With this function we can define a polynomial in two ways:

1. By supplying the roots of the polynomial. In this case the function will be called with these arguments: poly([roots], arg2, “roots”). Example with “x” being the unknown:

-->p=poly([1 2 1],"x","roots")
 p =

              2   3
 - 2 + 5x - 4x + x

-->

2. By supplying the coefficients of the polynomial. In this case the function will be called with these arguments: poly([coefficients], arg2, “coeffs”). Example with “x” being the unknown:

-->p=poly([-2 5 -4 1],"x","coeffs")
 p =

              2   3
 - 2 + 5x - 4x + x

-->

Strings

In Scilab, the variables of type string stores a character or a set of characters (words). Any of the ASCII characters can be used as values for string variables. In order to define in Scilab a string variables we must use quotes <‘ ‘> or double quotes <” “>:

-->str1 = 'w'
 str1 =
 
 w 
 
-->str2 = "This is a string variable"
 str2 =
 
 This is a string variable 
 
-->

If the string variable contains the quotes characters, simple or double:

-->str3 = "String ""ABC"" in double quotes"
 str3 =
 
 String "ABC" in double quotes 
 
-->

Matrices and vectors

A matrix is an array of values arranged in rows and columns. If the matrix has only one column or one row it is called a vector. In fact Scilab treats also the vectors as matrices, it makes no difference from the manipulation point of view.

Matrix elements description

Image: Matrix elements description

A matrix is characterised by its content and by its dimension. In Scilab a matrix can contain scalars (real, integer, complex, boolean), strings or polynomials. It is not possible to mix the variable types in the same matrix.

The dimension of the matrix is setup automatically after we define it:

-->matrix1 = [1 2 3; 4 5 6]
 matrix1 =
 
 1. 2. 3. 
 4. 5. 6. 
 
-->

A matrix is defined inside square parenthesis <[ ]>, and in order to finish a row the dot-comma character is used <;>.

If the number of columns is equal to the number of rows the matrix is square, otherwise is rectangular:

-->square_matrix = [10 11 12 3;15 9 7 5;1 2 43 21;87 4 2 12]
 square_matrix =
 
 10. 11. 12. 3. 
 15. 9.  7.  5. 
 1.  2.  43. 21. 
 87. 4.  2.  12. 
 
-->rectangular_matrix = [1 2;3 18;6 7]
 rectangular_matrix =
 
 1. 2. 
 3. 18. 
 6. 7. 
 
-->

Depending on the content of the matrix, it’s type will be automatically set. For example, if you define all the elements within a matrix of type real also the matrix will be of type real. The examples below summarises all the matrix types:

Real matrix

-->real_matrix = [1 2;-3 4]
 real_matrix =
 
  1. 2. 
 -3. 4. 
 
-->

String matrix

-->string_matrix = ["a" "abc";"hello" "_A"]
 string_matrix =
 
!a     abc !
!          !
!hello _A  !
 
-->

Boolean matrix

-->boolean_matrix = [%f %f;%t %f]
 boolean_matrix =
 
 F F 
 T F 
 
-->

Polynomial matrix

-->poly_matrix = [%s*2 %s+1;%s %s+3]
 poly_matrix =
 
 2s   1 + s 
 
 s    3 + s 
 
-->

By now you should be able to define the major part of Scilab variables. There are still some more advanced data structures which we can also treat as variables. These data structures are lists and cell and we will discuss about them in the next article.

For any questions, observations and queries regarding Scilab variables use the comment form below.

Don’t forget to Like, Share and Subscribe!

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh