Introduction to Scilab application to feedback control Yassine - - PowerPoint PPT Presentation

introduction to scilab
SMART_READER_LITE
LIVE PREVIEW

Introduction to Scilab application to feedback control Yassine - - PowerPoint PPT Presentation

Introduction to Scilab application to feedback control Yassine Ariba Brno University of Technology - April 2015 Y. Ariba - Icam, Toulouse. Brno University of Technology - April 2015 1 / 142 Sommaire 1 What is Scilab ? 4 Application to


slide-1
SLIDE 1

Introduction to Scilab

application to feedback control

Yassine Ariba

Brno University of Technology - April 2015

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 1 / 142

slide-2
SLIDE 2

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 2 / 142

slide-3
SLIDE 3

What is Scilab ?

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 3 / 142

slide-4
SLIDE 4

What is Scilab ? Introduction

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, Created by researchers from Inria in the 90’s, the software is now developed by Scilab Entreprises

www.scilab.org

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 4 / 142

slide-5
SLIDE 5

What is Scilab ? Introduction

Scilab includes hundreds of functions for various applications Mathematics and simulation 2D and 3D visualization Optimization Statistics Control system design and analysis Signal processing Application development

More informations : www.scilab.org

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 5 / 142

slide-6
SLIDE 6

What is Scilab ? Introduction

License

Scilab is an open source software. It is distributed under a GPL-compatible license. It is a free open source alternative to Matlab

R

1.

Scilab can be downloaded from : http://www.scilab.org/download/ The version used in this introduction is version 5.4.1

  • 1. Matlab is a registered trademark of The MathWorks, Inc.
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 6 / 142

slide-7
SLIDE 7

What is Scilab ? Introduction

Getting started

Firstly, Scilab can be used in an interactive way by typing instructions on the console. type scilab code on the prompt --> type enter, to execute it. Scilab return its answer on the console or in a new window for graphics.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 7 / 142

slide-8
SLIDE 8

What is Scilab ? Introduction

A first simple example :

  • -> A = 2;
  • -> t = [0:0.01:10];
  • -> y = A*sin (3*t);
  • -> plot(t,y);

Line 1 : assign the value 2 to the variable A. Line 2 : define a vector t that goes from 0 to 10 with a step of 0.01. Line 3 : compute a vector y from some mathematical operations. Line 4 : plot y with respect to t on a 2D graphic. Note that “ ; ” prevents from printing the result of an instruction.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 8 / 142

slide-9
SLIDE 9

What is Scilab ? Introduction

A first simple example :

  • -> A = 2;
  • -> t = [0:0.01:10];
  • -> y = A*sin (3*t);
  • -> plot(t,y);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 8 / 142

slide-10
SLIDE 10

What is Scilab ? Introduction

A second simple example : Let consider a system of linear equations    2x1 + x2 = −5 4x1 − 3x2 + 2x3 = x1 + 2x2 − x3 = 1 Let solve it with Scilab

  • -> A = [2 1 0 ; 4 -3 2 ; 1 2
  • 1];
  • -> b = [ -5;0;1];
  • -> x = inv(A)*b

x = 1.75

  • 8.5
  • 16.25
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 9 / 142

slide-11
SLIDE 11

What is Scilab ? Introduction

Scilab provides a graphical environment with several windows. the console command history file browser variable browser and others : editor, graphics, help, ...

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 10 / 142

slide-12
SLIDE 12

What is Scilab ? Basics

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 11 / 142

slide-13
SLIDE 13

What is Scilab ? Basics

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

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 12 / 142

slide-14
SLIDE 14

What is Scilab ? Basics

elementary operations + addition

  • subtraction

* multiplication / right division \ left division ˆ exponents elementary functions sin cos tan cotg asin acos atan sec sinh cosh tanh csc abs real imag conj exp log log10 log2 sign modulo sqrt lcm round floor ceil gcd

  • -> conj (3+2* %i)

ans =

  • 3. - 2.i
  • -> log10 (10^4)

ans = 4.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 13 / 142

slide-15
SLIDE 15

What is Scilab ? Basics

boolean operations the boolean value true is written : %T. the boolean value false is written : %F. & logical and | logical or ∼ logical not == equal ∼= or <> different < (<=) lower than (or equal) > (>=) greater than (or equal)

  • -> %T & %F

ans = F

  • -> 2 == 2

ans = T

  • -> 2 < 3

ans = T

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 14 / 142

slide-16
SLIDE 16

What is Scilab ? Basics

Variables

A variable can be directly defined via the assignment operator : “ = ”

  • -> a = 2.5;
  • -> b = 3;
  • -> c = a*b

c = 7.5

  • -> c+d

!--error 4 Undefined variable : d

Variable names may be defined with letters a → z, A → Z, numbers 0 → 9 and few additional characters %, , !, #, ?, $. Scilab is case sensitive. Do not confused the assignment operator “ = ” with the mathematical equal. Variable declaration is implicit, whatever the type.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 15 / 142

slide-17
SLIDE 17

What is Scilab ? Basics

Pre-defined variables %i imaginary number i = √−1 %e Euler’s number e %pi constant π %inf infinity ∞ %t ou %T boolean true %f ou %F boolean false

  • -> cos (2* %pi)

ans = 1.

  • -> %i^2

ans =

  • 1.
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 16 / 142

slide-18
SLIDE 18

What is Scilab ? Matrices

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 17 / 142

slide-19
SLIDE 19

What is Scilab ? Matrices

Defining and handling vectors

A vector is defined by a list of numbers between brackets :

  • -> u = [0 1 2 3]

u = 0. 1. 2. 3.

Automatic creation

  • -> v = [0:0.2:1]

v = 0. 0.2 0.4 0.6 0.8 1.

Syntax : start:step:end Mathematical functions are applied element-wise

  • -> cos(v)

ans = 1. 0.980 0.921 0.825 0.696 0.540

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 18 / 142

slide-20
SLIDE 20

What is Scilab ? Matrices

column vectors can also be defined with semi colon separator “ ; ”

  • -> u = [1;2;3]

u = 1. 2. 3.

Some useful functions : length return the length of the vector max return the maximal component min return the minimal component mean return the mean value sum return the sum of all components prod return the product of all components

  • -> length(v)

ans = 6.

  • -> mean(v)

ans = 0.5

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 19 / 142

slide-21
SLIDE 21

What is Scilab ? Matrices

Defining and handling matrices

Matrices are defined row by row with the separator “;”

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

A = 1. 2. 3. 4. 5. 6. 7. 8. 9.

Particular matrices : zeros(n,m) n × m matrix of zeros

  • nes(n,m)

n × m matrix of ones eye(n,n) identity matrix rand(n,m) n × m matrix of random numbers (values ∈ [0, 1])

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 20 / 142

slide-22
SLIDE 22

What is Scilab ? Matrices

Accessing the elements of a matrix : A(select row(s),select column(s))

  • -> A(2 ,3)

