Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Introduc) On To MATLAB For Control Engineers: EE 447 Autumn 2008 Eric Klavins

This document introduces MATLAB for control engineers. It discusses matrices and vectors, accessing elements, operations like transpose and multiplication. It also covers solving differential equations numerically using ode45, defining functions in separate files, and using MATLAB to check solutions rather than as a replacement for showing work. As an example, it models a pendulum using differential equations.

Uploaded by

Christina Hill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Introduc) On To MATLAB For Control Engineers: EE 447 Autumn 2008 Eric Klavins

This document introduces MATLAB for control engineers. It discusses matrices and vectors, accessing elements, operations like transpose and multiplication. It also covers solving differential equations numerically using ode45, defining functions in separate files, and using MATLAB to check solutions rather than as a replacement for showing work. As an example, it models a pendulum using differential equations.

Uploaded by

Christina Hill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

lnLroducuon Lo MA1LA8

for ConLrol Lnglneers


LL 447
AuLumn 2008
Lrlc klavlns
arL l
MaLrlces and vecLors
>> A=[1 2 3; 4 5 6; 7 8 9]; # A 3x3 matrix
>> x=[1;2;3]; # A n-dim (column) vector
WhlLespace separaLes enLrles and a semlcolon separaLes rows.
>> A(2,3)
ans =
6
>> A(:,2)
ans =
2
5
8
>> A(3,3)=-1
ans =
1 2 3
4 5 6
7 8 -1
?ou can access parLs
>> A # transpose
>> A*x # multiplication
>> A*A
>> x*x
>> det(A) # determinant
>> inv(A)
...
All Lhe sLandard sLu ls Lhere
Llgenvalues and LlgenvecLors
>> [P,D] = eigs(A)
P =
-0.3054 -0.2580 -0.7530
-0.7238 -0.3770 0.6525
-0.6187 0.8896 -0.0847
D =
11.8161 0 0
0 -6.4206 0
0 0 -0.3954
>> P*D*inv(P)
ans =
1.0000 2.0000 3.0000
4.0000 5.0000 6.0000
7.0000 8.0000 -1.0000
>> A
A =
1 2 3
4 5 6
7 8 -1
!ordan lorm
>> B=[1 3 4 1; 0 1 1 2; 0 0 1 0; 0 0 0 1]
B =
1 3 4 1
0 1 1 2
0 0 1 0
0 0 0 1
>> [M,J]=jordan(B)
M =
3.0000 0 0 0
0 1.0000 1.0000 2.3333
0 0 -1.0000 -2.0000
0 0 1.0000 1.0000
J =
1 1 0 0
0 1 1 0
0 0 1 0
0 0 0 1
>> M*J*inv(M)
ans =
1.0000 3.0000 4.0000 1.0000
0 1.0000 1.0000 2.0000
0 0 1.0000 0
0 0 0 1.0000
Solvlng Ax=b for x
>> A
A =
1 3 4 1
0 1 1 2
0 0 1 0
0 0 0 1
>> b=[1;0;1;0]
b =
1
0
1
0
>> x=A\b
x =
0
-1
1
0
Symbollc Calculauons
>> syms k # declares k to be a symbol
>> A=[1 k; k 1]
A =
[ 1, k]
[ k, 1]
>> [M,J]=jordan(A)
M =
[ 1/2, 1/2]
[ -1/2, 1/2]
J =
[ -k+1, 0]
[ 0, k+1]
>> syms lam
>> cp=det(lam * eye(2) - A)
cp =
lam^2-2*lam+1-k^2
>> lam=solve(cp)
lam =
k+1
-k+1
1he 2x2 ldenuLy
maLrlx
Solvlng ulerenual Lquauons LxacLly
>> sol=dsolve('Dx = a*x + b', 'x(0) = x0')
sol =
-1/a*b+exp(a*t)*(x0+1/a*b)
>> f=subs ( sol, {'a','b','x0'}, {-1,2,3} )
f =
2+exp(-t)
>> tp=0:0.1:10;
>> plot(tp,subs(f,tp))
Solvlng ulerenual Lquauons
ApproxlmaLely
# this defines a function of t and a column vector x
>> f = @(t,x) [ -x(1) - x(2)^2; sin(x(1)) ]
>> f(1,[2,3]) # for example, this is f
# applied to some arguments
ans =
-11.0000
0.9093
>> [t,x] = ode45 ( f, [0,20], [1,1] );
# the ode45 function numerically
# simulates the ode represented by f
>> plot ( t, x(:,1), t, x(:,2) )
>> legend('x1(t)', 'x2(t)')
ume lnLerval
lnlual condluon
Solvlng ulerenual Lquauons
ApproxlmaLely
# this defines a function of t and a column vector x
>> f = @(t,x) [ -x(1) - x(2)^2; sin(x(1)) ]
>> f(1,[2,3]) # for example, this is f
# applied to some arguments
ans =
-11.0000
0.9093
>> [t,x] = ode45 ( f, [0,20], [1,1] );
# the ode45 function numerically
# simulates the ode represented by f
>> plot ( t, x(:,1), t, x(:,2) )
>> legend('x1(t)', 'x2(t)')
ume lnLerval
lnlual condluon
?ou can also look aL Lhe x-y plane
f = @(t,x) [ -x(1) - x(2)^2; sin(x(1)) ];
hold all
[t,x] = ode45 ( f, [0,20], [2,1] ); plot ( x(:,1), x(:,2) );
[t,x] = ode45 ( f, [0,20], [1,3] ); plot ( x(:,1), x(:,2) );
[t,x] = ode45 ( f, [0,20], [-8,0] ); plot ( x(:,1), x(:,2) );
[X,Y] = meshgrid(-10:1:3,-2:1:3);
DX=-X-Y.^2;
DY=sin(X);
quiver(X,Y,DX,DY);
Pow does numerlcal slmulauon work?
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
k
s +
k
1
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
k
s +
k
1
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
k
s +
k
1
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
k
s +
k
1
f = @(t,x) [ -x(1) - x(2)^2; sin(x(1)) ];
tf=30;
zinit=[1;2];
figure
hold all
for h=0.51:-0.1:0.01
n=int16(tf/h);
t=zeros(1,n);
z=zeros(2,n);
z(:,1) = zinit;
t(1) = 0;
for i=2:n
z(:,i) = z(:,i-1) + h * f ( i*h, z(:,i-1) );
t(i) = i*h;
end
plot ( z(1,:), z(2,:) );
end
luncuons Can be uened ln
llles lnsLead (musL be ln same dlrecLory)
function dx = lorenz_func ( t, x )
sigma = 10;
rho = 28;
beta = 8/3;
dx = [
sigma * ( x(2) - x(1) );
x(1)*(rho-x(3))-x(2);
x(1) * x(2) - beta*x(3)
];
ConLenLs of lorenz_func.m
>> [t,x] = ode45 ( 'lorenz_func', [0,20], [-10,20,-5] );
>> plot3 ( x(:,1), x(:,2), x(:,3) )
>> xlabel('x')
>> ylabel('y')
>> zlabel('z')
luncuons Can be uened ln
llles lnsLead (musL be ln same dlrecLory)
function dx = lorenz_func ( t, x )
sigma = 10;
rho = 28;
beta = 8/3;
dx = [
sigma * ( x(2) - x(1) );
x(1)*(rho-x(3))-x(2);
x(1) * x(2) - beta*x(3)
];
ConLenLs of lorenz_func.m
>> [t,x] = ode45 ( 'lorenz_func', [0,20], [-10,20,-5] );
>> plot3 ( x(:,1), x(:,2), x(:,3) )
>> xlabel('x')
>> ylabel('y')
>> zlabel('z')
Pow Lo use MA1LA8 ln 1hls Class
unless an exerclses says Lo use MA1LA8, you
should do Lhe exerclse by hand.
?ou can use MA1LA8 Lo check your work.
?ou musL show your work ln maLhemaucal
problems and calculauons.
L.g. when ndlng [ordan form, whaL equauons dld
you seL up and solve?
arL ll: An Lxample

mgh
u


a sin b + u


a sin b

0
0

= 0
= 0, , 2, ...
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

1
1he endulum

mgh
u
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
1

mgh
u
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
1

mgh
u
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
1
1orque due Lo gravlLy
1orque due
Lo frlcuon
1orque from moLor
1o do:
1) undersLand Lhe naLural behavlor (when u=0)
2) 8ulld and analyze a feedforward conLroller
3) 8ulld and analyze a proporuonal conLroller
4) 8ulld a u conLroller
3) 8ulld and analyze a lu conLroller
naLural 8ehavlor (u=0)
Lqulllbrlum polnLs
We expecL LhaL 2nn are sLable and (2n+1)n are unsLable - buL we
don'L have Lhe analyucal Lools yeL.
So leL's ploL Lhe vecLor eld.

