Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
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
Gianluca Antonelli
Scilab 5.3
Variables
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
Gianluca Antonelli
Scilab 5.3
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
Gianluca Antonelli
Scilab 5.3
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.
Gianluca Antonelli
Scilab 5.3
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
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
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
Gianluca Antonelli
Scilab 5.3
Load/Save data
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 )
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
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
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
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
Gianluca Antonelli
Scilab 5.3
Dynamic Systems
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
--> B = [ -1; 1]; --> C = [ -1 0]; --> D =0; --> Sss = syslin ( c ,A ,B ,C , D )
Gianluca Antonelli
Scilab 5.3
Sss
! lss
- 5. 6.
- 1. 1.
Gianluca Antonelli
Scilab 5.3
- 1. 0.
0. 0. Sss (7) = Time domain = c --> typeof ( Sss ) ans = state - space
Gianluca Antonelli
Scilab 5.3
Conversion ss to/from tf
Conversions are subtles, refer to dynamic systems textbooks Aected by round-o errors See minss, minreal
Gianluca Antonelli
Scilab 5.3
Gianluca Antonelli
Scilab 5.3
Gianluca Antonelli
Scilab 5.3
--> 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
Gianluca Antonelli
Scilab 5.3
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
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
Gianluca Antonelli
Scilab 5.3
Bode
bode(); gainplot()
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
Gianluca Antonelli
Scilab 5.3
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
Gianluca Antonelli
Scilab 5.3
Gianluca Antonelli
Scilab 5.3
Run Xcos
Gianluca Antonelli
Scilab 5.3
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?
Gianluca Antonelli
Scilab 5.3
What is a superblock?
Gianluca Antonelli
Scilab 5.3
Palettes
A library of blocks
Gianluca Antonelli
Scilab 5.3
A basic diagram
Gianluca Antonelli
Scilab 5.3
A basic diagram
Gianluca Antonelli
Scilab 5.3
A basic diagram
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
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
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