Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Control System Toolbox For Matlab: Intro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CONTROL SYSTEM TOOLBOX FOR MATLAB

Intro
Matlab has a number of add-on packages called toolboxes. The Control Toolbox contains
suite of functions and system model representations that make it easy to create and
analyze linear dynamic systems. The Control Toolbox will be found on most university
installations of Matlab but must be purchased separately for the Student Version of
Matlab. At the end of this documents are a number of examples.

Creating system models


System models can be created in transfer function, state-space or zero/pole form. To
create a model in transfer function form, enter the coefficients of the numerator and
denominator. Here are some examples.

H ( s ) = ( s + 1) /( s + 2)

>> num = [1 1];


>> den = [1 2];
>> sys1 = tf(num,den);

Or, you can enter all in one line:

>> sys1 = tf([1 1],[1 2]);

H ( s ) = (3s 2 ) /( 2 s 2 + 6 s + 3)

>> num = [3 0 0]; % note the trailing zeros


>> den = [2 6 3];
>> sys2 = tf(num,den);

To see the transfer function, type the system name

>> sys1
>> sys2

Another way to enter system models that are specified in transfer function form is to
define 's' as the complex variable used in transfer functions, and then type in the transfer
function directly.

First, you define s

>> s = tf('s');

Matlab Control Toolbox Notes, Page 1 of 12


You only need this command once per Matlab session, and in any .m function file which
uses s in this manner.

New, just type in your transfer function. Here are two examples.

3s 2
H ( s) =
2s 2 + 6s + 3

>> sys1 = (3*s^2)/(2*s^2 + 6*s + 3)

1
G ( s) =
( s + 5)( s + 2) 2

>> sys2= 1/((s+5)*(s+2)^2)

For info on entering systems in state-space or pole zero forms:

>> help zpk


>> help ss
>> help ltimodels

You can create a new system that represents the parallel interconnection of two systems
by adding them together

>> sys3 = sys1 + sys2

Or, a system which represents a cascade of two systems by multiplying them together

>> sys = sys1*sys2

System analysis
To find the poles, zeros and gain of system sys:

>> pole(sys)
>> zero(sys)
>> dcgain(sys)

To plot a pole/zero map on the s-plane

>> pzmap(sys)

To get the damping and natural frequency of a system

>> damp(sys)

Matlab Control Toolbox Notes, Page 2 of 12


Time and frequency analysis
To plot a step or impulse response of the system

>> step(sys)
>> impulse(sys)

The above form takes a guess at how far out to plot the response. If you want the
response to stop at a specific time t, use

>> step(sys,t)

To create a custom plot, store the output of step as shown in this script file

[y,t] = step(sys);
plot(t,y)
xlabel(‘Time’)
ylabel(‘Step response’)

To create step responses out to time=stoptime of several systems sys1, sys2, …, sysN on
one figure use

>> step(sys1,sys2, …, sysN, stoptime)

For four systems, each with a different line style

>> step(sys1, sys2, ‘--‘, sys3, ‘o’, sys4, ‘+’)

To produce the initial condition response of a system use

>> initial(sys,x0)

Type “help initial” for more on this function.

Matlab Control Toolbox Notes, Page 3 of 12


A Bode plot produces the magnitude of the frequency response in dB and the phase.
Produce a Bode plot with

>> bode(sys)

To get the numbers out of a Bode plot without drawing the plot use

>> [mag,phase]=bode(sys)

If you want to specify a specific frequency range (in radians/sec) use

>> bode(sys,{wmin,wmax})

To plot the bode plots of several systems

>> bode(sys1, sys2, …, sysN)

As always, “help bode” will give all the details.

Matlab Control Toolbox Notes, Page 4 of 12


Model building
With the control toolbox, you can easily build up models of arbitrary complexity.

To cascade systems s1 and s2

>> s3 = s1*s2 or >> s3 = series(s1,s2) S1 S2

To combine two systems in parallel


S1
+
>> s3 = s1 + s2 or >> s3 = parallel(s1,s2)
+
S2

To build the closed loop transfer function of a feedback system where system s1 is in the
forward path and s2 is in the negative feedback path
+
S1
>> sCL = feedback(s1,s2) -
S2
For a closed-loop system with unity-gain feedback (i.e. nothing in the feedback path) use
+
S1
>> sCL = feedback(s1,1) -

For a closed loop system with a controller Gc in front of a plant Gp in a unity gain
feedback path (the most common configuration for P, PD, PID control) use
+
Gc Gp
>> sCL = feedback(Gc*Gp,1) -

Once you have and open or closed loop system, sys, the following analysis function are
useful:

>> pole(sys) % system poles


>> zero(sys) % system zeros
>> dcgain(sys) % dc gain of system
>> pzmap(sys) % s-plane plot of poles and zeros
>> step(sys) % step response
>> impulse(sys) % impulse response
>> bode(sys) % Bode magnitude and phase plots

Matlab Control Toolbox Notes, Page 5 of 12


ROOTLOCUS
The root locus method is a great way to see where the closed-loop poles of your system
will go as you manipulate the control gains. Create a forward path transfer function with
your plant Gp(s) and compensator Gc(s), then enter into the root locus tool

>> rlocus(Gc*Gp)

The resulting plot shows the positions of the closed-loop poles on the s-plane as K, the
gain in front of your compensator, varies from zero to infinity. To find the value of K for
any particular position, click on the pole plot and a popup window tells you K. Click and
drag to see information about any part of the plot. For example, you might want to see
what the value of K is for any pole which is just crossing into the right half plane. Note
that for a P control, Gc = 1 so you just need rlocus(Gp)

LTIVIEW
The control toolbox includes LTIVIEW a handy utility for examining several aspects of a
system. To start the viewer

>> ltiview

