Matlabhints
Matlabhints
Matlabhints
Matlab is basically like a calculator for matrices or vectors. Since many quanti-
ties can be represented as vectors or matrices you can do a lot with the program.
There is a special Control Systems Toolbox that contains many additional fea-
tures of particular interest for control. When you work with Matlab always use
the help facility when you are uncertain. You can just type help or you can type
help toolbox/control
or simply
help control
to get information about the control toolbox. If you are uncertain about com-
mands and what to do you can always refresh your memory by running demos,
simply type
demo
and you will get a menu to choose from.
A Learning Philosophy
It is essential that you know what you are doing. You will never become a good
engineer by just learning a program and being a key-board virtuoso. Therefore
you should always solve some simple problems by hand so that you understand
what goes on before you embark on elaborate use of Matlab. It is a very good
practice to always look at the results and ask yourself if they look reasonable.
Check special cases by hand calculations. Try to compute in different ways and
compare.
Polynomials
The polynomial
A(s) = s3 + 2s2 + 3s + 4
is represented as
A=[1 2 3 4]
You find the roots of the polynomial with the command
roots( A)
You can do polynomial calculations. However, you can only add and subtract
polynomials of the same degrees. If they have different degrees you have to make
a hack by patching in zeros in the beginning and the end. This is a consequence
of the fact that matrices is the basic data structure and you cannot add vectors
of the same dimension.
It is easy to multiply polynomials. For example to multiply the polynomial
A(s) above with
B (s) = 2s + 1
you proceed as follows
B=[2 1]
C=conv(A,B)
and you get
C(s) = 2s4 + 5s3 + 8s2 + 11s+4
2000-10-02 17:13 1
Time functions and signals
You can define time functions in the following way:
tmax=10; %length of time scale
dt=0.1; %resoultion of time
t=0:dt:tmax %defines a vector of time instants
u=sin(2*t) %defines a time function
Notice that the sine function is applied to all the elements of the vector t. You
can plot the result using the command
plot(t,u,’b--’)
Type help plot to see what the plot command does.
Transfer Functions
Since Matlab can deal with polynomials it is also easy to deal with transfer
functions which are rational functions. A linear time invariant system can be
defined in many different ways. A system with the transfer function
s+2
G (s) =
(s + 3)(s + 4)
is introduced as follows
G=tf([1 2],conv([1 3],[1 4]));
dx
= Ax + Bu
dt
y = Cx + Du
2000-10-02 17:13 2
Working with systems
Once a system is defined there are commands to do many things. The step re-
sponse of the system G defined above is obtained by
y=step(G,t)
plot(t,y)
Notice that t is the vector of time instants defined under Time functions and
signals. The response of the system with the transfer function G to the sinusoidal
input u defined above is given by the command
[y,t]=lsim(G,u,t)
plot(t,y)
The commands
pole(sys)
zero(sys)
computes the poles and zeros of a system.
Frequency Response
There are several command to generate frequency responses. The command
bode(G)
generates the Bode diagram of the system G. If you do not like the scale dB on
the gain curve you can do like this instead
w=logspace(-1,2);
[gain,phase]=bode(G,w);
subplot(2,1,1)
loglog(w,gain(:))
title(’Gain curve’)
subplot(2,1,2)
semilogx(w,phase(:))
title(’Phase curve’)
The command
nyquist(sys)
plots the Nyquist curve of the system.
Stability
To investigate the stability of a linear time invariant system you can compute
the poles by the command pole. Alternatively you can use the command eig to
obtain the eigenvalues of the A matrix.
Combining Systems
A system which is the cascade combination of two systems S1 and S2 is obtained
simply by the command S1 ∗ S2 .
2000-10-02 17:13 3
Transformation of Systems
In the control systems toolbox there are many commands to transform systems.
The command
[A,B,C,D]=tf2ss([1 2],[1 7 12])
converts the transfer function
s+2
G (s) =
s2 + 7s + 12
to state space form. The command
[CSYS,T]=canon(G,’modal’)
converts the system G to diagonal form.
An Example
For example the electric motor in Homework 3:2 has the state equations
R
d i − L − kLe i 1
= + L
u
dt ω ki
−D ω 0
J J
y = (0 1)u
The system can be introduced into Matlab with the following commands
R=5;
L=0.001;
ke=1.5;
ki=ke;
J=0.1;
D=0.001;
A=[-R/L -ke/L;ki/J -D/J];
B=[1/L;0];
C=[0 1];
motor=ss(A,B,C,0)
The poles and zeros of the system are obtaind with the commands
pole(motor) %This gives -500 and -4.5
zero(motor) %The system has no zeros
The poles can also be obtained as the eigenvalues of the matrix A
eig(A)
Check with the results of the command pole.
2000-10-02 17:13 4
represents the dynamics of one dimensional heat conduction in a wall that is
isolated on one side.
The transfer function G can be defined symbolically as
G=’1./cosh(sqrt(s))’
When the funtion is defined you can use it for calculations. The Nyquist curve
of the system can be plotted as follows
w=0:0.1:20;
s=i*w;
gv=eval(G); %evaluates the function G at w
plot(real(gv),imag(gv))
axis([-1 1 -1 1])
Suppose that we are interested in the critical gain of the system. Then we first
have to determine the frequency ω 180 where the process has a phase lag of 180 ○
and then determine the process gain at that frequency. This can be done as
follows
[x,k]=min(abs(angle(gv)+pi));
wc=w(k)
G180=gv(k)
This gives
wc = 19.7000
G180 =-0.0865 - 0.0003i
Since G180 is not purely real we repeat the calculations with higher resolution
of ω , e.g.
w=19.5:0.0001:20;
s=i*w;
gv=eval(G); %evaluates the function G at w
[x,k]=min(abs(angle(gv)+pi));
wc=w(k)
G180=gv(k)
kc=1/G180
This gives
G180=gv(k)
wc = 19.7392
G180 = -0.0863 - 0.0000i
Kc=-1/G180
Kc = 11.5919 + 0.0000i
The critical gain is thus 11.6. To check this we can make the Nyquist plot of the
system 11.6*G by the following command
plot(11.59*real(gv),11.59*imag(gv))
axis([-1 1 -1 1])
2000-10-02 17:13 5