Você está na página 1de 28

MANUAL MATLAB

Descritivo de Operação
MATLAB

1 12/03/08
MANUAL MATLAB

A. OPERACOES MATLAB ..................................................................................................................... 3


1. FUNÇÕES MATLAB ................................................................................................................................. 3
 Diary – gera arquivo de trabalho no matlab ...................................................................................... 3
 Matriz Simples .................................................................................................................................... 3
 Arquivo m – aquivo gerado em txt e salvo com .m ............................................................................. 3
 Sqrt – raiz quadrada ........................................................................................................................... 4
 Abs – absoluto .................................................................................................................................... 4
 Matriz – mudar matriz na coordenada desejada ................................................................................ 4
 Matriz- Inserir linha em uma matriz .................................................................................................. 4
 Matriz- retirar linhas em uma matriz ................................................................................................ 5
 Matriz – inserindo colunas ................................................................................................................. 5
 Who- verifica variaveis na memoria ................................................................................................... 5
 Whos – detalhar variáveis .................................................................................................................. 5
 Ans – usando informações contidas em ans ....................................................................................... 6
 Divisão................................................................................................................................................ 6
 Help - mostra o conteúdo completo ................................................................................................... 6
 Help – mostrar commandos ................................................................................................................ 6
 Help – mostrar detalhes do comando ................................................................................................. 7
 Matriz - transposta – linha virar coluna sem alterar a diagonal ...................................................... 7
 Matriz – adição e subtracao .............................................................................................................. 7
 Matriz – subtrair elemento da matriz ................................................................................................. 8
 Matriz – multiplicação........................................................................................................................ 8
 Função pi ............................................................................................................................................ 8
 Matriz – divisão .................................................................................................................................. 8
 Função clear....................................................................................................................................... 8
 Matriz – exponencial .......................................................................................................................... 9
 Operacoes com conjuntos - multiplicacao....................................................................................... 9
 Operacoes com conjuntos - exponenciacao...................................................................................... 10
 Operadores logicos .......................................................................................................................... 10
 vetor - incremento............................................................................................................................ 11
 vetor – decremento ........................................................................................................................... 11
 linspace :gera n pontos linear entre x1 e x2 ..................................................................................... 11
 matriz – Separar coordenadas, colunas e linhas .............................................................................. 11
 Matriz - inserir 1 ............................................................................................................................. 13
 Matriz – inserir 0 .............................................................................................................................. 13
 Matriz – modificar matriz ................................................................................................................. 13
 Gráficos- plot.................................................................................................................................... 14
 Grafico – plot - matriz x vetor .......................................................................................................... 14
 Grafico – plot - matriz x matriz ........................................................................................................ 15
 Graficos – plot –alterar estilo da linha ............................................................................................ 15
 Gráficos –clf limpar gráfico ............................................................................................................. 16
 Gráficos – mesh ................................................................................................................................ 16
 Gráfico – plot – definir pontos para plot.......................................................................................... 16
 Controle de fluxo – for...................................................................................................................... 17
 Controle de fluxo – while.................................................................................................................. 18
 Controle de fluxo – if e break ........................................................................................................... 18
 Conv – multiplicação de polinômios ................................................................................................ 18
 Gráficos – mostrar figuras ............................................................................................................... 19

2 12/03/08
MANUAL MATLAB

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-3 -2 -1 0 1 2 3

............................................................................ 19
2. SINAIS DE TESTES.................................................................................................................................. 19
 Sinais de teste – degrau .................................................................................................................... 19
 Sinais de teste – rampa ..................................................................................................................... 20
 Sinais de teste – parábola ................................................................................................................. 22
3. COMPORTAMENTO DOS SISTEMAS ......................................................................................................... 23
 Root – extrair raiz ............................................................................................................................ 23
 Pole – definir o pólo da funcao transferencia .................................................................................. 23
4. FUNÇÃO DE TRANSFERÊNCIA ................................................................................................................. 23
 Transformar equação diferencial em função de transferência (dsolve,simple,pretty) ..................... 23
5. DIAGRAMA DE BODE ............................................................................................................................. 25
 grafico .............................................................................................................................................. 25
6. MODELAGEM NO ESPAÇO DE ESTADOS ................................................................................................. 25
 Achar a função de transferencia....................................................................................................... 27

A. OPERACOES MATLAB

1. Funções Matlab

 Diary – gera arquivo de trabalho no matlab

>> diary('06_08_2005')

 Matriz Simples
>> A=[123;456;789]

A=

123
456
789

 Arquivo m – aquivo gerado em txt e salvo com .m