mgh
u


a sin b + u


a sin b

0
0

= 0
= 0, , 2, ...
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

1
a=1;
b=0.25;
f = @(t,x) [ x(2); -a*sin(x(1)) - b*x(2) ];
hold all
[t,x] = ode45 ( f, [0,20], [pi-0.01,-0.1] );
plot ( x(:,1), x(:,2) );
...
[TH,OM] = meshgrid(-10:1:10,-3:1:3);
DTH=OM;
DOM=-a*sin(TH)-b*OM;
quiver(TH,OM,DTH,DOM);
u
0
sLable unsLable
leedforward
Say we wanL 0 Lo go Lo a deslred angel r.
noLe, sln 0 = 0 so aL equlllbrlum we geL

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

1
Slnce we wanL 0 = r and u = 0 aL equlllbrlum we geL

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

1
leedforward
r=0.5;
aguess = 0.95;
uff = aguess * r;
f = @(t,x) [ x(2); -a*sin(x(1)) - b*x(2)
+ uff ];
[t,x] = ode45 ( f, [0,tf], [0,0] );
plot ( t, x(:,1), [0 tf], [r r] );
aguess=0.5;
uff = aguess * r;
f = @(t,x) [ x(2); -a*sin(x(1)) - b*x(2)
+ uff ];
[t,x] = ode45 ( f, [0,tf], [0,0] );
plot ( t, x(:,1), [0 tf], [r r] );
roporuonal leedback

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
1
Can make Lhe sLeady
sLaLe error small by
Lurnlng up kp.
1hls rlng" ls
annoylng.
roporuonal-uerlvauve ConLrol

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e
e =
x = lim
h0
x(t + h) x(t)
h
1
lnLegral leedback
uene a new sLaLe z (malnLalned by Lhe conLroller) and puL

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e
e =
z = e
1
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e + K
I
z
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
k
s +
k
2
aguess = 0.95;
uff = aguess * r;
f = @(t,x) [ x(2); -a*sin(x(1))-b*x(2)+uff+Kp*(r-x(1))-Kd*x(2)+Ki*x(3); r-x(1) ];
[t,x] = ode45 ( f, [0,tf], [0,0,0] );
plot ( t, x(:,1), [0 tf], [r r] );
Analysls

