Professional Documents
Culture Documents
1 / 115
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
2 / 115
Introduction
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
3 / 115
Introduction
What is Scilab ?
What is Scilab ?
Scilab is the contraction of Scientific Laboratory. Scilab is :
a numerical computing software,
an interpreted programming environment,
used for any scientific and engineering applications,
multi-platform : Windows, MacOS et Linux,
www.scilab.org
4 / 115
Introduction
What is Scilab ?
5 / 115
Introduction
License
License
6 / 115
Introduction
Getting started
Getting started
Firstly, Scilab can be used in an interactive way by typing instructions on
the console.
7 / 115
Introduction
Getting started
A = 2;
t = [0:0.01:10];
y = A * sin (3* t );
plot (t , y );
8 / 115
Introduction
Getting started
A = 2;
t = [0:0.01:10];
y = A * sin (3* t );
plot (t , y );
8 / 115
Introduction
Getting started
2x1 + x2
x1 + 2x2 x3
=
=
=
5
0
1
A = [2 1 0 ; 4 -3 2 ; 1 2 -1];
b = [ -5;0;1];
x = inv ( A )* b
=
1.75
- 8.5
- 16.25
9 / 115
Introduction
Getting started
the console
command history
file browser
variable browser
and others : editor, graphics, help, ...
Y. Ariba - Icam, Toulouse.
10 / 115
Basics
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
11 / 115
Basics
Elementary operations
Elementary operations
Simple numerical calculations :
--> (1+3)*0.1
ans =
0.4
--> 4^2/2
ans =
8.
--> 2*(1+2* %i )
ans =
2. + 4. i
--> %i ^2
ans =
- 1.
--> cos (3)^2 + sin (3)^2
ans =
1.
--> exp (5)
ans =
148.41316
--> abs (1+ %i )
ans =
1.4142136
12 / 115
Basics
elementary operations
+
*
/
\
addition
subtraction
multiplication
right division
left division
exponents
Elementary operations
elementary functions
sin
asin
sinh
abs
exp
sign
round
cos
acos
cosh
real
log
modulo
floor
tan
atan
tanh
imag
log10
sqrt
ceil
cotg
sec
csc
conj
log2
lcm
gcd
13 / 115
Basics
Elementary operations
boolean operations
the boolean value true is written : %T.
the boolean value false is written : %F.
&
|
==
= or <>
< (<=)
> (>=)
logical and
logical or
logical not
equal
different
lower than (or equal)
greater than (or equal)
--> %T & %F
ans =
F
--> 2 == 2
ans =
T
--> 2 < 3
ans =
T
14 / 115
Basics
Variables
Variables
A variable can be directly defined via the assignment operator : =
-->
-->
-->
c
a = 2.5;
b = 3;
c = a*b
=
7.5
--> c + d
! - - error 4
Undefined variable : d
15 / 115
Basics
Variables
Pre-defined variables
%i
%e
%pi
%inf
%t ou %T
%f ou %F
imaginary number i =
Eulers number e
constant
infinity
boolean true
boolean false
16 / 115
Matrices
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
17 / 115
Matrices
3.
Automatic creation
--> v = [0:0.2:1]
v =
0.
0.2
0.4
0.6
0.8
1.
Syntax : start:step:end
0.921
0.825
0.696
0.540
18 / 115
Matrices
return
return
return
return
return
return
the
the
the
the
the
the
--> length ( v )
ans =
6.
--> mean ( v )
ans =
0.5
19 / 115
Matrices
Particular matrices :
zeros(n,m)
ones(n,m)
eye(n,n)
rand(n,m)
n m matrix of zeros
n m matrix of ones
identity matrix
n m matrix of random numbers (values [0, 1])
20 / 115
Matrices
--> A (2 ,3)
ans =
6.
--> A (2 ,:)
ans =
4.
5.
6.
21 / 115
Matrices
--> B = [1 0 ; 2 2];
--> det ( B )
ans =
2.
--> inv ( B )
ans =
1.
0.
- 1.
0.5
--> triu ( A )
ans =
1.
2.
0.
5.
0.
0.
3.
6.
9.
22 / 115
Matrices
Matrix operations
Matrix operations
Basic operations
+, -, *, /,
8.
17.
26.
--> A + B
! - - error 8
Inconsistent addition .
23 / 115
Matrices
Matrix operations
1.8221
2.2255
2.7182
logm
cosm
sqrtm
24 / 115
Matrices
Matrix operations
Element-wise operations
.*
./
--> A = [0 4 ; 1 2];
--> B = [1 2 ; 5 -3];
--> A * B
ans =
20. - 12.
11. - 4.
--> A .* B
ans =
0.
8.
5. - 6.
--> A .^2
ans =
0.
1.
16.
4.
1.1388
1.2364
1.3591
25 / 115
Plotting
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
26 / 115
Plotting
2D graphics
2D graphics
To plot a curve in the x-y plan use function plot
--> x = [0:0.1:2* %pi ];
--> y = cos ( x );
--> plot (x ,y , * )
27 / 115
Plotting
2D graphics
2D graphics
To plot a curve in the x-y plan use function plot
--> x = [0:0.1:2* %pi ];
--> y = cos ( x );
--> plot (x ,y , * )
27 / 115
Plotting
-->
-->
-->
-->
-->
-->
-->
-->
2D graphics
x = [0:0.1:2* %pi ];
y2 = cos (2* x );
y3 = cos (4* x );
y4 = cos (6* x );
plot (x , y1 );
plot (x , y2 , r );
plot (x , y3 , k : );
plot (x , y4 , g - - );
28 / 115
Plotting
-->
-->
-->
-->
-->
-->
-->
-->
2D graphics
x = [0:0.1:2* %pi ];
y2 = cos (2* x );
y3 = cos (4* x );
y4 = cos (6* x );
plot (x , y1 );
plot (x , y2 , r );
plot (x , y3 , k : );
plot (x , y4 , g - - );
28 / 115
Plotting
3D graphics
3D graphics
To plot a parametric curve in 3D space use function : param3d
-->
-->
-->
-->
-->
t = 0:0.01:10* %pi ;
x = sin ( t );
y = cos ( t );
z = t;
param3d (x ,y , z );
29 / 115
Plotting
3D graphics
3D graphics
To plot a parametric curve in 3D space use function : param3d
-->
-->
-->
-->
-->
t = 0:0.01:10* %pi ;
x = sin ( t );
y = cos ( t );
z = t;
param3d (x ,y , z );
29 / 115
Plotting
3D graphics
30 / 115
Plotting
3D graphics
30 / 115
Plotting
Overview
Overview
Scilab provides several graphical functions :
plot
contour
surf
pie
histplot
hist3d
bar
polarplot
2D graphic
level curves in x-y plan
3D surface
pie plot
histogram plot
3D histogram plot
bar plot
polar coordinate plot
add a title
add a title and labels on axis
add a legend
31 / 115
Plotting
-->
-->
-->
-->
-->
-->
Overview
32 / 115
Plotting
-->
-->
-->
-->
-->
-->
Overview
32 / 115
Programming
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
33 / 115
Programming
Scripts
Scripts
A script is a set of instructions gathered in a file.
Scilab provides a programming language (interpreted).
Scilab includes an editor, but any text editor may be used.
File extension should be .sce (but this is not mandatory).
Editor launched from Applications > SciNotes or by typing editor
on the console.
34 / 115
Programming
Scripts
Scripts
A script is a set of instructions gathered in a file.
Scilab provides a programming language (interpreted).
Scilab includes an editor, but any text editor may be used.
File extension should be .sce (but this is not mandatory).
Editor launched from Applications > SciNotes or by typing editor
on the console.
34 / 115
Programming
Scripts
Dans la console :
--> exec ( myscript . sce , -1)
Area :
50.265482
Volume :
33.510322
35 / 115
Programming
Scripts
36 / 115
Programming
Scripts
On the console :
--> n = 50;
--> exec ( myscript2 . sce , -1)
see plot on the figure
37 / 115
Programming
Scripts
38 / 115
Programming
then
if (x >=0) then
disp ( " x is positive " );
else
disp ( " x is negative " );
end
39 / 115
Programming
select i
case 1
disp ( " One " );
case 2
disp ( " Two " );
case 3
disp ( " Three " );
else
disp ( " Other " );
end
40 / 115
Programming
end
n = 10;
for k = 1: n
y ( k ) = exp ( k );
end
41 / 115
Programming
(boolean expression)
instructions
end
x = 16;
while ( x > 1 )
x = x /2;
end
42 / 115
Programming
And also :
instruction break interrupt and exit a loop.
instruction continue skip to the next iteration of a loop.
43 / 115
Programming
Functions
Functions
A function is a command that makes computations from variables and
returns a result
outvar = afunction( invar )
44 / 115
Programming
Functions
45 / 115
Programming
Functions
46 / 115
Programming
Functions
f (x) = (x + 1) e2x
function y = f ( x )
y = ( x +1).* exp ( -2* x );
endfunction
--> y = f (4)
y =
0.0016773
--> y = f (2.5)
y =
0.0235828
--> t = [0:0.1:5];
--> plot (t , f )
47 / 115
Programming
Functions
function z = mytest ( x )
z = x + a;
a = a +1;
endfunction
--> a = 2;
--> mytest (3)
ans =
5.
--> a
a =
2.
48 / 115
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
49 / 115
50 / 115
In MATLAB
In Scilab
comments %
comments //
continuation of a statement ..
51 / 115
In MATLAB
In Scilab
division by a vector
>> x = 1/[1 2 3]
Error using / Matrix dimensions must
agree.
division by a vector
--> x = 1/[1 2 3]
x =
0.0714286
0.1428571
0.2142857
x is solution of [1 2 3]*x = 1
52 / 115
In MATLAB
In Scilab
hello!
the result is 3
3.
note that : prettyprint generates the
Latex code to represent a Scilab
object
53 / 115
In MATLAB
In Scilab
54 / 115
Meaning
the default value
print nothing
echo each command line
print prompt >
echo + prompt
stop before each prompt
stop + prompt + echo
55 / 115
56 / 115
In MATLAB
In Scilab
a function is a variable
variables in the function are local variables and variables from the
calling workspace are known
when defined (function ... endfunction), functions are not executed
bu loaded
any change in the function requires to reload it (executing the
environment)
57 / 115
Xcos
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
58 / 115
Xcos
Xcos
Xcos is a graphical environment to simulate dynamic systems.
R
It is the Simulink
counterpart of Scilab.
59 / 115
Xcos
Xcos
Xcos is a graphical environment to simulate dynamic systems.
R
It is the Simulink
counterpart of Scilab.
59 / 115
Xcos
A simple example
60 / 115
Xcos
A simple example
block
sinus
gain
scope
clock
sub-palette
Sources/GENSIN f
Math. Operations/GAINBLK f
Sinks/CSCOPE
Sources/CLOCK c
drag and drop blocks from the palette browser to the editing window
k is variable from the workspace (or from Simulation/Set context)
black lines are data flows and red lines are event flows
60 / 115
Xcos
61 / 115
Xcos
+ kx(t) = 0
with the initial conditions : x(0) = 5 and x(0)
=0
62 / 115
Xcos
+ kx(t) = 0
with the initial conditions : x(0) = 5 and x(0)
=0
m
Y. Ariba - Icam, Toulouse.
62 / 115
Xcos
block
sum
gain
integral
scope
x-y scope
clock
sub-palette
Math. Operations/BIGSOM f
Math. Operations/GAINBLK f
Cont. time systems/INTEGRAL m
Sinks/CSCOPE
Sinks/CSCOPXY
Sources/CLOCK c
63 / 115
Xcos
64 / 115
Xcos
65 / 115
Xcos
65 / 115
Xcos
66 / 115
Xcos
66 / 115
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
67 / 115
A brief review
A brief review
Objective : Design a controller to control a dynamical system.
68 / 115
A brief review
69 / 115
A brief review
69 / 115
A brief review
(s)
1
=
u
(s)
(s + 1)s
70 / 115
A brief review
(s)
1
=
u
(s)
(s + 1)s
Amplitude
1.5
0.5
0
0
0.5
1.5
Time (sec)
2.5
70 / 115
A brief review
Gain (dB)
20
0
20
40
1
10
10
10
10
Phase (degre)
0
45
90
135
180
1
10
10
pulsation
71 / 115
A brief review
72 / 115
A brief review
k
s2 + s + k
72 / 115
A brief review
Step Response
2.5
Amplitude
1.5
k=1
k=2
k=5
k=0.5
0.5
0
0
6
Time (sec)
10
12
73 / 115
A brief review
s2 + s
r(s)
s2 + s + k
/2
)
s
74 / 115
A brief review
s2 + s
r(s)
s2 + s + k
/2
)
s
K
n
=
=
=
1,
k
1/2 k
3
n
= 6s.
Brno University of Technology - April 2014
74 / 115
num = 1;
den = %s ^2+ %s ;
G = syslin ( c ,num , den )
=
1
----2
s + s
75 / 115
76 / 115
Systems connection
G1 (s) =
1
s+2
and
G2 (s) =
4
s
77 / 115
--> G1 * G2
ans =
4
----2
2s + s
// series connection
--> G1 + G2
ans =
8 + 5s
-----2
2s + s
// parallel connection
--> G1 /. G2
ans =
s
- -- - -- -- -
// feedback connection
2
4 + 2s + s
78 / 115
2.
0.
0.
79 / 115
Bode plot
Bode plot
Introductory example : RC circuit
80 / 115
Bode plot
Bode plot
Introductory example : RC circuit
e(t) = em cos(t + e )
v(t) = v cos(t + )
m
v
e = em eje
v = v ejv
m
80 / 115
Bode plot
e(t)
v(t)
0.5
0.5
1.5
2.5
3
temps (s)
3.5
4.5
81 / 115
Bode plot
Ohms law : u = Zi
ZR = R
and
ZC =
1
jC
82 / 115
Bode plot
Ohms law : u = Zi
ZR = R
and
ZC =
1
jC
ZC
e
ZC + ZR
82 / 115
Bode plot
Ohms law : u = Zi
ZR = R
and
ZC =
1
jC
ZC
e
ZC + ZR
1
jC
1
+
jC
1
.
jRC + 1
82 / 115
Bode plot
Magnitude (dB)
0
5
10
15
20
25
Phase (deg)
30
0
45
90
1
10
10
10
10
Frequency (rad/sec)
83 / 115
Bode plot
Magnitude (dB)
0
5
10
15
20
=8
25
Phase (deg)
30
0
45
90
1
10
10
10
10
Frequency (rad/sec)
83 / 115
Bode plot
0.5
0.5
1
10
15
20
25
30
35
40
45
temps (s)
84 / 115
Bode plot
0.5
0.5
0.5
0.5
10
15
20
25
30
temps (s)
35
40
45
e(t) (=4)
v(t)
10
temps (s)
84 / 115
Bode plot
0.5
0.5
0.5
0.5
10
15
20
25
30
35
40
e(t) (=4)
v(t)
45
temps (s)
10
temps (s)
e(t) (=8)
v(t)
0.5
0.5
1
3
3.5
4.5
5.5
6.5
temps (s)
84 / 115
Bode plot
0.5
0.5
0.5
0.5
10
15
20
25
30
35
40
e(t) (=4)
v(t)
45
e(t) (=8)
v(t)
0.5
0.5
0.5
1
3.5
4.5
5.5
6.5
10
e(t) (=40)
v(t)
0.5
6
temps (s)
temps (s)
10
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
temps (s)
84 / 115
Bode plot
Bode Diagram
5
Magnitude (dB)
0
5
10
15
20
=4
=0.8
Phase (deg)
25
0
=8
=40
45
90
1
10
10
10
10
Frequency (rad/sec)
85 / 115
Bode plot
Frequency analysis consists in studying the response of a LTI system with sine
inputs
U(s)
u(t)
u(t)==uu00sin(t)
sin(t)
F(s)
Y(s)
y(t)
y(t)==yy00sin(t+)
sin(t+)
86 / 115
Bode plot
Frequency analysis consists in studying the response of a LTI system with sine
inputs
U(s)
Y(s)
F(s)
u(t)
u(t)==uu00sin(t)
sin(t)
y(t)
y(t)==yy00sin(t+)
sin(t+)
1.5
0.5
u0
u(t)
y0
y(t)
0.5
1
0
10
12
14
16
18
20
The output signal is also a sine with the same frequency, but with a different
magnitude and a different phase angle.
Y. Ariba - Icam, Toulouse.
86 / 115
Bode plot
y0
u0
t
T
87 / 115
Bode plot
y0
u0
t
T
87 / 115
Bode plot
1/2
s+1
88 / 115
Bode plot
1/2
s+1
we express F (j) =
1/2
j + 1
|F (j0.05)| = 0.5
and
|F (j1.5)| = 0.277
and
for = 10 rad/s :
|F (j10)| = 0.05
and
88 / 115
Bode plot
u1 (t)
0.5
y1 (t)
0.5
50
100
150
u2 (t)
200
250
300
350
400
450
500
0.5
u3 (t)
0.5
y3 (t)
y2 (t)
0.5
0.5
10
12
14
16
0.2
0.4
0.6
0.8
1.2
1.4
1.6
1.8
89 / 115
Bode plot
Bode diagram : it plots the gain and the phase shift w.r.t. the frequency
the gain is expressed as decibels : gain dB = 20 log
y0
u0
property : the Bode diagram of F (s)G(s) is the sum of the one of F (s) and
the one of G(s).
in Scilab, the instruction bode(F) plots the Bode diagram of F (s).
90 / 115
Bode plot
Bode diagram : it plots the gain and the phase shift w.r.t. the frequency
the gain is expressed as decibels : gain dB = 20 log
y0
u0
property : the Bode diagram of F (s)G(s) is the sum of the one of F (s) and
the one of G(s).
in Scilab, the instruction bode(F) plots the Bode diagram of F (s).
90 / 115
91 / 115
block
step
sum
gain
transfert function
scope
clock
sub-palette
Sources/STEP FUNCTION
Math. Operations/BIGSOM f
Math. Operations/GAINBLK f
Cont. time systems/CLR
Sinks/CSCOPE
Sources/CLOCK c
settings : final value (step) = %pi/2, final integral time = 12, Ymin= 0,
Ymax= 2.5, Refresh period = 12
Y. Ariba - Icam, Toulouse.
91 / 115
Sommaire
1
Introduction
Basics
Matrices
Plotting
Programming
Xcos
92 / 115
93 / 115
Loopshaping
Loopshaping
1
r(s)
1 + G(s)C(s)
94 / 115
Loopshaping
95 / 115
Loopshaping
96 / 115
Loopshaping
4k
s2 + 3s + 3
97 / 115
Loopshaping
98 / 115
Loopshaping
98 / 115
Loopshaping
Step response of the closed-loop system (unit step) for k = {0.5, 1, 5, 10}
99 / 115
1 + s
,
1 + a s
with a > 1
100 / 115
1 + s
,
1 + a s
with a > 1
100 / 115
Example
G(s) =
4
s2 + 3s + 3
What value for the proportional gain k to have a static error of 10% ?
101 / 115
Example
G(s) =
4
s2 + 3s + 3
What value for the proportional gain k to have a static error of 10% ?
static error =
1
= 0.1
1 + 34 k
k = 6.75
101 / 115
20
Magnitude (dB)
0
20
40
60
80
0
Phase (deg)
45
90
135
180
2
10
10
10
10
10
Frequency (rad/s)
102 / 115
20
Magnitude (dB)
0
20
40
60
80
0
Phase (deg)
45
90
135
180
2
10
10
10
10
10
Frequency (rad/s)
Phase margin : before = 111 (at 1.24 rd/s) ; after = 34 (at 5.04 rd/s)
102 / 115
1 + s
1 + a s
with a > 1
103 / 115
1 + s
1 + a s
with a > 1
=1
103 / 115
Bode Diagram
20
Magnitude (dB)
0
20
40
60
80
0
Phase (deg)
45
90
135
180
2
10
10
10
10
10
Frequency (rad/s)
104 / 115
Bode Diagram
20
G(s)
kG(s)
kC(s)G(s)
Magnitude (dB)
0
20
40
60
80
0
Phase (deg)
45
90
135
180
3
10
10
10
10
10
10
Frequency (rad/s)
Phase margin : now, with the proportional gain and the phase lag controller
= 70 (at 1.56 rd/s)
104 / 115
105 / 115
1.4
1.2
Amplitude
0.8
0.6
0.4
0.2
Time (seconds)
105 / 115
1 + a s
,
1 + s
with a > 1
106 / 115
1 + a s
,
1 + s
with a > 1
106 / 115
1 + sin m
1 sin m
m a
107 / 115
Example
G(s) =
4
s(2s + 1)
108 / 115
Example
G(s) =
4
s(2s + 1)
Step Response
Bode Diagram
1.6
60
40
Magnitude (dB)
1.4
1.2
20
0
1
Amplitude
20
0.8
40
90
Phase (deg)
0.6
0.4
135
0.2
10
15
20
Time (seconds)
25
180
2
10
10
10
10
Frequency (rad/s)
108 / 115
1 + sin m
= 4.62
1 sin m
1
= 0.23
m a
109 / 115
1 + sin m
= 4.62
1 sin m
1
= 0.23
m a
1 + 1.07s
1 + 0.23s
109 / 115
Example
Step Response
Bode Diagram
1.6
60
40
Magnitude (dB)
1.4
1.2
20
0
1
Amplitude
20
0.8
40
90
Phase (deg)
0.6
0.4
135
0.2
10
15
Time (seconds)
20
25
180
2
10
10
10
10
Frequency (rad/s)
110 / 115
Example
Step Response
Bode Diagram
100
1.4
50
Magnitude (dB)
1.6
1.2
Amplitude
50
0.8
100
90
Phase (deg)
0.6
0.4
135
0.2
10
15
20
25
Time (seconds)
180
2
10
10
10
10
10
Frequency (rad/s)
110 / 115
PID controller
PID controller
kp + ki 1s + kd s
kp (1 +
1
)(1
i s
+ d s)
111 / 115
PID controller
1 + s
1 + a s
with a > 1
PI controller
C(s) =
1 + i s
i s
112 / 115
PID controller
1 + a s
1 + s
with a > 1
PD controller
C(s) = 1 + d s
113 / 115
PID controller
C(s) = k
1 + s 1 + a s
1
2 2
1 + a1 1 s
1 + 2 s
1
1 + 3 s
with 3 2
114 / 115
PID controller
115 / 115