Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

MATLAB Tutorial

U(s) input

G(s)

Y(s) output

Outline
Outline

Starting Matlab
Basics
Modeling
Control toolbox

m file
M-file
When writing a program in matlab save it as mfile
( filename.m)
2 types of M-file
1- script (has no input and output, simply execute
commands)
2- function (need input, starts with keyword
function)
function [y z]=mfunc(x)
y=(x*x')^.5; % norm of x
z=sum(x)/length(x); %%% using 'sum' function
end
3

Polynomials

Present polynomial with coefficients vector

s 4 3s 3 2 s 1

x = [1 3 0 -2 1];

Poly converts roots to coefficients of a polynomial:

( s 2)( s 5)( s 6)

2,5,6
( s 2)( s 5)

P3=poly([-2 -5 -6])
rootsP3=roots(P3)
P5=conv([1 2],[1 5])

Polynomials

p=poly([-2 1 5])
R=roots(p)
x=-3:0.1:6;
y=p(1)*x.^3+p(2)*x.^2+p(3)*x+p(4);
plot(x,y)
grid

40
30
20
10
0

p=

-10
-20

-4

-7

10

-30
-40
-3

-2

-1

R=
5.0000
-2.0000
1.0000
5

Partial Fraction Expansion

numf=[1 1 3 1];
denf=[1 0 1];
[r,p,k]=residue(numf,denf)

Dynamic system Representation


using Transfer Function
U(s) input

Roots of numerators are


zeros of a system,
Roots of denominators are
poles of a system.

G(s)

Y(s) output

S-plane

pole

I
m
R
e
zero

Transfer Function in MATLAB


Command tf by defining the

Command zpk

Using s=tf(s), then for example:


8

Transfer Function in MATLAB


tf2zp: converts the numerator, denominator from coefficient to
roots.
[Z,P,K] = TF2ZP(NUM,DEN)
Ex.:

[z,p,k]=tf2zp([1 1],[1 2 1])


z=-1, p=-1;-1, k=1

zp2tf:
converts the numerator, denominator from roots to
[NUM,DEN] = ZP2TF(Z,P,K)

coefficient.
9

Interconnection between blocks


G=series(G1,G2) or alternatively:
u

G1(s)

G2(s)

G=parallel (G1,G2) or alternatively:


u

G1(s)

G2(s)

10

Interconnection between blocks


Feedback:

G1(s)

H(s)

or alternatively for Negative feedback:

11

Symbolic computation in MATLAB


Syms s t :defines s, and t as symbolic variable
syms s a t
1)G4=laplace(exp(t)):
2)G5=laplace(exp(-t)):
3)G6=laplace(sin(a*t)):

G4 =1/(s - 1)
G5 =1/(s + 1)
G6 =a/(a^2 + s^2)

Hint:ilaplace(F,s,t): computes Inverse Laplace transform of F on


the complex variable s and returns it as a function of the time, t.
ilaplace(a/(s^2+a^2),s,t)=(a*sin(t*(a^2)^(1/2)))/(a^2)^(1/2)

12

Symbolic computation in MATLAB


Ex.:A=[1,1;0,1];syms t; Q=expm(A*t)
Hint: expm(M) computes the matrix exponential
of M.
G=laplace(Q,t,s) gives:
G=
[ 1/(s - 1), 1/(s - 1)^2]
[
0, 1/(s - 1)]
13

System Response
SystemStep, impulse, other inputs

Matlab commands:
inputs

lsim

Simulate LTI model response to arbitrary

sys=tf(num,den);
t=0:dt:final_t;
u=f(t);
[y t]=lsim(sys,u,t)

input

step

step(sys)
response

Simulate LTI model response to step


special case of lsim

14

First order systems

Transient response:
x(t)=x0*exp(a*t)
if a<0,its stable.

sImplane
s=a

Re
15