Gera comandos no txt e salva arquivos com .m , no exemplo foi criado uma matriz a=[123;456;789] no txt e
salvo com o nome gera.m

3 12/03/08
MANUAL MATLAB

>> gera

a=

123
456
789

 Sqrt – raiz quadrada


>> x=sqrt(4)

x=

 Abs – absoluto
>> x=abs(-1)

x=

 Matriz – mudar matriz na coordenada desejada


>> a=[111;222;333]

a=

111
222
333

>> a(2,1)=2*a(3,1)

a=

111
666
333

 Matriz- Inserir linha em uma matriz


>>a
a=

111
666
333

>> r=[10];
>> a=[a;r]

a=

111
666

4 12/03/08
MANUAL MATLAB

333
10

 Matriz- retirar linhas em uma matriz


>>a
a=

111
666
333
10
>> a=a(1:3,:)

a=

111
666
333

 Matriz – inserindo colunas


>> b=[1:2:3:4:5:6]
Warning: Colon operands must be real scalars.

b=

1 2 3 4 5 6

>> b=[1:2:3:4:5:6;1:2:3:4:5:6]
Warning: Colon operands must be real scalars.
b=

1 2 3 4 5 6
1 2 3 4 5 6

 Who- verifica variaveis na memoria


>> who

Your variables are:

A a ans b r x

 Whos – detalhar variáveis


>> whos
Name Size Bytes Class

A 3x1 24 double array


a 3x1 24 double array
ans 1x1 8 double array
b 2x6 96 double array
r 1x1 8 double array
x 1x1 8 double array

5 12/03/08
MANUAL MATLAB

Grand total is 21 elements using 168 bytes

 Ans – usando informações contidas em ans

>> 2*50

ans =

100

>> 2*ans

ans =

200

 Divisão
>> 1/4

ans =

0.2500

>> 4\1

ans =

0.2500

 Help - mostra o conteúdo completo


>> help
HELP topics

matlab\general - General purpose commands.


matlab\ops - Operators and special characters.
matlab\lang - Programming language constructs.
.
.
.

 Help – mostrar commandos


help control
Control System Toolbox
Version 6.0 (R14) 05-May-2004

General.
ctrlpref - Set Control System Toolbox preferences.
ltimodels - Detailed help on the various types of LTI models.
ltiprops - Detailed help on available LTI model properties.

Creating linear models.

6 12/03/08
MANUAL MATLAB

tf - Create transfer function models.


.
.
.

 Help – mostrar detalhes do comando


>>help lsim
LSIM Simulate time response of LTI models to arbitrary inputs.

LSIM(SYS,U,T) plots the time response of the LTI model SYS to the
input signal described by U and T. The time vector T consists of
regularly spaced time samples and U is a matrix with as many columns
as inputs and whose i-th row specifies the input value at time T(i).
For example,

 Matriz - transposta – linha virar coluna sem alterar a diagonal


>> a=[1,2,3;4,5,6;7,8,9]

a=

1 2 3
4 5 6
7 8 9

>> d=a'

d=

1 4 7
2 5 8
3 6 9

 Matriz – adição e subtracao


a=

1 2 3
4 5 6
7 8 9

>> d=a'

d=

1 4 7
2 5 8
3 6 9

>> c=a + d

c=

2 6 10
6 10 14
10 14 18

7 12/03/08
MANUAL MATLAB

 Matriz – subtrair elemento da matriz


>> x=[-1 0 2]'

x=

-1
0
2

>> y=x-1

y=

-2
-1
1

 Matriz – multiplicação
>> x=[-1 0 2]'

x=

-1
0
2

>> y=x-1

y=

-2
-1
1

>> x'*y

ans =

 Função pi
>> pi

ans =

3.1416

 Matriz – divisão

 Função clear
Limpar uma variável

8 12/03/08
MANUAL MATLAB

>> b=[13,14,15]

b=

13 14 15

>> clear b

limpar todas variáveis

>> who

Your variables are:

a ans c d x y
>> clear
>> who
>>

 Matriz – exponencial
>> b = [2,4;3,6]

b=

2 4
3 6

>> b^2

ans =

16 32
24 48

 Operacoes com conjuntos - multiplicacao

>> x=[-1 0 2]'

x=

-1
0
2

>> x*2

ans =

-2
0
4

>> x=[1 2 3];y=[4 5 6]

9 12/03/08
MANUAL MATLAB

y=

4 5 6

>> x=[1 2 3];y=[4 5 6];


>> z=x.*y

z=

4 10 18

 Operacoes com conjuntos - exponenciacao


>> x.^2

ans =

1
0
4

>> x

x=

1 2 3

>> y

y=

4 5 6

