17. 1.1. INTRODUCTION 17
1Scilab Core
In Scilab there are thousands of the ready-made keywords and functions. We will
discuss these keywords and function with suitable example(s).
1.1 Introduction
Scilab is a strong numerical programming software used for instructions to junior high
school students to graduate students. Scilab is written in ‘C’, ‘C++’ and ‘Java’ and its
applications are similar to the application of other languages. Unlike ‘C’ Scilab does not
required pre-definition of variable. Variables are automatically converted into integer,
character etc forms according to the values assigned to them, i.e. a value is assigned to a
variable by juggling. If a variable ‘a’ is being added with ‘2’ then the variable ‘a’ would be
considered as an integer type variable. But if it is to be added with ‘2.00202201220215’
then variable ‘a’ will be considered as an long type variable i.e. type of a variable is
considered as the use of it. Each statement performed internally should be terminated
by ‘;’. The statement whose result would be display in output window should not end
with ‘;’. Statements, strings and variables are grouped inside a parenthesis brackets, i.e.
‘(’ ... ‘)’. A syntax of a function can be spanned in multiple lines. When a part of syntax
is terminated by ‘..’, Scilab waits for remaining part of the syntax passed to it in second
line until syntax completion is not encountered.
✞
1 --> a=(
--> 1
3 --> 2
--> )
✌
✆
Execution of a function in multiline mode as shown above will produce errors while when
above function is modified as
✞
--> a=( ..
2 --> 1 ..
--> 2 ..
4 --> )
✌
✆
will be executed successfully and result will be
✞
ans =
12
✌
✆
Since Scilab 6, declaration of a number in two line is not supported as shown in above
example. According to the Code Conventions for the Scilab Programming Language it is
recommended:
1. Start each statement on a new line.
18. 18 Scilab Core
2. Write no more than one simple statement per line.
3. Break compound statements over multiple lines.
Following example uses two integers 2, 3 & ‘;’ and prints their result at output window.
✞
--> a=2; // Without pre -identification of
2 --> // variable and terminated by ’;’
--> b=3; // Without pre -identification of
4 --> // variable and terminated by ’;’
--> a+b // Sum of two variables is called
6 --> // and line does not terminated by ’;’
✌
✆
✞
ans =
5.
✌
✆
If result of an operation is not assigned to a variable, then result is automatically assigned
to ‘ans’ variable.
1.1.1 Everything As Matrix
Numerical programming by Scilab is vector or matrix based computation, therefore each
variable has a vector or matrix values. Each element of a variable is identified by its
indices. For example,
✞
--> a = 2 // one dimensional vector
2 --> a = [1,2]
--> a(1) // first element of vector
✌
✆
✞
ans =
1
✌
✆
The indices parameters inside the parentheses of variable ‘a’ may be one dimensional, two
dimensional or three dimensional.
✞
--> a(1) // One dimensional
2 --> a(1, 2) // Two dimensional
--> a(1, 2, 1)// Three dimensional
✌
✆
In the first line of above codes, first element of vector a is returned. In second line of
above codes, element at 1st
row (x) and 2nd
column (y) is returned. In the third line of
above codes, elements at 1st
row (x), 2nd
column (y) and 1st
layer (z) is returned.
✞
1 --> a = [1,2,3;5,8,4]
--> a(2,2)
3 --> b = 56;
--> b(1) // First element
5 --> b(2) // Second element . Error!!!
✌
✆
19. 1.1. INTRODUCTION 19
✞
a =
1. 2. 3.
5. 8. 4.
ans =
8.
ans =
56.
!--error 21
Invalid index.
✌
✆
1.1.2 Approximate Zero
Numerical programming languages use double datatype floating point arithmetic. There-
fore each number is denoted as a float value, i.e. 1 as 1.000000, 0.5 as 0.500000 etc.
Sometime, even zero is represented as 2.5 × 10−18
or as 13.2 × 10−19
etc. These values
are very very small and are close to zero. In floating point arithmetic, zero is taken as
comparative value rather than absolute value. For example, in numerical calculations,
identity matrix is represented as
33. where 1 is very large value in comparison to 3.5 × 10−12
or 35 × 10−12
.
1.1.3 Computational Error
Numerical software uses iteration to find the numeric solutions. For example, every one
knew that sin 90◦
is equal to one. But Numerical Software does not use fixed type values.
They uses series solutions, i.e. for sin θ, they uses sine series:
sin α = α −
α3
3!
+
α5
5!
− . . .
Here, α is in radian. This series is valid if infinite number of steps are taken for computa-
tion purpose. Computer does not take infinite terms of this series. It limits the number
of terms for computation. But, how many terms? If number of terms is less, result shall
be other than actual one but computational performance shall be good. And if number of
steps is large then result shall be near accurate value but performance shall be compro-
mised. Now, the question is that, how many steps shall be taken in this series. It depends
on the required significant digits in the result. At the optimal result, the deviation of
computed value from the actual one is called computational error. For the above series,
we shall compute value for three terms and four terms, and corresponding results shall
be compared.
For Three Terms
sin
π
2
=
22
14
−
22
14
3
3!
+
22
14
5
5!
34. 20 Scilab Core
Or
sin
π
2
= 1.5714286 − 0.6467444 + 0.0798531
It gives sin 90◦
= 1.0045373.
For Four Terms
sin
π
2
=
22
14
−
22
14
3
3!
+
22
14
5
5!
−
22
14
7
7!
Or
sin
π
2
= 1.5714286 − 0.6467444 + 0.0798531 − 0.0046950
It gives sin 90◦
= 0.9998423. It is seen here, the value oscillates about one as the number
of terms increases and tends to one when number of terms are infinite. Numerical Software
use sufficient long steps so that result is near to accurate result or upto desired significant
digits. Now, computational error is difference between exact value of sin 90◦
and computed
value. In first case computational error is 1 − 1.0045373, i.e. −0.0045373 and in second
case, computational error is 1 − 0.9998423, i.e. 0.0001577.
Error In Calculus In calculus, definite integration of a function is carried out by two
ways. (i) By using first principle method and (ii) By using direct method. For example,
I =
Z 1
0
x dx
may be solved by two ways. In first method, take dx = 0.1. It gives,
n =
b − a
dx
= 10
and x are computed by a + n × dx where a is lower limit of integration.
I = 0 × 0.1 + 0.1 × 0.1 + . . . + 0.8 × 0.1 + 0.9 × 0.1
On solving it, we have
I = 0 + 0.01 + 0.02 + 0.03 + 0.04 + 0.05 + 0.06 + 0.07 + 0.08 + 0.09 = 0.45
From direct method,
I =
Z 1
0
x dx =
x2
2
35.
36.
37.
38. 1
0
= 0.5
Now, the computational error by two methods is 0.50 − 0.45 = 0.05. So, computational
errors, not only occur due to selection of number of steps but they also occur when we
select different types of solution methods.
1.2 Simple Arithmetic
Before we start the expertise application of Scilab in numerical programming, we first be
hands out with simple mathematical applications like addition, subtraction, multiplica-
tion, division, scoping and application of inbuilt functions.
39. 1.2. SIMPLE ARITHMETIC 21
1.2.1 Addition
Arithmetic addition is the summation of two or more numbers. Scilab obeys the simple
arithmetic rules of addition. In the following example, two variables are declared and
assigned some values. When we called addition by variable method, we got desired
result. Remember, to print the result of a step of operation, it shall not terminated with
‘;’ (semi-colon). If you do so, the result shall be assigned to default answer variable ans
without displaying it in output console.
✞
-- a=10;
2 -- b=10;
-- a+b
✌
✆
✞
ans =
20.
✌
✆
The default output in Scilab is a float or a double. A float number has fraction parts,
i.e. fraction part succeed to decimal symbol (.). Numbers without (.) are pure integers.
int8(x) converts a float or double number (x) into integer number by truncating its fraction
part.
✞
-- a=10.5;
2 -- b=10.3;
-- int8 (a+b)
✌
✆
✞
ans =
20
✌
✆
We can make symbolic addition of two numbers too. But before doing so, string or
characters are defined as variable by using poly function of zero order. The first argument
of poly function is degree of equation and ‘b’ is symbolic variable.
✞
-- a=10;
2 -- // poly (degree of eqn ,symbol )
-- b=poly (0,’b’);
4 -- a+b
✌
✆
✞
ans =
b + 10
✌
✆
sum function does not takes inputs to be summed, but it accepts a vector or matrix as
its argument. It adds vector elements or elements of a matrix cumulatively. Elements of
vector or matrix are constructed by using ‘:’ (colon) loop operator twice. First argument
of colon loop is initial limit of the array, second argument is iteration step increment after
a loop and third argument is upper limit of the array.
✞
-- a=1:1:5
✌
✆
40. 22 Scilab Core
✞
ans =
1. 2. 3. 4. 5.
✌
✆
The sum of array elements
✞
-- a=1:1:10;
2 -- sum(a)
✌
✆
✞
ans =
55
✌
✆
abs function is used to return the absolute value of a number x. When x is complex,
abs(x) is the complex modulus (magnitude) of the elements of x. Mathematically
abs(x) = |x|
1.2.2 Subtraction
Arithmetic subtraction is the difference of two or more numbers. In standard form of
subtraction, second operand is subtracted from first operand of subtraction operator (–
). The result may be positive or negative. If there are several consecutive subtraction
without scope operators then LEFT-TO-RIGHT order of subtraction is followed.
✞
-- a=10;
2 -- b=10;
-- c=10;
4 -- a-b-c
✌
✆
✞
ans =
-10.
✌
✆
We can also make symbolic subtraction of two numbers similar to the symbolic addition
as explained above.
✞
-- a=10;
2 -- // poly (degree of eqn , symbol )
-- b=poly (0,’b’);
4 -- a-b
✌
✆
✞
ans =
-b + 10
✌
✆
1.2.3 Multiplication
Arithmetic multiplication in Scilab is performed by asterisk operator (*). If there are
several consecutive multiplication without scope operators then LEFT-TO-RIGHT order
of multiplication is followed.
41. 1.2. SIMPLE ARITHMETIC 23
✞
-- 2*3*4*5
✌
✆
✞
ans =
120.
✌
✆
Cumulative array product of array elements can be performed by prod() function. See
the example given below:
✞
-- a=1:1:10;
2 -- prod (a)
✌
✆
✞
ans =
3628800.
✌
✆
1.2.4 Division
Arithmetic division in Scilab is denoted by forward slash.
✞
-- 2/3
✌
✆
✞
ans =
0.6666667
✌
✆
The answer is in ten digit form. We can control the decimal places by using the function
format() function by supplying format code and number of fraction digits as its arguments.
✞
-- format(’v’ ,4)
✌
✆
Where ‘v’ is abbreviation of ‘variable’ and integer 4 controls the number of digits in
answer. It does also meant that, the fourth digit shall also be rounded up.
✞
1 -- format(’v’ ,4)
-- 2/3
✌
✆
✞
ans =
0.7
✌
✆
Changing the format of numbers kept in effective until Scilab session is not reset or it is
not restarted.
1.2.5 Scoping
The simple arithmetic, withing same scoping/grouping is performed according to BOD-
MAS rule. It is an acronym for Bracket, Of, Division, Multiplication, Addition and
42. 24 Scilab Core
Subtraction. Mathematical grouping is very essential to set the procedure of arithmetic
operations. For example
1
2
2
= 0.4
But
1
2
2
= 1
Simple use of forward slash as divisor we can’t identify the numerator and denominator
of a rational number or of a fractions. Same fractions represented in different way have
different answers. This is why in Scilab, scoping/grouping of variables becomes necessary.
If scoping/grouping is not in correct form the answer is unpredictable. For example,
withough scoping then division of unity by two is
✞
-- 1/2
✌
✆
✞
ans =
0.5
✌
✆
And division of half by two is
✞
-- 1/2/2
✌
✆
✞
ans =
0.25
✌
✆
Now division of half by half is
✞
-- 1/2/1/2
✌
✆
✞
ans =
0.25
✌
✆
It is not required answer as half divided by half must be unity. By scoping each fraction
according to our requirement, we have correct answer.
✞
-- (1/2) /(1/2)
✌
✆
✞
ans =
1
✌
✆
1.2.6 Square Root (sqrt)
If x is a real number then its square root is given by y =
√
x. In other words, if y is
square root of x, then y2
= x. Roots computed by this function may be real or complex.
Square root of negative numbers returned in complex form. In Scilab, square root is a
scalar operator and it performs elementwise operation. Square root of a number can be
obtained by
43. 1.2. SIMPLE ARITHMETIC 25
✞
-- sqrt (2)
✌
✆
✞
ans =
1.414
✌
✆
If argument is a vector, then square root of the vector is a vector of square root of its
elements. If argument is a matrix then square root of the matrix is a matrix of square
roots of its elements.
✞
-- a=[1 ,2;3 ,4];
2 -- sqrt (a)
✌
✆
✞
ans =
1. 1.4142136
1.7320508 2.
✌
✆
The algorithm used in computation of the square root of a real value, x, is
√
x = e
ln(x)
2
1.2.7 Exponent (ˆ Operator)
ˆ is called exponent operator. If n is an index or a degree, or power of base x then
exponents is represented by xn
. If |n| ≥ 1, then exponent means “base is multiplied by
itself n times”, and if |n| 1, then it is called nth
root of base x. To find the kth
root of
base number x, we use ˆ operator as xk
.
✞
1 -- 2^(0.33)
✌
✆
✞
ans =
1.2570134
✌
✆
The output of exponents depends on the values of base and exponent. For example,
if either base x or exponent n is less than one, then result is convergent, i.e. result is
approaching towards zero. Similarly, if both base x and exponent n are greater than one
then result is divergent. If n is one, then result is always base x itself. If base x is one
and n is whatever value, then result is always one. If exponent is a complex then it is
solved by using logarithm with natural base (e). For example, y = 2i
is simplified as
log y = log 2i
It gives
log y = i log 2 ⇒ y = ei log 2
Now, we can simplify right side using relation eiθ
= cos θ + i sin θ as
y = cos(log 2) + i sin(log 2)
44. 26 Scilab Core
Note that, ˆ acts like both, scalar and vector operator with vectors and matrices. If it is
used like ‘.ˆ’ then it acts as elementwise operator and if it is used without dot, then it
acts as vector operator.
✞
-- a=[1 ,2;3 ,4];
2 -- a^(0.33) // vector operator
✌
✆
✞
ans =
0.6956785 + 0.4728372 i 0.4784401 - 0.2162886 i
0.7176602 - 0.3244328 i 1.4133387 + 0.1484044 i
✌
✆
✞
1 -- a=[1 ,2;3 ,4];
-- a.^(0.33) // scalar operator
✌
✆
✞
ans =
1. 1.2570134
1.4369777 1.5800826
✌
✆
The algorithm used for computation of exponent is explained here. Assume a matrix of
order 2 × 2
A =
1 2
3 4
Now its eigenvalues are given by |A − λI| = 0 i.e.
60. = 0
Or
(1 − λ) × (4 − λ) − 6 = 0
On solving it
λ = −0.372281; 5.372281
Or eigenvalues in matrix form, when they are arranged in descending order is
d =
5.372281 0.000000
0.000000 −0.372281
For λ = 5.372281, eigenvector (v1) is (A − λI)v1 = 0. So,
1 − 5.372281 2
3 4 − 5.372281
x
y
= 0
−4.372281 2
3 −1.372281
x
y
= 0
61. 1.2. SIMPLE ARITHMETIC 27
Or
−4.372281x + 2y = 0; 3x − 1.372281y = 0
To get solutions, put x = 1 in −4.372281x + 2y = 0, we get the value of y = 2.186140.
To get eigenvectors, we shall normalize these two values as
x =
1
√
12 + 2.1861402
; y =
2.186140
√
12 + 2.1861402
It gives, x = 0.415973 and y = 0.909376. As coefficients of above two eigenvector equa-
tions are of opposite signs, hence values of x and y shall be either both negative or both
positive. Now, we shall submit x and y values in equation g = 3x − 1.372281y to get
minimum positive value.
g = 3 × 0.415973 − 1.372281 × 0.909376 = 0.000000407
Taking sign convention, the other possible set of solution be x = −0.415973 and y =
−0.909376.
g = 3 × −0.415973 − 1.372281 × −0.909376 = 0.000000407
When x = −0.415973 and y = −0.909376, we have positive f value. This gives first
eigenvector corresponding to λ = 5.372281.
v1 =
−0.415973
−0.909376
For λ = −0.372281, eigenvector (v2) is (A − λI)v2 = 0. So,
1 − (−0.372281) 2
3 4 − (−0.372281)
x
y
= 0
1.372281 2
3 4.372281
x
y
= 0
Or
1.372281x + 2y = 0; 3x + 4.372281y = 0
As coefficients of above two eigenvector equations are of same signs, hence values of x
and y shall be in opposite signs. On solving these two algebraic equations, as explained
for λ = 5.372281, we have x = −0.824564 and y = 0.565767 or x = 0.824564 and
y = −0.565767. This gives second eigenvector corresponding to λ = −0.372281.
v2 =
−0.824564
0.565767
The corresponding eigenvector matrix from above two eigenvectors (v1 and v2) is
v =
−0.415973 −0.824564
−0.909376 0.565767
Note that, each column of eignevectors is arranged to the corresponding eigenvalues. Now,
the matrix A can be written as A = v × d × v−1
. The square root of the matrix is given
by
A
1
2 = v × d
1
2 × v−1
62. 28 Scilab Core
1.2.8 Algebraic Equations
Algebraic equations are those equations in which an unknown variable is in linear addition
or in linear subtraction in different degrees. Numerical coefficients are multiplied with
each terms to give a suitable relation for the unknown variable. To form a linear equation,
first define the variable.
✞
1 -- x=poly (0,’x’);
-- f=2*x^2+2
✌
✆
f =
2 + 2x2
This algebraic equation is in symbolic form. All the algebraic function may be applicable
to this equation. In the following example, function roots() is used to algebraic equation
f to gets its all roots.
✞
-- x=poly (0,’x’);
2 -- f=2*x ^2+2;
-- roots(f)
✌
✆
✞
ans =
i
- i
✌
✆
Division of polynomials by a number or by a symbol can also be performed. Note that it
returns only quotient of the division.
✞
1 -- x=poly (0,’x’);// returns only degree 1 polynomial
-- f=2*x ^2+2;
3 -- pdiv (f,x),
✌
✆
✞
ans =
2x
✌
✆
LCM (Least Common Multiplier) and HCF (Highest Common Factor) of two algebraic
functions can also applicable with the algebraic equations.
✞
-- x=poly (0,’x’);
2 -- f=2*x ^2+2;
-- lcm([f,x])
✌
✆
✞
ans =
3
2x + 2x
✌
✆
63. 1.2. SIMPLE ARITHMETIC 29
In Scilab, HCF is named as GCD (Greatest Common Divisor).
✞
1 -- x=poly (0,’x’);
-- f=2*x ^2+2;
3 -- gcd([f,x])
✌
✆
✞
ans =
1
✌
✆
1.2.9 Logarithm
The natural logarithm of number 2 is obtained when this number is supplied as argument
to the log function.
✞
-- log (2)
✌
✆
✞
ans =
0.6931472
✌
✆
Exponential to a number (n) is represented mathematically as en
. In Scilab, it is given
as
✞
-- exp (2)
✌
✆
✞
ans =
7.3890561
✌
✆
We may use different bases for logarithms, i.e. base ‘10’, base ‘2’ or natural base ‘e’.
Logarithm of a number about base ‘10’ is computed as
✞
-- log10 (2)
✌
✆
✞
ans =
0.30103
✌
✆
Solved Problem 1.1 Write Scilab code for finding of natural and base ten logarithm of
number 2.05 and find their difference.
Solution The Scilab codes for the given problem is given below:
✞
-- log (2.05) -log10 (2.05)
✌
✆
✞
ans =
0.4060859
✌
✆
This is output of the given problem.
64. 30 Scilab Core
1.2.10 Index Expression
An index expression allows to scale, select or replace an element from a vector or matrix.
Indices may be scalars, vectors, ranges, or the special operator ‘:’, which may be used to
select entire rows or columns. Row and column indices, i.e. two parameters, are separated
by comma.
✞
-- A(row indices , column indices )
✌
✆
When only one parameter is passed to matrix variable to extract the sub matrix, then at
first, matrix is arranged in vector form in sequence of column vectors. For example,
✞
1 -- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
-- A(:)
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
2
5
8
4
3
3
6
5
1
1
7
1
4
8
2
3
✌
✆
Now, only one parameter is supplied to vector variable, it is treated as indices for elements
and it returns a vector of elements present at that indice. Note that, here index counting
started from 1 rather than 0.
✞
-- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 -- A(2) // element at index 2
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
65. 1.2. SIMPLE ARITHMETIC 31
ans =
5
✌
✆
Similarly, a range of indices can be given to matrix variable to extract a vector of elements
present at that index. In the following example, sub vectors are returned by constructing
elements present at indices from index 1 to index 2. There are two different way to pass
same indices.
✞
1 -- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
-- A(1:2) // 1st index to 2nd index
3 -- A([1;2]) // 1st index and 2nd index
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
2
5
ans =
2
5
✌
✆
When both parameters are passed to matrix variable, then first parameter is indices for
rows and second parameter is for columns. Rows or columns may be discrete indices or
range of indices but should not beyond the dimension of the matrix. In this case, matrix
is not arranged in sequeces of elements of column vectors. See the below example.
✞
1 -- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
-- A(2:4 ,3:4) // Row index from 2nd row to 4th row
3 // col index from 3rd col to 4th col
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
1 8
7 2
1 3
✌
✆
If both parameters are discrete indices, then row index are recursive for all column indices.
See the following example, in which elements at second and third column of first row, and
elements at second and third column of fourth row forms a sub matrix.
✞
1 -- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
-- A([1,4], [2 ,3])// 1st row - 2nd 3rd columns
3 // 4th row - 2nd 3rd columns
✌
✆
66. 32 Scilab Core
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3 1
5 1
✌
✆
If same index number is used twice or more in row parameter, i.e. as [1,1], then output
sub matrix has two rows as part of row index ‘1’ of the given matrix.
✞
-- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 -- A([1,1], [2 ,3])// 1st row - 2nd 3rd columns
// 1th row - 2nd 3rd columns
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3 1
3 1
✌
✆
end operator is used to denote last index of the vector. For example
✞
-- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 -- A(end)// last element of vector
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3
✌
✆
The parameter of matrix variable may be controlled by range indices. For example, odd
elements of matrix and even elements of matrix can be extracted by
✞
1 -- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
-- A(1:2: end) // Odd elements vector
3 -- A(2:2: end) // Even elements vector
✌
✆
✞
A =
2 3 1 4
5 3 1 8
67. 1.3. ARITHMETICAL KEYWORDS 33
8 6 7 2
4 5 1 3
ans =
2 8 3 6 1 7 4 2
ans =
5 4 3 5 1 1 8 3
✌
✆
1.3 Arithmetical Keywords
Followings are the main keywords used in Scilab. The key by key explanation is given
below.
1.3.1 Result Variable (ans)
ans means “answer”. This variable is created automatically when expressions are not
assigned to any other variable. It is the last un-assigned evaluated value.
✞
1 -- a=10;
-- b=5;
3 -- a+b
✌
✆
✞
ans =
15
✌
✆
1.3.2 Left Matrix Division ()
‘’ is used for left matrix division. Let A ∗ x = b has a solution of x = Ab. In this
expression, succeeding variable to ‘’ is divided by the preceding one.
✞
-- a=10;
2 -- b=5;
-- ab
✌
✆
✞
ans =
0.5
✌
✆
The second example of the backslash command is
✞
-- A=2;
2 -- b=[1;1;1];
-- x=Ab
✌
✆
✞
x =
0.5
0.5
0.5
✌
✆
68. 34 Scilab Core
1.3.3 Square Brackets ([...])
Square brackets are used to define a vector or a matrix. Each row of a vector or matrix
is separated by ‘;’ and each element of a row is separated by ‘,’ (comma) or space.
✞
-- x=[1;2;3]
✌
✆
✞
x =
1
2
3
✌
✆
A 3 × 3 matrix is written as
✞
-- x=[1 ,2 ,3;4 ,5 ,6;7 ,8 ,9]
✌
✆
✞
x =
1 2 3
4 5 6
7 8 9
✌
✆
In a row matrix or in a column matrix, elements of the row or matrix may be numbers or
function. The elements may be separated by comma or space. In otherwords, functions
may be part of the matrix elements. The exact element value of function element is its
return value.
✞
-- x=[1 sqrt (-1) 2 -sqrt (-1) 3]
✌
✆
✞
x =
1. i 2. - i 3.
✌
✆
Elements of a vector may contains text or strings too. See the following Scilab codes.
✞
-- [’This is’;’string’;’matrix’]
✌
✆
✞
ans =
!This is !
! !
!string !
! !
!matrix !
✌
✆
A new row of a matrix can also be started by adding row elements in new line.
✞
-- mat = [
2 Row A1
Row B1
4 Row C1
Row D1
6 ]
✌
✆
69. 1.3. ARITHMETICAL KEYWORDS 35
Consistency of columns for each rows should be maintained to avoid the row/cols consis-
tence error. Following example gives error.
✞
-- mat = [
2 Row A1,Row A2
Row B1,
4 Row C1
Row D1
6 ]
✌
✆
While this example will give a matrix of four rows and two columns.
✞
-- mat = [
2 Row A1,Row A2
Row B1,Row B2
4 Row C1,Row C2
Row D1,Row D2
6 ]
✌
✆
1.3.4 Element-Wise Operation
Element-wise operation is performed on each element of a supplied argument to a function.
The structural properties of the supplied arguments are not considered. For example,
A = [1] is a scalar while B = [1, 4] is a vector/matrix but in element wise operations,
both are considered as scalars. It means only elements are meaningful in elementwise
operation while structures of A and B are ignored.
1.3.5 Matrix-Wise Operation
In matrix-wise operation, elements are accepted as element of a matrix and their result
is put in the similar matrix form in output window.
1.3.6 Colon (Range Operator)
This operator (:) is used to define a range of variable. Normally, it creates a vector/set
of elements. It is used by two ways.
✞
-- ll : ul
2 -- ll : inc : ul
✌
✆
Increment may be a positive value, or a negative value. If it is positive value then range
is in ascending order, if it is negative value then range is in descending order. In first
method of application, initial and final values are declared by user and suitable increment
value is selected by Scilab itself. By default increment is ‘1’.
✞
-- a = 1:10
✌
✆
70. 36 Scilab Core
✞
a =
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
✌
✆
✞
-- a = 1:-10
✌
✆
✞
a =
[]
✌
✆
In second method of application (i.e. more than one colons (:) are used) first element
is initial value, second element is increment and third element is the upper level limit of
range. The last value of range does not exceed to the upper limit. After each increment,
initial value becomes equal to the sum of initial value and increment. This iteration is
followed until it becomes equal or more than the finale range of iteration.
✞
-- a = 1:2:10
✌
✆
✞
a =
1. 3. 5. 7. 9.
✌
✆
✞
-- a = 1:-2:-10
✌
✆
✞
a =
1. - 1. - 3. - 5. - 7. - 9.
✌
✆
The colon can be used for defining a rang into a matrix form. Each argument of this
function is comma separated. First argument is matrix range, second argument is number
of rows and third argument is the number of columns.
✞
-- a = matrix (1:12 ,6 ,2)
2 -- a
✌
✆
✞
a =
1. 7.
2. 8.
3. 9.
4. 10.
5. 11.
6. 12.
✌
✆
Elements of a matrix are retrieved by calling the matrix object as shown below:
✞
1 -- a(:)
✌
✆
71. 1.3. ARITHMETICAL KEYWORDS 37
✞
ans =
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
✌
✆
The specific columns of matrices can be accessed by using first argument as colon symbol
and second argument as column index. See the example below:
✞
1 -- a = matrix (1:12 ,6 ,2)
-- b=a(:,1) //1 = first column
3 -- c=a(:,2) //2 = second column
✌
✆
✞
a =
1. 7.
2. 8.
3. 9.
4. 10.
5. 11.
6. 12.
b =
1.
2.
3.
4.
5.
6.
c =
7.
8.
9.
10.
11.
12.
✌
✆
We can use row or column index vectors for accessing multiple rows or columns.
✞
1 -- m=[1 ,2 ,3;4 ,5 ,6;7 ,8 ,9]
-- m(: ,[1 ,2]) // for multiple column indices
✌
✆
✞
m =
72. 38 Scilab Core
1. 2. 3.
4. 5. 6.
7. 8. 9.
ans =
1. 2.
4. 5.
7. 8.
✌
✆
Specific rows can be retrieved if argument location of range operator and index number
is interchanged as given in following code snippets.
✞
-- a = matrix (1:12 ,6 ,2)
2 -- b=a(1,:) //1 = first row
-- c=a(5,:) //2 = fifth row
✌
✆
✞
a =
1. 7.
2. 8.
3. 9.
4. 10.
5. 11.
6. 12.
b =
1. 7.
c =
5. 11.
✌
✆
A single colon as argument to matrix handle means full range of the matrix elements.
We can limit the range operator (:) for specific indices if colon is used as incremental
operator.
✞
1 -- (initial limit : increments /decrements : final limit )
✌
✆
There is an increment or decrement, it is depends on the sign of increment/decrement
(second element in the range operator). Increments is always a positive integer and
decrements is always a negative integer. For example
✞
1 -- a(12: -3:1)
✌
✆
✞
a =
12.
9.
6.
3.
✌
✆
✞
1 -- a(1:2:10)
✌
✆
73. 1.3. ARITHMETICAL KEYWORDS 39
✞
a =
1.
3.
5.
7.
9.
✌
✆
We can extract a part of matrix by using colon operator within parentheses as shown in
below example.
✞
-- A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 -- A(2:4 ,3:4) //A(row range , col range )
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
ans =
1 8
7 2
1 3
✌
✆
We can use the colon operator to print integer numbers from 0 to n with special purposes.
In the following example, n is restricted to 5, i.e. integers from 0 to 5 are printed in output.
✞
-- %i : 5
✌
✆
✞
ans =
0. 1. 2. 3. 4. 5.
✌
✆
This method of implementation is removed since Scilab 6.0 version.
1.3.7 Comma
Comma operator (,) is used as the identifier for column, instruction and used as argument
separator. A 3 × 3 matrix is represented in vector form as
✞
-- [1,2,3;4,5,6;7,8,9]
✌
✆
✞
x =
1 2 3
4 5 6
7 8 9
✌
✆
Space operator “ ” is also used as separator of the elements in a row of the matrix.
74. 40 Scilab Core
1.3.8 Comments
Comments are the statements which explains to mathematical operations, numerical cal-
culations and special functions. Comments are embedded within the program codes. The
comments are never executed by the Scilab and they are simply skipped by the Scilab.
Symbol ‘//’ is used for the commenting of a line.
✞
-- a=5;
2 -- b=10;
-- a+b // summation of two variable .
✌
✆
✞
ans =
15
✌
✆
1.3.9 Dot
Dot symbol is used to represent the end of a sentence or the decimal fraction. It is also
used for element-wise dot product (.*) of two vectors or matrices. If A = aij and B = bij
are two matrices of n × n size then
[A · ∗B]ij = aij × bij
It is always true that [A ∗ B] = [B ∗ A]. If, A and B are 2 × 2 matrices as given by
A =
1 2
3 2
; B =
2 1
3 4
Then
C = A · ∗B =
1 × 2 2 × 1
3 × 3 2 × 4
=
2 2
9 8
✞
-- a=[1 ,2;3 ,2];
2 -- b=[2 ,1;3 ,4];
-- a.*b
✌
✆
✞
ans =
2. 2.
9. 8.
✌
✆
1.3.10 Empty
An empty vector or an empty matrix is represented by ‘[]’. Empty square brackets
represent to the empty set of matrix. White spaces inside the square brackets do not
affect the meaning of empty matrix.
✞
1 -- []
-- [ ]
✌
✆
75. 1.3. ARITHMETICAL KEYWORDS 41
✞
ans =
[]
ans =
[]
✌
✆
1.3.11 Equal Sign
Equal operator (==) compares two values numerically and returns the ‘true’ if both
values are equal otherwise returns ‘false’. Remember that symbol ‘=’ is not same as to
‘==’. Symbol ‘=’ assigns a value to a variable while ‘==’ compares two values.
✞
-- i = 5;
2 -- j = 10;
-- if(i==j)
4 -- disp (E);
-- elseif(ij)
6 -- disp (N);
-- end
✌
✆
✞
N
✌
✆
1.3.12 global
It declares a variable as a global variable. The variable defined global can be called from
everywhere.
1.3.13 Hat Symbol
It (ˆ) represents to the exponent, i.e. bn
. It raises base (number at the left side to the
hat) to the power of n (number at the right side to the hat). It is also sometime refers as
power.
✞
1 -- 2^4
✌
✆
✞
ans =
16
✌
✆
Hat symbol (ˆ) can be also used to find the nth
root of a number.
✞
-- x^(1/ n)
✌
✆
✞
1 -- 2^(1/4)
✌
✆
✞
ans =
1.1892071
✌
✆
76. 42 Scilab Core
1.3.14 Less Than
() is a comparative operator. It tells that the left hand value is lesser than the right
hand value. If left hand side value is less than the right hand side, then comparison is
true otherwise false. It returns “yes” on true state and “no” in false state.
✞
-- if (12)
2 -- disp (yes);
-- end
✌
✆
✞
ans =
yes
✌
✆
1.3.15 Minus
(–) sign is used to subtract right hand value from the left hand value. Subtraction is
algebraic. If right hand value is larger than left hand value then result is negative.
✞
-- 2-3
✌
✆
✞
ans =
-1
✌
✆
The two variables using minus may be a vector or a matrix. In case of vectors, subraction
takes place element-wise. Subtraction of vectors is a vector of similar size.
✞
-- [1 ,2] -[3 ,4]
✌
✆
✞
ans =
-2. -2.
✌
✆
In vector subtraction, size of two vectors must be equal.
1.3.16 Not
Symbol, ∼, is a logical NOT operator. It compares the two values and access them
according “value a is not equal to value b”. If the comparison is true according to this
statement, then it returns true otherwise it returns false.
✞
-- 2 ~= 3 //2 is not equal to 3.
✌
✆
✞
ans =
T
✌
✆
77. 1.3. ARITHMETICAL KEYWORDS 43
1.3.17 Parenthesis
Parenthesis, i.e. ‘(...)’ brackets are used to group the statements, operations executions.
A variable or function initiated inside the parenthesis has local scope.
1.3.18 Percent
(%) is a special character. When it is prepend to string, literals are considered as with
special meaning as shown in the following table.
Symbol Meaning
%pi π
%i:n Integer from 0 to n
%i
√
−1
The functions whose names begin with % character are special functions. They are
used as primitives and used in operator’s overloading.
1.3.19 Plus
(+) is an addition operator. It returns sum of two or more values. It also concatenates
two or more strings. Its operands may be vectors, scalars or strings.
✞
-- 2+3
2 -- This is my + country .
✌
✆
✞
ans =
5
ans =
This is mycountry .
✌
✆
The two variables may be a vector or a matrices. In case of vector operands, addition
takes place element-by-element wise.
✞
--[1,2]+[3,4]
✌
✆
✞
ans =
4. 6.
✌
✆
In vector subtraction, size of two vectors must be equal.
1.3.20 Quote
There are two types of quotes, called single quotes (’) and double quotes (”). In Scilab,
single quotes and double quotes have different meaning. The contents written between
single quotes or double quotes are considered as strings. See the following example:
78. 44 Scilab Core
✞
-- x = ‘The Good Boy ’
2 -- y = The Good Boy
✌
✆
✞
x =
The Good Boy
y =
The Good Boy
✌
✆
Single quote is also used as string delimiter (”), i.e. string within string. As string
delimiter, the single quote is used in a group of twice.
✞
-- x = ‘disp (‘‘Boy ’’)’
2 -- y = disp (‘‘Boy ’’)
-- z = disp (‘‘disp (‘‘disp (‘‘Boy ’’) ’’) ’’)
✌
✆
✞
x =
disp (‘Boy ’)
y =
disp (‘Boy ’)
z =
disp (‘disp (‘disp (‘Boy ’) ’) ’)
✌
✆
Single quote is also used in mathematics, specially in matrix. If a matrix is real matrix
then single quotes converts row vector into column vector and vice-versa. See the example
given below:
✞
-- a=[1 ,2]
2 -- a’
✌
✆
✞
a =
1. 2.
ans =
1.
2.
✌
✆
If matrix is a complex matrix then (’) is a complex conjugate transpose operator. It is
used in a matrix for conjugate transpose of the matrix. It is also used as string delimiter.
Assume a complex matrix of order 2 × 2 as
A =
1 + i 2
3 2 + i
Conjugate transpose of this matrix (A) will be
A =
1 − i 3
2 2 − i
79. 1.3. ARITHMETICAL KEYWORDS 45
✞
1 -- [1+%i , 2; 3, 2+%i]’
✌
✆
✞
ans =
1. - i 3.
2. 2. - i
✌
✆
If a dot (.) is applied before (’), then the matrix transpose is called non-conjugate
transpose of matrix. Assume a complex matrix of order 2 × 2 as
A =
3 + i 2
1 2 + i
Then non-conjugate transpose of the matrix will be
A =
3 + i 1
2 2 + i
✞
1 -- [1+%i ,2; 3,2+ %i].’
✌
✆
✞
ans =
1. + i 3
2. 2. + i
✌
✆
1.3.21 Return
return assigns local variables to the calling variable.
✞
1 -- [a,b]= return (1,2)
✌
✆
✞
b =
2.
a =
1.
✌
✆
It is also used to return a value from inside of a function. A function is terminated in
execution, when keyword return is encountered.
✞
-- function [y]= myFunc(i, j)
2 -- y=i+j;
-- return 0;
4 -- y=i^j; // Not executed
-- endfunction
6 -- myFunc (2, 3)
✌
✆
✞
ans =
5.
✌
✆
80. 46 Scilab Core
✞
-- function [y]= myFunc(i, j)
2 -- y=i+j;
-- // return 0;
4 -- y=i^j; // Executed and overloaded
-- // to previous result
6 -- endfunction
-- myFunc (2, 3)
✌
✆
✞
ans =
8.
✌
✆
1.3.22 Semicolon
Semicolon ‘;’ is used as a row separator in a matrix. It is also used to end a statement
line. Application of semicolon as a row separator is shown below:
✞
-- [1,2,3;4,5,6;7,8,9]
✌
✆
✞
x =
1 2 3
4 5 6
7 8 9
✌
✆
Application of semicolon as a end of line is shown in the following example.
✞
-- i = 5; // end of first line
2 -- j = 10; // end of second line
-- if(ij)
4 -- disp (L);
-- elseif(ij)
6 -- disp (G);
-- elseif(i==j)
8 -- disp (E);
-- end
✌
✆
✞
L
✌
✆
End-of-line application of semicolon (;) does not mean the skip of contents by the Scilab
or commenting of the rest of contents in the line. It only separates the two statements
written in a single line. Again, when a statement is terminated with semicolon, then the
output or result is stored in memory and it is not visible in the output console. The
output is obtained only when the object is called without terminating by semicolon.
✞
1 -- a=5+5; b=6+5;
-- a
3 -- b
✌
✆
81. 1.3. ARITHMETICAL KEYWORDS 47
✞
a =
10.
b =
11.
✌
✆
1.3.23 Forward Slash
Slash (/) is used in division. The left side value to the forward slash is divided by its
right side value. When forward slash is used in pair, it acts as a commenting symbol. In
Scilab, line commenting symbol is ‘//’.
✞
-- i = 5; // end of first line
2 -- j = 10; // end of second line
-- i/j
✌
✆
✞
ans =
0.5
✌
✆
1.3.24 Space
Space operator (“ ”) is used as the identifier for columns in a matrix or in a vector. A
3 × 3 matrix is represented in vector form as
✞
-- [1 2 3;4 5 6;7 8 9]
✌
✆
✞
x =
1 2 3
4 5 6
7 8 9
✌
✆
Comma operator (,) is also used as separator of the elements in a row.
1.3.25 Asterisk
Star (*), i.e. asterisk is used as multiplier between two variables from left to right.
Multiplication of two vectors are allowed if number of rows in first vector is equals to the
number of columns in second vector. If A = aij and B = bij are two matrices of size
n × n then
[A ∗ B]kj =
n
X
i,k=0
aki × bik
It is not always true that [A ∗ B] = [B ∗ A]. If two inputs are integers then it performs
simple arithmetic.
82. 48 Scilab Core
✞
-- i = 5; // end of first line
2 -- j = 10; // end of second line
-- i*j
✌
✆
✞
ans =
50
✌
✆
Multiplication of vector and scalar:
✞
-- [1 ,2]*4
✌
✆
✞
ans =
4. 8.
✌
✆
Asterisk performs multiplication of matrices. A matrix multiplication is performed if the
matrix product conditions are satisfied. For two vectors or matrices, if number of columns
of first matrix is equal to the number of rows of second matrix then matrix multiplication
is carried out. See the following example.
✞
-- [1 ,2]*[4;3] // 1x2 matrix by 2x1 matrix
✌
✆
✞
ans =
10.
✌
✆
If, A and B are 2 × 2 matrices as given by
A =
1 2
3 2
; B =
2 1
3 4
Then
C = A ∗ B =
1 × 2 + 2 × 3 1 × 1 + 2 × 4
3 × 2 + 2 × 3 3 × 1 + 2 × 4
=
8 9
12 11
✞
-- a=[1 ,2;3 ,2];
2 -- b=[2 ,1;3 ,4];
-- a*b
✌
✆
✞
ans =
8. 9.
12. 11.
✌
✆
If two matrices are not equal in size then they are taken as vectors and vector multiplica-
tion is performed. It happens when second matrix is 1 × 1 type matrix. Second operand
may be a scalar too as seen above in the product of vector and scalar. See the following
example.
✞
1 -- [1 ,2]*[4]
✌
✆
83. 1.4. CORE KEYWORDS 49
✞
ans =
4. 8.
✌
✆
Consistency shall be maintained while performing vector or matrix multiplication. For
example, if second operand is a multicolumn vector then it must satisfy the condition
of matrix multiplication. See the following example, in which Scilab failed to give desire
result due to inconsistent multiplication.
✞
-- [1 ,2]*[3 ,4]
✌
✆
✞
!--error 10
Inconsistent multiplication .
✌
✆
1.3.26 Tilda
Tilda, (!) is a logical NOT. It is also used like (∼=) i.e. not equals to. It is an boolean
expression for NOT Gate.
✞
-- 2 != 3 //2 is not equal to 3.
✌
✆
✞
ans =
T
✌
✆
1.4 Core Keywords
1.4.1 Abort Process (abort)
abort interrupts current evaluation and gives the prompt. Within a pause level abort
return to level 0 prompt.
✞
-- i = 0;
2 -- while i10 do
-- disp (i);
4 -- i = i + 1;
-- if(i5)
6 -- abort;// abort key word
-- end
8 -- end
✌
✆
✞
0.
1.
2.
3.
4.
5.
✌
✆
84. 50 Scilab Core
1.4.2 Add a Demo (add demo)
This function, add demo, is used to add user define file or codes into Scilab demos.
✞
-- // Create a simple demo script
2 -- path = TMPDIR+’/foo.sce ’;
-- mputl(’disp Hello’,path )
4 -- add_demo (’My first demo ’,path )
-- // Demo can be run using Demos menu .
✌
✆
We can find ‘My first demo’ in Demos options of ’Scilab Demonstrations’.
1.4.3 Number of Arguments (argn)
argn is similar to the argc command in ‘C’. It returns the number of input/output argu-
ments in a function call. Scilab uses ‘lhs’ and ‘rhs’ for identification of inputs arguments
and output arguments. For example, in the following function
y = f(x)
left hand side (lhs) argument is y while right hand side (rhs) argument is x. Actually for
this relation, x is input argument and y is output argument. argn has three arguments.
‘0’ for both lhs (output arguments) and rhs (input arguments) strings. ‘1’ for lhs (output
arguments) only and ‘2’ for rhs (input arguments) only. Its syntax are
✞
1 -- [lhs , rhs] = argn () // for both input/output arguments
-- lhs=argn (1) // for output arguments only
3 -- rhs=argn (2) // for input arguments only
✌
✆
The given example is
✞
1 -- funcprot (0);
-- function [t] = myf(a,b,c)
3 -- [lhs , rhs] = argn (0);
-- mprintf (Nos of outputs : %dn,lhs);
5 -- mprintf (Nos of inputs : %dn,rhs);
-- outs = argn (1);
7 -- ins = argn (2);
-- mprintf (Nos of outputs : %dn,outs );
9 -- mprintf (Nos of inputs : %dn,ins);
-- endfunction
11 -- x=myf (2,2,3)
✌
✆
✞
Nos of outputs : 1
Nos of inputs : 3
Nos of outputs : 1
Nos of inputs : 3
✌
✆
85. 1.4. CORE KEYWORDS 51
1.4.4 Banner
It shows the default banner of Scilab.
✞
-- banner ()
✌
✆
1.4.5 Boolean
Boolean operators , | and ∼ are used to express the boolean relation between two
variables. These operators are used as logical AND, logical OR and logical NOT as short
cuts and as element-wise.
Shortcut And Elementwise
Before, Scilab 6.0, shortcut and elementwise boolean AND operators were same and both
were represented by . Since Scilab 6.0, shortcut and element-wise boolean operators are
now distinct. is shortcut boolean AND, while is element-wise AND operations.
Both shortcut and element-wise operators are evaluated from left to right.
✞
1 -- [1,3] [5,2] // is not defined before scilab 6.0
-- // Error shows in Scilab v 6.0
✌
✆
✞
[1,3] [5,2]
!-- error 2
Invalid factor.
✌
✆
✞
1 --[1,3] [5,2] // Works in all version of scilab
✌
✆
✞
ans =
T T
✌
✆
Shortcut || And Elementwise |
Before, Scilab 6.0, shortcut and elementwise boolean OR operators were same and both
were represented by . Since Scilab 6.0, shortcut and element-wise boolean operators
are now distinct. || is shortcut boolean OR, while | is element-wise OR operations. Both
shortcut and element-wise operators are evaluated from left to right.
✞
-- [1,3] || [5,2] // is not defined before scilab 6.0
2 -- // Error in scilab v 6.0
✌
✆
✞
[1,3] || [5,2]
!-- error 2
Invalid factor.
✌
✆
86. 52 Scilab Core
✞
1 --[1,3] | [5,2] // Works in all version of scilab
✌
✆
✞
ans =
T T
✌
✆
1.4.6 Break a Process (break)
break keyword interrupt a current loop. It is always used inside the loop functions or
switch functions.
✞
-- i = 0;
2 -- while i10 do
-- disp (i);
4 -- i = i + 1;
-- if(i5)
6 -- break;// break the loop
-- end
8 -- end
✌
✆
✞
0.
1.
2.
3.
4.
5.
✌
✆
1.4.7 Case
case is used to compare a select value with arbitrary chosen value. If select value is equal
to case value, statement inside the case is executed.
✞
-- A = 2
2 -- select A
-- case 1 then
4 -- disp (1)
-- case 2 then
6 -- disp (2)
-- else
8 -- disp (3)
-- end
✌
✆
✞
2
✌
✆
87. 1.4. CORE KEYWORDS 53
1.4.8 Kill the Value (clear)
clear kills the value of a variable. If a variable is cleared inside the loop, an error is
displayed as undefined variable.
✞
1 --i = 0;
--while i5 do
3 -- disp (i);
-- i = i + 1;
5 -- if(i2)
-- // Clear variable i. Now , in next loop variable
7 -- // i shall not be identified inside while loop .
-- clear i;
9 -- end
--end
✌
✆
✞
0.
1.
2.
while i5 do
!--error 4
Undefined variable : i
✌
✆
1.4.9 Remove a Function (clearfun)
clearfun is used to remove a function name in the table of Scilab functions.
✞
-- // Get the func ptr associated with the abs function
2 -- absptr=funptr(abs);
-- // Create an alias of the abs function
4 -- newfun(abs_bis,absptr);
-- clearfun (abs)
✌
✆
✞
ans =
T
✌
✆
1.4.10 Clear Global
clearglobal clears the global variable defined previously.
1.4.11 Compile Scilab Function (comp)
comp compiles a function in Scilab. If a function is already compiled then it gives warning.
✞
-- comp (abs ,0)
✌
✆
✞
Warning : Function is already compiled .
✌
✆
88. 54 Scilab Core
There are three options of compilation of function. Default is ‘0’ and others are ‘1’ and
‘2’.
Options Meaning
0 Default compilation value.
1 Obsolete, similar to option ‘0’.
2 Function to be compiled “for profiling”.
1.4.12 Continue
continue pass the control to the next iteration of loop. To understand the working of
continue keyword, we use same example in three different methods. In following example
two for loops are used.
✞
1 -- for j =1:1:3
-- for k=3: -1:1
3 -- if jk then
-- disp (j*k);
5 -- end
-- end
7 -- end
✌
✆
✞
2.
6.
3.
✌
✆
Now we put the continue command after disp(j*k) as
✞
1 -- for j =1:1:3
-- for k=3: -1:1
3 -- if jk then
-- disp (j*k);
5 -- continue ;
-- end
7 -- end
-- end
✌
✆
✞
2.
6.
3.
✌
✆
There is no change in output as disp put the result before continue encountered. Again
if we put the continue command before disp(j*k) as
✞
1 -- for j =1:1:3
-- for k=3: -1:1
3 -- if jk then
-- continue ;
89. 1.4. CORE KEYWORDS 55
5 -- disp (j*k);
-- end
7 -- end
-- end
✌
✆
There is no output as continue pushes to the next iterator before disp put the result in
output window.
1.4.13 Debug
debug is used to find the errors and bad scripting in a code script. There are five levels
of debugging, ranged from -1 to 4. This range defines the level of debugging.
✞
-- debug(-1)
✌
✆
✞
ans =
1
✌
✆
1.4.14 Delete Break
delbpt is used to delete a break point from the function. To set a breakpoint for function
‘foo’, we use setbpt. The setbpt sets break point one over other as stack in setbpt list. On
call of delbpt the top most breakpoint is deleted from the stack.
✞
-- setbpt(’foo’ ,1); // set first break point at 1
2 -- setbpt(’foo’ ,10);// set second break point at 10
-- delbpt(’foo’ ,10);// deleted second break point at 10
4 -- dispbpt () // show first break point at 1
✌
✆
✞
Breakpoints of function : foo
1
✌
✆
1.4.15 Display Break Points (dispbpt)
dispbpt is used to show a break point of a function from the setbpt list.
✞
-- setbpt(’foo’ ,1); // set first break point at 1
2 -- setbpt(’foo’ ,10);// set second break point at 10
-- delbpt(’foo’ ,10);// deleted second break point at 10
4 -- dispbpt () // show first break point at 1
✌
✆
✞
Breakpoints of function : foo
1
✌
✆
90. 56 Scilab Core
1.4.16 Do
do keyword is used to execute a group of statements or programming script until unless
a condition is being satisfied. do keyword is always used with while function. The scope
of do keyword is from the next line of statement to the corresponding end keyword.
✞
-- i = 0
2 -- while i5 do
-- disp (i);
4 -- i = i + 1;
-- end
✌
✆
✞
ans =
0
1
2
3
4
✌
✆
1.4.17 Edit
Function edit is used to open a existing file or existing function in the Scilab editor or
open editor for definition of new function.
✞
-- // open editor with text of existing function
2 -- edit (’edit ’)
-- // open editor for a new function
4 -- edit (’myF’)
✌
✆
The argument of this function is either a function name or a file name as absolute path.
✞
-- // open the file bar.sci in TMPDIR directory
2 -- edit ( fullfile (TMPDIR+’bar.sci’))
✌
✆
An existing file may be open in the Scilab editor with reaching to specific line of the
script. The script line is specified by the second argument. Second argument is a pure
integer value.
✞
-- edit (’test_run ’, 123)
✌
✆
User defined function can also be open in default Scilab editor with the function scripts.
✞
1 -- function a=b(c)
-- c=1;
3 -- endfunction
-- edit (’b’)
✌
✆
This will open function ’b’ in Scilab editor.
91. 1.4. CORE KEYWORDS 57
1.4.18 Else
else is the false case of if condition. If a condition is not satisfied, statement in else
section are executed. Another if-else loop can be initiated in the statement section of
else as a nested loop.
✞
-- i = 5;
2 -- j = 10;
-- if(i==j)
4 -- disp (T);
-- else
6 -- disp (F);
-- end
✌
✆
✞
T
✌
✆
1.4.19 Else-If
elseif is used as false case of if with new if conditions. It is also used as chain conditional
case. A nested loop can also be used within the elseif statement portion.
✞
1 -- i = 5;
-- j = 10;
3 -- if(ij)
-- disp (L);
5 -- elseif(ij)
-- disp (G);
7 -- elseif(i==j)
-- disp (E);
9 -- end
✌
✆
✞
L
✌
✆
1.4.20 End
end keyword is used to terminate a conditional statement. end is used for the following
purposes
1. To finish a condition.
2. To finish a function definition by using endfunction for Scilab version 5.5 and prior
or using end only since Scilab 6.0 and later.
3. To finish a statement.
✞
1 -- i = 5;
-- j = 10;
3 -- if(ij)
92. 58 Scilab Core
-- disp (L);
5 -- elseif(ij)
-- disp (G);
7 -- elseif(i==j)
-- disp (E);
9 -- end
✌
✆
✞
L
✌
✆
1.4.21 Catch Error (errcatch)
errcatch is used to trap an error. This function is removed since Scilab 6.
✞
1 -- errcatch (1,’pause’);
✌
✆
The first variable in errcatch is number that is used to trap an error. If it is less than 0
then all errors are catch and if it is grater than 0 then errcatch traps that specific error
assigned to this number. Second variable is action in string form that is to be performed
if error catches. The actions are either ‘pause’, ‘continue’, ‘kill’ or ‘stop’.
1.4.22 Clear The Error (errclear)
In scilab, error table contains the list of all errors being rose during the operation. errclear
is used to clear a specific or all errors from the error list. It is used like
✞
1 -- errclear (n);
✌
✆
Where ‘n’ is the error number. If ‘n’ is positive (n 0), it is the number of the cleared
error. If (n ≤ 0) then all errors are cleared (default case).
1.4.23 Error
error function is standard way of printing errors during the process by Scilab. A cus-
tomised error may also be constructed by using this function. It accepts two arguments,
in which first argument is message and second argument is error number associated with
message. It is used like
✞
1 -- error(message )
-- error(message , n)
3 -- error(n)
-- error(n, message )
5 -- error(n, pos )
✌
✆
Here ‘message’ is the error message. ‘n’ is error number and ‘pos’ is a parameter of error
message. By default ‘n’ is ‘10000’.
✞
1 -- error(! This value must be an integer !, 10)
✌
✆