You are on page 1of 8

Matlab tutorial Part 5

State Space Methods

ME 461
Professor Tilbury
Note: These tutorials have been completely revised and updated. See Control Tutorials for
Matlab for the latest version.
In this tutorial, we will use the example of the magnetically suspended ball. We derived the
equations in class (see your notes for March 24th for details):
M d^2h/ dt ^2 = M g - k i ^2/ h

V = L di / dt + i R
where h is the height of the ball, i is the current through the electromagnet, V is the voltage applied,
and M, g, L, R, and k are constants which reflect the physical properties of the system (mass, etc).
We chose values M =1, k =1, L =1, R =1, g =10, and determined that the system was at equilibria
whenever h =i^2/10 (at which point dh/dt =0). We then linearized the equations about the point h =
0.1, and got the state-space equations:
dx/ dt = A x + B u
y = C x
where x =[(delta h) d(delta h)/dt (delta i)]' is the state of the system (a 3- vector), u =V is the input,
and y =(delta h) is the output. Enter the system matrices into a m-file in your matlab directory (call
it magball.m)
A = [ 0 1 0
10 0 - 20
0 0 - 1] ;

B = [ 0
1] ;

C = [ 1 0 0] ;
and run the m-file by typing "magball" to the matlab prompt.
One of the first things you might want to do with the state equations is to find the poles of the
system; these are the values of s where det(sI - A) =0, and are exactly the eigenvalues of the A
pol es = ei g( A)
Some of them are in the right-half plane, which means that the system is unstable in open-loop.
Check what happens to an unstable system with a nonzero initial condition. To your m-file, add the
Pgina 1 de 8 Matlab tutorial - 5
t = 0: 0. 01: 2;
u = 0*t ;
x0 = [ 0. 1 0. 1 0. 1] ;
[ y, x] = l si m( A, B, C, 0, u, t , x0) ;
pl ot ( t , x)
and run the file again. It looks like the ball will fall down to minus infinity, but probably it hits the
table or the floor first (and also probably goes out of the range where our linearization is valid).