>> clear z
>> z= x.^y

z=

1 32 729

>> z=2.^[x y]

z=

2 4 8 16 32 64

 Operadores logicos
>> 1==1 & 4==3

ans =

>> 1==1 | 4==3

10 12/03/08
MANUAL MATLAB

ans =

 vetor - incremento
incrementa vetor de 1 ate 5 de 1 em 1
>> x=1:5

x=

1 2 3 4 5

incrementa vetor de 1 ate 10 de 2 em 2


>> x=1:2:10

x=

1 3 5 7 9

 vetor – decremento
Decrementa x de 5 ate 1 de 1 em 1
>> x=5:-1:1

x=

5 4 3 2 1

 linspace :gera n pontos linear entre x1 e x2


>> linspace (0,1,6)

ans =

0 0.2000 0.4000 0.6000 0.8000 1.0000

 matriz – Separar coordenadas, colunas e linhas


>> a

a=

1 2 3
4 5 6
7 8 9

>> a(3,3)=a(1,3)+a(3,1)

a=

1 2 3
4 5 6
7 8 10

>> a(1,3)

11 12/03/08
MANUAL MATLAB

ans =

>> a(3,1)

ans =

exemplo 2

>> a=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25]

a=

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

>> a(1:5,3) % de a 1 a 5 pego a coluna 3

ans =

3
8
13
18
23

>> a(1:5,4:5) % de 1 a 5 pego a coluna 4 e 5

ans =

4 5
9 10
14 15
19 20
24 25

exemplo 3
a=

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

>> a(1:2:5,:)% da coluna de 1 a 5 pega 1 coluna e pulo a outra

ans =

12 12/03/08
MANUAL MATLAB

1 2 3 4 5
11 12 13 14 15
21 22 23 24 25

 Matriz - inserir 1

>> b= ones (5)

b=

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

 Matriz – inserir 0
>> b= zeros(5)

b=

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

 Matriz – modificar matriz


>> a=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25]

a=

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

>> b=zeros(5)

b=

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

>> b(1:1:3,4:5)=a(5:-1:3,1:2)

13 12/03/08
MANUAL MATLAB

b=

0 0 0 21 22
0 0 0 16 17
0 0 0 11 12
0 0 0 0 0
0 0 0 0 0

 Gráficos- plot
>> y=[0.0,0.48,0.84,1.0,0.91,0.6,0.14];
>> plot (y)

onde 0:defasagem:60 frequencia :1000tempo


t=0:60:1000;
>> y=sin(t);
>> plot(t,y)
>> plot(t,y,'k')
>> hold on
>> t=180:60:1000;
>> y=sin(t);
>> plot(t,y,'y')
>> hold on
>> t=360:60:1000;
>> y=sin(t);
>> plot(t,y,'r')

 Grafico – plot - matriz x vetor


>> y=[1,2,3;4,5,6;7,8,9]

y=

1 2 3
4 5 6
7 8 9

>> x=[1,2,3]

x=

1 2 3

>> plot (x,y)


>> grid on

14 12/03/08
MANUAL MATLAB

1
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3

 Grafico – plot - matriz x matriz


>> x=[1,2,3;4,5,6;7,8,9]

x=

1 2 3
4 5 6
7 8 9

>> y=x'

y=

1 4 7
2 5 8
3 6 9

>> plot (x,y)


>> grid on
9

1
1 2 3 4 5 6 7 8 9

 Graficos – plot –alterar estilo da linha


>> t=0:60:1000;
>> plot(t,cos(t),'k--*')%tracejar
>> plot(t,cos(t),'b.')%pontilhar

15 12/03/08
MANUAL MATLAB

 Gráficos –clf limpar gráfico


>> clf

 Gráficos – mesh

>> x=[1,2,3;4,5,6;7,8,9]

x=

1 2 3
4 5 6
7 8 9

>> y=x'

y=

1 4 7
2 5 8
3 6 9
>> z=x.*exp(-x.^2-y.^2);
>> mesh(x,y,z)

0.5

-0.5
2
1 2
0 1
0
-1 -1
-2 -2

 Gráfico – plot – definir pontos para plot


fplot('sin',[-pi pi])

16 12/03/08
MANUAL MATLAB

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-3 -2 -1 0 1 2 3

 Controle de fluxo – for


>> for i=1:5,
x(i)=i^2
end
>> i

i=

>> x(i)

ans =

25

outro exemplo
>> for i=1:8,
for j=1:8,
a(i,j)=i+j;
b(i,j)=i-j;
end
end
>> a

a=

2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 11
5 6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13 14
8 9 10 11 12 13 14 15
9 10 11 12 13 14 15 16

