0% found this document useful (0 votes)
111 views37 pages

A Little Book On Scilab (Alpha) : Allan Jay L. Baco September 21, 2010

This document is a summary of a book about using Scilab as a numerical tool, programming language, visualization tool, and modeling tool. The book is divided into four parts that cover basic computation in Scilab, matrices, built-in functions, scripts and functions, plotting, dialog boxes, GUI programming, and modeling with Xcos. It is intended as a quick reference guide for using Scilab rather than an in-depth tutorial.

Uploaded by

allanj29b
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views37 pages

A Little Book On Scilab (Alpha) : Allan Jay L. Baco September 21, 2010

This document is a summary of a book about using Scilab as a numerical tool, programming language, visualization tool, and modeling tool. The book is divided into four parts that cover basic computation in Scilab, matrices, built-in functions, scripts and functions, plotting, dialog boxes, GUI programming, and modeling with Xcos. It is intended as a quick reference guide for using Scilab rather than an in-depth tutorial.

Uploaded by

allanj29b
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

A Little Book on Scilab (alpha)

version 0.0.2

Allan Jay L. Baco

September 21, 2010


2
Contents

I Scilab as Numerical Tool 7


1 Introduction 9
1.1 Basic Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 Calling a Scilab function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Matrix Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Boolean in Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.6 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 More on Matrices 13
2.1 Matrix Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Matrix Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 AutoGeneration of Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3 Built-in Constants & Functions 15


3.1 Built-in Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Hyperbolic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.4 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

II Scilab as Programming Language 17


4 Scripts 19
4.1 Single-line Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2 Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.3 Designing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

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

6 Interfacing with Java 23

3
4 CONTENTS

III Scilab as Visualization Tool 25


7 Plotting 27
7.1 Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.2 Function plot2d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.3 Customizing Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.4 Animating Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

8 Dialog Boxes 29

9 GUI Programming 31

IV Scilab as Modeling Tool 33


10 Xcos Modeling 37
Preface

Warning: This is an alpha version.

Inspired by the little book titled Elements of Style by


William Strunk Jr, et.al., the author made his own version
of the little book series on some engineering tools and
course. This is a the first of its kind, a little book on
Scilab. This is not a detailed, idiot’s approach of teaching
Scilab but is intended to be used as a quick reference.
This little book is divided into four parts.
Part I: introduces Scilab as a numerical tool.
Chapter 1 introduces the basic capabilities of Scilab
such as scalar and matrix computation, complex numbers,
polynomials.
Chapter 2 extends the matrix manipulations.
Chapter 3 provides a dedicated chapter that collects
some elementary functions and other built-in constants
and functions mostly used in numerical computation.
Part II: introduces Scilab as a programming language.
Chapter 4 starts the programming with Scripts.
Chapter 5 extends the programming by creating a user-
defined functions.
Chapter 6, Java interfacing, shows how to connect to
Java and vice versa making Scilab a computing engine and
Java as extended and more dynamic GUI application.
Part III: introduces Scilab as a visualization tool.
Chapter 7 adds more plotting functions and customiz-
ing the plot with additional animation capabilities.
Chapter 8 discusses the built-in dialog boxes.
Chapter 9 adds more flexible capabilities of program-
ming a user-interactive Scilab applications by introduc-
ing components useful in generating graphical view of the
program.
Part IV: introduces Scilab as a modeling tool.
Chapter 10 discusses the basic of Xcos, a modeling tool
in Scilab. This modeling tool is a causal type where in-
volving transfer functions and signal connections.

5
6 CONTENTS
Part I

Scilab as Numerical Tool

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

ans = Command Description


3. + 4.i roots Returns the roots a polynomial.
coeff Returns the coefficients of a
polynomial in ascending order.
numer Returns the numerator of a ra-
Some Complex Functions tional polynomial.
denom Returns the denominator of a
Some functions used to manipulate a complex number. rational polynomial.
degree Returns the order or degree of a
Command Description polynomial.
complex Creates a complex number from pol2str Converts a polynomial into
given real and imaginary. string.
abs Absolute value of a complex eval Evaluates a polynomial ex-
number. pressed in string form.
real Real part. varn Change the variable used in the
imag Imaginary part. polynomial.
conj Complex conjugate.
polar Converts the rectangular into Evaluating a function in string form
polar form returning the mag-
A math function can be declared in string form and eval-
nitude and angle in radians.
uated using eval. Note that the variable used by the
function must be assigned by a numeric value first before
evaluation.
1.7 Polynomials ->f=’x^2+3*x-10’
f =
In Scilab, there are two built-in variables used for polyno- x^2+3*x-10
mial construction, namely %z and %s. Basically, these are
the variables used in Laplace transform and z-transform -->x=10
and that serve its purpose. This variables are handled x =
in Scilab as variable and are evaluated symbolically, as 10.
shown below.
-->eval(f)
ans =
-->%s+%s*%s+10 120.
ans =
2 Here, if the function is declared in string, the variable is
10 + s + s no longer limited to s and z only.