0 1 0
a K
p
b K
d
K
i
1 0 0

0
K
p
1

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e
e =
1
sin

0 1 0
a K
p
b K
d
K
i
1 0 0

0
K
p
1

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e
1
e =
z = e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e + K
I
z
x = lim
h0
x(t + h) x(t)
h
x(t + h) x(t)
h
f(x, t)
x(t + h) x(t) + hf(x, t)
u = u
+
+ u

e = e
+
+ e

y = y
+
+ y

e
+
+ e

y
+
= u
+
y
+
y

= u

y
+
y

y = y
+
y

= (u
+
u

)
= u
2
sin

0 1 0
a K
p
b K
d
K
i
1 0 0

0
K
p
1

mgh
u


a sin b + u


a sin b

0
0

= 0
a sin b u a b u u = a + b
u = ar
= 0, , 2, ...
x = f(x, t)
e = r
u = u
ff
+ u
fb
= ar + K
p
e
u = u
ff
+ u
fb
= ar + K
p
e + K
d
e
1
syms a b Kp Kd Ki th om z r
A = [ 0 1 0 ; -a-Kp, -b-Kd, Ki; -1 0 0 ];
B = [ 0; Kp; 1 ];
% steady state analysis
ss=A\(-B*r)
% transient analysis
lam=eig(A);
subs (lam, {a,b,Ki,Kd,Kp}, {1,1/4,1,1,1} )
lb=simplify(subs (lam, {a,b,Kp,Kd}, {1,1/4,5,3} ));
plot ( 0:80, subs(lb(1,1),{Ki},{0:80} ), 0:80, subs(lb(2,1),{Ki},{0:80} ),
0:80, subs(lb(3,1),{Ki},{0:80} ) )
ss =
r
0
1/Ki*r*a
ans =
-0.6214
-0.3143 + 1.2291i
-0.3143 - 1.2291i
kl
8
e
(
l
a
m
b
d
a
)

Slmullnk uemo

You might also like