Professional Documents
Culture Documents
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 m-file
( 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
s 4 3s 3 2s 1
x = [1 3 0 -2 1];
(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
1 -4 -7 10
-20
-30
R=
-40
-3
-2
-1
5.0000
-2.0000
1.0000
numf=[1 1 3 1];
denf=[1 0 1];
[r,p,k]=residue(numf,denf)
G(s)
Y(s) output
S-plane
pole
Im
Re
zero
Command zpk
zp2tf:
converts the numerator, denominator from roots to
coefficient. [NUM,DEN] = ZP2TF(Z,P,K)
9
G1(s)
y
G2(s)
G1(s)
G2(s)
10
G1(s)
H(s)
11
G4 =1/(s - 1)
G5 =1/(s + 1)
G6 =a/(a^2 + s^2)
12
System Response
SystemStep, impulse, other inputs
Matlab commands:
lsim
sys=tf(num,den);
t=0:dt:final_t;
u=f(t);
[y t]=lsim(sys,u,t)
step
step(sys)
response
14
Transient response:
x(t)=x0*exp(a*t)
if a<0,its stable.
s-plane
Im
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
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
Amplitude
s-plane
1.2
Re
0.8
0.6
0.4
0.2
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
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
Y CX DU
A
B
1 x1 0
x 0
1
u (t )
x2 k / m b / m x2 1 / m
u (t )
ky
by
x
1
y x1 [1 0] 0
x2
Y (s)
1
U ( s ) ms 2 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)
22
Step
StepResponse
Response
Step Response
6
Amplitude
10
15
20
25
30
Time (sec)
23
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:
[T,X]=ode23(@lin1,[0 10], [a(i) b(j)]); -3-3
-2
-1
0
1
2
x1
plot(X(:,1),X(:,2))
[u.v]=eig(A)
Hint:For using ode command, the diff. equations
u=
should be written in the first order format,i.e. a second
0 1
order diff. eq. should be written as two first order diff.
1 0
equations.
24
function dx=pendulum(t,x)
dx=zeros(2,1)
dx(1)=x(2);
dx(2)=-0.5*x(2)-sin(x(1));
Viscose Damping term
x2
0.5
Gravity term
-0.5
-1
-1.5
-2
-1.5
-1
-0.5
0
x1
0.5
1.5
Angle
0.5
Angular velocity
-0.5
-1
-1.5
-2
10
12
14
16
18
20
25