Select File > Import and a dialog box opens listing all systems in your current Matlab
workspace. Click on one to bring it into ltiview.

Select Edit > Plot Configurations and pick the 2 by 2 matrix of plot windows. Using the
Response type entry boxes on the right, have plots 1 thru 4 show step, impluse, bode and
pole/zero representations of the system. Enlarge the plot window so you can see all
representations. Right click on the step or bode plots and select Characteristics to
measure system properties. Left clicking on data points brings up additional information
about the system.

SISO Design Tool


The control toolbox also include SISO, another handy utility that simplifies the task of
designing control systems for SISO (single-input, single-output) plants using the root
locus, compensator, and/or bode methods. After creating your plant and compensator
structure in the Matlab workspace, open the tool with

>> sisotool(Gp)

where Gp is the transfer function for your plant.

Select File > Import to input other plant models or to import your compensator. Change
controller gain by entering a number in the C(s) box, or by dragging handles in the root

Matlab Control Toolbox Notes, Page 6 of 12


locus plot. To view the resulting closed-loop step response, use Tools > Loop Responses
> Plant Output (Step). As you play with the controller, the step will automatically update.
The SISO tool has many features. Explore on your own, making use of the on-line help.
The Control System Toolbox manual (print, on the toolbox CD, or on the Matlab web
site) has some nice examples showing use of the SISO tool.

The following pages contain examples showing Matlab in use

Matlab Control Toolbox Notes, Page 7 of 12


Step Response Examples

The following demonstrates how you can use the Control Toolbox of Matlab to conduct a
parameter study showing how the natural frequency affects the step response. In this case
we examine the system described by the transfer function

ω n2
H (s) =
s 2 + 2ξω n s + ω n2
We put ω n2 in the numerator so that the steady state gain of the transfer function (which
you can find by setting s = 0 ) is one, which means all the steps will line up in amplitude.
The first .m file shown is a script to plot one step response. Edit the file to change zeta or
wn, save, and run to get different values of damping and natural frequency.

The second .m file and plot shows how you can plot the step response with four different
values of the natural frequency. The .m file demonstrates several useful Matlab tricks.
Notice how the frequency values are defined as a vector a. By putting a(k) in the loop, we
can successively grab the different frequency values and put in the transfer function. The
vector marker is used to make different line style for each successive plot. In Matlab type
“help plot” to see how the marker styles work. The initial ‘k’ means to plot every line in
black which is just what you want if you don’t have a color printer. The marker vector is
set up with semicolons because it is an array of strings (characters) where each new word
must be a new row in the matrix. The clf function clears the prior plot. Without it, the
legend command gets messed up. Everything else is relatively ordinary Matlab code.

The third .m file and plot shows how to create a plot with 4 different values of zeta. You
could do the same thing by running a loop where zeta in incremented.

% 2nd order step response


zeta=0.1;
wn=1;
num=[wn^2];
den=[1 2*zeta*wn wn^2];
sys1=tf(num,den);
step(sys1,10)

Matlab Control Toolbox Notes, Page 8 of 12


% poleloc.m
% demo script showing effect of nat frequency
% do help plot and help legend for info on markers

a = [1 2 3 4]; % create vector with freq values


zeta = 0.2; % the damping ratio

marker = ['k-x'; 'k-+'; 'k-*'; 'k-d']; % array of line styles


tstop = 10; % duration of step response

clf % clear out any prior plots


hold % for overlay plot

for k = 1:4
wn = a(k); % nat freq;
sys = tf([wn^2], [1 2*zeta*wn wn^2]); % transfer function
[y,t] = step(sys,tstop); % create the step
plot(t,y,marker(k,:))
end
xlabel('Time (sec)')
ylabel('Step response')
title('Effect of wn^2 on step response')
legend('wn=1','wn=2','wn=3','wn=4')

Effect of wn2 on step response


1.6
wn=1
wn=2
1.4
wn=3
wn=4
1.2

1
Step response

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7 8 9 10
Time (sec)

Matlab Control Toolbox Notes, Page 9 of 12


%steps.m
s=tf('s'); % to enable direct writing of
% transfer functions

z=.1;
sys1=1/(s^2+2*z*s+1);
z=.5;
sys2=1/(s^2+2*z*s+1);
z=.707;
sys3=1/(s^2+2*z*s+1);
z=1;
sys4=1/(s^2+2*z*s+1);
step(sys1,sys2,sys3,sys4,20)

Matlab Control Toolbox Notes, Page 10 of 12


Step and impulse response examples

>> sys=tf(10,[1 2 10])

Transfer function:
10
--------------
s^2 + 2 s + 10

>> pole(sys)

ans =

-1.0000 + 3.0000i
-1.0000 - 3.0000i

>> damp(sys)

Eigenvalue Damping Freq. (rad/s)

-1.00e+00 + 3.00e+00i 3.16e-01 3.16e+00


-1.00e+00 - 3.00e+00i 3.16e-01 3.16e+00

>> [y1,t1]=step(sys);
>> [y2,t2]=impulse(sys);

step response
1.5

0.5

0
0 1 2 3 4 5 6

impulse response
3

-1
0 1 2 3 4 5 6

Matlab Control Toolbox Notes, Page 11 of 12


Step and impulse response with a zero

>> sys=tf([10 10],[1 2 10])

Transfer function:
10 s + 10
--------------
s^2 + 2 s + 10

>> pole(sys)

ans =

-1.0000 + 3.0000i
-1.0000 - 3.0000i

>> zero(sys)

ans =

-1

>> [y1,t1]=step(sys);
>> [y2,t2]=impulse(sys);

step response
3

0
0 1 2 3 4 5 6

impulse response
10

-5
0 1 2 3 4 5 6

Matlab Control Toolbox Notes, Page 12 of 12

You might also like