Você está na página 1de 68

Introduction to Scilab 5.

3 and Xcos
Gianluca Antonelli
Universit` a degli Studi di Cassino antonelli@unicas.it http://webuser.unicas.it/lai/robotica http://www.docente.unicas.it/gianluca antonelli

Gianluca Antonelli

Scilab 5.3

License

Copyright (c) 2011 GIANLUCA ANTONELLI. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is available at www.gnu.org

Gianluca Antonelli

Scilab 5.3

Introduction
The open source platform for numerical computation

Available at http://www.scilab.org

Gianluca Antonelli

Scilab 5.3

The others

Matlab Octave Freemat Scicoslab ...

Gianluca Antonelli

Scilab 5.3

Running Scilab

From Linux or Windows just selecting the application from the Graphical Menu From Linux also from the shell typing ./scilab from the correct path

Gianluca Antonelli

Scilab 5.3

Scilab command line

Scilab command line is denoted with the symbol


-->

It accepts variables declaration, expressions, script and function calls Scilab scripts and functions are ASCII les To re-use a previous command type the up arrow

Gianluca Antonelli

Scilab 5.3

Help

help opens a new window where browse for commands and search

keywords
help name fun opens the help window directly at the page corresponding to the function name fun

An on-line help is available http://www.scilab.org/product/man/ A comparison between Matlab/Scilab function in


http://www.scilab.org/product/dic-mat-sci/M2SCI doc.htm

Gianluca Antonelli

Scilab 5.3

Variables

A variable can be dened simply with the syntax


--> name = i n s t r u c t i o n ;

The ; symbol prevents to print on screen the results Standard rules common to languages as C or Pascal apply for Scilab variables Names are case sensitive: a=A To see the variable value just type its name on the prompt or use the tool browsevar()

Gianluca Antonelli

Scilab 5.3

Workspace

Variables and functions stay in the workspace commands of general use are
who who_user clear load save diary b r o w s e v a r ()

Gianluca Antonelli

Scilab 5.3

Variable types

A specic set of operators is available for the corresponding type Common types are
constant , polynomial , function , handle , string , boolean , list , rational , state - space , sparse , b o o l e a n sparse

The type of a variable can be obtained


typeof ( v a r i a b l e _ n a m e )

Gianluca Antonelli

Scilab 5.3

Write protected variables

Certain variables are predened and write-protected %i i = 1 immaginary unit %pi = 3.1415927 . . . pi grek %e e = 2.718281 . . . number of Nepero %eps = 2.2 1016 precision (machine dependent) %inf innity %nan NotANumber %s s polynomial variable %z z polynomial variable %t true boolean variable %f false boolean variable

Gianluca Antonelli

Scilab 5.3

Matrices

To insert a matrix the syntaxes are


--> A = [1 , 2 , 3; 4 , 5 , 6; 7 , 8 , 9]; --> A = [1 , 2 , 3 - - >4 5 6 - - >7 ,8 ,9] A = 1. 4. 7. 2. 5. 8. 3. 6. 9.

Gianluca Antonelli

Scilab 5.3

Scalars and vectors

Scalars and vectors are matrices!


--> a =[1 3 5]; --> size ( a ) ans = 1. 3.

-->b =3; size ( b ) ans = 1. 1.

Gianluca Antonelli

Scilab 5.3

Incremental vectors

--> x =1:4 x = 1.

2.

3.

4.

--> x = 1 : 2 : 1 0 x = 1. 3.

5.

7.

9.

Also available linspace(x0,xf,npti) and logspace(...)

Gianluca Antonelli

Scilab 5.3

Accessing matrix elements

A(i,j) access the element in row i and column j A(2,4:6) select the columns from 4 to 6 of the second row B=A(1:3,4:6) assign to B the selected submatrix A(:,5) select the column 5 A([1 3],5) select a 1x2 vector of two elements: A(1,5) and A(3,5)

Gianluca Antonelli

Scilab 5.3

Matrix operations

A wide number of operations are available, in addition to the basic operations such as sum, product, transpose, matrix esponential, inverse, rank, kernel, etc. A library of operations are available under the Linear Algebra section of the help Some operations are also dened to be performed on the single elements of the matrix

Gianluca Antonelli

Scilab 5.3

Polynomial operations

Dening a variable as polynomial allows to access several specic operations


--> my_pol = 3*% s ^2 + 2*% s my_pol = 2 2s + 3s --> roots ( my_pol ) ans = 0 - 0.6666667

Gianluca Antonelli

Scilab 5.3

Polynomial operations

Dene a polynomial from the coecient: poly Dene a polynomial from its expression (previous example) Dene a polynomial by operations on elementary polynomials Extract the coecients of a polynomial: coeff Evaluate the polynomial in one single point: horner Symbolic substitution of the polynomial variable with another

Gianluca Antonelli

Scilab 5.3

Rational operations

Rational functions are the division between two polynomials Several commands dened to their use similar to the polynomial type

Gianluca Antonelli

Scilab 5.3

Script

It is a collection of commands saved in a single ASCII le whose extension is conventionally .sce It is executed typing
--> exec ( f i l e _ n a m e. sce ) ;

Gianluca Antonelli

Scilab 5.3

Functions

A function is a piece of code returning an output given several inputs whose syntax is
f u n c t i o n [ y1 ,... , ym ] = fun ( x1 ,... , xn ) commands endfunction

Several functions can be saved in a single ASCII le whose extension is conventionally .sci

Gianluca Antonelli

Scilab 5.3

Functions

User-dened functions are not available until not explicitely called by


getf(name file.sci)

Loading, saving or clearing the variables causes Scilab to do the same to the functions! Functions see all the workspace Inputs are passed by reference if the function does not change their value otherwise by copy

Gianluca Antonelli

Scilab 5.3

Programming

Common programming constructions


for, end while, end if, then, else, end select, case, else, end

Logical operators: & |


& and | or not

Gianluca Antonelli

Scilab 5.3

Load/Save data

Data in the workspace can be saved in a binary le:


save(name file)

Data previously saved can be loaded in the workspace:


load(name file)

C and Fortran-like commands are available to save formatted data in ASCII les Data saved with older version of Matlab can also be loaded

Gianluca Antonelli

Scilab 5.3

Graphics

Graphics commands are executed on windows dierent from the main one (the Scilex window) To create a new graphic window
xset ( window , num ) scf ( num )

General windows-related commands are


clf ( num ) , xclear ( num ) , x s e l e c t () , xdel ( num )

Gianluca Antonelli

Scilab 5.3

Plot2d

The command plot2d draws bidimentional curves It uses the current graphic window superimposing the curves It creates a new graphic window if none is open Several drawing possibilities depending on the syntax
plot2d ([ x ] ,y , < opt_args >)

Gianluca Antonelli

Scilab 5.3

Plot2d

plot2d(x,y) where x and y are vectors of the same size draws a curve with x data in the horizontal and y data in vertical axes plot2d(y) where y is a vector assumes x=1:n plot2d(x,y) where x is a vector and y is a matrix assumes the same horizontal data for the columns of y plot2d(x,y) where x and y are matrices draws each column of y versus the corresponding column of x

Gianluca Antonelli

Scilab 5.3

Plot2d

Let us draw a sinusoidal signal with = 5 and phase = /4 for two periods
--> w =5; f =% pi /4; tf =(4*% pi -% pi /4) / w ; --> t = l i n s p a c e (0 , tf ,100) ; plot2d (t , sin ( w * t + f ) )

Refer to the syntax to change colors, add symbols, add labels, title, etc.

Gianluca Antonelli

Scilab 5.3

Understanding a graphic window

The graphic window is an object containing several childs: gure, axis, curves In the previous example the relationship is given by
Figure (1) - Axes (1) - C o m p u n d (1) - P o l y l i n e (1)

Gianluca Antonelli

Scilab 5.3

Modyng the graphic properties

A GUI opens with the command xset() that allows to change the graphic context From the graphic window it is possible to browse the properties of the objects under the menu [edit] From a script/function it is possible to have an handle to an object and modyng its parameters
h = gcf () h = gca () h = gce ()

Gianluca Antonelli

Scilab 5.3

More axes on the same gure

The subplot(ijk) command divides the gure in a matrix of i rows and j columns and select the k element
s u b p l o t (211) ; plot2d ( ya ) s u b p l o t (212) ; plot2d ( yb )

Gianluca Antonelli

Scilab 5.3

Exporting your gure

Several commands exist to export your gure such as


xs2eps , xs2fig , xs2gif , xs2ppm , xs2ps

and, only for Windows,


xs2bmp , xs2emf

Gianluca Antonelli

Scilab 5.3

Dynamic Systems

A powerful tool to the numerical study of


Input-Output dynamic systems Input-State-Output dynamic systems Feedback analysis Feedback control design

Gianluca Antonelli

Scilab 5.3

Transfer function

--> s =% s ; --> num =1+ s ; den =( s +2) *( s +3) ; --> P = syslin ( c , num , den ) P = 1 + s --------2 6 + 5s + s --> typeof ( P ) ans = rational

Gianluca Antonelli

Scilab 5.3

Transfer function

--> z =% z ; --> Pd = syslin ( d ,1 ,z -0.5) Pd = 1 ------- 0.5 + z --> typeof ( Pd ) ans = rational

Gianluca Antonelli

Scilab 5.3

State space representation

--> A = [ -5 -1 --> 6 0];

--> B = [ -1; 1]; --> C = [ -1 0]; --> D =0; --> Sss = syslin ( c ,A ,B ,C , D )

Gianluca Antonelli

Scilab 5.3

State space representation

Sss

= Sss (1) ( state - space system :) D X0 dt !

! lss

- 5. 6.

Sss (2) = A matrix = - 1. 0. Sss (3) = B matrix =

- 1. 1.

Gianluca Antonelli

Scilab 5.3

State space representation

- 1. 0.

Sss (4) = C matrix = 0. Sss (5) = D matrix = Sss (6) = X0 ( i n i t i a l state ) =

0. 0. Sss (7) = Time domain = c --> typeof ( Sss ) ans = state - space

Gianluca Antonelli

Scilab 5.3

Conversion ss to/from tf

Conversions are always possible


tf2ss ss2tf

Conversions are subtles, refer to dynamic systems textbooks Aected by round-o errors See minss, minreal

Gianluca Antonelli

Scilab 5.3

Extract information from tf

The tf is a rational and all the corresponding functions can be applied:


--> out = roots ( P . den ) out = - 2. - 3.

Gianluca Antonelli

Scilab 5.3

Extract information from ss

Extract, e.g., the dynamic matrix


Sss . A

Extract all matrices


[A ,B ,C , D ]= abcd ( Sss ) ;

Gianluca Antonelli

Scilab 5.3

Smart view of ss systems

--> s s p r i n t( Sss ) . | -5 -1 | | -1 | x = | 6 0 |x + | 1 |u y = | -1 0 |x

Gianluca Antonelli

Scilab 5.3

Pole-zero map - continuous time


plzr(P);sgrid

Gianluca Antonelli

Scilab 5.3

Pole-zero map - discrete time


plzr(P);zgrid

Gianluca Antonelli

Scilab 5.3

Root locus
The basic command is evans(Stf)

Gianluca Antonelli

Scilab 5.3

Root locus
Default points useless! use evans(Stf,kmax)

Gianluca Antonelli

Scilab 5.3

Root locus design

The basic operation needed to design with the root locus tool is to calculate the value of k that corresponds to a certain point in the locus:
--> k = -1/ real ( horner ( Stf ,[1 ,% i ]* locate (1) ) ) locate returns the coordinates of a point in the graphic selected with the mouse horner computes a rational or polynomial in a given point

Gianluca Antonelli

Scilab 5.3

Nichols
black(); chart()

Gianluca Antonelli

Scilab 5.3

Nichols

The curve is parametrized according to a constant range(!)


Continuous time: [103 , 103 ] Hz Discrete time: [103, 0.5]

Better to use the whole syntax assigning frequency range:


black ( sl , [ fmin , fmax ] [ , step ] [ , c o m m e n t s ])

Gianluca Antonelli

Scilab 5.3

Bode

bode(); gainplot()

Same considerations done for the Nichols diagram Better to use:


bode ( sl , [ fmin , fmax ] [ , step ] [ , c o m m e n t s ])

Gianluca Antonelli

Scilab 5.3

Nyquist

nyquist(); m circle()

Same considerations done for the Nichols and Bode diagrams Better to use:
n y q u i s t( sl , [ fmin , fmax ] [ , step ] [ , c o m m e n t s ])

Gianluca Antonelli

Scilab 5.3

Horner & Phasemag

horner() evaluates a polynomial/rational in a point

Evaluate F (j ) (in dB and deg) with = 5


--> F = syslin ( c ,1 ,% s +2) ; --> out = horner (F ,5*% i ) out = 0.0689655 - 0.1724138i - - >[ phi , db ]= p h a s e m a g( out ) db = - 14.62398 phi = - 68.198591

Gianluca Antonelli

Scilab 5.3

A nal birds-eye view

Stability margins (g margin, p margin) Continuous-discrete time conversion (cls2dls) Simple numerical simulation of dynamics systems Numerical resolution of dierential equations (ode) Observability, controllability, Kalman lter Controller design commands

Gianluca Antonelli

Scilab 5.3

Xcos

Graphical dynamic system simulator


Library of blocks Continuous/discrete time systems Add custom blocks in C/Fortran/Scilab Generate C code Real-time HIL tests Hard real-time executable

Gianluca Antonelli

Scilab 5.3

How it looks like

Gianluca Antonelli

Scilab 5.3

Run Xcos

Within the Scilab environment


From the command prompt --> xcos Menu -> Applications -> Xcos

Gianluca Antonelli

Scilab 5.3

Continuous time on a PC?

Xcos emulates continuous-time dynamic systems via numerical algorithms (see discretization on textbooks)
Conceptually similar to the dierential vs dierence equations

In alternative Xcos executes blocks when an activation input is given Activation signals are used also for discrete-time systems

Gianluca Antonelli

Scilab 5.3

What is a block?

A graphical function with (eventual)


Input State Output Scalar/vectorial quantities Continuous/discrete time Activation inputs Activation outputs

Gianluca Antonelli

Scilab 5.3

What is a superblock?

Is a block containing other blocks


Same concept as functions inside other functions Improve readability of the diagram

Gianluca Antonelli

Scilab 5.3

Palettes

A library of blocks

Gianluca Antonelli

Scilab 5.3

A basic diagram

Drag-and-drop a source. . . a transfer function and a scope

Gianluca Antonelli

Scilab 5.3

A basic diagram

the whole diagram

Gianluca Antonelli

Scilab 5.3

A basic diagram

Select all the parameters

Gianluca Antonelli

Scilab 5.3

A basic diagram

Run!

Gianluca Antonelli

Scilab 5.3

Blocks parameters

Avoid the use of constants in the blocks parameters Use variables instead:
put all the variables in a le (var.sce) open Diagram -> Context and write exec(var.sce) use the variables in the blocks

In alternative global variables

Gianluca Antonelli

Scilab 5.3

Importing data

From proper blocks Reading from binary or formatted les From the workspace

Gianluca Antonelli

Scilab 5.3

Exporting data

To graphic windows (scope. . . ) To binary or formatted les To the workspace Specic graphic output for matrices

Gianluca Antonelli

Scilab 5.3

Beyond the simple diagram

The Debug block Creation of custom blocks Use of C functions Generation of code C from a Xcos diagram Batch simulation of diagrams

Gianluca Antonelli

Scilab 5.3

Você também pode gostar