Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Engenharia Electrotécnica
Controlo
1.ª AULA
Matlab
Docente
MATLAB
• 1ª aula
O MATLAB (MATrix LABoratory) é uma linguagem de programação concebida
pela MathWorks, Inc. baseadas na manipulação de matrizes e destinada a realizar
cálculos matemáticos e de engenharia tornando-se assim uma poderosa ferramenta de
software muito utilizada na concepção, simulação e análise de sistemas de controlo.
3 5
EX: a = 4.3 , b = 6 , A = [ 5 2.2 3] , B =
1 10
» a=4.3
a=
4.3000
»
O mesmo que a variável a mas com ; no fim faz com que não apareça no Workspace o
resultado da operação.
» A=[ 5 2.2 3]
A=
5.0000 2.2000 3.0000
»B=[3 5 ; 1 10]
B=
3 5
1 10
»
a 3 5
Criar a matriz C á custa das variáveis a e b já criadas C =
4 5 b
» C=[ a 3 5; 4 5 b]
C=
4.3000 3.0000 5.0000
4.0000 5.0000 6.0000
»
Todas as variáveis uma vez criadas podem ser visualizadas através do comando whos:
» whos
Name Size Bytes Class
A 1x3 24 double array
B 2x2 32 double array
C 2x3 48 double array
a 1x1 8 double array
b 1x1 8 double array
Grand total is 15 elements using 120 bytes
»
» clear
»whos
»
Dimensão
Para saber a dimensão de uma matriz basta o comando size(G):
» G= [3 4 5; 13 4 7 ];
»size(G)
ans=
2 3
»
» g= [ 4 7 5 13 4 7 ];
»length(g)
ans=
6
»
Transposta
» E=C'
E=
4.3000 4.0000
3.0000 5.0000
5.0000 6.0000
»
Multiplicação
» B*C
ans =
32.9000 34.0000 45.0000
44.3000 53.0000 65.0000
»
No caso de haver engano é enviado uma mensagem de erro, dando pistas para a não
concretização do pedido:
» C*B
??? Error using ==> *
Inner matrix dimensions must agree.
»
SOMA
»b=6;
»f=a+b
f=
10.3000
»D=[1 1 ;1 1];
»A+D
ans =
4 6
2 11
» B+C
??? Error using ==> +
Matrix dimensions must agree.
»
Divisão
» A/a
ans =
1.1628 0.5116 0.6977
»
C C12 C13
C = 11
C21 C22 c23
A = [A1 A2 A3 ]
Sub-matrizes
A partir do vector A=[ 5 2.2 3 ] formar outro vector s baseado nos dois últimos
elementos de A, s=[ 2.2 3 ] :
»s=A(2:3)
s=
2.2000 3.0000
»
Por Engª Sónia Marques 5
Instituto Politécnico de Setúbal - Controlo - 02/03
1 2 3 6
5 7 4 9
E = 2 1 5 2 E E33 1 5
D = 32 =
E 42 E 43 7 8
5 7 8 4
7 1 3 5
Matriz Identidade
Para criar uma matriz identidade basta fazer eye(5) onde 5 significa a dimensão da
matriz:
» eye(5)
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
»
HELP
Existem uma série de bibliotecas com funções já definidas.
» help
HELP topics:
matlab\general - General purpose commands.
matlab\ops - Operators and special characters.
matlab\lang - Programming language constructs.
matlab\elmat - Elementary matrices and matrix manipulation.
matlab\elfun - Elementary math functions.
matlab\specfun - Specialized math functions.
matlab\matfun - Matrix functions - numerical linear algebra.
matlab\datafun - Data analysis and Fourier transforms.
matlab\polyfun - Interpolation and polynomials.
matlab\funfun - Function functions and ODE solvers.
matlab\sparfun - Sparse matrices.
matlab\graph2d - Two dimensional graphs.
matlab\graph3d - Three dimensional graphs.
matlab\specgraph - Specialized graphs.
matlab\graphics - Handle Graphics.
matlab\uitools - Graphical user interface tools.
matlab\strfun - Character strings.
matlab\iofun - File input/output.
matlab\timefun - Time and dates.
matlab\datatypes - Data types and structures.
matlab\winfun - Windows Operating System Interface Files (DDE/ActiveX)
matlab\demos - Examples and demonstrations.
toolbox\ncd - Nonlinear Control Design Blockset
toolbox\control - Control System Toolbox.
control\ctrlguis - Control System Toolbox -- GUI support functions.
control\obsolete - Control System Toolbox -- obsolete commands.
stateflow\sfdemos - Stateflow demonstrations and samples.
toolbox\sb2sl - SystemBuild to Simulink Translator
stateflow\stateflow - Stateflow
simulink\simulink - Simulink
simulink\blocks - Simulink block library.
simulink\simdemos - Simulink 3 demonstrations and samples.
simulink\dee - Differential Equation Editor
toolbox\tour - MATLAB Tour
MATLABR11\work - (No table of contents file)
toolbox\local - Preferences.
» help matlab\elfun
Exponential.
exp - Exponential.
log - Natural logarithm.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm and dissect floating point number.
pow2 - Base 2 power and scale floating point number.
sqrt - Square root.
nextpow2 - Next higher power of 2.
Complex.
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data from real and imaginary parts.
conj - Complex conjugate.
imag - Complex imaginary part.
real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
Sempre que se pretenda saber se uma função está já definida no matlab basta escrever
help nome_da_função, como por exemplo:
» help cos
COS Cosine.
COS(X) is the cosine of the elements of X.
»
Complexos
» k=1.5+2j
k=
1.5000 + 2.0000i
» » f=4-5i
f=
4.0000 - 5.0000i
»
Polinómio e raízes
O polinómio é representado pelos seus coeficientes, como por exemplo o polinómio
x 3 + 2 x 2 + 3x + 4 é representado pelo vector [1 2 3 4],
» p=[1 2 3 4]
p=
1 2 3 4
»
» a=roots(p)
ans =
-1.6506
-0.1747 + 1.5469i
-0.1747 - 1.5469i
»
Pode-se reconstruir o polinómio original baseado nas raízes pelo comando poly(a):
» poly(a)
ans =
1.0000 2.0000 3.0000 4.0000
»
Diagrama de Blocos
Considere o seguinte diagrama de blocos
1 1
s +s
2
s +1
2
é equivalente a:
1
s + s + s2 + s
4 3
» num= 1;
» den=[1 1 0];
» sys1=tf(num,den)
Transfer function:
1
-------
s^2 + s
» sys2=tf(num,[1 0 1])
Transfer function:
1
-------
s^2 + 1
» series(sys1,sys2)
Transfer function:
1
-------------------
s^4 + s^3 + s^2 + s
»
ou
» sys1*sys2
Transfer function:
1
-------------------
s^4 + s^3 + s^2 + s
»
1
s +s
2 +
+
1
s +1
2
é equivalente a
1 1
+ 2
s + s s +1
2
2s 2 + s + 1
s4 + s3 + s2 + s
» parallel(sys1,sys2)
Transfer function:
2 s^2 + s + 1
-------------------
s^4 + s^3 + s^2 + s
»
1
s +12
s2 +1
s4 + s3 + s2 + s
» feedback(sys1,sys2)
Transfer function:
s^2 + 1
-----------------------
s^4 + s^3 + s^2 + s + 1
»
Modelos
1
Considerando a função de transferência com pólos distintos G ( s ) = pode-se
s ( s + 1)
expandir em fracções parciais pelo comando residue(b,a) :
» num=1
num =
1
» den
den =
1 1 0
» [r,p,k]=residue(num,den)
r=
-1
1
p=
-1
0
k=
[]
»
onde r significa os zeros, p os pólos e k o
1 r1 r 2 1 1
ganho: G( s ) = = + +k = −
s( s + 1) p1 p2 s s +1
1
A transformada de Laplace inversa da função g ( t ) = L−1 [G( s )] = L−1 =? é
s( s + 1)
dado pelo comando ilaplace(função) onde é necessário primeiro declarar s como
variável symbolica e a função tem de ser dada na forma de resíduo,
1 − 1 1
g ( t ) = L−1 [G( s )] = L−1 = L−1 + =?
s( s + 1) s s + 1
» syms s
» ilaplace((-1/s)+1/(s+1))
ans =
-1+exp(-t)
»
[ ]
G( s ) = L − 1 + e −t = −
1
+
1
s s +1
» syms t
» laplace(-1+exp(-t))
ans =
-1/s+1/(s+1)
»
» num=[25];
»den=[ 1 4 25];
» G=tf(num,den)
Transfer function:
25
--------------
s^2 + 4 s + 25
»
» K=25
K=
25
» sys1=zpk(Z,P,K)
Zero/pole/gain:
25
---------------
(s^2 + 4s + 25)
»
s 2 + 2s + 3 tf2zp s + 1 ± 1.414i
G ( s) = G ( s) =
s 3 + 3s 2 + 3s + 1 ( s + 1) 3
» num=[1 2 3];
» den=[1 3 3 1];
» [z,p,k]=tf2zp(num,den)
z=
-1.0000 + 1.4142i
-1.0000 - 1.4142i
p=
-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i
k=
1
s + 1 ± 1.414i zp2tf s 2 + 2s + 3
G ( s) = G ( s) =
( s + 1) 3 s 3 + 3s 2 + 3s + 1
» [num,den]=zp2tf(z,p,k)
num =
0 1.0000 2.0000 3.0000
den =
1.0000 3.0000 3.0000 1.0000
»
» num=[0 1 2 3];
» den=[ 1 –12 16 272 –1017 740];
» sys=tf(num,den)
Transfer function:
s^2 + 2 s + 3
----------------------------------------------
s^5 - 12 s^4 + 16 s^3 + 272 s^2 - 1017 s + 740» num
» pzmap(sys)
Pole-zero map
1.5
0.5
Imag Axis
-0.5
-1
-1.5
-6 -4 -2 0 2 4 6
Real Axis
Ganho estático
O ganho estático do sistema G (0) = lim G (t ) = lim G ( s) pode ser obtido pelo comando
t →∞ s →0
» dcgain(sys)
ans =
0.0041
Convolução
Considere os seguintes polinómios x 3 + 2 x 2 + 3x + 4 e 7 x 2 + 8 x + 9 representados
pelos vectores a=[ 1 2 3 4] e b=[7 8 9] a convolução é exequível pelo comando
conv(a,b) e corresponde à multiplicação entre polinómios cujo resultado é
7 x 5 + 22 x 4 + 46 x 3 + 70 x 2 + 59 x + 36 ,
» a=[ 1 2 3 4];
» b =[ 7 8 9];
»c=conv(a,b)
c=
7 22 46 70 59 36
»
Deconvolução
»deconv(c,a)
ans =
7 8 9
»