>> b

b=

0 -1 -2 -3 -4 -5 -6 -7
1 0 -1 -2 -3 -4 -5 -6
2 1 0 -1 -2 -3 -4 -5

17 12/03/08
MANUAL MATLAB

3 2 1 0 -1 -2 -3 -4
4 3 2 1 0 -1 -2 -3
5 4 3 2 1 0 -1 -2
6 5 4 3 2 1 0 -1
7 6 5 4 3 2 1 0

 Controle de fluxo – while


>> a=1;b=15;
>> while a<b,
clc %limpa tela e vai para o inicio
a=a+1
b=b-1
end

 Controle de fluxo – if e break


>> for i=1:5,
for j=1:5,
if i == j
a(i,j)=2;
else if abs(i-j)==1
a(i,j)=-1;
else
a(i,j)=0;
end
end
end
end
>> a

a=

2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2

 Conv – multiplicação de polinômios


>> num=1*10

num =

10

>> p1=[20000 200 1]

p1 =

20000 200 1

>> p2=[443 1]

p2 =

18 12/03/08
MANUAL MATLAB

443 1

>> den=conv(p1,p2)

den =

8860000 108600 643 1

>> gma=tf([num],[den])

Transfer function:
10
-------------------------------------
8.86e006 s^3 + 108600 s^2 + 643 s + 1

 Gráficos – mostrar figuras


>> figure (1)

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-3 -2 -1 0 1 2 3

2. Sinais de Testes

 Sinais de teste – degrau


>> g=tf([5],[1 2 10])

Transfer function:
5
--------------
s^2 + 2 s + 10

>> step(g)

19 12/03/08
MANUAL MATLAB

Step Response
0.7

0.6

0.5

0.4
Amplitude

0.3

0.2

0.1

0
0 1 2 3 4 5 6
Time (sec)

 Sinais de teste – rampa


>> t=2:1/100:5;%2 ponto de inicio, 1/100 divisoes, 5 maximo
>> k=1;
>> u=k.*t;%fazendo a coordenada = abssissa
>> plot(t,u,'c')

4.5

3.5

2.5

2
2 2.5 3 3.5 4 4.5 5

>> hold on
>> g=tf([5],[1 2 10])%entrada do sistema e criacao da funcao transferencia

Transfer function:
5
--------------
s^2 + 2 s + 10

>> y=lsim(g,u,t);
Warning: Simulation will start at the nonzero initial time T(1).
> In lti.lsim at 91
>> plot (t,y,'y')

4.5

3.5

2.5

1.5

0.5

0
2 2.5 3 3.5 4 4.5 5

outro exemplo
>> t=0:0.01:5;%0 ponto de inicio, 1/100 divisoes, 5 maximo

20 12/03/08
MANUAL MATLAB

>> k=1;
>> u=k.*t;%fazendo a coordenada = abcissa
>> y=lsim(g,u,t);
>> plot (t,y,'b*')
>> hold on
>> k=0:0.01:5;
>> plot (t,k,'b*')

4.5

3.5

2.5

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

outro exemplo

>> degrau = tf([1],[1 0]) %(1/s * 1/s = 1/s^2)

Transfer function:
1
-
s

>> degrau = tf([1],[1 0]) %(1/s)

Transfer function:
1
-
s

>> step (degrau)%(1/s * 1/s = 1/s^2)

Step Response
1.4

1.2

0.8
Amplitude

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (sec)

>> hold on
>> g=tf([5],[1 2 10])%entrada do sistema e criacao da funcao transferencia

Transfer function:
5
--------------

21 12/03/08
MANUAL MATLAB

s^2 + 2 s + 10

>> t=0:1/100:1;
>> u=step(degrau);
>> y=lsim(g,u,t);
>> plot (t,y,'r')

Step Response
1.4

1.2

0.8
Amplitude

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (sec)

 Sinais de teste – parábola


>> r=1;
>> rampa =tf([r],[1 0])%1/s

Transfer function:
1
-
s

>> step(rampa)%1/s^2
Step Response
1.4

1.2

0.8
Amplitude

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (sec)

>> hold on
>> t=0:1/100:1;
>> u=step(rampa);%1/s^3 ???
>> g=tf([5],[1 2 10])%entrada do sistema e criacao da funcao transferencia

Transfer function:
5
--------------
s^2 + 2 s + 10

>> y=lsim(g,u,t);
>> plot (t,y,'r')

22 12/03/08
MANUAL MATLAB

Step Response
1.4

1.2

0.8
Amplitude

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (sec)

3. Comportamento dos sistemas

 Root – extrair raiz


>> roots ([1 -1 -2 0])