ans = 6.

  • -> A(2 ,:)

ans = 4. 5. 6.

  • -> A(: ,[1 3])

ans = 1. 3. 4. 6. 7. 9.

For vectors, one argument is enough v(3) (gives 0.4) Elements may be modified

  • -> A(2 ,3) = 0;
  • -> A

A = 1. 2. 3. 4. 5. 0. 7. 8. 9.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 21 / 142

slide-23
SLIDE 23

What is Scilab ? Matrices

Some useful functions : size return the dimensions of a matrix det compute the determinant of a matrix inv compute the inverse matrix rank return the rank of a matrix diag extract the diagonal of a matrix triu extract the upper triangular part of a matrix tril extract the lower triangular part of a matrix spec return the eigenvalues of a matrix

  • -> B = [1 0 ; 2 2];
  • -> det(B)

ans = 2.

  • -> inv(B)

ans = 1. 0.

  • 1.

0.5

  • -> triu(A)

ans = 1. 2. 3. 0. 5. 6. 0. 0. 9.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 22 / 142

slide-24
SLIDE 24

What is Scilab ? Matrices

Matrix operations

Basic operations +, -, *, /, ˆ can be directly performed Watch out for dimension compatibility ! transpose operator : “.’” , transpose and conjugate operator : “’”

  • -> C = [ 1 0 ; 3 1 ; 0 2];
  • -> D = [1 1 ; 4 0];
  • -> B + D

ans = 2. 1. 6. 2.

  • -> B * inv(B)

ans = 1. 0. 0. 1.

  • -> A * C

ans = 7. 8. 19. 17. 31. 26.

  • -> A + B

!--error 8 Inconsistent addition.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 23 / 142

slide-25
SLIDE 25

What is Scilab ? Matrices

Elementary functions are applied element-wise

  • -> M = [0 %pi /2 ; -%pi /2 %pi ];
  • -> sin(M)

ans = 0. 1.

  • 1.

1.225D -16

  • -> t = [0:0.2:1];
  • -> exp(t)

ans = 1. 1.2214 1.4918 1.8221 2.2255 2.7182

There are specific versions of those functions for matrix operations expm logm sqrtm sinm cosm ˆ

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 24 / 142

slide-26
SLIDE 26

What is Scilab ? Matrices

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. 16. 1. 4.

  • -> exp(t)./(t+1)

ans = 1. 1.0178 1.0655 1.1388 1.2364 1.3591

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 25 / 142

slide-27
SLIDE 27

What is Scilab ? Plotting

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 26 / 142

slide-28
SLIDE 28

What is Scilab ? Plotting

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,’*’)
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 27 / 142

slide-29
SLIDE 29

What is Scilab ? Plotting

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

plot traces a point for each couple x(i)-y(i). x and y must have the same size. By default, a line is drawn between points. The third argument defines the style of the plot.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 27 / 142

slide-30
SLIDE 30

What is Scilab ? Plotting

  • -> 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--’);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 28 / 142

slide-31
SLIDE 31

What is Scilab ? Plotting

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

Several graphics can be displayed. clf : clear the current graphic figure.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 28 / 142

slide-32
SLIDE 32

What is Scilab ? Plotting

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);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 29 / 142

slide-33
SLIDE 33

What is Scilab ? Plotting

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);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 29 / 142

slide-34
SLIDE 34

What is Scilab ? Plotting

To plot a surface in 3D space use function : surf

  • -> x = [-%pi :0.2: %pi ];
  • -> y = [-%pi :0.2: %pi ];
  • -> [X,Y] = meshgrid(x,y);
  • ->

Z = cos(X).* sin(Y);

  • -> surf(X,Y,Z)
  • -> f=gcf ();
  • -> f.color_map = jetcolormap (32);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 30 / 142

slide-35
SLIDE 35

What is Scilab ? Plotting

To plot a surface in 3D space use function : surf

  • -> x = [-%pi :0.2: %pi ];
  • -> y = [-%pi :0.2: %pi ];
  • -> [X,Y] = meshgrid(x,y);
  • ->

Z = cos(X).* sin(Y);

  • -> surf(X,Y,Z)
  • -> f=gcf ();
  • -> f.color_map = jetcolormap (32);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 30 / 142

slide-36
SLIDE 36

What is Scilab ? Plotting

Overview

Scilab provides several graphical functions : plot 2D graphic contour level curves in x-y plan surf 3D surface pie “pie” plot histplot histogram plot hist3d 3D histogram plot bar bar plot polarplot polar coordinate plot Some instructions allow to add features to the figure : title add a title xtitle add a title and labels on axis legend add a legend

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 31 / 142

slide-37
SLIDE 37

What is Scilab ? Plotting

  • -> x = linspace ( -20 ,20 ,1000);
  • -> y1 = x.* sin(x);
  • -> y2 = -x;
  • -> plot(x,y1 ,’b’,x,y2 ,’r’)
  • -> xtitle(’mongraphique ’,’labelaxex’,’labelaxey’);
  • -> legend(’y1=x*sin(x)’,’y2=-x’);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 32 / 142

slide-38
SLIDE 38

What is Scilab ? Plotting

  • -> x = linspace ( -20 ,20 ,1000);
  • -> y1 = x.* sin(x);
  • -> y2 = -x;
  • -> plot(x,y1 ,’b’,x,y2 ,’r’)
  • -> xtitle(’mongraphique ’,’labelaxex’,’labelaxey’);
  • -> legend(’y1=x*sin(x)’,’y2=-x’);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 32 / 142

slide-39
SLIDE 39

What is Scilab ? Programming

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 33 / 142

slide-40
SLIDE 40

What is Scilab ? Programming

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

  • n the console.
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 34 / 142

slide-41
SLIDE 41

What is Scilab ? Programming

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

  • n the console.
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 34 / 142

slide-42
SLIDE 42

What is Scilab ? Programming

Example of a script : myscript.sce

// radius

  • f a sphere

r = 2; // calculation

  • f the

area A = 4* %pi*r^2; // calculation

  • f the

volume V = 4* %pi*r^3/3; disp(A,’Area:’); disp(V,’Volume:’);

On the console :

  • ->exec(’myscript.sce ’,
  • 1)

Area: 50.265482 Volume: 33.510322

The file must be located in the current directory

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 35 / 142

slide-43
SLIDE 43

What is Scilab ? Programming

Comments : words following // are not interpreted. The current directory can be modified in menu File of the console. The path may be specified instead exec(’C:\Users\yassine\scilab\myscript.sce’, -1) Scripts may also be run from the shortcut in the toolbar. Variables defined in the workspace (from the console) are visible and can be modified in the script.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 36 / 142

slide-44
SLIDE 44

What is Scilab ? Programming

Another example : myscript2.sce

x1 =

  • 1; x2 = 1;

