A Little Book On Scilab (Alpha) : Allan Jay L. Baco September 21, 2010
A Little Book On Scilab (Alpha) : Allan Jay L. Baco September 21, 2010
version 0.0.2
2 More on Matrices 13
2.1 Matrix Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Matrix Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 AutoGeneration of Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5 Functions 21
5.1 Reading Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 Assigning Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.3 Calling Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.4 Variable Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.5 Variable Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.6 Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3
4 CONTENTS
8 Dialog Boxes 29
9 GUI Programming 31
5
6 CONTENTS
Part I
7
Chapter 1
Introduction
Scilab is not just a tool but also a programming lan- 1.2 Calling a Scilab function
guage, a high-programming language with an environ-
ment and editor of its own. Programming environment Functions in Scilab accepts and returns argument in ma-
is not limited to text programming but also a graphical trix,vector or scalar form. Input and output arguments
modeling used in control systems. Add these capabilities can be multiple, single or variable list. If the function
to a matrix-based tool makes Scilab an engineer’s choice returns multiple arguments, when calling that function,
simulation tool. This chapter discusses the basic of Scilab user must specify the exact order of the arguments. If
as a tool. no specified argument, then the function returns the first
output argument.
Illustration:
1.1 Basic Computation
If a function myf un returns arguments
Basic computation covers the scalar computation and ba- out1, out2, out3 in order. Then the proper func-
sic arithmetic operator such as +,-,*,/,\,**,^, where tion call would be [var1,var2,var3]=myfun(),
** and ^ evaluates to the same result. Basic arithmetic assuming myf un does not accept any input ar-
computation follows the following syntax: num1 op num2 gument. out1 is stored into var1, out2 into
where num1 and num2 are numbers and op is an opera- var2, and out3 into var3.
tor. Special case:
--> 3.14+5e-1-2^2 If function call does not include a vector of out-
ans = put variable, such as myfun(). Then the out1
7.64 is stored into default ans variable and the rest
of the output arguments are discarded.
Numbers
Valid numbers in Scilab are as follows: 3,3.14,3e-1. 1.3 Matrix Computation
Integers, floating-point, and scientific notation, respec-
tively. The computation above involves a scalar operand, as im-
plemented mostly by programming languages. In Scilab,
it is possible to apply an operation to an array of numbers
Variable Assignment called a matrix.
Results of every computation is stored into a default vari-
able, ans, and is overwritten every new computation. In
Matrix Construction
order to preserve the result, storing it into a different vari-
able is the solution. Variable assignment follows the basic Construction of matrix follows a basic syntax. To create
syntax: var = expr. It uses the equal sign (=) to store a matrix, just enclose the list of numbers, separated by a
the expression into the variable. comma or a space, with a pair of brackets ([]) such as
9
10 CHAPTER 1. INTRODUCTION
[1,2,3]. To add a row, just add a semicolon or press a 1.4 Boolean in Scilab
return key prior to inserting the new list of numbers of
the same length as the previously declared array such as Boolean can be either true or f alse. In Scilab, T for true
[1,2,3;4,5,6]. and F for false. When using as a command, you need to
add a percent (%) sign such as %t and %f, as true and
--> [1,2,3;4 5 6] false, respectively.
ans = Command Description
1 2 3 %t A variable that represents true
4 5 6 value.
%f A variable that represents false
value.
Matrix Operation isdef Check if variable is defined in
The basic operator in performing matrix operation are: workspace.
+,-,*. Both matrix addition and matrix subtraction fol- isempty Check if variable is empty.
lows the element-by-element operation. Matrix multipli- isnan Check if variable is not-a-
cation follows this operation: number.
isreal Check if variable is real or com-
a11 a12 b11 b12 c11 c12 plex.
∗ =
a21 a22 b21 b22 c21 c22 isinf Check if variable is infinite
isequal Check if 2 objects are equal.
where
b1j
cij =
ai1 ai2 ai3
∗ b2j
1.5 Relational Operators
b3j Relational operators are used in comparing two variables
expanding to or expression. Usually inside a conditional block. If used
to a matrix, it returns a matrix of boolean values, apply-
3
X ing condition to each element, respectively.
cij = ai1 × b1j + ai2 × b2j + ai3 × b3j = aik × bkj
Command Description
k=1
> Greater than.
Matrix division uses matrix multiplication after invert- >= Greater than or equal to.
ing1 the second operand. To illustrate, let A and B as < Less than.
matrices, <= Less than or equal to.
A/B = A × B −1 == Equal to.
~= Not equal. Same as <>.
and back-division & And. Test if both values are
true.
A\B = A−1 × B | Or. Test if atleast one value is
true.
which is a solution to the simultaneous linear equation if
B is a column vector.
1.6 Complex Numbers
Elementwise Operation Complex numbers are native number in Scilab handled
easily just like any other
√ numeric type. A complex num-
Sometimes, it is necessary to perform an element-by- ber is represented by −1 and is denoted by i (denoted
element multiplication, division and exponentiation. It by j in engineering). In Scilab, it is represented by %i.
is also possible in Scilab by affixing a dot before the op- To create a complex number, just affix %i to the number,
erator such as .* ./ .\ .^. as shown below.
1 see More on Matrices chapter for more details -->3+4*%i
1.7. POLYNOMIALS 11
-->%z/(3*%z^3+5)
ans =
z
-----
3
5 + 3z
More on Matrices
13
14 CHAPTER 2. MORE ON MATRICES
15
16 CHAPTER 3. BUILT-IN CONSTANTS & FUNCTIONS
Part II
17
Chapter 4
Scripts
19
20 CHAPTER 4. SCRIPTS
input data
↓
process data
↓
display result
Sample Program
Problem Statement
Develop a program that computes for the root using nu-
merical methods technique. Use Bisection method.
Chapter 5
Functions
Syntax:
function [<out1>,...]=<fname>(<in1>,...)
<statements>
endfunction
21
22 CHAPTER 5. FUNCTIONS
Chapter 6
23
24 CHAPTER 6. INTERFACING WITH JAVA
Part III
25
Chapter 7
Plotting
Plotting in Scilab uses 2 basic functions: plot and 7.3 Customizing Plot
plot2d. The first one is intended to emulate the syn-
tax of matlab plot and will be discussed in this section. 7.4 Animating Plot
The latter will have a dedicated chapter for it.
s Styles c Color m Marker
- solid r red + plus
– dashed g green o circle
: dotted b blue * asterisk
-. dash- c cyan . point
dotted
m magenta x cross
y yellow s square
k black d diamond
w white ^ up triangle
v down triangle
> left triangle
< right triangle
No marker (de-
fault)
Syntax:
plot(x,y,’scm’)
27
28 CHAPTER 7. PLOTTING
Chapter 8
Dialog Boxes
29
30 CHAPTER 8. DIALOG BOXES
Chapter 9
GUI Programming
31
32 CHAPTER 9. GUI PROGRAMMING
Part IV
33
35
Xcos Modeling
37