Exp(a*t)
When a is a complex number:
t=0:0.1:5;
a=-1+4*i; % a is complex with negative real part
f=exp(a*t);
X=real(f);
Y=imag(f);
plot(X,Y)
xlabel('Re')
ylabel('Im')
axis('square')
Plot(t,f)

16

First order systems(contd)


When a is complex, with
Negative real part
in polar coordinates:
Rho=sqrt(X.^2+Y.^2);
Theta=atan2(Y,X);
polar(Theta,Rho)

17

Second
System
Secondorder
order systems
In Laplace domain:
Like mass-spring-damper

s=tf('s')
w=1;
zeta=[0.2 0.4 0.7 1 2];
for i=1:length(zeta)
G=w^2/(s^2+2*zeta(i)*w*s+w^2)
step(G,10)
hold on
end

18

Second order systems


Damping ratio is
constant, wn changes.

1.2

Im

Amplitude

s-plane

Step Response

1.4

Re

0.8

Damping ratio is 0.5:


Overshoots are equal.

0.6

0.4

0.2

0
0
0.5
s=tf('s')
zeta=0.5
w=[sqrt(12) 4 sqrt(20)];
for i=1:length(w)
G=(w(i))^2/(s^2+2*zeta*w(i)*s+(w(i))^2)
step(G,3)
hold on
end

1.5

2.5

Time (sec)

19

Second order systems


Undamped system(mass-spring)
Damping ratio is zero:
Step Response

2.5

w =1
w =2
2

1.5
Amplitude

w=[1 2];
for i=1:length(w)
G=tf(w(i)^2,[1 0
w(i)^2]);
step(G,20)
hold on
end

0.5

-0.5

10
Time (sec)

12

14

16

18

20

20

Dynamic system representation


ma F my u ky by
x1 y
x x2
1

x2 y
mx 2 u bx2 kx1
X AX BU

Y CX DU
A
B
1 x1 0
x 0
1

u (t )
x 2 k / m b / m x2 1 / m

u (t )

ky

by

C x
y x1 [1 0] 1 0
x2

Y ( s)
1
System Transfer Function
2
U ( s ) ms bs k

21

MATLAB code

k=.2;
b=.5;
m=1;
A=[0 1;-k/m -b/m];
B=[0;1/m];
C=[1 0];
D=0;
F=ss(A,B,C,D)
step(F)
[num den]=ss2tf(A,B,C,D)
Gs=tf(num,den)

% spring stiffness coefficient


% damping coefficient
% mass
% Represent A.
% Represent column vector B.
% Represent row vector C.
% Represent D.
% Create an LTI object and display.

% system transfer function

22

Step
StepResponse
Response
Step Response

Amplitude

10

15

20

25

30

Time (sec)

23

Ordinary differential equations


3

x2

function dx=lin1(t,x)
dx=zeros(2,1);
0
dx(1)=-x(1);
dx(2)=-2*x(2);
-1
A=[-1 0;0 -2];
-2
[u,v]=eig(A)
In workspace:
-3
[T,X]=ode23(@lin1,[0 10],
-3
-2
-1
0
1
2
x1
[a(i) b(j)]);
plot(X(:,1),X(:,2))
Hint:For using ode command, the diff. equations
[u.v]=eig(A)
should be written in the first order format,i.e. a second
u=
order diff. eq. should be written as two first order diff.
0
1
equations.
1
0

24

Nonlinear Differential eq. of a pendulum


1.5

Viscose Damping term

Gravity term

0.5

-0.5

[T,X]=ode23(@pendulum,[0 10],
[a(i) 0]);
plot(X(:,1),X(:,2
))
Angle
Angular
velocity

x2

function dx=pendulum(t,x)
dx=zeros(2,1)
dx(1)=x(2);
dx(2)=-0.5*x(2)-sin(x(1));

-1

-1.5
-2

-1.5

-1

-0.5

0
x1

0.5

1.5

0.5

-0.5

-1

-1.5

-2

10

12

14

16

18

20

25

You might also like