x = linspace(x1 ,x2 ,n); y = exp (-2*x).* sin (3*x); plot(x,y); disp(’seeplotonthefigure ’);

On the console :

  • -> n = 50;
  • ->exec(’myscript2.sce ’,
  • 1)

see plot on the figure

Here the variable n must be defined beforehand.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 37 / 142

slide-45
SLIDE 45

What is Scilab ? Programming

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 38 / 142

slide-46
SLIDE 46

What is Scilab ? Programming

Looping and branching

Scilab language includes classical control structures Conditional statements if if boolean expression then instructions 1 else instructions 2 end

if (x >=0) then disp("xispositive"); else disp("xisnegative"); end

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 39 / 142

slide-47
SLIDE 47

What is Scilab ? Programming

Branching with respect to the value of a variable select select variable case value 1 instructions 1 case value 2 instructions 2 else instructions 3 end

select i case 1 disp("One"); case 2 disp("Two"); case 3 disp("Three"); else disp("Other"); end

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 40 / 142

slide-48
SLIDE 48

What is Scilab ? Programming

Loop control statements for for variable = start: step: end instructions end

n = 10; for k = 1:n y(k) = exp(k); end

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 41 / 142

slide-49
SLIDE 49

What is Scilab ? Programming

Loop control based on a boolean expression while while (boolean expression) instructions end

x = 16; while ( x > 1 ) x = x/2; end

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 42 / 142

slide-50
SLIDE 50

What is Scilab ? Programming

And also : instruction break interrupt and exit a loop. instruction continue skip to the next iteration of a loop. Note that as much as possible, use vector / matrix operations instead of

  • loops. The code may run 10 to 100 times faster. This feature of Scilab is

known as the vectorization.

tic S = 0; for k = 1:1000 S = S + k; end t = toc (); disp(t); tic N = [1:1000]; S = sum(N); t = toc (); disp(t);

  • ->exec(’myscript.sce ’,
  • 1)

0.029 0.002

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 43 / 142

slide-51
SLIDE 51

What is Scilab ? Programming

Functions

A function is a command that makes computations from variables and returns a result

  • utvar = afunction( invar )

afunction is the name of the function invar is the input argument

  • utvar is the output argument, returned by the function

Examples :

  • -> y = sin (1.8)

y = 0.9738476

  • -> x =[0:0.1:1];
  • -> N = length(x)

N = 11.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 44 / 142

slide-52
SLIDE 52

What is Scilab ? Programming

User can define its own functions function [out1,out2,...] = myfunction(in1,in2,...) body of the function endfunction

  • nce the environment function...endfunction is executed myfunction

is defined and loaded in Scilab after any change in the function, it must be reloaded to be taken into account files including functions generally have the extension “.sci”

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 45 / 142

slide-53
SLIDE 53

What is Scilab ? Programming

Example 1 : calculation of the roots of a quadratic equation. Define and load the function

function [x1 ,x2] = roots_equ2d (a,b,c) // roots of ax^2 + bx + c = 0 delta = b^2 - 4*a*c x1 = (-b - sqrt(delta ))/(2*a) x2 = (-b + sqrt(delta ))/(2*a) endfunction

Then, you can use it as any other Scilab function

  • -> [r1 ,r2] = roots_equ2d (1,3,2)

r2 =

  • 1.

r1 =

  • 2.
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 46 / 142

slide-54
SLIDE 54

What is Scilab ? Programming

Example 2 : functions are appropriate to define mathematical functions. f(x) = (x + 1) e−2x

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)
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 47 / 142

slide-55
SLIDE 55

What is Scilab ? Programming

Variables from workspace are known inside the function but any change inside the function remain local.

function z=mytest(x) z = x + a; a = a +1; endfunction

  • -> a = 2;
  • -> mytest (3)

ans = 5.

  • -> a

a = 2.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 48 / 142

slide-56
SLIDE 56

For MATLAB users

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 49 / 142

slide-57
SLIDE 57

For MATLAB users MATLAB vs Scilab

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 50 / 142

slide-58
SLIDE 58

For MATLAB users MATLAB vs Scilab

For MATLAB users

Many instructions have the same syntax, but some others not... A dictionary gives a list of the main MATLAB functions with their Scilab equivalents

http://help.scilab.org/docs/5.4.1/en_US/section_36184e52ee88ad558380be4e92d3de21.html

Some tools are provided to convert MATLAB files to Scilab (e.g. mfile2sci)

http://help.scilab.org/docs/5.4.1/en_US/About_M2SCI_tools.html

A good note on Scilab for MATLAB users

Eike Rietsch, An Introduction to Scilab from a Matlab User’s Point of View, May 2010 http://www.scilab.org/en/resources/documentation/community

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 51 / 142

slide-59
SLIDE 59

For MATLAB users MATLAB vs Scilab

Somme differences about the syntax

In MATLAB search with keywords lookfor comments % predefined constants i, pi, inf, true special characters in name of variables continuation of a statement ... flow control switch case otherwise last element of a vector x(end) In Scilab search with keywords apropos comments // predefined constants %i, %pi, %inf, %t special characters in name of variables , #, !, ?, $ continuation of a statement .. flow control select case else last element of a vector x($)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 52 / 142

slide-60
SLIDE 60

For MATLAB users MATLAB vs Scilab

Different responses for a same command

In MATLAB length, the larger of the number of rows and columns after a first plot, a second one clears the current figure division by a vector >> x = 1/[1 2 3] Error using / Matrix dimensions must agree.

  • perators == and ∼= compare elements

>> [1 2 3] == 1 ans = 1 0 0 >> [1 2 3] == [1 2] Error using == Matrix dimensions must agree. >> [1 2] == [’1’,’2’] ans = 0 0 In Scilab length, the product of the number of rows and columns after a first plot, a second one holds the previous 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

  • perators == and ∼= compare objects
  • -> [1 2 3] == 1

ans = T F F

  • -> [1 2 3] == [1 2]

ans = F

  • -> [1 2] == [’1’,’2’]

ans = F

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 53 / 142

slide-61
SLIDE 61

For MATLAB users MATLAB vs Scilab

Different responses for a same command

In MATLAB for a matrix A=[1 2 4;4 8 2;6 0 9] >> max(A) ans = 7 8 9 >> sum(A) ans = 12 10 18 disp must have a single argument >> a=3; >> disp([’the result is ’,int2str(a),’ ...bye!’]) the result is 3 ...bye! In Scilab for a matrix A=[1 2 4;4 8 2;6 0 9]

  • -> max(A)

ans = 9.

  • -> sum(A)

ans = 36. disp may have several arguments

  • -> a = 3;
  • -> disp(a,’the result is ’ +

string(a),’hello!’) hello! the result is 3 3. note that : prettyprint generates the Latex code to represent a Scilab

  • bject
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 54 / 142

slide-62
SLIDE 62

For MATLAB users MATLAB vs Scilab

Difference when running a script

In MATLAB