Matlab can also compute the transfer function of this system:
NUM( s) - 1
H( s) = - - - - - - - - = C( sI - A) B + D
DEN( s)
using the command
[ num, den] = ss2t f ( A, B, C, 0, 1)
Note that the denominator has some negative coefficients; this should tell you (from the
Routh criterion) that the system is unstable in open-loop, which we knew from its poles.
You could also find the poles of the system using
r oot s( den)
Check to see that this gives you the same answer as before.
Controller design
Let's build a controller for this system. The first thing to check is whether the system is controllable.
We do this by forming the controllability matrix:
Pgina 2 de 8 Matlab tutorial - 5
Cc = [ B ( AB) ( A^2 B) ]
and checking that it has full-rank (non-zero determinant). We can build up this matrix in matlab
(using a few transposes):
Cc = [ B' ; ( A*B) ' ; ( A*A*B) ' ] '
det ( Cc)
or matlab has a built-in function called CTRB which finds this matrix for any dimension of state.
Check that the determinant is non-zero:
det ( ct r b( A, B) )
If the system is controllable, then by using full-state feedback we can place the poles anywhere we
want. As we did in class, we could convert the system into control canonical form, choose the
control matrix K which would give the desired poles, and then convert back to the original
coordinates. Or we could use the matlab function PLACE.
Before attempting either method, we should decide where we want the closed-loop poles to be. If the
criteria for the controller were settling time <1 sec and overshoot <5%, then we might try to place
the two dominant poles at -5 +/- 5i (at zeta =0.7 or 45 degrees with sigma =5 >4.6). The third pole
we might place at -10 for starters, and see what the closed-loop behavior is:
p1 = - 5 + 5i ;
p2 = - 5 - 5i ;
p3 = - 10;

K = pl ace( A, B, [ p1 p2 p3] ) ;

l si m( A- B*K, B, C, 0, u, t , x0) ;

Pgina 3 de 8 Matlab tutorial - 5
If we wanted a faster response (with the same overshoot), we might place the poles further to the
p1 = - 10 + 10i ;
p2 = - 10 - 10i ;
p3 = - 20;
Compare the control effort required (K) in both cases. In general, the farther you move the poles, the
more control effort it takes.

Now we will examine the method proposed in class. We will perform a state transformation from x
to z
x = Tc z
and in the z coordinates we want the system equations to be in control canonical form. From your
notes, recall the the transformation into control canonical form is found as:
Cc = ct r b( A, B) ;
t 3 = [ 0 0 1] *i nv( Cc) ;
t 2 = t 3*A;
t 1 = t 2*A;
Tci = [ t 1
t 2
t 3] ;
Tc = i nv( Tci ) ;
Ac = Tci *A*Tc
If you enter the lines as above and run your m-file, you should see the output of Ac (since there is no
semicolon after that line); as expected, it is in control canonical form. The coefficients of the
characteristic polynomial are the first row of Ac.
Pgina 4 de 8 Matlab tutorial - 5
The desired characteristic polynomial is given by the product of the three factors (s - pi) for the three
desired poles:
al pha = conv( conv( [ 1 - p1] , [ 1 - p2] ) , [ 1 - p3] )
and the desired gains can be easily computed as the first row of the control form A matrix plus the
last three elements of alpha, the desired characteristic polynomial.
Kc = Ac( 1, : ) + al pha( 2: 4)
K = Kc*Tci
Check that these are the same gains as those given by the PLACE command. Note that if you want to
place two or more poles at the same position, PLACE will not work. You can use the method
outlined above, or you can use a function called ACKER which works similarly to PLACE:
K = acker ( A, B, [ p1 p2 p3] )
Introducing the reference input
If we were to take the control system as defined above, and apply a step input,
t = 0: 0. 01: 2;
u = ones( si ze( t ) ) ;
l si m( A- B*K, B, C, 0, u, t )

the system does not track the step well at all; not only is the magnitude not one, but it is negative
instead of positive!
Following the notes from class (or the book section 7.3.2), we can introduce the reference input by
computing the N matrices:
Pgina 5 de 8 Matlab tutorial - 5
N = i nv( [ A B; C 0] ) *[ 0 0 0 1] ' ;
Nx = N( 1: 3) ;
Nu = N( 4) ;
Nbar = Nu + K*Nx
Now, if we want to find the response of the system under state feedback with this introduction of the
reference, we simply note the fact that the input is multiplied by this new factor Nbar:
l si m( A- B*K, B*Nbar , C, 0, u, t )
and now
indeed, a step can be tracked reasonably well. If you need a faster response, go back to the design of
K and move the poles farther to the left half plane.
Observer design
When we can't measure all the states x (as is commonly the case), we can build an observer to
estimate the states while only measuring the output y =C x. For the magnetic ball example, we will
add three new states to the system.
Before doing anything, we should probably check that the system is observable. As in the
controllability case, Matlab has a built-in function to compute the observability matrix:
det ( obsv( A, C) )
Check that this is nonzero.
Now we need to choose the observer gain L. If we want to use place, we need to put the three
observer poles in different places.
op1 = - 20;
op2 = - 21;
op3 = - 22;
Pgina 6 de 8 Matlab tutorial - 5
Because of the duality between controllability and observability, we can use the same technique, but
replace B by C and take transposes:
L = pl ace( A' , C' , [ op1 op2 op3] ) ' ;
The combined state equations are (from the book or your notes):
At = [ A - B*K zer os( si ze( A) )
L*C - B*K A - L*C] ;
Bt = [ B*Nbar
B*Nbar ] ;
Ct = [ C zer os( si ze( C) ) ] ;
and you can see the results for a nonzero initial condition using the command:
l si m( At , Bt , Ct , 0, zer os( si ze( t ) ) , t , [ x0 0 0 0] ) ;

If you wanted to see all the states, including the state of the observer,
[ y, x] = l si m( At , Bt , Ct , 0, zer os( si ze( t ) ) , t , [ x0 0 0 0] ) ;
pl ot ( t , x) ;
Pgina 7 de 8 Matlab tutorial - 5

Return to ME461 Homepage
Last modified: 10 april 1995 /
Please send any comments, clarifications, suggestions, or corrections to:
Thanks! I hope you found this document helpful.
Pgina 8 de 8 Matlab tutorial - 5