Scilab Book
Scilab Book
Scilab is open source software and can be downloaded for installation from the
web page of its developer organization viz.: https://scilab.org. The developer
organization is presently owned by ESI group. Scilab is supported on UNIX, Macintosh,
and Windows environments.
Just like its commercial counterpart MATLAB, in Scilab also all the types of
variables namely, real, complex, Boolean, integer, string and polynomial variables, are
considered as matrices. Another salient feature of Scilab is that it understands the
difference between real numbers and purely real complex numbers.
Scilab Advantages
It simplifies the analysis of mathematical models
It frees you from coding in lower-level languages (saves a lot of time but with some
computational speed penalties)
Provides an extensible programming/visualization environment
Provides professional looking graphs
Scilab Disadvantages
The only disadvantage of Scilab over the lower level computational programming
languages is that it being an interpreted (i.e., not a pre-compiled) language can turn out
as slow during large scale computations.
Remark: The choice of preferring Scilab over lower level computational programming
languages depends upon the requirement of its additional graphics features and
availability of inbuilt programs over the scale of the data of the problem to be solved.
This window is the default layout of the Scilab desktop. It is a set of tools for
managing files, variables, and applications associated with Scilab.
1. The Console is a command window used for entering Scilab functions and other
commands at the command line prompt appearing as -->
C:\Users\Maths50\Scilab\yourname
This should always be done at the start of a new session. When you open a Scilab
document, it opens in the associated tool. If the tool is not already open, it opens
when you open the document and appears in the position it occupied when last
used. Figures open undocked, regardless of the last position occupied.
5. Scilab provides a variable browser, which displays the list of variables currently
used in the environment.
6. The Editor, named as SciNotes, is used to access and edit Scilab program files. The
Scilab program files are called script files. The editor can be accessed from the menu
of the console, under the Applications > SciNotes menu, or from the console, as
presented in the following session.
--> editor()
7. Script Files: Script files are normal ASCII (text) files that contain Scilab commands.
There are two types of script files in Scilab, namely, programs and functions. It is
essential to suffix an appropriate extension name after these files. Extension name
for program files is “.sce” (e.g., scriptname.sce) and for functions is “.sci” (e.g.,
functionscript.sci).
-->exec('D:\Puducherry\Class_2021_10_12_for_Loop.sce', -1)
This execution can alternatively be done by pressing the function key F5 while
keeping the the script file in SciNotes as active Window.
Example:
--> 2+3/4*5
ans =
5.7500
Note that in this calculation the result was 2+(3/4)*5 and not 2+3/(4*5) because
Scilab works according to the priorities of operations given in the following order.
1. quantities in brackets ( )
2. powers or exponent ^ or **
3. multiplication *, left division /, and right division \, working left to right
4. addition + and subtraction -, working left to right
Assignment Operator
The assignment operator “=” is used for assigning a value (or a matrix) to
variable. On the left hand side of “=” is placed a variable name to which the value on
right hand side is to be assigned. For example, let us observe the following Scilab
command demonstrating the use of assignment operator.
-->x=1
x =
1.
Variable Names
Variable names may be as long as the user wants, but only the first 24 characters
are taken into account in Scilab. For consistency, we should consider only variable
names which are not made of more than 24 characters. All ASCII letters from "a" to "z",
from "A" to "Z" and from "0" to "9" are allowed, with the additional letters "%", "_", "#",
"!", "$", "?".
Caution: Variable names for which the first letter is "%" have a special meaning in
Scilab. These represent the mathematical pre-defined variables, which are discussed in
a later section.
Variable names which are allowed and not allowed in Scilab are illustrated below.
Allowed: NetCost, Left2Pay, x3, X3, z25c5
Not allowed: Net-Cost, 2pay, %x, @sign
%e Euler’s constant 𝑒
Complex numbers
Example: The complex number 2 + 3𝑖 is input in Scilab as: 2 + 3 * %i .
Booleans
The truth value “True” is input in Scilab by using %t or %T, whereas the truth value
“False” is input using %f or %F.
-->A = "Scilab"
A =
Scilab
-->B = "Software"
B =
Software
Remarks:
The concatenation (i.e., join) of two strings can be done by using the
concatenation operator (+). The use of concatenation operator is demonstrated below
through a Scilab session.
-->"Scilab" + "Software"
ans =
ScilabSoftware
-->A+B
ans =
ScilabSoftware
ans =
Scilab Software
-->a = 1
a =
1.
-->b = 2
b =
2.
-->c = a + b
c =
3.
-->"The sum of " + string(a) + " and " + string(b) + " is " +
string(c) + "."
ans =
-->x = 2
x =
2.
-->x = 2;
does not display the action performed but will keep output in the computer’s temporary
memory being used, in exactly same way as was done in the previous command. The
value of this output for the variable x will remain there in the temporary memory
(RAM) for further use, unless it changes by reassigning some other value to this variable
-->x = 2 + 3 * %i
x =
2. + 3.i
-->y = 4*x
y =
8. + 12.i
-->x = 5
x =
5.
-->y = 2*x
y =
10.
Commands which are too long to be typed in a single line can be continued in
multiple subsequent lines by putting two dots at the end of each previous line. In Scilab,
In the following Scilab session in Console, we give examples of Scilab comments and
continuation lines.
-->// This is my comment .
-->x =1..
-->+2..
-->+3..
-->+4
x =
10.
Remark: At this stage of introduction of Scilab, the use of comments and continuation
lines is demonstrated here through Console, but they are more justifiably used in script
file also.
1A detailed description of these functions can be accessed from the help document of Scilab, which is
available within the software and on the website of the Scilab developers as well.
Remark: All the elementary mathematical functions listed above are discussed as real
functions or real vector valued functions. Those mathematical functions which are
extended from real to complex functions (for example, trigonometric, exponential and
logarithmic functions) have same function names. This means that, if their input
argument is a complex number, the same function returns the output as a complex
number, behaving as a complex function. This feature of Scilab is phrased as
“elementary functions in Scilab are overloaded for complex numbers”. The following
Scilab session illustrates this feature.
-->y = sin(%pi/2)
y =
1.
w =
9.1544991 - 4.168907i
Some other functions provided in Scilab which help managing complex numbers are
provided below.
--> z = 2 + 3 * %i
2. + 3.i
-->x = real(z)
x =
2.
-->y = imag(z)
y =
3.
-->z1 = imult(z)
z1 =
- 3. + 2.i
-->isreal(z)
ans =
-->isreal(2)
ans =
Remark: Scilab distinguishes between real and purely real complex numbers. This
feature can be verified by appropriately using the isreal function as demonstrated
below.
-->z2 = 2 + 0 * %i
z2 =
2.
-->isreal(z2)
ans =
ans =
Booleans
Some comparison operators and logical connective operators available in Scilab
are described given below.
Comparison operators
The use of all the operators is demonstrated through a Scilab session in Console, as
given below.
-->y = 2;
-->z = x + y;
z =
3.
-->z==3
ans =
-->x<y
ans =
-->x<=y
ans =
-->x>z
ans =
-->x>=1
ans =
-->x~=1
ans =
-->x<>1
ans =
z1 =
-->z2 = ~z1
z2 =
Strings
String is an array of characters. To represent a string in Scilab, a set of characters
is enclosed within double quotes (for example, “A string in Scilab”).
Strings can be stored in variables by using assignment operator, just as other values
of real or complex numbers can be stored.
x =
String
-->y = "Concatenation"
y =
Concatenation
-->z = x + y
z =
String Concatenation
The Scilab in-built function string gives the output as a string of numeric
characters corresponding to any number supplied as input. Its use is demonstrated
through a Scilab session in console.
-->n = 4;
ans =
2 divides 4
Exercise 1
In the console:
1. Use assignment operator for creating and setting (assigning) variables to float value,
string, Boolean values,
2. Use of comment and continuation line,
3. Use of inbuilt Mathematical function and operators,
4. Use of pre-defined Mathematical variables,
5. Use of Booleans and comparison operators,
6. Use of complex numbers, and operations on them,
7. Use of strings and concatenation operator, and use of comparison operator ‘==’ for
comparison of two strings for checking their equality.
8. Swap the values assigned to two variables without using third variable.
Scilab works with essentially only one kind of object a rectangular, numerical
array of numbers, possibly complex, called a matrix. In some situations, 1 × 1 matrices
are interpreted as scalars and matrices with only one row or one column are
interpreted as vectors. Matrices can be introduced into Scilab in several different ways:
Scilab contains no size or type declarations for variables. Scilab allocates storage
automatically, up to available memory.
2.1 Vectors
Vectors come in two formats - row vectors and column vectors. In either case
they are lists of numbers separated by either commas or spaces.
The number of entries is known as the "length" of the vector and the entries
are called elements or components of the vector.
The entries must be enclosed by square brackets "[" and "]". Entries of a row
vector are separated by comma (,) or space. Whereas entries of a column vector
are separated by semicolon (;).
A row vector can be transposed to a column vector and vice-versa using the
transpose operator (.‟). Whereas, transpose conjugate operation be performed
by using (‟). Thus, both of these operators give the output for real vectors.
Binary operations applicable on vectors are addition (+), subtraction (-), scalar
multiplication (*), and dot product (.*).
A row (column) vector can be joined with another row (column) vector. The
same is demonstrated below.
3. 9. 6.7082039
-->v + v2
ans =
4. 12. 8.9442719
-->v - v2
ans =
- 2. - 6. - 4.472136
-->v3 = v.'
v3 =
1.
3.
2.236068
-->v4 = v2.'
v4 =
3.
9.
6.7082039
v5 =
1. 3. 2.236068 3. 9. 6.7082039
v6 =
1.
3.
2.236068
9.
6.7082039
v6 =
1. 3.
3. 9.
2.236068 6.7082039
Remark: 1. It is to be noted from the above demonstration of Scilab session that the
dimensions of vectors must agree for joining the vectors.
2. In all vector arithmetic with vectors of equal length, the operations are
carried out element-wise.
-->v
v =
1. 3. 2.236068
-->v(1)
ans =
1.
-->v(2)
ans =
3.
-->v(3)
ans =
2.236068
-->v(2) = 5
v =
The addition (+) and subtraction (-) operations in Scilab work element-wise by
giving output as addition and subtraction of corresponding elements. For example:
a =
5. 7. 9.
1. - 3. - 7.
b =
- 1. 2. 5.
9. 0. 5.
-->a+b
ans =
4. 9. 14.
10. - 3. - 2.
-->a-b
ans =
6. 5. 4.
- 8. - 3. - 12.
We shall describe two ways in which a meaning may be attributed to the product
of two vectors. In both cases, the vectors concerned must have the same length. The first
product is the standard scalar product. Suppose that u and v are two vectors of length
n, 𝑢 being a row vector and 𝑣 a column vector:
The scalar product is defined by multiplying the corresponding elements together and
adding the results to give a single number (scalar).
𝑛
𝑢∗𝑣 = 𝑢𝑖 𝑣𝑖
𝑖=1
u =
v =
20.
-21.
-22.
prod =
167
w =
2. 1. 3.
z =
7.
6.
5.
-->u*w
Inconsistent multiplication.
An error results because w is not a column vector. Recall from earlier that
transposing (with ') turns column vectors into row vectors and vice versa. So, to form
the scalar product of two row vectors or two column vectors,
-96
We shall refer to the Euclidean length of a vector as the norm of a vector; it is denoted
by the symbol . and defined by
𝑢 = 𝑢𝑖 2
𝑖=1
where 𝑛 is its dimension. This can be computed in Scilab in one of two ways examplified
below:
--> [sqrt(u*u.'),norm(u)]
ans =
19.104973 19.104973
where norm is a built in Scilab function that accepts a vector 𝑢 as input and delivers a
scalar 𝑢 as the output.
The second way of forming the product of two vectors of the same length is
known as the Hadamard product. It is not often used in Mathematics but is an invaluable
Scilab feature. It involves vectors of the same type. If u and v are two vectors of the same
type (both row vectors or both column vectors), the mathematical definition of this
product, which we shall call the dot product, is the vector having the components
𝑢. 𝑣 = ,𝑢1 𝑣1 , 𝑢2 𝑣2 , . . . . . , 𝑢𝑛 𝑣1 -
--> w.*w
ans =
4. 1. 9.
>> w.*z'
ans =
14. 6. 15.
ans =
--> a./a
ans =
1. 1. 1. 1. 1.
--> u
u =
10. -11. 12.
--> u.^2
ans =
100. 121. 144.
--> u.*u
ans =
100. 121. 144.
--> u.*w.^(-2)
ans =
Observe that powers (.^ in this case) are done first, before any other arithmetic
operation.
v = i : j
where i is the starting index and j is the ending index, with i ≤ j. This creates a row
vector
-->3:7
ans =
3. 4. 5. 6. 7.
2. The complete syntax allows configuring the increment used when generating the
index values, i.e., the step. The complete syntax for the colon operator is
v = i : s : j
Observation:
(a) If s divides j - i, then the last index in the vector of index values is j, because
in that case i + ns = j.
While, in most situations the step s is positive, it can be negative also. Following
Scilab session demonstrates all the cases discussed here.
-->v = 4 : 2 : 10
v =
4. 6. 8. 10.
-->v = 3 : 2 : 10
v =
3. 5. 7. 9.
-->v = 10 : -2 : 4
v =
10. 8. 6. 4.
-->v = 10 : -2 : 3
v =
10. 8. 6. 4.
-->eye(2, 2)
-->linspace(1,2,5)
ans =
-->linspace(1+%i,2+2*%i,5)
ans =
-->linspace([1:4]',[5:8]',5)
ans =
1. 2. 3. 4. 5.
2. 3. 4. 5. 6.
3. 4. 5. 6. 7.
4. 5. 6. 7. 8.
square brackets "[" and "]"mark the beginning and the end of the matrix,
commas "," or spaces separate the values on different columns,
semicolons ";" separate the values of different rows.
The following syntax can be used to define a matrix, where blank spaces are
optional (but make the line easier to read) and "..." are designing intermediate values:
A = [a11, a12, ... , a1n; a21, a22, ... , a2n; ...; an1, an2,
... , ann]
--> A = [1 2 3; 4 5 6; 7 8 9]
--> A = [1 2 3
--> 4 5 6
--> 7 8 9]
create the same matrix and assign it to the variable A. Scilab responds to this
command by storing in the following matrix against the variable name A and displaying
the same in Console.
A =
1 2 3
4 5 6
7 8 9
Scilab always prints out the variable in the executed line (which can be very
awkward and time consuming for large arrays) unless you end the line with a semicolan
(;). It is good practice to use the semicolan at the end of the line. You can always ask
Scilab to print the matrix later also by calling the variable name, as shown below.
--> A
A =
1 2 3
4 5 6
7 8 9
Identity matrix
-->eye(2, 2)
ans =
1. 0.
0. 1.
Remark: For the case of 𝑚 ≠ 𝑛, the Scilab command eye(m, n)creates a rectangular
matrix with m rows and n columns with the identity matrix of order = min*𝑚, 𝑛+ and
additional rows or columns as zeros. For example:
ans =
1. 0. 0.
0. 1. 0.
-->eye(3, 2)
ans =
1. 0.
0. 1.
0. 0.
-->ones(2, 3)
ans =
1. 1. 1.
1. 1. 1.
-->zeros(2, 3)
ans =
1. 1. 1.
1. 1. 1.
Random matrix
--> rand(5, 5)
ans =
--> rand(3,4)
ans =
0.7577 0.6555 0.0318 0.0971
0.7431 0.1712 0.2769 0.8235
0.3922 0.7060 0.0462 0.6948
a =
5. 7. 9.
1. - 3. - 7.
b =
- 1. 2. 5.
9. 0. 5.
-->a+b
ans =
10. - 3. - 2.
-->a-b
ans =
6. 5. 4.
- 8. - 3. - 12.
The dot product works as for vectors: corresponding elements are multiplied
together - so the matrices involved must have the same size.
b =
-1 2 5
9 0 5
--> a.*b
ans =
-5 14 45
9 0 -35
-->a.*c
!--error 9999
Inconsistent element-wise operation
-->a.*c'
ans =
0 21 36
1 6 -14
Product of matrices
To form the product of an m × n matrix A and a n × p matrix B, written as AB, we
visualize the first matrix (A) as being composed of m row vectors of length n stacked on
Check that you understand what is meant by this definition by taking through the
following examples.
a =
5 7 9
1 -3 -7
b =
0 1
3 -2
4 2
--> c=a*b
c =
57 9
-37 -7
--> d=b*a
d =
1 -3 -7
13 27 41
22 22 22
--> e=b'*a'
e =
57 -37
9 -7
We see that e = c’ suggesting that (a * b)' = b' * a' Why is b * a a 3 × 3 matrix while a * b is
2 × 2?
A =
1. 2.
3. 4.
-->B = [2, 5, 4; 5, 6, 7]
B =
2. 5. 4.
5. 6. 7.
-->A\B
ans =
1. - 4. - 1.
-->inv(A)*B
ans =
1. - 4. - 1.
𝐴𝑥 = 𝑏.
𝑥1 + 2𝑥2 = 3
3𝑥1 + 4𝑥2 = 7
1 2 𝑥1 3
the matrix form is 𝐴𝑥 = 𝑏, where 𝐴 = , 𝑥 = 𝑥 , and 𝑏 = .
3 4 2 7
As determinant of 𝐴 is -2, which is non-zero, therefore the matrix 𝐴 is invertible. Hence,
the solution of this system of equations can be obtained as 𝑥 = 𝐴−1 𝑏. This is can be
solved through the following Scilab session.
2Solving the system of equations will be discussed again in detail in a later chapter and there this
operator would be utilized as a part of the complete discussion.
A =
1. 2.
3. 4.
-->b = [3; 7]
b =
3.
7.
-->x = A\b
x =
1.
1.
Alternatively, the same can be achieved by using the in-built Scilab function inv for
inverse of a matrix.
-->x = inv(A)*b
x =
1.
1.
-->C = [2, 3; 4, 5; 6, 7]
C =
2. 3.
4. 5.
6. 7.
-->D = [1, 2; 3, 4]
1. 2.
3. 4.
-->C/D
ans =
0.5 0.5
- 0.5 1.5
- 1.5 2.5
-->C*inv(D)
ans =
0.5 0.5
- 0.5 1.5
- 1.5 2.5
Transpose of a matrix
Just like on vectors, the transpose of a matrix is obtained by the command (.‟)
and the transpose conjugate of a matrix is obtained by the command (‟). For example:
-->A
A =
1. 2. 3.
4. 5. 6.
-->A'
ans =
1. 4.
2. 5.
3. 6.
-->A.'
ans =
1. 4.
2. 5.
Query Matrices
We can get the size (or order or dimensions) of a matrix with the command
size. The size function returns the two output arguments nr and nc, which are the
number of rows and the number of columns.
nr = size(A ,sel)
which allows to get only the number of rows or the number of columns and where sel
can have the following values
In the following session, we use the size function in order to compute the total number
of elements of a matrix.
For a matrix which is already defined and assigned to the variable A, the whole matrix
can be assessed by calling the same by its variable name. For exampe,
For a matrix which is already defined and assigned to the variable A, the element in row
number i and column number j of the matrix A can be assessed by calling it through
the syntax A(i, j). For exampe,
-->B = [1, 2, 3; 4, 5, 6]
B =
1. 2. 3.
4. 5. 6.
-->B(2, 3)
For a matrix which is already defined and assigned to the variable A, its submatrix can
be accessed by specifying the indices for row numbers and column numbers as row-
vectors. For the vector of row indices is defined as u and vetcor for column indices is v,
the submatrix of matrix A can be assessed by calling it through the syntax A(u, v). For
exampe, for a matrix B (defined below), the submatrix of with entries of 2nd and 4th row
which lie in 1st and 4th coulmn can be accessed by the commands demonstrated in the
following Scilab session.
1. Colon operator can also be used for defining the above discussed vector of indices.
For example, the submatrix of the above mentioned matrix B with entries from the
first three rows which lie in 1st, 3rd and 5th column can be accessed by following the
following Scilab command.
-->B(1:3, 1:2:5)
ans =
1. 3. 5.
6. 8. 10.
11. 13. 15.
For example, for the matrix B mentioned above, complete 2nd column can be
accessed through the command
-->B(:, 2)
ans =
2.
7.
12.
17.
3. The order in which indices are placed in the index vector u or v for accessing rows
and columns, corresponding rows of the submatrix are arranged in the output
obtained through the command A(u, v). For example, for the matrix B mentioned
above, observe the output obtained through the following command
The command B(u, :) gives the complete 1st and 2nd rows but in reverse order, as
the vector u depicts the row indices 2 first and 1 second.
The dollor operator ($) enables to refer to the last row index or last column
index of a matrix without manually finding those. For example, if A is a matrix of order
𝑚 × 𝑛, then for referring to the elements of matrix A while counting the indices in
decending order starting from the last one, can be done through the following
commands.
Command Description
A(i, $) the element of matrix A in row i and the last column (i.e., column n)
A($, j) the element of matrix A in the last row (i.e., row m) and column i
A(i, $-j) the element of matrix A in the row i and the column (n - j)
A($-i, $-j) the element of matrix A in the row (m - i) and the column (n - j)
These commands are illustrated below through the following Scilab session.
-->A = [1, 2, 3; 4, 5, 6]
A =
1. 2. 3.
4. 5. 6.
-->A(2, $)
ans =
6.
-->A($, 1)
ans =
4.
-->A($, $)
ans =
6.
-->A($-1, $-2)
ans =
1.
First four functions on matrices introduced in the table given above are self
explanatory, whereas the spec function requires little elaboration through practical
demonstrations through a Scilab session as given below. For this demonstration we
have take simple most examples of matrices whose eigenvalues and eigenvectors can be
identified at the first look due to known properties of linear algebra.
-->A = eye(2, 2)
A =
1. 0.
0. 1.
3Here only some of the in-built functions have been listed. Interested readers may refer to the help
document of Scilab for exploring more in-built functions available in Scilab.
diagevals =
1. 0.
0. 1.
R =
1. 0.
0. 1.
-->spec(A)
ans =
1.
1.
-->evals = spec(A)
evals =
1.
1.
-->B = [2, 0; 0, 3]
B =
2. 0.
0. 3.
-->spec(B)
ans =
2.
3.
diagevals =
2. 0.
0. 3.
R =
1. 0.
0. 1.
-->B = [2, 0, 0; 0, 2, 0; 0, 0, 3]
B =
2. 0. 0.
0. 2. 0.
0. 0. 3.
-->[R,diagevals] =spec(B)
diagevals =
2. 0. 0.
0. 2. 0.
0. 0. 3.
R =
1. 0. 0.
0. 1. 0.
0. 0. 1.
The discussion on matrices is vast and it is impossible to list its all features and
ways to deal them in Scilab. The demonstrations made in this chapter are sufficient to
explain the fundamentals for all the basic dealings of matrices. This makes it
appropriate to conclude this chapter here to the scope of this book. For advanced topics,
readers are suggested to refer to the help document of Scilab.
Scilab is a software which enables the users to work more freely for utilizing its
available computing functionalities by developing their own programs for any method
or algorithm. Scilab has its own programming syntax for various statements which are
required for any programming language. Among foremost fundamental statements of
such a programming language are branching statements and looping statements. In this
chapter the syntax and use these statements are discussed in the context of Scilab as a
programming language.
The if statement
The syntax of “if statement” in Scilab as given above, first evaluates the
condition given through the expression “logical test”. If the output of this
evaluation comes out with Boolean value “True”, then the Scilab commands written
between the keywords then and end are executed sequentially. Whereas, for the case,
when the output of this evaluation is comes out with Boolean value “False”, then
nothing is executed.
Let us understand this concept through a small program coded in Scilab editor
“SciNotes”.
clear
r = input("Enter a real number ")
if (r == 0) then
disp("The given number is equal to zero.")
end
The output of the above program upon execution is demonstrated below for two
different inputs.
Output:
-->exec('D:\SCILAB\Ch3_1_Ex1_if_Statement.sce', -1)
Enter a real number 0
The given number is equal to zero.
-->exec('D:\SCILAB\Ch3_1_Ex1_if_Statement.sce', -1)
Enter a real number 2
-->
It is to be noted here that in case of first execution of the program, as the input is
number zero, therefore a message is displayed due to the logical test (r == 0) having
truth value “True”. Whereas, when in second execution of the program a non-zero
number is input, nothing appears as a message, because there is no command to be
executed if the logical test gives truth value “False”.
This statement allows executing exactly one out of two desirable sequences of
actions depending on if a certain logical condition gets satisfied or not. The syntax for
this statement is
The syntax of “if-else statement” in Scilab as given above, first evaluates the
condition given through the expression “logical test”. If the output of this
evaluation comes out with Boolean value “True”, then the Scilab commands Scilab
Command A1... written between the keywords then and else are executed
sequentially. Whereas, for the case, when the output of this evaluation is comes out with
Boolean value “False”, then the Scilab commands Scilab Command B1... written
between the keywords else and end are executed sequentially.
Let us understand this concept through another small program coded in Scilab
editor “SciNotes”.
Example 3.2: The following program having an “if-else statement” tests whether a
given real number is equal to zero. It then displays a message for the case when output
of the logical test is “True” (i.e., when the number is equal to zero), whereas, in case of
output of the logical test “False”, displays another message.
clear
r = input("Enter a real number ")
if (r == 0) then
disp("The given number is equal to zero.")
else
disp("The given number is not equal to zero.")
end
The output of the above program upon execution is demonstrated below for two
different inputs.
Output:
-->exec('D:\SCILAB\Ch3_1_Ex2_if_else_Statement.sce', -1)
Enter a real number 0.001
The given number is not equal to zero.
-->exec('D:\SCILAB\Ch3_1_Ex2_if_else_Statement.sce', -1)
Enter a real number 0
The given number is equal to zero.
-->
A branching statement with multiple logical tests can also be used sequentially
for execution of different sequences of commands for different outcomes of logical tests.
Such a statement is explained below.
This statement allows multi-stage testing for execution of exactly one sequence
of actions, among many, depending on which combination of logical conditions gets
satisfied or dissatisfied. A basic syntax for this statement is
if (logical test 1) then
Scilab Command A1
Scilab Command A2
...
elseif (logical test 2) then
Scilab Command B1
Scilab Command B2
...
else
Scilab Command C1
Scilab Command C2
...
end
The syntax of “if-elseif-else statement” in Scilab as given above, first
evaluates the condition given through the expression “logical test 1”. If the
output of this evaluation comes out with Boolean value “True”, then the Scilab
commands Scilab Command A1... written between the keywords then and
elseif are executed sequentially. Whereas, for the case, when the output of this first
evaluation is comes out with Boolean value “False”, then further the expression
“logical test 2” is evaluated. If the output of this second evaluation comes out
with Boolean value “True”, then the Scilab commands Scilab Command B1...
written between the keywords elseif and else are executed sequentially. Whereas,
for the case, when the output of this second evaluation is comes out with Boolean value
“False”, then the Scilab commands Scilab Command C1... written between the
keywords else and end are executed sequentially.
Let us understand this concept through a small program coded in Scilab editor
“SciNotes”.
The output of the above program upon execution is demonstrated below for three
different inputs.
Output:
-->exec('D:\SCILAB\ Ch3_1_Ex3_if_elseif_else_Statement.sce', -
1)
Enter a real number 2
The given number is positive.
-->exec('D:\SCILAB\ Ch3_1_Ex3_if_elseif_else_Statement.sce', -
1)
Enter a real number -5
The given number is negative.
-->exec('D:\SCILAB\ Ch3_1_Ex3_if_elseif_else_Statement.sce', -
1)
Enter a real number 0
The given number is equal to zero.
-->
A branching statement with multiple logical tests can also be used sequentially
for execution of different sequences of commands for different outcomes of logical tests.
Such a statement is explained below.
Let us understand this concept through a small program coded in Scilab editor
“SciNotes”.
Example 3.4: The following program uses the “select statement” to check multiple
test condition and executes the Scilab commands accordingly.
clear
r = input("Enter the Roll Number ")
select r
case 1 then
disp("Your Roll Number is 1.")
case 2 then
disp("Your Roll Number is 2.")
else
disp("Entered Roll Number is not in the list.")
end
The output of the above program upon execution is demonstrated below with two
different inputs.
Output:
-->exec('D:\SCILAB\Ch3_1_Ex4_select_Statement.sce', -1)
Enter the Roll Number 1
Your Roll Number is 1.
Exercise 3.1
1. Write a Scilab program to test whether a given number divides the other given number.
2. Write a Scilab program to test whether a given number is even or odd.
3. Write a Scilab program to test whether a given number is purely real number or a
complex number.
4. Write a Scilab program to test whether a given number is positive, negative, or zero.
5. Write a Scilab program to test whether a given number is positive, negative, or zero,
using select statement.
6. Write a Scilab program to solve a Quadratic Equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0. The
input to the function are the values “𝑎, 𝑏, 𝑐” and the output of the function should
be in the variable names “𝑝, 𝑞” appropriately declared.
for i = v
Scilab Command 1
Scilab Command 2
...
end
Syntax of the “for statement” in Scilab as given above, indicates that the
sequence of Scilab commands will be executed repeatedly sequentially for each value of
the vector v assigned to the variable i. Let us learn this concept through some
examples.
Example 3.5: The following program computes the sum of first 5 natural numbers.
Sum = 0
for i=1:5
Sum = Sum + i
end
disp("Final Sum")
disp(Sum)
In the above program coded in SciNotes, the variable i takes repeatedly and
sequentially values from the vector 1:5 and corresponding to each value taken by the
variable i, it is added to the variable Sum. Each time in the loop, the value of i is added
to the variable Sum, the variable Sum gets value of the partial sum to finally get the
desired value at the termination of the loop after variable i completing the commands
for value i=5. The output of this program is as following.
-->exec('D:\SCILAB\Ch3_2_Ex5_for_loop.sce', -1)
Final Sum
15.
Example 3.6: The following program demonstrates the sum of odd natural numbers
from 1 to 10.
Sum = 0
for i=1:2:10
Sum = Sum + i
end
disp("Sum of odd natural numbers between 1 and 10")
disp(Sum)
In the above program coded in SciNotes, the variable i takes repeatedly and
sequentially values from the vector 1:2:10 (i.e., odd numbers between 1 and 10) and
corresponding to each value taken by the variable i, it is added to the variable Sum. The
output of this program in terms of the final value of variable Sum gives the desired
results, which is depicted as following.
-->exec('D:\SCILAB\Ch3_2_Ex6_for_loop.sce', -1)
25.
In the Examples 3.4 and 3.5 discussed above, the colon operators are used
appropriately to define vectors. Even, any vector of values can in general be used in
“for loop”, as demonstrated in the next example.
Example 3.7: The following program demonstrates the sum of values of a pre-defined
vector.
Sum = 0
v = [1, 9, %e, 5]
for i=v
Sum = Sum + i
end
disp("Sum of values in the vector v:")
disp(Sum)
In the above program coded in SciNotes, the variable i takes repeatedly and
sequentially values from the vector v and corresponding to each value taken by the
variable i, it is added to the variable Sum. The output of this program in terms of the
final value of variable Sum gives the desired results, which is depicted as following.
-->exec('D:\SCILAB\Ch3_2_Ex7_for_loop.sce', -1)
17.718282
Exercise 3.2
-->exec('D:\SCILAB\Ch3_2_Ex8_while_loop.sce', -1)
Exercise 3.3
1. Write a Scilab program to find the number of digits of a natural number ‘n’.
2. Write a Scilab program to obtain a number with digits as the reverse of a given natural
number ‘n’.
3. Write a Scilab program to test whether a given number is Palindrome.
4. Write a Scilab program to test whether a given number is Armstrong number.
5. Write a Scilab program to obtain the binary equivalent of a given decimal number.
6. Write a Scilab program to obtain the decimal equivalent of a given binary number.
7. Write a Scilab program to compute sum of first ‘n’ prime numbers.
Caution: Any space should not be given after the function name and after the left
parenthesis.
There are multiple built-in functions in Scilab. Some of them we have discussed
in Section 1.4 of Chapter 1. Built-in functions are simply to be called for getting the
values of their output arguments by simply entering their correct calling sequence.
1. The code of any function program has to start with the keyword function and
end with the keyword endfunction.
2. As function programs are intended to give mathematical outputs, so the
desirable outputs are needed to be assigned to the variables written as output
arguments.
3. As function programs, once developed, are used for multiple values of inputs
arguments, therefore whichever variables are used as inputs they should be
included as input arguments.
Remarks:
1. Space must be provided after output arguments and the assignment operator
sign “=”.
2. The name of the function must abide the rules of defining a valid variable name
in Scilab.
3. User defined function names should be avoided to be same as built-in function
names.
Let us learn this through an example of a function program developed by modifying the
program in Example 3.3 in Chapter 3.
function [y]=signum(x)
if (x == 0) then
y = 0;
elseif (x > 0) then
y = 1;
else
y = -1;
end
endfunction
This function can be executed and then called for different inputs as demonstrated
below.
-->exec('D:\SCILAB\signum.sci', -1)
-->[y] = signum(5)
y =
1.
-->[y] = signum(-10)
y =
- 1.
-->[y] = signum(0)
y =
0.
1. Although, x is the input variable for the above function program but it can be
called using any other variable name also. For example, for the above function
program:
-->a = 5;
-->[y] = signum(a)
y =
1.
2. Similarly, any variable name can be used as output variable during the call of a
function program. For example, for the above function program:
-->[b] = signum(a)
b =
1.
Exercise 4
7. Write a Scilab program to find the number of digits of a natural number ‘n’.
8. Write a Scilab program to obtain a number with digits as the reverse of a given
natural number ‘n’.
10. Write a Scilab program to test whether a given number is Armstrong number.
11. Write a Scilab program to obtain the binary equivalent of a given decimal
number.
12. Write a Scilab program to obtain the decimal equivalent of a given binary
number.
One of most useful and rapid ways of analysing data is through plots and
graphics. Supplementing reports on data analysis with plots and graphics is most
prevalently used practice since long in industry, academia, and research.
Scilab provides multiple built-in features for creating and customizing various
types of plots and charts. In this chapter, we will learn about creating 2D plots, contour
plots and 3D plots. In the final section of this chapter, we will learn about graphics
features available in Scilab for customizing the plots by labelling them with the title and
and axes lables, and the legend of our graphics.
Scilab enables plotting of multiple charts like 2-dimensional plots, Contour plots,
surface (3-dimensional) plots, histograms, bar charts, etc. The precise details of each of
these can be accessed through the help document of Scilab. Details are presented here
only for some of these basic plots while keeping into consideration the learning
objectives of beginners.
Step 1. First, the function is defined as a Scilab function. For example, for obtaining
the 2D plot of the function 𝑓: ,−2, 2- → ℝ as 𝑓(𝑥) = 𝑥 2 . The function be
defined as:
function f_x=f(x)
f_x = x.^2
endfunction
(Here, the output variable of the Scilab function program is computed using the
element-wise power. The reason for the same will get clear in Step 3.)
For example, for predefined values of a, b, and n, the vector may be obtained
by the command:
xv = linspace(a, b, n)
Step 3. Then, the vector of corresponding image values can be obtained by calling the
function defined in Step 1. For example,
yv = f(xv)
Example 5.1: Let us sequence these commands as a Scilab program to obtain the plot of
the function 𝑓(𝑥) = 𝑥 2 , as defined in Step 1.
Program:
function f_x=f(x)
f_x = x.^2
endfunction
a = -2; b = 2; n = 51
xv = linspace(a, b, n)
yv = f(xv)
plot(xv, yv)
Upon execution of this program, following figure appears as a 2D plot in the graphics
window of Scilab.
In the equation given above, different curves are represented for different values
of the constant. This indicates it as a family of contours across different values of
constant. Contours can be related with surface plots (discussed in the next section) as
level curves on a 3D surface. The reason behind this is that a 3D surface is represented
through the equation
Therefore, for each real number value of the constant in equation (1), contour
represents closed curve(s) obtained by slicing the surface (2) parallel to the x-y plane at
the level 𝑧 (i.e., keeping the variable 𝑧 as constant).
Step 1. First, the function 𝑓(𝑥, 𝑦) is defined as a Scilab function with two input
arguments. For example, for obtaining contour plots for the function
𝑓(𝑥, 𝑦) = 𝑥 2 + 𝑦 2 over the 2-dimensional region given by ,−2, 2- × ,−2, 2-.
The function be defined as:
(Here, the output variable of the Scilab function program is computed using the
element-wise power due to the same reason as explained in the previous
section.)
Step 2. Following the theory discussed above, vectors of values both 𝑥 and 𝑦 variables
are obtained in their respective domains say, ,𝑎, 𝑏- × ,𝑐, 𝑑-.
For example, for predefined values of a, b, c, d, and n, vectors may be obtained
by the commands:
xv = linspace(-2, 2, 50)
yv = linspace(-2, 2, 50)
Step 3. Then, the vector of corresponding image values can be obtained by calling the
function defined in Step 1. For example,
zv = f2(xv, yv);
Step 4. Finally, contours are plotted using the built-in function contour as
demonstrated below.
contour(xv, yv, f2, n)
Example 5.2: Let us sequence these commands as a Scilab program to obtain the
contour plot of the function 𝑓(𝑥) = 𝑥 2 + 𝑦 2 , as defined in Step 1.
Program:
Step 1. First, the function 𝑓(𝑥, 𝑦) is defined as a Scilab function with two input
arguments. For example, for obtaining the 3-D plot for the surface
𝑧 = 𝑓(𝑥, 𝑦) = 𝑥𝑦(sin 𝑥 + 2 cos 𝑦) over the 2-dimensional region given by
,−2, 2- × ,−2, 2-. The function be defined as:
function z=f2(x, y)
X.*Y.*(sin(X) + 2*cos(Y));
endfunction
yv = linspace(-2, 2, 50)
Step 3. A meshgrid is to be created for obtaining points (𝑥, 𝑦) with coordinates given
by components of vectors created in the previous step. The built-in function
meshgrid is used for this purpose for creating the cross product of vectors
xv and yv, as demonstrated below.
[X,Y] = meshgrid(xdata, ydata);
Step 4. Then, the vector of corresponding image values can be obtained by calling the
function defined in Step 1. For example,
zv = f2(xv, yv);
Step 5. Finally, the 3-dimensional surface is plotted using the built-in function surf
as demonstrated below.
surf(xv, yv, zv)
Example 5.3: Let us sequence these commands as a Scilab program to obtain the 3-D
plot of the surface given by 𝑧 = 𝑥𝑦(sin 𝑥 + 2 cos 𝑦), as defined in Step 1.
Program:
Upon execution of this program, following figure appears as a 3-D plot in the graphics
window of Scilab.
Title
The title command enables to give the introductory detail of the figure in the
form of a message coded as a string. The syntax of this command is
title("Title as a string")
Axis
To configure the labelling of both the axis of a 2-D plotting, appropriate messages
(coded as a strings) can be used through the command having a syntax as demonstrated
below.
Legends
While plotting multiple curves in a single figure, labels can be given to each curve
for its identification using the legends. The syntax of this command is
For using the legend command, the plot command is required to be used with
its advanced feature by specifying it color or style option. Therefore, user needs to learn
these options also for using the feature of configuring legend of a 2D-plot.
The style option in the plot command is a character string that consists of 1, 2 or 3
characters that specify the color and/or the line style. Different color, line-style and
marker-style options are summarized in following Table.
Example 5.3: All these graphics configurations are demonstrated through the following
program for plotting of curves 𝑦 = 𝑥 2 and 𝑦 = 𝑥 4 in a 2D-plot.
Program:
function f=myquadratic(x)
f= x.^2
endfunction
function f=myquadratic2(x)
f= x.^4
endfunction
xdata = linspace(-2,2,50);
ydata = myquadratic( xdata );
plot(xdata,ydata, "+-")
ydata2 = myquadratic2(xdata );
plot(xdata, ydata2 ,"o-")
xtitle("THE GRAPH OF f(x)=x^2 AND f(x)=x^4" , "X AXIS" ,
"Y AXIS" );
legend("x.^2" , "x.^4");
2. Write a Scilab program to obtain the plot of 2-dimensional graph of two given
function with single input argument and single output argument. The given
function is 𝑓(𝑥) = 𝑥 2 and 𝑔(𝑥) = 𝑥 3 in the domain ,−2, 2-. Label the title of the
figure and axes appropriately. Demonstrate the use of style options and legends
in the program to distinguish the two curves while plotting the figure.
3. Write a Scilab program to obtain plot a contour plot of a given function with two
input arguments and single output argument. The given function is 𝑓(𝑥, 𝑦) =
𝑥2 𝑦2
+ , where each of 𝑥 and 𝑦 to vary in the interval ,−10, 10-. Label the title of
4 9
the figure and axes appropriately.
𝑑𝑦
= 𝑓(𝑡, 𝑦)
𝑑𝑡
𝑦(𝑡0 ) = 𝑦0 .
Calling sequence
y = ode(y0,t0,t,f)
ydot = f(t,y)
where t is a real scalar (the time) and y is a real vector (the state) and ydot is a
𝑑𝑦
real vector (the first order derivative ).
𝑑𝑡
The type of problem solved and the method used depend on the value of the first
optional argument type which can be one of the following strings:
"fix" Same solver as "rkf", but the user interface is very simple, i.e., only
rtol and atol parameters can be passed to the solver. *
"discrete" Discrete time simulation. See help on ode_discrete for more details.
* The tolerances rtol and atol are thresholds for relative and absolute estimated errors.
The estimated error on y(i) is: rtol(i)*abs(y(i))+atol(i) and integration is
carried out as far as this error is small for all components of the state. If rtol and/or
atol is a constant, rtol(i) and/or atol(i) are set to this constant value. Default
values for rtol and atol are respectively rtol=1.d-5 and atol=1.d-7 for most
solvers and rtol=1.d-3 and atol=1.d-4 for "rfk" and "fix".
function ydot=f(t, y)
ydot=y^2-y*sin(t)+cos(t)
endfunction
y0=0;
t0=0;
t=0:0.1:%pi;
y = ode(y0,t0,t,f);
plot(t,y)
Output:
𝑑𝑦
Example 6.2: In the following example, we solve the equation = 𝐴𝑦. The exact
𝑑𝑡
solution is y(t)=expm(A*t)*y(0), where expm is the matrix exponential. The
unknown is the 2 × 1 matrix y(t).
function ydot=f(t, y)
ydot=A*y
endfunction
function J=Jacobian(t, y)
J=A
endfunction
A=[10,0;0,-1];
y0=[0;1];
t0=0;
t=1;
Output:
y =
0.
0.3678794
Exact solution:
y =
0.
0.3678794
𝑑2 𝑦 𝑑𝑦
2
+ 𝑔(𝑡) = 𝑓(𝑡, 𝑦)
𝑑𝑡 𝑑𝑡
𝑦(𝑡0 ) = 𝑦0 , 𝑦 ′ (𝑡0 ) = 𝑦1 .
𝑑𝑦
𝑤= .
𝑑𝑡
This reduces the given second-order ordinary differential equation into a pair of
first-order ordinary differential equations, as expressed below.
𝑑𝑦
=𝑤
𝑑𝑡
𝑑𝑥
= 𝐹(𝑡, 𝑥),
𝑑𝑡
𝑦 𝑤
where 𝑥 = , 𝐹(𝑡, 𝑥) = 𝑓(𝑡, 𝑦) − 𝑔(𝑡)𝑤 = ,𝑤; 𝑓(𝑡, 𝑦) − 𝑔(𝑡)𝑤-, and therefore
𝑤
𝑑𝑦
𝑑𝑥 𝑑𝑦 𝑑𝑤
= 𝑑𝑡 = ; .
𝑑𝑡 𝑑𝑤 𝑑𝑡 𝑑𝑡
𝑑𝑡
Example 6.3: Through this example, we solve demonstrate the procedure to simplify
𝑑2𝑦
appropriately the second-order ordinary differential equation = sin 2𝑡 with initial
𝑑𝑡 2
conditions 𝑦(0) = 0 and 𝑦 ′ (0) = −1/2.
Procedure: Let us convert the given second-order ordinary differential equation into a
first-order one, by an appropriate substitution
𝑑𝑦
= 𝑧.
𝑑𝑡
Thereby, the given equation reduces to
𝑑𝑤
= sin 2𝑡.
𝑑𝑡
This forms the vectorized first-order differential equation
𝑑𝑥
= 𝑓(𝑡, 𝑥)
𝑑𝑡
where 𝑥 = ,𝑦; 𝑤-, 𝑓(𝑡, 𝑥) = ,𝑤; sin 2𝑡-, and therefore
𝑑𝑥 𝑑𝑦 𝑑𝑤
= ; .
𝑑𝑡 𝑑𝑡 𝑑𝑡
function dx=f(t, x)
dx(1)=x(2)+cos(t)
dx(2)= sin(2*t)
endfunction
Exercise 6
1. Solve and plot the graph of the solution of following ordinary differential
equation
𝑑𝑦
= 𝑦 2 − 𝑦 sin 𝑡 + cos 𝑡
𝑑𝑡
with initial value 𝑦(0) = 0 (i.e., 𝑦0 = 0 at 𝑡0 = 0).
2. Solve and plot the graph of the solution of following ordinary differential
equation
𝑑𝑦
= 10𝑦
𝑑𝑡
with initial value 𝑦(0) = 5.
3. Solve and plot the graph of the solution of following ordinary differential
equation
𝑦′′ = 2
with initial value 𝑦(0) = 5, 𝑦′(0) = 6.
where, the details of arguments of this calling sequence are as given below.
"flag" string ("roots", "coeff"), for specifying that values given in vector
a are to be considered as roots or coefficients of the polynomial being
defined.
Shortcuts can be also used: "r" for "roots" and "c" for "coeff".
Let us learn the concept of defining polynomials in some detail through the
discussion given below. The discussion can be separated into two sections, depending
on the type of first argument being used.
For the case, when the first argument „a‟ is a vector, component values of the
vector represent either the coefficients of the polynomial or the roots of the
p1 =
3
2 + 5x + 4x
p2 =
2 3
1 + 2y + 3y + 4y
Example 7.2: For defining a polynomial in Scilab, in variable 𝑥 which has roots four
roots, namely, 2, 5, 0, and 4, the following command is to be used.
p3 =
23 4
- 40x + 38x - 11x + x
Remark 1: The same output can be achieved by a similar command in which the third
input argument is not mentioned. This is demonstrated through the following
command.
For defining a polynomial in a symbol variable ‘s’, the seed for polynomial is
defined using the following command.
This command given above to define the seed for polynomial basically defines s
as a polynomial in symbol variable s. A polynomial in the symbol variable ‘s’ using this
seed for polynomial can be defined using the following command.
p = 1+s+2*s^2;
-->s=poly(0,"s")
s =
-->p=1+s+2*s^2
p =
2
1 + s + 2s
p1 =
1 + s + 2s
-->p==p1
ans =
For the case, when the first argument „a‟ is a 2-dimensional matrix, the calling
sequence
For example,
-->A=ones(2,2)
A =
1. 1.
1. 1.
-->poly(A,"x")
ans =
2
- 2x + x
-->B = [0, 1; 1, 0]
B =
0. 1.
1. 0.
-->poly(B,"x")
ans =
2
- 1 + x
-->s=poly(0,'s')
s =
M1 =
2
1 + s 1 + s
3
s - s 2 + s
p1 =
2 3
1 + 2x + 3x + 4x
p2 =
3 4
2 + 4x + 9x + 5x
-->p3 = p1 + p2
p3 =
2 3 4
3 + 6x + 3x + 13x + 5x
-->p4 = p1 - p2
2 3 4
- 1 - 2x + 3x - 5x - 5x
-->p5 = p1 * p2
p5 =
2 3 4 5 6 7
-->p6 = p1/p2
p6 =
2 3
1 + 2x + 3x + 4x
----------------
3 4
2 + 4x + 9x + 5x
-->p7 = 2*p1
p7 =
2 3
2 + 4x + 6x + 8x
-->s=poly(0,'s')
s =
M1 =
2
1 + s 1 + s
3
s - s 2 + s
M2 =
2
1 + s - s
3 2 3
s - 2 + s + s
-->M1+M2
ans =
2 2
2 + s + s 1 - s + s
2 3
s s + s + s
-->M3 = 2*M1
M3 =
2
2 + 2s 2 + 2s
3
2s - 2s 4 + 2s
-->M4 = M1 - M2
M4 =
2 2
s - s 1 + s + s
3 2 3
s - 2s 4 + s - s - s
M5 =
2 3 5 2 3 4 5
1 + s + s + 2s + s - 2 - s - 2s + s + s + s
3 4 5 2 3 4
s + 2s + s - s - 4 - 2s + s + 3s + 2s
horner(P,x)
This function evaluates the polynomial or rational matrix P = P(s) when the variable
s of the polynomial is replaced by x:
horner(P,x)=P(x)
Some examples from a Scilab session demonstrate the working of this function.
-->s=poly(0,'s')
P =
1 + 2s + 3s
-->horner(P,1)
ans =
6.
-->horner(P,0)
1.
-->horner(P,s)
ans =
1 + 2s + 3s
-->horner(P,s^2)
ans =
2 4
1 + 2s + 3s
-->x=poly(0,'x')
x =
-->horner(P,x)
ans =
1 + 2x + 3x
-->horner(P,[1 2 5])
ans =
6. 17. 86.
-->A=eye(2, 2)
A =
0. 1.
-->horner(P, A)
ans =
6. 1.
1. 6.
-->horner(P,%i)
ans =
- 2. + 2.i
-->horner(M, 1)
ans =
1. 1.
-->horner(M,1/s)
ans =
1 s
- -
s 1
-->X= [1 2; 3 4]
X =
1. 2.
3. 4.
p =
2
1 + s + 2s
-->p=poly(1:3,'x','c')
p =
2
1 + 2x + 3x
-->m=horner(p, X)
m =
6. 17.
34. 57.
-->1*X.^0+2*X.^1+3*X.^2
ans =
6. 17.
34. 57.
Observation: The last command confirms that the horner function evaluates a
polynomial on a matrix, in a pointwise manner. The same may be confirmed through the
following commands in a continuation of previous ones.
-->Y= [1 2 3; 4 5 6]
Y =
1. 2. 3.
4. 5. 6.
-->m1=horner(p, Y)
m1 =
6. 17. 34.
ans =
6. 17. 34.
Exercise 7
and 𝑝2 (𝑥) = 2 + 4𝑥 + 9𝑥 3 + 5𝑥 4 .
(b) Obtain and display the sum, difference, product, and fraction of above two
polynomials, as
(c) Evaluate each of above defined six polynomials at a given value or a matrix
(pointwise, in case of matrices).
1 2 3
Call this function for testing with input as (1) 𝑥 = 1, (2) 𝑥 = .
0 1 1
2. Write a Scilab program to:
(c) Obtain characteristic roots of the given matrix 𝐴, the spec function on 𝐴.
(d) Test whether the characteristic roots obtained by method (b) and (c) are
equal.
(e) Call this function for testing with input as each of the following three
matrices
1 2
(1) 𝐴 = ,
0 1
1 2
(2) 𝐴 = ,
0 2
1 2 3
(3) 𝐴 = 0 2 4 .
0 0 4