script is invoked by typing its name myscript the m-file must be in a directory of the search path (or specify the path in the call) use a semi-colon to print or not the result of an instruction

In Scilab

script is invoked with the exec command

  • -> exec(’myscript.sce’)

the file must be the working directory (or specify the path in the call) a second argument may be appended (mode) to specify what to print it does not seem to do what the documentation says... not clear for me

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 55 / 142

slide-63
SLIDE 63

For MATLAB users MATLAB vs Scilab

a simple example, myscript.sce :

// a simple script: myscript a = 1 b = a+3; disp(’resultis’+string(b))

the second argument mode Value Meaning the default value

  • 1

print nothing 1 echo each command line 2 print prompt −− > 3 echo + prompt 4 stop before each prompt 7 stop + prompt + echo

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 56 / 142

slide-64
SLIDE 64

For MATLAB users MATLAB vs Scilab

  • -> exec(’myscript.sce ’ ,0)

a = 1. result is 4

(as Matlab works)

  • -> exec(’myscript.sce ’ ,-1)

result is 4

(only output of disp is printed)

  • -> exec(’myscript.sce ’ ,1)
  • ->// a simple

script: myscript

  • ->a = 1

a = 1.

  • ->b = a+3;
  • ->disp(’resultis’+string(b))

result is 4