-->%z/(3*%z^3+5)
ans =
z
-----
3
5 + 3z

Some Polynomial Functions


Some functions used to manipulate a polynomial.
12 CHAPTER 1. INTRODUCTION
Chapter 2

More on Matrices

As shown in the introduction, matrices are created by Illustration 1:


enclosing an array of numbers in bracket. In this chap- Let A be an n × n matrix and aij be an ele-
ter, more matrix manipulation are shown to illustrate the ment in A located at row i and column j, then
capabilities of Scilab in handling matrix. The type is not A([3, 1, 2], [5, 4]) is expanded to
limited to real only but includes complex and symbolic
variable, s and z.
 
a35 a34
 a15 a14 
a25 a24
2.1 Matrix Manipulation
Illustration 2:
Accessing an Element Not only scalars and vectors are allowed as ar-
gument but a matrix of rows and columns is also
To access an element, parenthesis is used. Similar to func-
valid. Such command is A([1, 2; 3, 4], [1, 2; 3, 4]])
tion call treating the matrix variable as a function and the
pretty printed as
argument is the position of the element. Position assign-
ment in matrix is a pair (r, c) where r is the row and c 
1 2
 
1 2

for column position. A ,
3 4 3 4
Special case:
and interpreted as
If the argument is a single element, then the
positioning of a matrix is counted from top to
       
1   1  
bottom, then left to right.  a , 1 3 a , 2 4
  3  3 
    
 2   2   
Additionally, there are two commands the user will find a , 1 3 a , 2 4
4 4
very useful in simplified accessing of submatrix. Shown
in the table below.
which when expanded becomes
Command Description
 
: All elements. a11 a13 a12 a14
$ Last element.  a31 a33 a32
 a34 

 a21 a23 a22 a24 
a41 a43 a42 a44
Accessing a Submatrix
Syntax in accessing a submatrix follows the same princi- Replacing an Element
ple with accessing a single element, a call to the matrix
variable with a pair of argument r, c where r and c are Basic idea is to access an element, then assign a new value
both vectors. using the assignment operator =.

13
14 CHAPTER 2. MORE ON MATRICES

Replacing a Submatrix Command Description


and Returns true if all elements are
Basic idea is to access the submatrix, then assign a new nonzero.
submatrix with the same dimension. or Returns true if atleast one ele-
ment is nonzero.
find Returns the indices of nonzero
Deleting a Submatrix element.
Deleting a single element in Scilab is not allowed. The
concept of deleting elements is to shrink the original di- Replacing Elements using Conditions
mension. Either the whole column or whole row must Replacing an element or a submatrix of a parent matrix
be deleted. To delete a submatrix, just assign an empty follows a basic rule: access the element/submatrix, then
matrix, []. assign a new element/submatrix with the same size. Such
replacement applies to a fixed position of an element.

2.2 Matrix Operation


2.3 AutoGeneration of Matrix
Basic Matrix Functions Command Description
: Increments a number a specified
Functions used commonly in a matrix. step. Step defaults to 1.
Command Description eye Identity matrix.
’ Transpose of a matrix. Applies ones Matrix with elements equal to 1.
conjugate to a complex matrix. zeros Matrix with elements equal to 0.
det Determinant of a matrix. rand Generates a random number be-
inv Inverse of a matrix. tween 0 and 1.
lu Decomposes a matrix into lower grand Generate a random integer
and upper triangular matrix. number.
diag Extracts the diagonal of a ma- linspace Linearly-spaced vector.
trix. logspace Logarithmically-spaced vector.
rref Reduced-row echelon form of a testmatrix Generates a test matrices such
matrix. as magic square.
toeplitz Generates a toeplitz matrix.
Note:

1. For diag, if the argument is a vector, it returns a


matrix whose diagonal elements are the input vector
and zero elsewhere.
2. For rref, the coefficient is transformed into an iden-
tity matrix and the constant vector becomes the so-
lution of the equations. If the bottommost row are
all zero, then the solution is infinite. If the bottom-
most row of the coefficient matrix are all zero but not
the constant matrix, then the solution is inconsistent
or has no solution.

Element Lookup in a Matrix


Searching an element in a matrix is very useful when ver-
ifying for a matrix if a certain element is present.
Chapter 3

Built-in Constants & Functions

3.1 Built-in Constants


Command Description
%eps Very small number that rounds
off to zero.
%e 2.71828. . .
%pi 3.1415. . .