ans =

0
2
-1
sistema do tipo 1 (uma raiz)

 Pole – definir o pólo da funcao transferencia


>> g=tf([1],[1 1 -2 0])

Transfer function:
1
---------------
s^3 + s^2 - 2 s

>> pole(g)%polo é a raiz do denominador, e zero é a raiz do numerador

ans =

0
-2
1

4. função de transferência

 Transformar equação diferencial em função de transferência (dsolve,simple,pretty)

a*d^2y/dt^2 + bdy/dt+ cy = d*d^u/dt^2+e*du/dt +f

23 12/03/08
MANUAL MATLAB

matlab
>> y=dsolve('a*D2y+b*Dy+c*y=1','y(0)=0,Dy(0)=0')

y=

-1/2*exp(-1/2*(b-(b^2-4*c*a)^(1/2))/a*t)*(b+(b^2-4*c*a)^(1/2))/(b^2-4*c*a)^(1/2)/c+1/2*exp(-1/2*(b+(b^2-
4*c*a)^(1/2))/a*t)*(b-(b^2-4*c*a)^(1/2))/(b^2-4*c*a)^(1/2)/c+1/c

>> s=laplace(y)

s=

-1/2*(b+(b^2-4*c*a)^(1/2))/(b^2-4*c*a)^(1/2)/c/(s+1/2/a*(b-(b^2-4*c*a)^(1/2)))+1/2*(b-(b^2-
4*c*a)^(1/2))/(b^2-4*c*a)^(1/2)/c/(s+1/2/a*(b+(b^2-4*c*a)^(1/2)))+1/c/s

>> ss=simple(s)

ss =

1/(s^3*a+c*s+s^2*b)

>> pretty(ss)

1
-----------------
3 2
s a+cs+s b
segunda parte

>> u=dsolve('d*D2u+e*Du +f=1','u(0)=0,Du(0)=0')

u=

-(-1+f)/e^2*d*exp(-e/d*t)+t/e-t/e*f+d*(-1+f)/e^2

>> s=laplace(u)

s=

-d*(-1+f)/e^2/(s+e/d)+1/e/s^2-1/e*f/s^2+d*(-1+f)/e^2/s

>> ss=simple(s)

ss =

(1-f)/(s*d+e)/s^2

>> pretty(ss)

1-f

24 12/03/08
MANUAL MATLAB

------------
2
(s d + e) s

5. Diagrama de bode

 grafico
>> g=tf([100],[1 6 100])

Transfer function:
100
---------------
s^2 + 6 s + 100

>> bode(g)

Bode Diagram
20

0
Magnitude (dB)

-20

-40

-60

-80
0

-45
Phase (deg)

-90

-135

-180
-1 0 1 2 3
10 10 10 10 10
Frequency (rad/sec)

6. Modelagem no Espaço de Estados


Equação do sistema
My..+cy.+ky=u

>> A=[0 1;-2 -3]

A=

0 1
-2 -3

>> B=[0;1]

25 12/03/08
MANUAL MATLAB

B=

0
1

>> C=[1 0]

C=

1 0

>> C=[1 0]

C=

1 0

>> D=0

D=

>> impulse(A,B,C,D)

Impulse Response
0.25

0.2

0.15
Amplitude

0.1

0.05

0
0 1 2 3 4 5 6
Time (sec)
>>

obter a funcao de transferência

>> [num,den]=ss2tf(A,B,C,D)

num =

0 -0.0000 1.0000

den =

1 3 2

>> printsys(num,den)

26 12/03/08
MANUAL MATLAB

num/den =

-4.4409e-016 s + 1
------------------
s^2 + 3 s + 2

 Achar a função de transferência apartir da equação no espaço de estado


>> m=5%exercicio da pagina 47 do caderno

m=

>> c=2

c=

>> k=1

k=

>> A=[0 1;-k/m -c/m]

A=

0 1.0000
-0.2000 -0.4000

>> B=[0;1/m]

B=

0
0.2000

>> C=[1 0]

C=

1 0

>> D=0

D=

>> num=[1]

num =

27 12/03/08
MANUAL MATLAB

>> den=[m c k]

den =

5 2 1

>> [A,B,C,D]=tf2ss(num,den)

A=

-0.4000 -0.2000
1.0000 0

B=

1
0

C=

0 0.2000

D=

>> [num,den]=ss2tf(A,B,C,D)

num =

0 0.0000 0.2000

den =

1.0000 0.4000 0.2000

>> g=tf(num,den)

Transfer function:
1.11e-016 s + 0.2
-----------------
s^2 + 0.4 s + 0.2

28 12/03/08

Você também pode gostar