(everything is printed (instructions and outputs)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 57 / 142

slide-65
SLIDE 65

For MATLAB users MATLAB vs Scilab

Difference when using user defined functions

In MATLAB

a function is a file, they must have the same name variables in the function are local variables any other functions defined in the file are local functions

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 but loaded any change in the function requires to reload it (executing the environment)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 58 / 142

slide-66
SLIDE 66

For MATLAB users MATLAB vs Scilab

A function may be defined directly in the console :

  • ->
  • ->function [y] = addition(u1 ,u2)
  • ->

y = u1 + u2;

  • ->endfunction
  • ->
  • -> addition (2 ,3)

ans = 5.

  • r in a file anyscript.sce, and the file must be executed :
  • -> addition (2 ,3)

!--error 4 Variable non d´ efinie : addition

  • -> exec(’anyscript.sce ’,
  • 1)
  • -> addition (2 ,3)

ans = 5.

Any change needs to redefine (reload) the function to be taken into account.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 59 / 142

slide-67
SLIDE 67

For MATLAB users Exercices

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 60 / 142

slide-68
SLIDE 68

For MATLAB users Exercices

Exercices

Exercice 1 Calculate the sum of the first 100 integers squared, 1 + 22 + 32 + 42 + . . . + 1002 in 3 different ways : with instruction for, with instruction while, with instruction sum.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 61 / 142

slide-69
SLIDE 69

For MATLAB users Exercices

Exercice 2 Let us consider a monotonic increasing function f over an interval [a, b] such that f(a) < 0 and f(b) > 0 Write an algorithm, based on a dichotomic search, to find the root x0 such that f(x0) = 0. f1(x) = 2x4 + 2.3x3 − 16x2 − 8x − 17.5 with x ∈ [0, 100], f2(x) = tan(x2) − x with x ∈ [0.5, π/3].

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 62 / 142

slide-70
SLIDE 70

For MATLAB users Exercices

Exercice 3 The Fourier expansion of a square wave f with a period T and an amplitude A is of the form : f(t) =

+∞

  • n=1

an cos(nωt) with an = 2A nπ sin

2

  • and

ω = 2π T . Plot the Fourier expansion for different numbers of harmonics. numerical values : A = 2, T = 0.5s, t ∈ [0, 2].

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 63 / 142

slide-71
SLIDE 71

For MATLAB users Exercices

Exercice 4 Numerical integration for estimating π π = surface of a unit disc x2 + y2 = 1 Compute the area of a quarter of the disc with the rectangle method.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 64 / 142

slide-72
SLIDE 72

Xcos

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 65 / 142

slide-73
SLIDE 73

Xcos Basics

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 66 / 142

slide-74
SLIDE 74

Xcos Basics

Xcos

Xcos is a graphical environment to simulate dynamic systems. It is the Simulink

R

counterpart of Scilab.

It is launched in Application/Xcos or by typing xcos

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 67 / 142

slide-75
SLIDE 75

Xcos Basics

Xcos

Xcos is a graphical environment to simulate dynamic systems. It is the Simulink

R

counterpart of Scilab.

It is launched in Application/Xcos or by typing xcos

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 67 / 142

slide-76
SLIDE 76

Xcos Basics

A simple example

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 68 / 142

slide-77
SLIDE 77

Xcos Basics

A simple example

block sub-palette sinus Sources/GENSIN f gain

  • Math. Operations/GAINBLK f

scope Sinks/CSCOPE clock 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

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 68 / 142

slide-78
SLIDE 78

Xcos Basics

Settings : frequency = 2π/3, k = 2, final integral time = 12, Ymin= −3, Ymax= 3, Refresh period = 12 Run simulation from Simulation/Start

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 69 / 142

slide-79
SLIDE 79

Xcos Basics

Let simulate a mass-spring-damper system The system can be described by the equation of motion m¨ x(t) + f ˙ x(t) + kx(t) = 0 with the initial conditions : x(0) = 5 and ˙ x(0) = 0

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 70 / 142

slide-80
SLIDE 80

Xcos Basics

Let simulate a mass-spring-damper system The system can be described by the equation of motion m¨ x(t) + f ˙ x(t) + kx(t) = 0 with the initial conditions : x(0) = 5 and ˙ x(0) = 0 The acceleration of the mass is then given by ¨ x(t) = − 1 m

  • kx(t) + f ˙

x(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 70 / 142

slide-81
SLIDE 81

Xcos Basics

modeling and simulation with Xcos

block sub-palette sum

  • Math. Operations/BIGSOM f

gain

  • Math. Operations/GAINBLK f

integral

  • Cont. time systems/INTEGRAL m

scope Sinks/CSCOPE x-y scope Sinks/CSCOPXY clock Sources/CLOCK c

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 71 / 142

slide-82
SLIDE 82

Xcos Basics

parameters : m = 1, k = 2 and f = 0.2

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 72 / 142

slide-83
SLIDE 83

Xcos Basics

Let add an external force

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 73 / 142

slide-84
SLIDE 84

Xcos Basics

Let add an external force Define a superblock : Edit/Region to superblock

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 73 / 142

slide-85
SLIDE 85

Xcos Basics

Example 3 : simulation of a PWM signal

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 74 / 142

slide-86
SLIDE 86

Xcos Basics

Example 3 : simulation of a PWM signal

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 74 / 142

slide-87
SLIDE 87

Xcos Physical modeling

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 75 / 142

slide-88
SLIDE 88

Xcos Physical modeling

Physical modeling

Xcos includes, as a module extension, Modelica blocks. It provides a physical approach for modeling

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 76 / 142

slide-89
SLIDE 89

Xcos Physical modeling

Physical modeling

Xcos includes, as a module extension, Modelica blocks. It provides a physical approach for modeling physical modeling

  • r

acausal modeling = causal modeling with input/output relations Modelica blocks in Xcos require a C compiler

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 76 / 142

slide-90
SLIDE 90

Xcos Physical modeling

Examples Electrical system Mechanical system

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 77 / 142

slide-91
SLIDE 91

Xcos Physical modeling

Installation requirements : install a gcc compiler (in the OS), install MinGW toolbox (in Scilab), install Coselica Toolbox for more Modelica blocks (in Scilab, optional)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 78 / 142

slide-92
SLIDE 92

Xcos Physical modeling

Installation requirements : install a gcc compiler (in the OS), install MinGW toolbox (in Scilab), install Coselica Toolbox for more Modelica blocks (in Scilab, optional)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 78 / 142

slide-93
SLIDE 93

Xcos Exercices

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 79 / 142

slide-94
SLIDE 94

Xcos Exercices

Exercices

Exercice 1 Let us consider a RC circuit The system can be described by the linear differential equation RC ˙ v(t) + v(t) = e(t) with the initial condition : v(0) = 0. Simulate the response v(t) to a step input e(t) = 5V .

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 80 / 142

slide-95
SLIDE 95

Xcos Exercices

Exercice 2 Let us consider the predator-prey model based on Lotka-Volterra equations        ˙ x(t) = x(t)

  • a − by(t)
  • ˙

y(t) = y(t)

  • − c + dx(t)
  • where

x(t) is the number of prey y(t) is the number of predator a and d are parameters describing the growth of the prey population and the predator population b and c are parameters describing the death rate of the prey population and the predator population Simulate 2 the evolution of populations with initial conditions x(0) = 4 and y(0) = 2.

  • 2. numerical values : a = 3, b = 1, c = 2, d = 1
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 81 / 142

slide-96
SLIDE 96

Application to feedback control

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 82 / 142

slide-97
SLIDE 97

Application to feedback control A brief review

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 83 / 142

slide-98
SLIDE 98

Application to feedback control A brief review

A brief review

Objective : Design a controller to control a dynamical system. The output to be controlled is measured and taken into account by the controller. ⇒ feedback control

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 84 / 142

slide-99
SLIDE 99

Application to feedback control A brief review

Example : angular position control of a robotic arm.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 85 / 142

slide-100
SLIDE 100

Application to feedback control A brief review

Example : angular position control of a robotic arm. u(t) is the control voltage of the DC motor (actuator) θ(t) is the angular position of the arm (measured with a sensor) The input-output relationship is given by : ¨ θ(t) + ˙ θ(t) = u(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 85 / 142

slide-101
SLIDE 101

Application to feedback control A brief review

The corresponding transfer function is G(s) = ˆ θ(s) ˆ u(s) = 1 (s + 1)s It has 2 poles : −1 and 0 ⇒ system is unstable

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 86 / 142

slide-102
SLIDE 102

Application to feedback control A brief review

The corresponding transfer function is G(s) = ˆ θ(s) ˆ u(s) = 1 (s + 1)s It has 2 poles : −1 and 0 ⇒ system is unstable Its step response is divergent

0.5 1 1.5 2 2.5 3 0.5 1 1.5 2 2.5 Step Response Time (sec) Amplitude

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 86 / 142

slide-103
SLIDE 103

Application to feedback control A brief review

The asymptotic bode diagram :

10

−1

10 10

1

−40 −20 20 Gain (dB) 10

−1

10 10

1

−180 −135 −90 −45 Phase (degre)

pulsation ω

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 87 / 142

slide-104
SLIDE 104

Application to feedback control A brief review

Closed-loop control with a proportional gain k

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 88 / 142

slide-105
SLIDE 105

Application to feedback control A brief review

Closed-loop control with a proportional gain k The closed-loop transfer function is F(s) = k s2 + s + k The Routh criterion shows that F(s) is stable ∀k > 0.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 88 / 142

slide-106
SLIDE 106

Application to feedback control A brief review

Response of θ(t) for a step reference r(t) = π

2 2 4 6 8 10 12 0.5 1 1.5 2 2.5 Step Response Time (sec) Amplitude k=1 k=2 k=5 k=0.5

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 89 / 142

slide-107
SLIDE 107

Application to feedback control A brief review

Quick analysis of the feedback system The tracking error is given by : ε(t) = r(t) − θ(t) ˆ ε(s) = s2 + s s2 + s + k ˆ r(s) the static error is zero : εs = lim

s→0 s ˆ

ε(s) = 0 (with ˆ r(s) = π/2

s )

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 90 / 142

slide-108
SLIDE 108

Application to feedback control A brief review

Quick analysis of the feedback system The tracking error is given by : ε(t) = r(t) − θ(t) ˆ ε(s) = s2 + s s2 + s + k ˆ r(s) the static error is zero : εs = lim

s→0 s ˆ

ε(s) = 0 (with ˆ r(s) = π/2

s )

Using the standard form of 2nd order systems : F(s) = Kω2

n

s2 + 2ζωns + ω2

n

⇒    K = 1, ωn = √ k ζ = 1/2 √ k we can conclude that when k ր, damping ζ ց and oscillations ր settling time t5% ≈

3 ζωn = 6s.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 90 / 142

slide-109
SLIDE 109

Application to feedback control System analysis in Scilab

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 91 / 142

slide-110
SLIDE 110

Application to feedback control System analysis in Scilab

System analysis in Scilab

Definition of a transfer function

  • -> num = 1;
  • -> den = %s ^2+ %s;
  • -> G = syslin(’c’,num ,den)

G = 1

  • 2

s + s

  • -> roots(den)

ans =

  • 1.

The argument c stands for continuous-time system (d for discrete) The instruction roots is useful to calculate the poles of a transfer function The instruction plzr plots the pole-zero map in the complex plane

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 92 / 142

slide-111
SLIDE 111

Application to feedback control System analysis in Scilab

Computation of the time response

  • -> t = [0:0.02:3];
  • -> theta = csim(’step ’,t,G);
  • -> plot(t,theta)

The string argument step is the control, it can be impuls, a vector or a function. To define the time vector, you may also use the linspace instruction. For frequency analysis, different instructions are provided : repfreq, bode, nyquist, black.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 93 / 142

slide-112
SLIDE 112

Application to feedback control System analysis in Scilab

Systems connection

+

  • +

+

The mathematical operators can handle syslin type Example G1(s) = 1 s + 2 and G2(s) = 4 s

  • -> G1 = syslin(’c’,1,%s +2);
  • -> G2 = syslin(’c’,4,%s);
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 94 / 142

slide-113
SLIDE 113

Application to feedback control System analysis in Scilab

  • -> G1 * G2

// series connection ans = 4

  • 2

2s + s

  • -> G1 + G2

// parallel connection ans = 8 + 5s

  • 2

2s + s

  • -> G1 /. G2

// feedback connection ans = s

  • 2

4 + 2s + s

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 95 / 142

slide-114
SLIDE 114

Application to feedback control System analysis in Scilab

Back to our case study Let simulate the closed-loop control with a proportional gain

  • -> k = 2;
  • -> F = (G*k) /. 1

F = 2

  • 2

2 + s + s

  • -> routh_t(%s ^2+ %s +2)

ans = 1. 2. 1. 0. 2. 0.

  • -> [wn , zeta] = damp(F)

zeta = 0.3535534 0.3535534 wn = 1.4142136 1.4142136

  • -> t = linspace (0 ,12 ,200);
  • -> theta = csim(’step ’,t,F)* %pi /2;
  • -> plot(t,theta)
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 96 / 142

slide-115
SLIDE 115

Application to feedback control Bode plot

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 97 / 142

slide-116
SLIDE 116

Application to feedback control Bode plot

Bode plot

Introductory example : RC circuit

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 98 / 142

slide-117
SLIDE 117

Application to feedback control Bode plot

Bode plot

Introductory example : RC circuit Sinusoidal steady state :    e(t) = em cos(ωt + φe) v(t) = vm cos(ωt + φv) ⇒    e = emejφe v = vmejφv

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 98 / 142

slide-118
SLIDE 118

Application to feedback control Bode plot

For R = 1kΩ and C = 200µF, let apply a voltage e(t) = cos(8t).

1 1.5 2 2.5 3 3.5 4 4.5 5 −1 −0.5 0.5 1 temps (s) e(t) v(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 99 / 142

slide-119
SLIDE 119

Application to feedback control Bode plot

Responses of the circuit with ω = {0.8, 4, 8, 40}

10 15 20 25 30 35 40 45 −1 −0.5 0.5 1

temps (s)

e(t) (ω=0.8) v(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 100 / 142

slide-120
SLIDE 120

Application to feedback control Bode plot

Responses of the circuit with ω = {0.8, 4, 8, 40}

10 15 20 25 30 35 40 45 −1 −0.5 0.5 1

temps (s)

e(t) (ω=0.8) v(t) 2 3 4 5 6 7 8 9 10 −1 −0.5 0.5 1

temps (s)

e(t) (ω=4) v(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 100 / 142

slide-121
SLIDE 121

Application to feedback control Bode plot

Responses of the circuit with ω = {0.8, 4, 8, 40}

10 15 20 25 30 35 40 45 −1 −0.5 0.5 1

temps (s)

e(t) (ω=0.8) v(t) 2 3 4 5 6 7 8 9 10 −1 −0.5 0.5 1

temps (s)

e(t) (ω=4) v(t) 3 3.5 4 4.5 5 5.5 6 6.5 7 −1 −0.5 0.5 1

temps (s)

e(t) (ω=8) v(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 100 / 142

slide-122
SLIDE 122

Application to feedback control Bode plot

Responses of the circuit with ω = {0.8, 4, 8, 40}

10 15 20 25 30 35 40 45 −1 −0.5 0.5 1

temps (s)

e(t) (ω=0.8) v(t) 2 3 4 5 6 7 8 9 10 −1 −0.5 0.5 1

temps (s)

e(t) (ω=4) v(t) 3 3.5 4 4.5 5 5.5 6 6.5 7 −1 −0.5 0.5 1

temps (s)

e(t) (ω=8) v(t) 10 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 −1 −0.5 0.5 1 e(t) (ω=40) v(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 100 / 142

slide-123
SLIDE 123

Application to feedback control Bode plot

Bode diagram of the transfer function

−30 −25 −20 −15 −10 −5 5

Magnitude (dB)

10

−1

10 10

1

10

2

−90 −45

Phase (deg) Bode Diagram Frequency (rad/sec)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 101 / 142

slide-124
SLIDE 124

Application to feedback control Bode plot

Bode diagram of the transfer function

−30 −25 −20 −15 −10 −5 5

Magnitude (dB)

10

−1

10 10

1

10

2

−90 −45

Phase (deg) Bode Diagram Frequency (rad/sec)

ω = 8

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 101 / 142

slide-125
SLIDE 125

Application to feedback control Bode plot

−25 −20 −15 −10 −5 5

Magnitude (dB)

10

−1

10 10

1

10

2

−90 −45

Phase (deg) Bode Diagram Frequency (rad/sec)

ω=8 ω=4 ω=0.8 ω=40

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 102 / 142

slide-126
SLIDE 126

Application to feedback control Bode plot

Frequency analysis consists in studying the response of a LTI system with sine inputs

F(s)

Y(s) U(s)

u(t) = u0 sin(ωt) u(t) = u0 sin(ωt) y(t) = y0 sin(ωt+φ) y(t) = y0 sin(ωt+φ)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 103 / 142

slide-127
SLIDE 127

Application to feedback control Bode plot

Frequency analysis consists in studying the response of a LTI system with sine inputs

F(s)

Y(s) U(s)

u(t) = u0 sin(ωt) u(t) = u0 sin(ωt) y(t) = y0 sin(ωt+φ) y(t) = y0 sin(ωt+φ)

2 4 6 8 10 12 14 16 18 20 −1 −0.5 0.5 1 1.5

u0 y0 y(t) u(t)

T T ∆ t

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.

Brno University of Technology - April 2015 103 / 142

slide-128
SLIDE 128

Application to feedback control Bode plot

A system can then be characterized by its gain : y0 u0 phase shift : ±360∆t T The magnitude and the phase depend on the frequency ω

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 104 / 142

slide-129
SLIDE 129

Application to feedback control Bode plot

A system can then be characterized by its gain : y0 u0 phase shift : ±360∆t T The magnitude and the phase depend on the frequency ω It can be shown that : gain = |F(jω)|, phase shift = arg F(jω). F(jω) is the transfer function of the system where the Laplace variable s has been replaced by jω.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 104 / 142

slide-130
SLIDE 130

Application to feedback control Bode plot

Example : let consider system F(s) = 1/2 s + 1 What are the responses to these inputs ? u1 = sin(0.05 t) u2 = sin(1.5 t) u3 = sin(10 t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 105 / 142

slide-131
SLIDE 131

Application to feedback control Bode plot

Example : let consider system F(s) = 1/2 s + 1 What are the responses to these inputs ? u1 = sin(0.05 t) u2 = sin(1.5 t) u3 = sin(10 t)

we express F(jω) = 1/2 jω + 1 for ω = 0.05 rad/s : |F(j0.05)| = 0.5 and arg F(j0.05) = −2.86◦. for ω = 1.5 rad/s : |F(j1.5)| = 0.277 and arg F(j1.5) = −56.3◦. for ω = 10 rad/s : |F(j10)| = 0.05 and arg F(j10) = −84.3◦.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 105 / 142

slide-132
SLIDE 132

Application to feedback control Bode plot

50 100 150 200 250 300 350 400 450 500 −1 −0.5 0.5 1

u1(t) y1(t)

2 4 6 8 10 12 14 16 −1 −0.5 0.5 1

u2(t) y2(t)

0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 −1 −0.5 0.5 1

u3(t) y3(t)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 106 / 142

slide-133
SLIDE 133

Application to feedback control 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).

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 107 / 142

slide-134
SLIDE 134

Application to feedback control 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).

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 107 / 142

slide-135
SLIDE 135

Application to feedback control Simulation with Xcos

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 108 / 142

slide-136
SLIDE 136

Application to feedback control Simulation with Xcos

Simulation with Xcos

Let simulate the closed-loop control with a proportional gain

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 109 / 142

slide-137
SLIDE 137

Application to feedback control Simulation with Xcos

Simulation with Xcos

Let simulate the closed-loop control with a proportional gain

block sub-palette step Sources/STEP FUNCTION sum

  • Math. Operations/BIGSOM f

gain

  • Math. Operations/GAINBLK f

transfert function

  • Cont. time systems/CLR

scope Sinks/CSCOPE clock 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.

Brno University of Technology - April 2015 109 / 142

slide-138
SLIDE 138

Application to feedback control Exercices

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 110 / 142

slide-139
SLIDE 139

Application to feedback control Exercices

Exercices

Let us consider a system modeled by G(s) = 4 4s2 + 4s + 5 Calculate poles. Is the system stable ? Calculate the closed-lopp transfer function (with a proportional gain k). Calculate the minimal value of k to have a static error ≥ 10%. Calculate the maximal value of k to have an overshoot ≤ 30%. Simulate all these cases with Xcos.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 111 / 142

slide-140
SLIDE 140

Classical control design

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 112 / 142

slide-141
SLIDE 141

Classical control design

Classical control design

Control design aims at designing a controller C(s) in order to assign desired performances to the closed loop system Classical control is a frequency domain approach and is essentially based on Bode plot Main controllers, or compensators, are phase lag, phase lead, PID (proportional integral derivative)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 113 / 142

slide-142
SLIDE 142

Classical control design Loopshaping

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 114 / 142

slide-143
SLIDE 143

Classical control design Loopshaping

Loopshaping

Let express the tracking error ˆ e(s) = 1 1 + G(s)C(s) ˆ r(s) So, a high open-loop gain results in a good tracking it leads to better accuracy and faster response (depending on the bandwidth) but it leads to a more aggressive control input (u) but it reduces stability margins

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 115 / 142

slide-144
SLIDE 144

Classical control design Loopshaping

Let define the open-loop transfer function L = GC Closed-loop performances can be assessed from the Bode plot of L PM and GM are phase and gain margins noise disturbances are a high frequency signals

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 116 / 142

slide-145
SLIDE 145

Classical control design Loopshaping

Loopshaping consists in designing the controller C(s) so as to “shape” the frequency response of L(s) we recall that |L|db = |GC|db = |G|db + |C|db The desired “shape” depends on performance requirements for the closed-loop system

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 117 / 142

slide-146
SLIDE 146

Classical control design Loopshaping

A simple example with a proportional controller The open-loop transfer function is L(s) = 4k s2 + 3s + 3

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 118 / 142

slide-147
SLIDE 147

Classical control design Loopshaping

Bode plot of L(s) for k = {0.5, 1, 5, 10}

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 119 / 142

slide-148
SLIDE 148

Classical control design Loopshaping

Bode plot of L(s) for k = {0.5, 1, 5, 10} when k increases, the phase margin decreases

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 119 / 142

slide-149
SLIDE 149

Classical control design Loopshaping

Step response of the closed-loop system (unit step) for k = {0.5, 1, 5, 10} the static error decreases as k increases

  • scillations increase as k increases
  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 120 / 142

slide-150
SLIDE 150

Classical control design Phase lag controller

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 121 / 142

slide-151
SLIDE 151

Classical control design Phase lag controller

Phase lag controller

The transfer function of the phase lag controller is of the form C(s) = 1 + τs 1 + aτs, with a > 1

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 122 / 142

slide-152
SLIDE 152

Classical control design Phase lag controller

Phase lag controller

The transfer function of the phase lag controller is of the form C(s) = 1 + τs 1 + aτs, with a > 1 a and τ are tuning parameters It allows a higher gain in low frequencies But the phase lag must not reduce the phase margin

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 122 / 142

slide-153
SLIDE 153

Classical control design Phase lag controller

Example G(s) = 4 s2 + 3s + 3 What value for the proportional gain k to have a static error of 10% ?

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 123 / 142

slide-154
SLIDE 154

Classical control design Phase lag controller

Example G(s) = 4 s2 + 3s + 3 What value for the proportional gain k to have a static error of 10% ? static error = 1 1 + 4

3k = 0.1

⇒ k = 6.75 close-loop system response

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 123 / 142

slide-155
SLIDE 155

Classical control design Phase lag controller

Precision ok, but too much oscillations

−80 −60 −40 −20 20

Magnitude (dB)

10

−2

10

−1

10 10

1

10

2

−180 −135 −90 −45

Phase (deg) Bode Diagram Frequency (rad/s)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 124 / 142

slide-156
SLIDE 156

Classical control design Phase lag controller

Precision ok, but too much oscillations

10

−2

10

−1

10 10

1

10

2

−180 −135 −90 −45

Phase (deg) Bode Diagram Frequency (rad/s)

−80 −60 −40 −20 20

Magnitude (dB)

Phase margin : before = 111◦ (at 1.24 rd/s) ; after = 34◦ (at 5.04 rd/s)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 124 / 142

slide-157
SLIDE 157

Classical control design Phase lag controller

Phase lag controller C(s) = 1 + τs 1 + aτs with a > 1

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 125 / 142

slide-158
SLIDE 158

Classical control design Phase lag controller

Phase lag controller C(s) = 1 + τs 1 + aτs with a > 1 We want a high gain only at low frequencies Phase lag must occur before the crossover frequency 1 τ < ω0 = 1.24 ⇒ τ = 1 Then, we want to recover a gain of 1 a = 6.75

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 125 / 142

slide-159
SLIDE 159

Classical control design Phase lag controller 10

−2

10

−1

10 10

1

10

2

−180 −135 −90 −45

Phase (deg) Bode Diagram Frequency (rad/s)

−80 −60 −40 −20 20

Magnitude (dB)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 126 / 142

slide-160
SLIDE 160

Classical control design Phase lag controller −80 −60 −40 −20 20

Magnitude (dB)

10

−3

10

−2

10

−1

10 10

1

10

2

−180 −135 −90 −45

Phase (deg) Bode Diagram Frequency (rad/s)

G(s) kG(s) kC(s)G(s)

Phase margin : now, with the proportional gain and the phase lag controller = 70◦ (at 1.56 rd/s)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 126 / 142

slide-161
SLIDE 161

Classical control design Phase lag controller

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 127 / 142

slide-162
SLIDE 162

Classical control design Phase lag controller

close-loop system response

1 2 3 4 5 6 0.2 0.4 0.6 0.8 1 1.2 1.4

Step Response Time (seconds) Amplitude

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 127 / 142

slide-163
SLIDE 163

Classical control design Phase lead controller

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 128 / 142

slide-164
SLIDE 164

Classical control design Phase lead controller

Phase lead controller

The transfer function of the phase lead controller is of the form C(s) = 1 + aτs 1 + τs , with a > 1

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 129 / 142

slide-165
SLIDE 165

Classical control design Phase lead controller

Phase lead controller

The transfer function of the phase lead controller is of the form C(s) = 1 + aτs 1 + τs , with a > 1 a and τ are tuning parameters It provides a phase lead in a frequency range But the gain may shift the crossover frequency

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 129 / 142

slide-166
SLIDE 166

Classical control design Phase lead controller

The phase lead compensator is used to increase the phase margin Procedure : firstly, adjust a proportional gain k to reach a tradeoff between speed/accuracy and overshoot. measure the current phase margin and subtract to the desired margin ϕm = PMdesired − PMcurrent compute a a = 1 + sin ϕm 1 − sin ϕm at the maximum phase lead ϕm, the magnitude is 20 log √a. Find the frequency ωm for which the magnitude of kG(s) is −20 log √a compute τ τ = 1 ωm √a

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 130 / 142

slide-167
SLIDE 167

Classical control design Phase lead controller

Example G(s) = 4 s(2s + 1)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 131 / 142

slide-168
SLIDE 168

Classical control design Phase lead controller

Example G(s) = 4 s(2s + 1) close-loop system response

5 10 15 20 25 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6

Step Response Time (seconds) Amplitude

  • pen-loop bode diagram

−40 −20 20 40 60

Magnitude (dB)

10

−2

10

−1

10 10

1

−180 −135 −90

Phase (deg) Bode Diagram Frequency (rad/s)

Phase margin : 20◦ at 1.37 rd/s

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 131 / 142

slide-169
SLIDE 169

Classical control design Phase lead controller

Design of a phase lead compensator current phase margin is 20◦, and the desired margin is, say, 60◦ ϕm = 40◦ = 0.70 rd compute a a = 1 + sin ϕm 1 − sin ϕm = 4.62 at the maximum phase lead ϕm, the magnitude is 6.65 db. At the frequency ∼ 2 rd/s the magnitude of G(s) is −6.65 db compute τ τ = 1 ωm √a = 0.23

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 132 / 142

slide-170
SLIDE 170

Classical control design Phase lead controller

Design of a phase lead compensator current phase margin is 20◦, and the desired margin is, say, 60◦ ϕm = 40◦ = 0.70 rd compute a a = 1 + sin ϕm 1 − sin ϕm = 4.62 at the maximum phase lead ϕm, the magnitude is 6.65 db. At the frequency ∼ 2 rd/s the magnitude of G(s) is −6.65 db compute τ τ = 1 ωm √a = 0.23 Hence, the controller is of the form C(s) = 1 + 1.07s 1 + 0.23s

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 132 / 142

slide-171
SLIDE 171

Classical control design Phase lead controller

Example close-loop system response

5 10 15 20 25 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6

Step Response Time (seconds) Amplitude

  • pen-loop bode diagram

−40 −20 20 40 60

Magnitude (dB)

10

−2

10

−1

10 10

1

−180 −135 −90

Phase (deg) Bode Diagram Frequency (rad/s)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 133 / 142

slide-172
SLIDE 172

Classical control design Phase lead controller

Example close-loop system response

5 10 15 20 25 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6

Step Response Time (seconds) Amplitude

  • pen-loop bode diagram

−100 −50 50 100

Magnitude (dB)

10

−2

10

−1

10 10

1

10

2

−180 −135 −90

Phase (deg) Bode Diagram Frequency (rad/s)

New phase margin : 53.7◦ at 2 rd/s

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 133 / 142

slide-173
SLIDE 173

Classical control design PID controller

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 134 / 142

slide-174
SLIDE 174

Classical control design PID controller

PID controller

A PID controller consists in 3 control actions ⇒ proportional, integral and derivative Transfer function of the form : C(s) = kp + ki 1

s + kds

= kp(1 +

1 τis)(1 + τds)

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 135 / 142

slide-175
SLIDE 175

Classical control design PID controller

The phase lag controller is an approximation of the PI controller Phase lag controller C(s) = 1 + τs 1 + aτs with a > 1 PI controller C(s) = 1 + τis τis

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 136 / 142

slide-176
SLIDE 176

Classical control design PID controller

The phase lead controller is an approximation of the PD controller Phase lead controller C(s) = 1 + aτs 1 + τs with a > 1 PD controller C(s) = 1 + τds

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 137 / 142

slide-177
SLIDE 177

Classical control design PID controller

A PID controller is a combination of phase lag and phase lead controllers C(s) = k 1 + τ1s 1 + a1τ1s 1 + a2τ2s 1 + τ2s

  • with a1 > 1 and a2 > 1.

Transfer function of the form : the phase lag part is designed to improve accuracy and responsiveness the phase lead part is designed to improve stability margins an extra low-pass filter may be added to reduce noise C1(s) = 1 1 + τ3s with τ3 ≪ τ2

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 138 / 142

slide-178
SLIDE 178

Classical control design PID controller

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 139 / 142

slide-179
SLIDE 179

Classical control design Exercices

Sommaire

1 What is Scilab ?

Introduction Basics Matrices Plotting Programming

2 For MATLAB users

MATLAB vs Scilab Exercices

3 Xcos

Basics Physical modeling Exercices

4 Application to feedback control

A brief review System analysis in Scilab Bode plot Simulation with Xcos Exercices

5 Classical control design

Loopshaping Phase lag controller Phase lead controller PID controller Exercices

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 140 / 142

slide-180
SLIDE 180

Classical control design Exercices

Exercices

Exercice 1 Let us consider a system modeled by ¨ y(t) + 4 ˙ y(t) + 2y(t) = 3u(t) Calculate the closed-lopp transfer function (with a proportional gain k). What is the static error for k = 1 ? Calculate the value of k to have a static error = 5%. How much is the

  • vershoot for that value ?

Design a phase lag controller. Simulate all these cases with Xcos.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 141 / 142

slide-181
SLIDE 181

Classical control design Exercices

Exercice 2 We now consider a ball and beam system A simple model is given by m¨ x(t) = mg sin(θ(t)) ⇒ linearizing : ¨ x(t) = gθ(t) Is the open-loop system stable ? Is the closed-loop system stable ? Design a phase lead controller. Simulate all these cases with Xcos.

  • Y. Ariba - Icam, Toulouse.

Brno University of Technology - April 2015 142 / 142