3.2 Trigonometric Functions


Command Description
cos Cosine.
sin Sine.
tan
acos
asin
atan

3.3 Hyperbolic Functions


Command Description
cosh
sinh
tanh
acosh
asinh
atanh

3.4 Miscellaneous Functions


Command Description

15
16 CHAPTER 3. BUILT-IN CONSTANTS & FUNCTIONS
Part II

Scilab as Programming Language

17
Chapter 4

Scripts

Console environment is designed for immediate re- Branching Expressions


sult computation, though it handles multiline statement.
Most commands executed under console are single-line if-end
statements, sometimes for debugging purposes for large Syntax:
programs.
if <cond>
A statement is the basic fundamental code in <statements>
Scilab programming. It may or may not be elseif <cond>
ended by a semicolon but a newline is required <statements>
to end a statement. else
<statements>
Statements can be single-line or multiline. Multiline are
end
usually called block statements because it is enclosed in
a block.
select-end
Syntax:
4.1 Single-line Statements
select <expr>
A single-line statement is a statement that usually occu- case <expr>:
pies a single line code. It is categorized into three: (1) <statements>
variable assignment, (2) displaying output, and (3) call- break;
ing a function. Variable assignment was already discussed otherwise:
in the first chapter. It involves an equal sign, an expres- <statements>
sion, and the stored variable. Displaying output is simple end
as omitting a semicolon. Calling a function is commonly
used in programming. A function may or may not return Looping Expressions
values, it processes a certain input and returns result or
if not, it just display them. while-end
Syntax:
4.2 Block Statements while <cond>
<statements>
Block functions consists of multiple statements. It does end
a certain function such as branching out expressions and
looping evaluation of expressions. Block statements are
for-end
usually categorized into two: (1) branching expressions,
and (2) looping expressions. Syntax:

19
20 CHAPTER 4. SCRIPTS

for <var>=<vector> Algorithm


<statements>
f (x) = 0
end
xl + xu
xr =
Scripts consists of these statements, single-line and 2
block. The basic function of script is it processes and
computes the input data and display them in a manner
readable to the user such as matrix in rows and columns,
polynomials with proper exponents display, and rational
expressions in upper numerator and lower denominator
placement.
Basic arrangement of scripts:

input data

process data

display result

Input data constitutes the user runtime input,


stored in a variable, or read from an external
text file.
Process data is a combination of single-line
and block statements designed to process a cer-
tain data and follows algorithm to achieve the
desired result.
Displaying result can be one of the following,
console display, save to an external text file, or
to a spreadsheet file.

4.3 Designing a Program


Program designing is very important to consider when
creating an application. A good algorithm optimizes the
performance and successfully achieved the desired accu-
racy. Knowledge of the syntax is halfway of creating a
program. The arrangement of code completes the pro-
gramming.

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

5.1 Reading Input


5.2 Assigning Output
5.3 Calling Function
5.4 Variable Input
5.5 Variable Output
5.6 Spreadsheets
Internal data are lost after the program exits. Storing
data can be done in an external text file or more effi-
cient when storing in an organized spreadsheet. Scilab
has a built-in function that interfaces with Microsoft Ex-
cel spreadsheet in Windows.

21
22 CHAPTER 5. FUNCTIONS
Chapter 6

Interfacing with Java

23
24 CHAPTER 6. INTERFACING WITH JAVA
Part III

Scilab as Visualization Tool

25
Chapter 7

Plotting

7.1 Basic Plotting 7.2 Function plot2d

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’)

,where x,y are the data and ’scm’ is a string con-


taining the options as listed in the table above.

Plotting previously discussed uses the Matlab-like plot


function for Matlab user to get familiar with Scilab plot-
ting easily. This chapter will introduce a new plotting
function that is more flexible than plot function.

27
28 CHAPTER 7. PLOTTING
Chapter 8

Dialog Boxes

Dialog boxes are popup dialogs that momentarily ap-


pear that serves as a notification.

29
30 CHAPTER 8. DIALOG BOXES
Chapter 9

GUI Programming

Graphical user interface, popularly known as GUI, is


way of visualizing a program for more interactive appli-
cation.

31
32 CHAPTER 9. GUI PROGRAMMING
Part IV

Scilab as Modeling Tool

33
35

Scilab does not only compute matrices and numbers,


not only a programming language but it can do more such
as modeling a system. Modeling physical systems is block
diagram has been the solution of control systems design-
ing. In Scilab, handles such modeling using its built-in
tool, Xcos.
36
Chapter 10

Xcos Modeling

Xcos is a tool built-in Java using Jgraph library. Its


former name was Scicos.

37

You might also like