Simulink Basics Tutorial
Simulink Basics Tutorial
example=Introduction§ion=SimulinkControl
Key MATLAB commands used in this tutorial are: tf , ssdata , pole , eig , step , pzmap , bode ,
linearSystemAnalyzer
Contents
The time response represents how the state of a dynamic system changes in time when subjected to a
particular input. Since the models we have derived consist of differential equations, some integration
must be performed in order to determine the time response of the system. For some simple systems, a
closed-form analytical solution may be available. However, for most systems, especially nonlinear
systems or those subject to complicated inputs, this integration must be carried out numerically.
Fortunately, MATLAB provides many useful resources for calculating time responses for many types of
inputs, as we shall see in the following sections.
The time response of a linear dynamic system consists of the sum of the transient response which
depends on the initial conditions and the steady-state response which depends on the system input.
These correspond to the homogenous (free or zero input) and the particular solutions of the governing
differential equations, respectively.
All the examples presented in this tutorial are modeled by linear constant coefficient differential
equations and are thus linear time-invariant (LTI). LTI systems have the extremely important property
that if the input to the system is sinusoidal, then the steady-state output will also be sinusoidal at the
same frequency, but, in general, with different magnitude and phase. These magnitude and phase
differences are a function of the frequency and comprise the frequency response of the system.
The frequency response of a system can be found from its transfer function in the following way: create
a vector of frequencies (varying between zero or "DC" to infinity) and compute the value of the plant
transfer function at those frequencies. If is the open-loop transfer function of a system and is the
frequency vector, we then plot versus . Since is a complex number, we can plot both its
magnitude and phase (the Bode Plot) or its position in the complex plane (the Nyquist Diagram). Both
methods display the same information, but in different ways.
Stability
For our purposes, we will use the Bounded Input Bounded Output (BIBO) definition of stability
which states that a system is stable if the output remains bounded for all bounded (finite) inputs.
Practically, this means that the system will not "blow up" while in operation.
The transfer function representation is especially useful when analyzing system stability. If all poles of
the transfer function (values of for which the denominator equals zero) have negative real parts, then
the system is stable. If any pole has a positive real part, then the system is unstable. If we view the poles
on the complex s-plane, then all poles must be in the left-half plane (LHP) to ensure stability. If any pair
of poles is on the imaginary axis, then the system is marginally stable and the system will tend to
oscillate. A system with purely imaginary poles is not considered BIBO stable. For such a system, there
will exist finite inputs that lead to an unbounded response. The poles of an LTI system model can easily
be found in MATLAB using the pole command, an example of which is shown below:
s = tf('s');
G = 1/(s^2+2*s+5)
pole(G)
G =
1
-------------
s^2 + 2 s + 5
ans =
-1.0000 + 2.0000i
-1.0000 - 2.0000i
Thus this system is stable since the real parts of the poles are both negative. The stability of a system
may also be found from the state-space representation. In fact, the poles of the transfer function are the
eigenvalues of the system matrix . We can use the eig command to calculate the eigenvalues using
either the LTI system model directly, eig(G), or the system matrix as shown below.
[A,B,C,D] = ssdata(G);
eig(A)
ans =
-1.0000 + 2.0000i
-1.0000 - 2.0000i
System Order
The order of a dynamic system is the order of the highest derivative of its governing differential
equation. Equivalently, it is the highest power of in the denominator of its transfer function. The
important properties of first-, second-, and higher-order systems will be reviewed in this section.
First-Order Systems
First-order systems are the simplest dynamic systems to analyze. Some common examples include
mass-damper systems and RC circuits.
(1)
(2)
where the parameters and completely define the character of the first-order system.
DC Gain
The DC gain, , is the ratio of the magnitude of the steady-state step response to the magnitude of the
step input. For stable transfer functions, the Final Value Theorem demonstrates that the DC gain is the
value of the transfer function evaluated at = 0. For first-order systems of the forms shown, the DC gain
is .
Time Constant
The time constant of a first-order system is which is equal to the time it takes for the
system's response to reach 63% of its steady-state value for a step input (from zero initial conditions) or
to decrease to 37% of the initial value for a system's free response. More generally, it represents the
time scale for which the dynamics of the system are significant.
Poles/Zeros
First-order systems have a single real pole, in this case at . Therefore, the system is stable if is
positive and unstable if is negative. Standard first-order system have no zeros.
Step Response
We can calculate the system time response to a step input of magnitude using the following MATLAB
commands:
k_dc = 5;
Tc = 10;
u = 2;
s = tf('s');
G = k_dc/(Tc*s+1)
step(u*G)
G =
5
--------
10 s + 1
Note: MATLAB also provides a powerful graphical user interface for analyzing LTI systems which can
be accessed using the syntax linearSystemAnalyzer('step',G).
If you right-click on the step response graph and select Characteristics, you can choose to have several
system metrics overlaid on the response: peak response, settling time, rise time, and steady-state.
Settling Time
The settling time, , is the time required for the system output to fall within a certain percentage (i.e.
2%) of the steady-state value for a step input. The settling times for a first-order system for the most
common tolerances are provided in the table below. Note that the tighter the tolerance, the longer the
system response takes to settle to within this band, as expected.
10% 5% 2% 1%
Rise Time
The rise time, , is the time required for the system output to rise from some lower level x% to some
higher level y% of the final steady-state value. For first-order systems, the typical range is 10% - 90%.
Bode Plots
Bode diagrams show the magnitude and phase of a system's frequency response, , plotted with
respect to frequency . We can generate the Bode plot of a system in MATLAB using the syntax
bode(G) as shown below.
bode(G)
Again the same results could be obtained using the Linear System Analyzer GUI,
linearSystemAnalyzer('bode',G).
Bode plots employ a logarithmic frequency scale so that a larger range of frequencies are visible. Also,
the magnitude is represented using the logarithmic decibel unit (dB) defined as:
(3)
As with the frequency axis, the decibel scale allows us to view a much larger range of magnitudes on a
single plot. Also, as we shall see in subsequent tutorials, when components and controllers are placed in
series, the transfer function of the overall system is the product of the individual transfer functions.
Using the dB scale, the magnitude plot of the overall system is simply the sum of the magnitude plots of
the individual transfer functions. The phase plot of the overall system is also just the sum of the
individual phase plots.
The low frequency magnitude of the first-order Bode plot is . The magnitude plot has a bend
at the frequency equal to the absolute value of the pole (ie. ), and then decreases 20 dB for every
factor of ten increase in frequency (slope = -20 dB/decade). The phase plot is asymptotic to 0 degrees at
low frequencies, and asymptotic to -90 degrees at high frequencies. Between frequency 0.1a and 10a,
the phase changes by approximately -45 degrees for every factor of ten increase in frequency (-45
degrees/decade).
We will see in the Frequency Methods for Controller Design Section how to use Bode plots to calculate
closed-loop stability and performance of feedback systems.
Second-Order Systems
Second-order systems are commonly encountered in practice, and are the simplest type of dynamic
system to exhibit oscillations. Examples include mass-spring-damper systems and RLC circuits. In fact,
many true higher-order systems may be approximated as second-order in order to facilitate analysis.
(4)
The canonical second-order transfer function has the following form, in which it has two poles and no
zeros.
(5)
DC Gain
The DC gain, , again is the ratio of the magnitude of the steady-state step response to the magnitude
of the step input, and for stable systems it is the value of the transfer function when . For the forms
given,
(6)
Damping Ratio
The damping ratio is a dimensionless quantity charaterizing the rate at which an oscillation in the
system's response decays due to effects such as viscous friction or electrical resistance. From the above
definitions,
(7)
Natural Frequency
The natural frequency is the frequency (in rad/s) that the system will oscillate at when there is no
damping, .
(8)
Poles/Zeros
(9)
Underdamped Systems
If , then the system is underdamped. In this case, both poles are complex-valued with negative
real parts; therefore, the system is stable but oscillates while approaching the steady-state value.
Specifically, the natural response oscillates with the damped natural frequency, (in
rad/sec).
k_dc = 1;
w_n = 10;
zeta = 0.2;
s = tf('s');
G1 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2);
pzmap(G1)
axis([-3 1 -15 15])
step(G1)
axis([0 3 0 2])
Settling Time
The settling time, , is the time required for the system ouput to fall within a certain percentage of the
steady-state value for a step input. For a canonical second-order, underdamped system, the settling time
can be approximated by the following equation:
(10)
The settling times for the most common tolerances are presented in the following table:
10% 5% 2% 1%
Percent Overshoot
The percent overshoot is the percent by which a system's step response exceeds its final steady-state
value. For a second-order underdamped system, the percent overshoot is directly related to the
damping ratio by the following equation. Here, is a decimal number where 1 corresponds to 100%
overshoot.
(11)
For second-order underdamped systems, the 1% settling time, , 10-90% rise time, , and percent
overshoot, , are related to the damping ratio and natural frequency as shown below.
(12)
(13)
(14)
Overdamped Systems
If , then the system is overdamped. Both poles are real and negative; therefore, the system is
stable and does not oscillate. The step response and a pole-zero map of an overdamped system are
calculated below:
zeta = 1.2;
step(G2)
axis([0 1.5 0 1.5])
Critically-Damped Systems
If , then the system is critically damped. Both poles are real and have the same magnitude,
. For a canonical second-order system, the quickest settling time is achieved when the system
is critically damped. Now change the value of the damping ratio to 1, and re-plot the step response and
pole-zero map.
zeta = 1;
pzmap(G3)
axis([-11 1 -1 1])
step(G3)
axis([0 1.5 0 1.5])
Undamped Systems
If , then the system is undamped. In this case, the poles are purely imaginary; therefore, the
system is marginally stable and the step response oscillates indefinitely.
zeta = 0;
pzmap(G4)
axis([-1 1 -15 15])
step(G4)
axis([0 5 -0.5 2.5])
Bode Plot
We show the Bode magnitude and phase plots for all damping conditions of a second-order system
below:
bode(G1,G2,G3,G4)
legend('underdamped: zeta < 1','overdamped: zeta > 1','critically-damped: zeta =
1','undamped: zeta = 0')
The magnitude of the bode plot of a second-order system drops off at -40 dB per decade in the limit,
while the relative phase changes from 0 to -180 degrees. For underdamped systems, we also see a
resonant peak near the natural frequency, = 10 rad/s. The size and sharpness of the peak depends on
the damping in the system, and is charaterized by the quality factor, or Q-Factor, defined below. The
Q-factor is an important property in signal processing.
(15)
Introduction: System Modeling
The first step in the control design process is to develop appropriate mathematical models of the system
to be controlled. These models may be derived either from physical laws or experimental data. In this
section, we introduce the state-space and transfer function representations of dynamic systems. We then
review some basic approaches to modeling mechanical and electrical systems and show how to generate
these models in MATLAB for further analysis.
Contents
Dynamic Systems
State-Space Representation
Transfer Function Representation
Mechanical Systems
Example: Mass-Spring-Damper System
Entering State-Space Models into MATLAB
Entering Transfer Function Models into MATLAB
Electrical Systems
Example: RLC Circuit
System Identification
System Conversions
Dynamic Systems
Dynamic systems are systems that change or evolve in time according to a fixed rule. For many
physical systems, this rule can be stated as a set of first-order differential equations:
(1)
In the above equation, x(t) is the state vector, a set of variables representing the configuration of the
system at time t. For instance, in a simple mechanical mass-spring-damper system, the two state
variables could be the position and velocity of the mass. u(t) is the vector of external inputs to the
system at time t, and is a (possibly nonlinear) function producing the time derivative (rate of change) of
the state vector, dx/dt, for a particular instant of time.
The state at any future time, x(t1), may be determined exactly given knowledge of the initial state, x(t0),
and the time history of the inputs, u(t), between t0 and t1 by integrating Equation (1). Though the state
variables themselves are not unique, there is a minimum number of state variables, n, required in order
to capture the "state" of a given system and to be able to predict the system's future behavior (solve the
state equations). n is referred to as the system order and determines the dimensionality of the state-
space. The system order usually corresponds to the number of independent energy storage elements in
the system.
The relationship given in Equation (1) is very general and can be used to describe a wide variety of
different systems; unfortunately, it may be very difficult to analyze. There are two common
simplifications which make the problem more tractable. First, if the function does not depend
explicitly on time, i.e. , then the system is said to be time invariant. This is often a very
reasonable assumption because the underlying physical laws themselves do not typically depend on
time. For time-invariant systems, the parameters or coefficients of the function are constant. The state
variables, , and control inputs, , however, may still be time dependent.
The second common assumption concerns the linearity of the system. In reality, nearly every physical
system is nonlinear. In other words, is typically some complicated function of the state and inputs.
These nonlinearities arise in many different ways, one of the most common in control systems being
"saturation" in which an element of the system reaches a hard physical limit to its operation.
Fortunately, over a sufficiently small operating range (think tangent line near a curve), the dynamics of
most systems are approximately linear. In this case, the system of first-order differential equations can
be represented as a matrix equation, that is, .
Until the advent of digital computers (and to a large extent thereafter), it was only practical to analyze
linear time-invariant (LTI) systems. Consequently, most of the results of control theory are based on
these assumptions. Fortunately, as we shall see, these results have proven to be remarkably effective and
many significant engineering challenges have been solved using LTI techniques. In fact, the true power
of feedback control systems are that they work (are robust) in the presence of the unavoidable modeling
uncertainty.
State-Space Representation
For continuous linear time-invariant (LTI) systems, the standard state-space representation is given
below:
(2)
(3)
where is the vector of state variables (nx1), is the time derivative of the state vector (nx1), is the
input or control vector (px1), is the output vector (qx1), is the system matrix (nxn), is the input
matrix (nxp), is the output matrix (qxn), and is the feedforward matrix (qxp).
The output equation, Equation (3), is necessary because often there are state variables which are not
directly observed or are otherwise not of interest. The output matrix, , is used to specify which state
variables (or combinations thereof) are available for use by the controller. Also, it is often the case that
the outputs do not directly depend on the inputs (only through the state variables), in which case is the
zero matrix.
The state-space representation, also referred to as the time-domain representation, can easily handle
multi-input/multi-output (MIMO) systems, systems with non-zero initial conditions, and nonlinear
systems via Equation (1). Consequently, the state-space representation is used extensively in "modern"
control theory.
Transfer Function Representation
LTI systems have the extremely important property that if the input to the system is sinusoidal, then the
output will also be sinusoidal with the same frequency as the input, but with possibly different
magnitude and phase. These magnitude and phase differences are a function of frequency and capture
what is known as the frequency response of the system.
Using the Laplace transform, it is possible to convert a system's time-domain representation into a
frequency-domain input/output representation, known as the transfer function. In so doing, it also
transforms the governing differential equation into an algebraic equation which is often easier to
analyze.
(4)
where the parameter is a complex frequency variable. It is very rare in practice that you will
have to directly evaluate a Laplace transform (though you should certainly know how to). It is much
more common to look up the transform of a time function in a table such as the one found here: Laplace
Transform Table
(5)
Frequency-domain methods are most often used for analyzing LTI single-input/single-output (SISO)
systems, e.g. those governed by a constant coefficient differential equation, as shown below:
(6)
(7)
where and are the Laplace Transforms of and , respectively. Note that when finding
transfer functions, we always assume that the each of the initial conditions, , , , etc. is zero.
The transfer function from input to output is, therefore:
(8)
It is useful to factor the numerator and denominator of the transfer function into what is termed zero-
pole-gain form:
(9)
The zeros of the transfer function, , are the roots of the numerator polynomial, i.e. the values
of such that . The poles of the transfer function, , are the roots of the denominator
polynomial, i.e. the values of such that . Both the zeros and poles may be complex valued
(have both real and imaginary parts). The system Gain is .
Note that we can also determine the transfer function directly from the state-space representation as
follows:
(10)
Mechanical Systems
Newton's laws of motion form the basis for analyzing mechanical systems. Newton’s second law,
Equation (11), states that the sum of the forces acting on a body equals the product of its mass and
acceleration. Newton's third law, for our purposes, states that if two bodies are in contact, then they
experience the same magnitude contact force, just acting in opposite directions.
(11)
When applying this equation, it is best to construct a free-body diagram (FBD) of the system showing
all of the applied forces.
The free-body diagram for this system is shown below. The spring force is proportional to the
displacement of the mass, , and the viscous damping force is proportional to the velocity of the mass,
. Both forces oppose the motion of the mass and are, therefore, shown in the negative -direction.
Note also that corresponds to the position of the mass when the spring is unstretched.
Now we proceed by summing the forces and applying Newton’s second law, Equation (11), in each
direction. In this case, there are no forces acting in the -direction; however, in the -direction we have:
(12)
This equation, known as the governing equation, completely characterizes the dynamic state of the
system. Later, we will see how to use this to calculate the response of the system to any external input,
, as well as to analyze system properties such as stability and performance.
To determine the state-space representation of the mass-spring-damper system, we must reduce the
second-order governing equation to a set of two first-order differential equations. To this end, we
choose the position and velocity as our state variables.
(13)
The position variable captures the potential energy stored in the spring, while the velocity variable
captures the kinetic energy stored by the mass. The damper only dissipates energy, it doesn't store
energy. Often when choosing state variables it is helpful to consider what variables capture the energy
stored in the system.
(14)
If, for instance, we are interested in controlling the position of the mass, then the output equation is:
(15)
Now we will demonstrate how to enter the equations derived above into an m-file for MATLAB. Let's
assign the following numerical values to each of the variables.
m mass 1.0 kg
k spring constant 1.0 N/m
b damping constant 0.2 Ns/m
F input force 1.0 N
Create a new m-file and enter the following commands.
m = 1;
k = 1;
b = 0.2;
F = 1;
A = [0 1; -k/m -b/m];
B = [0 1/m]';
C = [1 0];
D = [0];
sys = ss(A,B,C,D)
sys =
A =
x1 x2
x1 0 1
x2 -1 -0.2
B =
u1
x1 0
x2 1
C =
x1 x2
y1 1 0
D =
u1
y1 0
The Laplace transform for this system assuming zero initial conditions is
(16)
and, therefore, the transfer function from force input to displacement output is
(17)
Now we will demonstrate how to create the transfer function model derived above within MATLAB.
Enter the following commands into the m-file in which you defined the system parameters.
s = tf('s');
sys = 1/(m*s^2+b*s+k)
sys =
1
---------------
s^2 + 0.2 s + 1
Note that we have used the symbolic s variable here to define our transfer function model. We
recommend using this method most of the time; however, in some circumstances, for instance in older
versions of MATLAB or when interfacing with SIMULINK, you may need to define the transfer
function model using the numerator and denominator polynomial coefficients directly. In these cases,
use the following commands:
num = [1];
den = [m b k];
sys = tf(num,den)
sys =
1
---------------
s^2 + 0.2 s + 1
Electrical Systems
Like Newton’s laws for mechanical systems, Kirchoff’s circuit laws are fundamental analytical tools for
modeling electrical systems. Kirchoff’s current law (KCL) states that the sum of the electrical
currents entering a node in a circuit must equal the sum of electrical currents exiting the node.
Kirchoff’s voltage law (KVL) states that the sum of voltage differences around any closed loop in a
circuit is zero. When applying KVL, the source voltages are typically taken as positive and the load
voltages are taken as negative.
We will now consider a simple series combination of three passive electrical elements: a resistor, an
inductor, and a capacitor, known as an RLC Circuit.
Since this circuit is a single loop, each node only has one input and one output; therefore, application of
KCL simply shows that the current is the same throughout the circuit at any given time, . Now
applying KVL around the loop and using the sign conventions indicated in the diagram, we arrive at the
following governing equation.
(18)
We note that that the governing equation for the RLC circuit has an analogous form to the mass-spring-
damper mechanical system. In particular, they are both second-order systems where the charge (integral
of current) corresponds to displacement, the inductance corresponds to mass, the resistance corresponds
to viscous damping, and the inverse capacitance corresponds to the spring stiffness. These analogies and
others like them turn out to be quite useful conceptually in understanding the behavior of dynamical
systems.
The state-space representation is found by choosing the charge on the capacitor and current through the
circuit (inductor) as the state variables.
(19)
where,
(20)
(21)
We choose the current as ouput as follows:
(22)
The transfer function representation may be found by taking the Laplace transform as we did for the
mass-spring-damper or from the state-space equation as follows:
(23)
(24)
The RLC state-space and transfer function models can be entered into MATLAB using the same
procedure as discussed for the mass-spring-damper system above.
System Identification
In this section, we have seen how to model systems using basic physical principles; however, often this
is not possible either because the parameters of the system are uncertain, or the underlying processes are
simply not understood. In these cases, we must rely on experimental measurements and statistical
techniques to develop a system model, a process known as system identification.
System identification may be performed using either time-domain or frequency-domain data, see the
Introduction: System Identification page for further details. A couple of system identification activities
can also be found from the Hardware tab located at the top of this window.
Also refer to MATLAB’s System Identification Toolbox for more information on this subject.
System Conversions
Most operations in MATLAB can be performed on either the transfer function, the state-space model, or
the zero-pole-gain form. Furthermore, it is simple to transfer between these forms if the other
representation is required. If you need to learn how to convert from one representation to another, see
the Introduction: System Conversions page.
Introduction: Frequency Domain Methods for
Controller Design
The frequency response method of controller design may be less intuitive than other methods you have
studied previously. However, it has certain advantages, especially in real-life situations such as
modeling transfer functions from physical data. In this tutorial, we will see how we can use the open-
loop frequency response of a system to predict its closed-loop time response behavior.
Key MATLAB commands used in this tutorial are: bode , nyquist , margin , lsim , step , feedback ,
controlSystemDesigner
Contents
Recall from the Introduction: System Analysis page that the frequency response of a system consists of
evaluating how a sinusoidal input to a system is scaled and shifted by the system. The manner in which
the scaling and shifting of the sinusoidal output changes as a function of frequency provides useful
information about the system's time response. One aspect, in particular, that a system's frequency
response is used for determining is a system's "robustness." For example, how close is the system to
becoming unstable? Here we use two quantities, gain margin and phase margin to indicate the margin
the system has before it would go unstable.
where is a variable (constant) gain and is the plant under consideration. The gain margin is
defined as the change in open-loop gain required to make the closed-loop system unstable. Systems with
greater gain margins can withstand greater changes in system parameters before becoming unstable in
closed-loop.
The phase margin is defined as the change in open-loop phase shift required to make the closed-loop
system unstable. The phase margin also measures the system's tolerance to time delay. If there is a time
delay greater than in the loop (where is the frequency in rad/sec where the magnitude is 0 dB
and PM is the phase margin converted to radians), the closed-loop system will become unstable. The
time delay, , can be thought of as an extra block in the forward path of the block diagram that adds
phase lag to the system, but has no effect on the gain. That is, a time delay can be represented as a block
with magnitude of 1 and phase .
For now, we won't worry about where all this comes from and rather will concentrate on identifying the
gain and phase margins on a Bode plot.
The closed-loop system's phase margin is the additional amount of phase lag that is required for the
open-loop system's phase to reach -180 degrees at the frequency where the open-loop system's
magnitude is 0 dB (the gain crossover frequency, ). Likewise, the gain margin is the additional
amount of gain (usually in dB) required for the open-loop system's magnitude to reach 0 dB at the
frequency where the open-loop system's phase equals -180 degrees (the phase crossover frequency, ).
One nice thing about the phase margin is that you don't need to replot the Bode diagram in order to find
the new phase margin when changing the loop gain. If you recall, adding gain only shifts the magnitude
plot up (or down). This is equivalent to changing the y-axis on the magnitude plot. Finding the phase
margin is simply a matter of finding the new gain crossover frequency and reading off the phase margin.
For example, suppose you employed the following commands to generate the Bode plot shown below:
s = tf('s');
G = 50/(s^3 + 9*s^2 + 30*s +40);
bode(G)
grid on
title('Bode Plot with No Gain')
You should see that the phase margin is about 100 degrees. Now suppose you added a gain of 100, by
entering the command bode(100*G). You should get the following plot:
bode(100*G)
grid on
title('Bode Plot with Gain = 100')
As you can see, the phase plot is exactly the same as before, and the magnitude plot is shifted up by 40
dB (gain of 100). The phase margin is now about -60 degrees. This same result could be achieved if the
y-axis of the magnitude plot was shifted down 40 dB. Try this, look at the first Bode plot, find where
the curve crosses the -40 dB line, and read off the phase margin. It should be about -60 degrees, the
same as the second Bode plot.
We can have MATLAB calculate and display the gain and phase margins using the margin(G)
command. This command returns the gain and phase margins, the gain and phase crossover frequencies,
and a graphical representation of these quantities on the Bode plot. See below, for an example:
margin(100*G)
Bandwidth Frequency
The bandwidth frequency is defined as the frequency at which the closed-loop magnitude drops 3 dB
below its magnitude at DC (magnitude as the frequency approaches 0). However, when we design via
frequency response, we are interested in predicting the closed-loop behavior from the open-loop
response. Therefore, we will use a second-order system approximation and say that the bandwidth
frequency equals the frequency at which the open-loop magnitude response is between -6 and -7.5 dB,
assuming the open-loop phase response is between -135 deg and -225 deg. For a complete derivation of
this approximation, consult your textbook.
In order to illustrate the importance of the bandwidth frequency, we will show how the output changes
with different input frequencies. We will find that sinusoidal inputs with frequency less than (the
bandwidth frequency) are tracked "reasonably well" by the system. Sinusoidal inputs with frequency
greater than are attenuated (in magnitude) by a factor of 0.707 or greater (and are also shifted in
phase).
Let's say we have the following closed-loop transfer function representing a system:
(1)
First, consider a sinusoidal input with a frequency lower than . We must also keep in mind that we
want to view the steady state response. Therefore, we will modify the axes in order to see the steady
state response clearly (ignoring the transient response).
Nyquist Diagram
The Nyquist diagram allows us to predict the stability and performance of a closed-loop system by
observing its open-loop behavior. The Nyquist criterion can be used for design purposes regardless of
open-loop stability (remember that the Bode design methods assume that the system is stable in open-
loop). Therefore, we use this criterion to determine closed-loop stability when the Bode plots display
confusing information.
Note: The MATLAB nyquist command does not provide an adequate representation for systems
that have open-loop poles on the imaginary axis. Therefore, we suggest that you copy the nyquist1.m
file as a new m-file. This m-file creates more accurate Nyquist plots, since it correctly deals with
poles and zeros on the imaginary axis.
The Nyquist diagram is basically a plot of where is the open-loop transfer function and is a
vector of frequencies which encloses the entire right-half plane. In drawing the Nyquist diagram, both
positive (from zero to infinity) and negative frequencies (from negative infinity to zero) are taken into
account. We will represent positive frequencies in red and negative frequencies in green. The frequency
vector used in plotting the Nyquist diagram usually looks like this (if you can imagine the plot
stretching out to infinity):
However, if we have open-loop poles or zeros on the imaginary axis, will not be defined at those
points and we must loop around them when we are plotting the contour. An example contour would
appear as follows:
Please note that the contour loops around the pole on the imaginary axis. As we mentioned before, the
MATLAB nyquist command does not take poles or zeros on the imaginary axis into account and,
therefore, produces an incorrect plot. To correct this, please download and use nyquist1.m. If we have a
pole on the imaginary axis, we have to use nyquist1. If there are no poles or zeros on the imaginary
axis, or if we have pole-zero cancellation, we can use either the nyquist command or nyquist1.m.
The Cauchy criterion (from complex analysis) states that when taking a closed contour in the complex
plane, and mapping it through a complex function , the number of times that the plot of
encircles the origin is equal to the number of zeros of enclosed by the frequency contour minus the
number of poles of enclosed by the frequency contour. Encirclements of the origin are counted as
positive if they are in the same direction as the original closed contour or negative if they are in the
opposite direction.
When studying feedback control, we are not as interested in and, rather, are more concerned with
the closed-loop transfer function:
(2)
If encircles the origin, then will enclose the point -1. Since we are interested in the closed-
loop stability, we want to know if there are any closed-loop poles (zeros of ) in the right-half
plane. More details on how to determine this will come later.
Therefore, the behavior of the Nyquist diagram around the -1 point in the real axis is very important;
however, the axis on the standard nyquist diagram might make it hard to see what's happening around
this point. To correct this, you can add the lnyquist.m function to your files. The lnyquist.m command
plots the Nyquist diagram using a logarithmic scale and preserves the characteristics of the -1 point.
To view a simple Nyquist plot using MATLAB, we will define the following transfer function and view
the Nyquist plot:
(3)
s = tf('s');
G = 0.5/(s - 0.5);
nyquist(G)
axis([-1 0 -1 1])
Now we will look at the Nyquist diagram for the following transfer function:
(4)
Note that this function has a pole at the origin. We will see the difference between using the nyquist,
nyquist1, and lnyquist commands with this particular function.
G = (s + 2)/(s^2);
nyquist(G)
nyquist1(G)
lnyquist(G)
Note that the nyquist plot is not correct. In particular, it does not capture the behavior of the Nyquist
plot for a radius approaching infinity ( approaching 0). The nyquist1 plot has the correct shape, which
allows us to evaluate encirclements of the -1 point and apply the Nyquist criterion, but it's difficult to
see what happens close to the -1 point. The lnyquist function employs a modified scale that maintains
the correct behavior of the plot near the -1 point and keeps the correct basic shape.
In order to predict closed-loop performance from open-loop frequency response, we need to make a few
assumptions:
The system must be stable in open-loop if we are going to use Bode plots for our design.
For canonical second-order systems, the closed-loop damping ratio is approximately equal to the phase
margin divided by 100 if the phase margin is between 0 and 60 degrees. We can use this concept with
caution if the phase margin is greater than 60 degrees.
For canonical second-order systems, a relationship between damping ratio, bandwidth frequency, and
settling time is given by an equation described on the Extras: Bandwidth page.
A very rough estimate that you can use, is that the bandwidth is approximately equal to the natural
frequency.
Let's use these concepts to design a controller for the following system:
Where is the controller and is:
(5)
There are two ways of solving this problem: one is graphical and the other is numerical. Within
MATLAB, the graphical approach is best, so that is the approach we will use. First, let's look at the
Bode plot. Create an m-file with the following code:
P = 10/(1.25*s + 1);
bode(P)
There are several characteristics of the system that can be read directly from this Bode plot. First of all,
we can see that the bandwidth frequency is around 10 rad/sec. Since the bandwidth frequency is roughly
the same as the natural frequency (for a first-order system of this type), the rise time is 1.8/BW = 1.8/10
= 0.18 seconds. This is a rough estimate, so we will say the rise time is about 0.2 seconds.
The phase margin for this system is approximately 95 degrees. The relation, damping ratio = PM/100,
only holds for PM < 60. Since the system is first-order, there should be no overshoot.
The last major point of interest is steady-state error. The steady-state error can be read directly off the
Bode plot as well. The error constant ( , , or ) is found from the intersection of the low frequency
asymptote with the = 1 rad/sec line. Just extend the low frequency line to the = 1 line. The magnitude
at this point is the error constant. Since the Bode plot of this system is a horizontal line at low
frequencies (slope = 0), we know this system in unity feedback is type zero. Therefore, the intersection
is easy to find. The gain is 20 dB (magnitude 10). What this means is that the constant for the error
function is 10. The steady-state error is 1/(1+ ) = 1/(1+10) = 0.091.
If our system was type one instead of type zero, the constant for the steady-state error would be found in
a manner similar to the following.
Let's check our predictions by looking at a step response plot. This can be done by adding the following
two lines of code into the MATLAB command window.
sys_cl = feedback(P,1);
step(sys_cl)
title('Closed-Loop Step Response, No Controller')
As you can see, our predictions were very good. The system has a rise time of about 0.2 seconds, has no
overshoot, and has a steady-state error of about 9%. Now we need to choose a controller that will allow
us to meet the design criteria. We choose a PI controller because it will yield zero steady-state error for
a step input. Also, the PI controller has a zero, which we can place. This gives us additional design
flexibility to help us meet our criteria. Recall that a PI controller is has the following form:
(6)
The first thing we need to find is the damping ratio corresponding to a percent overshoot of 40%.
Plugging in this value into the equation relating overshoot and damping ratio (or consulting a plot of
this relation), we find that the damping ratio corresponding to this overshoot is approximately 0.28.
Therefore, our phase margin should be at least 30 degrees. We must have a bandwidth frequency greater
than or equal to 12 if we want our settling time to be less than 1.75 seconds which meets the design
specifications.
Now that we know our desired phase margin and bandwidth frequency, we can start our design.
Remember that we are looking at the open-loop Bode plots. Therefore, our bandwidth frequency will be
the frequency corresponding to a gain of approximately -7 dB.
Let's see how the integrator portion of the PI controller affects our response. Change your m-file to the
following (this adds an integral term but no proportional term):
P = 10/(1.25*s + 1);
C = 1/s;
bode(C*P, logspace(0,2))
Our phase margin and bandwidth frequency are too small. We will add gain and phase with a zero. Let's
place the zero at -1 for now and see what happens. Change your m-file to look like the following:
P = 10/(1.25*s + 1);
C = (s + 1)/s;
bode(C*P, logspace(0,2))
It turns out that the zero at -1 with a unit gain gives us a satisfactory answer. Our phase margin is
greater than 60 degrees (even less overshoot than expected) and our bandwidth frequency is
approximately 11 rad/s, which will give us a satisfactory response. Although satisfactory, the response
is not quite as good as we would like. Therefore, let's try to get a higher bandwidth frequency without
changing the phase margin too much. Let's try to increase the gain to 5 and see what happens. This will
make the gain shift and the phase will remain the same.
P = 10/(1.25*s + 1);
C = 5*((s + 1)/s);
bode(C*P, logspace(0,2))
That looks really good. Let's look at our step response and verify our results. Add the following two
lines to your m-file:
sys_cl = feedback(C*P,1);
step(sys_cl)
As you can see, our response is better than we had hoped for. However, we are not always quite as
lucky and usually have to play around with the gain and the position of the poles and/or zeros in order to
achieve our design requirements.
Remember from the Cauchy criterion that the number (N) of times that the plot of encircles
-1+j.0 is equal to the number (Z) of zeros of enclosed by the frequency contour minus the
number (P) of poles of enclosed by the frequency contour (N = Z - P). Keeping careful
track of the open- and closed-loop transfer functions, as well as numerators and denominators, you
should convince yourself that:
The Nyquist Stability Criterion that relates these three quantities is then:
(7)
Note: This is only one convention for the Nyquist criterion. Another convention states that a
positive N counts the counterclockwise or anti-clockwise encirclements of -1. The P and Z variables
remain the same. In this case the equation becomes Z = P - N. Throughout these tutorials, we
will use a positive sign for clockwise encirclements.
It is very important (and somewhat tricky) to learn how to count the number of times that the Nyquist
diagram encircles -1. Therefore, we will go into some detail to help you visualize this. One approach is
to imagine you are standing on top of the -1 point and are following the diagram from beginning to end.
Now ask yourself: How many times did I turn my head a full 360 degrees? Again, if the motion was
clockwise, N is positive, and if the motion is counterclockwise, N is negative.
Knowing the number of right-half plane (unstable) poles of the open loop (P), and the number of
encirclements of -1 made by the Nyquist diagram (N), we can determine the closed-loop stability of the
system. If Z = P + N is a positive, nonzero number, the closed-loop system is unstable.
We can also use the Nyquist diagram to find the range of gains for a closed-loop unity feedback system
to be stable. The system we will test looks like this:
where:
(8)
This system has a gain which can be varied in order to modify the response of the closed-loop system.
However, we will see that we can only vary this gain within certain limits, since we have to make sure
that our closed-loop system will be stable. This is what we will be looking for: the range of gains that
will make this system stable in the closed-loop.
The first thing we need to do is find the number of positive real poles in our open-loop transfer function:
roots([1 -8 15])
ans =
5
3
The poles of the open-loop transfer function are both positive. Therefore, we need two counterclockwise
(N = -2) encirclements of the Nyquist diagram in order to have a stable closed-loop system (Z = P +
N). If the number of encirclements is less than two or the encirclements are not counterclockwise, our
system will be unstable.
nyquist(20*G)
The diagram expanded. Therefore, we know that the closed-loop system will be stable no matter how
much we increase the gain. However, if we decrease the gain, the diagram will contract and the system
might become unstable. Let's see what happens for a gain of 0.5:
nyquist(0.5*G)
The system is now unstable. By trial and error we find that this system will become unstable for gains
less than 0.80. We can verify our answers by zooming in on the Nyquist plots as well as by looking at
the closed-loop step responses for gains of 0.79, 0.80, and 0.81.
Gain Margin
We already defined the gain margin as the change in open-loop gain expressed in decibels (dB),
required at -180 degrees of phase shift to make the open-loop magnitude equal 0 dB. Now we are going
to find out where this comes from. First of all, let's say that we have a system that is stable if there are
no Nyquist encirclements of -1, such as:
(9)
Looking at the roots, we find that we have no open loop poles in the right-half plane. Therefore, there
are no closed-loop poles in the right-half plane if there are no Nyquist encirclements of -1. Now, how
much can we vary the gain before this system becomes unstable in closed-loop? Let's look at the
following figure:
The open-loop system represented by this plot will become unstable in closed loop if the gain is
increased past a certain boundary. The negative real axis area between -1/a (defined as the point where
the open-loop has -180 degree of phase, that is, where the diagram crosses the real axis) and -1
represents the amount of increase in gain that can be tolerated before closed-loop instability.
If we think about it, we realize that if the added gain is equal to a, the diagram will touch the -1 point:
(10)
or
(11)
Therefore, we say that the gain margin is a units. However, we mentioned before that the gain margin is
usually measured in decibels. Hence, the gain margin is:
(12)
We will now find the gain margin of the stable, open-loop transfer function we viewed before. Recall
that the function is:
(13)
(14)
which means (this is the rightmost point in the Nyquist diagram) or . We can then find
the value of at this point using polyval:
w = sqrt(30);
polyval(50,j*w)/polyval([1 9 30 40],j*w)
ans =
-0.2174
The answer is: -0.2174 + 0i. The imaginary part is zero, so we know that our answer is correct. We
can also verify this by looking at the Nyquist plot again. The real part also makes sense. Now we can
proceed to find the gain margin.
We found that the -180 degrees phase shift occurs at -0.2174 + 0i. This point was previously defined
as -1/a. Therefore, we now have a, which is the gain margin. However, we need to express the gain
margin in decibels:
(15)
(16)
(17)
We now have our gain margin. Let's see how accurate it is by using a gain of a = 4.6 and zooming in
on the Nyquist plot:
a = 4.6;
nyquist(a*sys)
The plot appears to go right through the -1 point. We will now verify the accuracy of our results by
viewing the zoomed Nyquist diagrams and closed-loop step responses for gains of 4.5, 4.6, and 4.7.
Phase Margin
We have already discussed the importance of the phase margin. Therefore, we will only talk about
where this concept comes from. We have defined the phase margin as the change in open-loop phase
shift required at unity gain to make a closed-loop system unstable. Let's look at the following graphical
definition of this concept to get a better idea of what we are talking about.
Let's analyze the previous plot and think about what is happening. From our previous example we know
that this particular system will be unstable in closed-loop if the Nyquist diagram encircles the -1 point.
However, we must also realize that if the diagram is shifted by theta degrees, it will then touch the -1
point at the negative real axis, making the system marginally stable in closed-loop. Therefore, the angle
required to make this system marginally stable in closed-loop is called the phase margin (measured in
degrees). In order to find the point we measure this angle from, we draw a circle with radius of 1, find
the point in the Nyquist diagram with a magnitude of 1 (gain of 0 dB), and measure the phase shift
needed for this point to be at an angle of -180 degrees.
Introduction: State-Space Methods for
Controller Design
In this section, we will show how to design controllers and observers using state-space (or time-domain)
methods.
Key MATLAB commands used in this tutorial are: eig , ss , lsim , place , acker
Contents
Modeling
Stability
Controllability and Observability
Control Design Using Pole Placement
Introducing the Reference Input
Observer Design
Modeling
There are several different ways to describe a system of linear differential equations. The state-space
representation was introduced in the Introduction: System Modeling section. For a SISO LTI system,
the state-space form is given below:
(1)
(2)
where is an n by 1 vector representing the system's state variables, is a scalar representing the input,
and is a scalar representing the output. The matrices (n by n), (n by 1), and (1 by n) determine the
relationships between the state variables and the input and output. Note that there are n first-order
differential equations. A state-space representation can also be used for systems with multiple inputs
and multiple outputs (MIMO), but we will primarily focus on single-input, single-output (SISO)
systems in these tutorials.
To introduce the state-space control design method, we will use the magnetically suspended ball as an
example. The current through the coils induces a magnetic force which can balance the force of gravity
and cause the ball (which is made of a magnetic material) to be suspended in mid-air. The modeling of
this system has been established in many control text books (including Automatic Control Systems by B.
C. Kuo, the seventh edition).
The equations for the system are given by:
(3)
(4)
where is the vertical position of the ball, is the current through the electromagnet, is the applied
voltage, is the mass of the ball, is the acceleration due to gravity, is the inductance, is the
resistance, and is a coefficient that determines the magnetic force exerted on the ball. For simplicity,
we will choose values = 0.05 kg, = 0.0001, = 0.01 H, = 1 Ohm, = 9.81 m/s^2. The system is at
equilibrium (the ball is suspended in mid-air) whenever = (at which point = 0). We
linearize the equations about the point = 0.01 m (where the nominal current is about 7 Amps) and
obtain the linear state-space equations:
(5)
(6)
where:
(7)
is the set of state variables for the system (a 3x1 vector), is the deviation of the input voltage from its
equilibrium value ( ), and (the output) is the deviation of the height of the ball from its equilibrium
position ( ). Enter the system matrices into an m-file.
A = [ 0 1 0
980 0 -2.8
0 0 -100 ];
B = [ 0
0
100 ];
C = [ 1 0 0 ];
Stability
One of the first things we want to do is analyze whether the open-loop system (without any control) is
stable. As discussed in the Introduction: System Analysis section, the eigenvalues of the system matrix,
, (equal to the poles of the transfer function) determine stability. The eigenvalues of the matrix are
the values of that are solutions of .
poles = eig(A)
poles =
31.3050
-31.3050
-100.0000
From inspection, it can be seen that one of the poles is in the right-half plane (i.e. has positive real part),
which means that the open-loop system is unstable.
To observe what happens to this unstable system when there is a non-zero initial condition, add the
following lines to your m-file and run it again:
t = 0:0.01:2;
u = zeros(size(t));
x0 = [0.01 0 0];
sys = ss(A,B,C,0);
[y,t,x] = lsim(sys,u,t,x0);
plot(t,y)
title('Open-Loop Response to Non-Zero Initial Condition')
xlabel('Time (sec)')
ylabel('Ball Position (m)')
It looks like the distance between the ball and the electromagnet will go to infinity, but probably the ball
hits the table or the floor first (and also probably goes out of the range where our linearization is valid).
A system is controllable if there always exists a control input, , that transfers any state of the system
to any other state in finite time. It can be shown that an LTI system is controllable if and only if its
controllabilty matrix, , has full rank (i.e. if rank( ) = n where n is the number of states variables). The
rank of the controllability matrix of an LTI model can be determined in MATLAB using the commands
rank(ctrb(A,B)) or rank(ctrb(sys)).
(8)
All of the state variables of a system may not be directly measurable, for instance, if the component is in
an inaccessible location. In these cases it is necessary to estimate the values of the unknown internal
state variables using only the available system outputs. A system is observable if the initial state, ,
can be determined based on knowledge of the system input, , and the system output, , over some
finite time interval . For LTI systems, the system is observable if and only if the observability
matrix, , has full rank (i.e. if rank( ) = n where n is the number of state variables). The observability
of an LTI model can be determined in MATLAB using the command rank(obsv(A,C)) or
rank(obsv(sys)).
(9)
Controllability and observability are dual concepts. A system ( , ) is controllable if and only if a
system ( , ) is observable. This fact will be useful when designing an observer, as we shall see
below.
Let's build a controller for this system using a pole placement approach. The schematic of a full-state
feedback system is shown below. By full-state, we mean that all state variables are known to the
controller at all times. For this system, we would need a sensor measuring the ball's position, another
measuring the ball's velocity, and a third measuring the current in the electromagnet.
For simplicity, let's assume the reference is zero, = 0. The input is then
(10)
The state-space equations for the closed-loop feedback system are, therefore,
(11)
(12)
The stability and time-domain performance of the closed-loop feedback system are determined
primarily by the location of the eigenvalues of the matrix ( ), which are equal to the closed-loop
poles. Since the matrices and are both 3x3, there will be 3 poles for the system. By choosing an
appropriate state-feedback gain matrix , we can place these closed-loop poles anywhere we'd like
(because the system is controllable). We can use the MATLAB function place to find the state-
feedback gain, , which will provide the desired closed-loop poles.
Before attempting this method, we have to decide where we want to place the closed-loop poles.
Suppose the criteria for the controller were settling time < 0.5 sec and overshoot < 5%, then we might
try to place the two dominant poles at -10 +/- 10i (at = 0.7 or 45 degrees with = 10 > 4.6*2). The third
pole we might place at -50 to start (so that it is sufficiently fast that it won't have much effect on the
response), and we can change it later depending on what closed-loop behavior results. Remove the lsim
command from your m-file and everything after it, then add the following lines to your m-file:
p1 = -10 + 10i;
p2 = -10 - 10i;
p3 = -50;
K = place(A,B,[p1 p2 p3]);
sys_cl = ss(A-B*K,B,C,0);
lsim(sys_cl,u,t,x0);
xlabel('Time (sec)')
ylabel('Ball Position (m)')
From inspection, we can see the overshoot is too large (there are also zeros in the transfer function
which can increase the overshoot; you do not explicitly see the zeros in the state-space formulation).
Try placing the poles further to the left to see if the transient response improves (this should also make
the response faster).
p1 = -20 + 20i;
p2 = -20 - 20i;
p3 = -100;
K = place(A,B,[p1 p2 p3]);
sys_cl = ss(A-B*K,B,C,0);
lsim(sys_cl,u,t,x0);
xlabel('Time (sec)')
ylabel('Ball Position (m)')
This time the overshoot is smaller. Consult your textbook for further suggestions on choosing the
desired closed-loop poles.
Compare the control effort required ( ) in both cases. In general, the farther you move the poles to the
left, the more control effort is required.
Note: If you want to place two or more poles at the same position, place will not work. You can use a
function called acker which achieves the same goal (but can be less numerically well-conditioned):
K = acker(A,B,[p1 p2 p3])
Now, we will take the control system as defined above and apply a step input (we choose a small value
for the step, so we remain in the region where our linearization is valid). Replace t, u , and lsim in your
m-file with the following:
t = 0:0.01:2;
u = 0.001*ones(size(t));
sys_cl = ss(A-B*K,B,C,0);
lsim(sys_cl,u,t);
xlabel('Time (sec)')
ylabel('Ball Position (m)')
axis([0 2 -4E-6 0])
The system does not track the step well at all; not only is the magnitude not one, but it is negative
instead of positive!
Recall the schematic above, we don't compare the output to the reference; instead we measure all the
states, multiply by the gain vector , and then subtract this result from the reference. There is no reason
to expect that will be equal to the desired output. To eliminate this problem, we can scale the
reference input to make it equal to in steady-state. The scale factor, , is shown in the following
schematic:
We can calcuate within MATLAB employing the function rscale (place the following line of code
after K = ...).
Nbar = rscale(sys,K)
Nbar =
-285.7143
Note that this function is not standard in MATLAB. You will need to download it here, rscale.m, and
save it to your current workspace. Now, if we want to find the response of the system under state
feedback with this scaling of the reference, we simply note the fact that the input is multiplied by this
new factor, :
lsim(sys_cl,Nbar*u,t)
title('Linear Simulation Results (with Nbar)')
xlabel('Time (sec)')
ylabel('Ball Position (m)')
axis([0 2 0 1.2*10^-3])
and now a step can be tracked reasonably well. Note, our calculation of the scaling factor requires good
knowledge of the system. If our model is in error, then we will scale the input an incorrect amount. An
alternative, similar to what was introduced with PID control, is to add a state variable for the integral of
the output error. This has the effect of adding an integral term to our controller which is known to
reduce steady-state error.
Observer Design
When we can't measure all state variables (often the case in practice), we can build an observer to
estimate them, while measuring only the output . For the magnetic ball example, we will add
three new, estimated state variables ( ) to the system. The schematic is as follows:
The observer is basically a copy of the plant; it has the same input and almost the same differential
equation. An extra term compares the actual measured output to the estimated output ; this will
help to correct the estimated state and cause it to approach the values of the actual state (if the
measurement has minimal error).
(13)
(14)
(15)
First, we need to choose the observer gain . Since we want the dynamics of the observer to be much
faster than the system itself, we need to place the poles at least five times farther to the left than the
dominant poles of the system. If we want to use place, we need to put the three observer poles at
different locations.
op1 = -100;
op2 = -101;
op3 = -102;
Because of the duality between controllability and observability, we can use the same technique used to
find the control matrix by replacing the matrix by the matrix and taking the transposes of each
matrix:
The equations in the block diagram above are given for the estimate . It is conventional to write the
combined equations for the system plus observer using the original state equations plus the estimation
error: . We use the estimated state for feedback, , since not all state variables are
necessarily measured. After a little bit of algebra (consult your textbook for more details), we arrive at
the combined state and error equations for full-state feedback with an observer.
At = [ A-B*K B*K
zeros(size(A)) A-L*C ];
Bt = [ B*Nbar
zeros(size(B)) ];
Ct = [ C zeros(size(C)) ];
To see how the response to a non-zero initial condition with no reference input appears, add the
following lines into your m-file. Here we will assume that the observer begins with an initial estimate
equal to zero, such that the initial estimation error is equal to the initial state vector, .
sys = ss(At,Bt,Ct,0);
lsim(sys,zeros(size(t)),t,[x0 x0]);
t = 0:1E-6:0.1;
x0 = [0.01 0.5 -5];
[y,t,x] = lsim(sys,zeros(size(t)),t,[x0 x0]);
n = 3;
e = x(:,n+1:end);
x = x(:,1:n);
x_est = x - e;
plot(t,h,'-r',t,h_est,':r',t,h_dot,'-b',t,h_dot_est,':b',t,i,'-g',t,i_est,':g')
legend('h','h_{est}','hdot','hdot_{est}','i','i_{est}')
xlabel('Time (sec)')
From the above, we can see that the observer estimates converge to the actual state variables quickly
and track the state variables well in steady-state.
Key MATLAB commands used in this tutorial are: c2d , pzmap , zgrid , step , rlocus
Contents
Introduction
Zero-Hold Equivalence
Conversion Using c2d
Example: Mass-Spring-Damper
Stability and Transient Response
Discrete Root Locus
Introduction
The figure below shows the typical continuous-time feedback system that we have been considering so
far in this tutorial. Almost all continuous-time controllers can be implemented employing analog
electronics.
The continuous controller, enclosed in the shaded rectangle, can be replaced by a digital controller,
shown below, that performs the same control task as the continuous controller. The basic difference
between these controllers is that the digital system operates on discrete signals (samples of the sensed
signals) rather than on continuous signals. Such a change may be necessary if we wish to implement our
control algorithm in software on a digital computer, which is frequently the case.
The various signals of the above digital system schematic can be represented by the following plots.
The purpose of this Digital Control Tutorial is to demonstrate how to use MATLAB to work with
discrete functions, either in transfer function or state-space form, to design digital control systems.
Zero-Hold Equivalence
In the above schematic of the digital control system, we see that the system contains both discrete and
continuous portions. Typically, the system being controlled is in the physical world and generates and
responds to continuous-time signals, while the control algorithm may be implemented on a digital
computer. When designing a digital control system, we first need to find the discrete equivalent of the
continuous portion of the system.
For this technique, we will consider the following portion of the digital control system and rearrange as
follows.
The clock connected to the D/A and A/D converters supplies a pulse every seconds and each D/A
and A/D sends a signal only when the pulse arrives. The purpose of having this pulse is to require that
acts only on periodic input samples , and produces periodic outputs only at discrete
intervals of time; thus, can be realized as a discrete function.
The philosophy of the design is the following. We want to find a discrete function so that for a
piecewise constant input to the continuous system , the sampled output of the continuous system
equals the discrete output. Suppose the signal represents a sample of the input signal. There are
techniques for taking this sample and for holding it to produce a continuous signal . The sketch
below shows one example where the continuous signal is held constant at each sample over the
interval to . This operation of holding constant over the sample period is called a zero-
order hold.
The held signal then is passed to and the A/D produces the output that will be the same
piecewise signal as if the discrete signal had been passed through to produce the discrete
output .
Now we will redraw the schematic, replacing the continuous portion of the system with .
Now we can design a digital control system dealing with only discrete functions.
Note: There are certain cases where the discrete response does not match the continuous response
generated by a hold circuit implemented in a digital control system. For further information, see the
page Lagging Effect Associated with a Hold.
There is a MATLAB function c2d that converts a given continuous system (either in transfer function or
state-space form) to a discrete system using the zero-order hold operation explained above. The basic
syntax for this in MATLAB is sys_d = c2d(sys,Ts,'zoh')
The sampling time (Ts in sec/sample) should be smaller than 1/(30BW), where BW is the system's
closed-loop bandwidth frequency.
Example: Mass-Spring-Damper
Transfer Function
Assuming the closed-loop bandwidth frequency is greater than 1 rad/sec, we will choose the sampling
time (Ts) equal to 1/100 sec. Now, create a new m-file and enter the following commands.
m = 1;
b = 10;
k = 20;
s = tf('s');
sys = 1/(m*s^2+b*s+k);
Ts = 1/100;
sys_d = c2d(sys,Ts,'zoh')
sys_d =
4.837e-05 z + 4.678e-05
-----------------------
z^2 - 1.903 z + 0.9048
State-Space
(2)
(3)
All constants are the same as before. The following m-file converts the above continuous-time state-
space model to a discrete-time state-space model.
A = [0 1;
-k/m -b/m];
B = [ 0;
1/m];
C = [1 0];
D = [0];
Ts = 1/100;
sys = ss(A,B,C,D);
sys_d = c2d(sys,Ts,'zoh')
sys_d =
A =
x1 x2
x1 0.999 0.009513
x2 -0.1903 0.9039
B =
u1
x1 4.837e-05
x2 0.009513
C =
x1 x2
y1 1 0
D =
u1
y1 0
(4)
(5)
For continuous systems, we know that certain behaviors result from different pole locations in the s-
plane. For instance, a system is unstable when any pole is located to the right of the imaginary axis. For
discrete systems, we can analyze the system behaviors from different pole locations in the z-plane. The
characteristics in the z-plane can be related to those in the s-plane by the following expression.
(6)
The figure below shows the mapping of lines of constant damping ratio ( ) and natural frequency ( )
from the s-plane to the z-plane using the expression shown above.
You may have noticed that in the z-plane the stability boundary is no longer the imaginary axis, but
rather is the circle of radius one centered at the origin, |z| = 1, referred to as the unit circle. A discrete
system is stable when all poles are located inside the unit circle and unstable when any pole is located
outside the circle.
For analyzing the transient response from pole locations in the z-plane, the following three equations
used in continuous system designs are still applicable.
(7)
(8)
(9)
where,
= damping ratio
= natural frequency (rad/sec)
= 1% settling time
= 10-90% rise time
= maximum overshoot
Important: The natural frequency ( ) in the z-plane has units of rad/sample, but when you use the
equations shown above, must be represented in units of rad/sec.
(10)
Create a new m-file and enter the following commands. Running this m-file in the command window
gives you the following plot with the lines of constant damping ratio and natural frequency.
numDz = 1;
denDz = [1 -0.3 0.5];
sys = tf(numDz,denDz,-1); % the -1 indicates that the sample time is undetermined
pzmap(sys)
axis([-1 1 -1 1])
zgrid
From this plot, we see the poles are located approximately at a natural frequency of (rad/sample)
and a damping ratio of 0.25. Assuming that we have a sampling time of 1/20 sec (which leads to =
28.2 rad/sec) and using the three equations shown above, we can determine that this system should have
a rise time of 0.06 sec, a settling time of 0.65 sec, and a maximum percent overshoot of 45% (0.45 times
more than the steady-state value). Let's obtain the step response and see if these are correct. Add the
following commands to the above m-file and rerun it in the command window. You should obtain the
following step response.
sys = tf(numDz,denDz,1/20);
step(sys,2.5);
As we can see from the plot, the rise time, settling time and overshoot came out to be what we expected.
This demonstrates how we can use the locations of poles and the above three equations to analyze the
transient response of a discrete system.
The root-locus is the locus of points where roots of the characteristic equation can be found as a single
parameter is varied from zero to infinity. The characteristic equation of our unity-feedback system with
simple proportional gain, , is:
(11)
where is the digital controller and is the plant transfer function in z-domain (obtained by
implementing a zero-order hold).
The mechanics of drawing the root-loci are exactly the same in the z-plane as in the s-plane. Recall
from the continuous Root-Locus Tutorial, we used the MATLAB function sgrid to find the root-locus
region that gives an acceptable gain ( ). For the discrete root-locus analysis, we will use the function
zgrid, which has the same function as sgrid. The syntax zgrid(zeta, Wn) draws lines of constant
damping ratio (zeta) and natural frequency (Wn).
and the requirements are a damping ratio greater than 0.6 and a natural frequency greater than 0.4
rad/sample (these can be found from the design requirements, the sampling period (sec/sample), and the
three equations shown in the previous section). The following commands draw the root-locus with the
lines of constant damping ratio and natural frequency. Create a new m-file and enter the following
commands. Running this m-file should generate the following root-locus plot.
numDz = [1 -0.3];
denDz = [1 -1.6 0.7];
sys = tf(numDz,denDz,-1);
rlocus(sys)
axis([-1 1 -1 1])
zeta = 0.4;
Wn = 0.3;
zgrid(zeta,Wn)
From this plot, we can see that the system is stable for some values of since there are portions of the
root locus where both branches are located inside the unit circle. Also, we can observe two dotted lines
representing the constant damping ratio and natural frequency. The natural frequency is greater than 0.3
outside the constant-Wn line, and the damping ratio is greater than 0.4 inside the constant-zeta line. In
this example, portions of the generated root-locus are within in the desired region. Therefore, a gain ( )
chosen to place the two closed-loop poles on the loci within the desired region should provide us a
response that satisfies the given design requirements.
Simulink Basics Tutorial
Simulink is a graphical extension to MATLAB for modeling and simulation of systems. One of the
main advantages of Simulink is the ability to model a nonlinear system, which a transfer function is
unable to do. Another advantage of Simulink is the ability to take on initial conditions. When a transfer
function is built, the initial conditions are assumed to be zero.
Contents
Starting Simulink
Model Files
Basic Elements
Simple Example
Running Simulations
Building Systems
In Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams are
available, such as transfer functions, summing junctions, etc., as well as virtual input and output devices
such as function generators and oscilloscopes. Simulink is integrated with MATLAB and data can be
easily transfered between the programs. In these tutorials, we will apply Simulink to the examples from
the MATLAB tutorials to model the systems, build controllers, and simulate the systems. Simulink is
supported on Unix, Macintosh, and Windows environments; and is included in the student version of
MATLAB for personal computers. For more information on Simulink, please visit the MathWorks
home.
The idea behind these tutorials is that you can view them in one window while running Simulink in
another window. System model files can be downloaded from the tutorials and opened in Simulink. You
will modify and extend these system while learning to use Simulink for system modeling, control, and
simulation. Do not confuse the windows, icons, and menus in the tutorials for your actual Simulink
windows. Most images in these tutorials are not live - they simply display what you should see in your
own Simulink windows. All Simulink operations should be done in your Simulink windows.
Starting Simulink
Simulink is started from the MATLAB command prompt by entering the following command:
simulink
Alternatively, you can hit the Simulink button at the top of the MATLAB window as shown here:
When it starts, Simulink brings up a single window, entitled Simulink Start Page which can be seen
here.
Once you click on Blank Model, a new window will appear as shown below.
Model Files
In Simulink, a model is a collection of blocks which, in general, represents a system. In addition to
creating a model from scratch, previously saved model files can be loaded either from the File menu or
from the MATLAB command prompt. As an example, download the following model file by right-
clicking on the following link and saving the file in the directory you are running MATLAB from.
simple.slx
Open this file in Simulink by entering the following command in the MATLAB command window.
(Alternatively, you can load this file using the Open option in the File menu in Simulink, or by hitting
Ctrl-O in Simulink).
simple
Basic Elements
There are two major classes of items in Simulink: blocks and lines. Blocks are used to generate,
modify, combine, output, and display signals. Lines are used to transfer signals from one block to
another.
Blocks
There are several general classes of blocks within the Simulink library:
Blocks have zero to several input terminals and zero to several output terminals. Unused input terminals
are indicated by a small open triangle. Unused output terminals are indicated by a small triangular point.
The block shown below has an unused input terminal on the left and an unused output terminal on the
right.
Lines
Lines transmit signals in the direction indicated by the arrow. Lines must always transmit signals from
the output terminal of one block to the input terminal of another block. On exception to this is a line can
tap off of another line, splitting the signal to each of two destination blocks, as shown below (right-click
here and then select Save link as ... to download the model file called split.slx).
Lines can never inject a signal into another line; lines must be combined through the use of a block such
as a summing junction.
A signal can be either a scalar signal or a vector signal. For Single-Input, Single-Output (SISO)
systems, scalar signals are generally used. For Multi-Input, Multi-Output (MIMO) systems, vector
signals are often used, consisting of two or more scalar signals. The lines used to transmit scalar and
vector signals are identical. The type of signal carried by a line is determined by the blocks on either
end of the line.
Simple Example
The simple model consists of three blocks: Step, Transfer Function, and Scope. The Step is a Source
block from which a step input signal originates. This signal is transferred through the line in the
direction indicated by the arrow to the Transfer Function Continuous block. The Transfer Function
block modifies its input signal and outputs a new signal on a line to the Scope. The Scope is a Sink
block used to display a signal much like an oscilloscope.
There are many more types of blocks available in Simulink, some of which will be discussed later.
Right now, we will examine just the three we have used in the simple model.
Modifying Blocks
A block can be modified by double-clicking on it. For example, if you double-click on the Transfer
Function block in the Simple model, you will see the following dialog box.
This dialog box contains fields for the numerator and the denominator of the block's transfer function.
By entering a vector containing the coefficients of the desired numerator or denominator polynomial,
the desired transfer function can be entered. For example, to change the denominator to
(1)
[1 2 4]
and hit the close button, the model window will change to the following,
which reflects the change in the denominator of the transfer function.
The Step block can also be double-clicked, bringing up the following dialog box.
The default parameters in this dialog box generate a step function occurring at time = 1 sec, from an
initial level of zero to a level of 1 (in other words, a unit step at t = 1). Each of these parameters can be
changed. Close this dialog before continuing.
The most complicated of these three blocks in the Scope block. Double-clicking on this brings up a
blank oscilloscope screen.
When a simulation is performed, the signal which feeds into the scope will be displayed in this window.
Detailed operation of the scope will not be covered in this tutorial.
Running Simulations
To run a simulation, we will work with the following model file:
Download and open this file in Simulink following the previous instructions for this file. You should
see the following model window.
Before running a simulation of this system, first open the scope window by double-clicking on the
scope block. Then, to start the simulation, either select Run from the Simulation menu, click the Play
button at the top of the screen, or hit Ctrl-T.
The simulation should run very quickly and the scope window will appear as shown below.
Note that the step response does not begin until t = 1. This can be changed by double-clicking on the
step block. Now, we will change the parameters of the system and simulate the system again. Double-
click on the Transfer Function block in the model window and change the denominator to:
[1 20 400]
Re-run the simulation (hit Ctrl-T) and you should see the following in the scope window.
Since the new transfer function has a very fast response, it compressed into a very narrow part of the
scope window. This is not really a problem with the scope, but with the simulation itself. Simulink
simulated the system for a full ten seconds even though the system had reached steady state shortly after
one second.
To correct this, you need to change the parameters of the simulation itself. In the model window, select
Model Configuration Parameters from the Simulation menu. You will see the following dialog box.
There are many simulation parameter options; we will only be concerned with the start and stop times,
which tell Simulink over what time period to perform the simulation. Change Start time from 0.0 to 0.8
(since the step doesn't occur until t = 1.0). Change Stop time from 10.0 to 2.0, which should be only
shortly after the system settles. Close the dialog box and rerun the simulation. Now, the scope window
should provide a much better display of the step response as shown below.
Building Systems
In this section, you will learn how to build systems in Simulink using the building blocks in Simulink's
Block Libraries. You will build the following system.
If you would like to download the completed model, right-click here and then select Save link as ....
First, you will gather all of the necessary blocks from the block libraries. Then you will modify the
blocks so they correspond to the blocks in the desired model. Finally, you will connect the blocks with
lines to form the complete system. After this, you will simulate the complete system to verify that it
works.
Gathering Blocks
Create a new model (New from the File menu or hit Ctrl-N). You will get a blank model
window.
Click on the Tools tab and then select Library Browser.
Then click on the Sources listing in the Simulink library browser.
This will bring up the Sources block library. Sources are used to generate signals.
Drag the Step block from the Sources window into the left side of your model window.
Click on the Math Operations listing in the main Simulink window.
From this library, drag a Sum and Gain block into the model window and place them to the right
of the Step block in that order.
Click on the Continuous listing in the main Simulink window.
First, from this library, drag a PID Controller block into the model window and place it to the
right of the Gain block.
From the same library, drag a Transfer Function block into the model window and place it to the
right of the PID Controller block.
Double-click on the Sum block. Since you will want the second input to be subtracted, enter +-
into the list of signs field. Close the dialog box.
Double-click the Gain block. Change the gain to 2.5 and close the dialog box.
Double-click the PID Controller block and change the Proportional gain to 1 and the Integral
gain to 2. Close the dialog box.
Double-click the Transfer Function block. Leave the numerator [1], but change the denominator
to [1 2 4]. Close the dialog box. The model should appear as:
Change the name of the PID Controller block to PI Controller by double-clicking on the word
PID Controller.
Similarly, change the name of the Transfer Function block to Plant. Now, all the blocks are
entered properly. Your model should appear as:
Now that the blocks are properly laid out, you will now connect them together. Follow these steps.
Drag the mouse from the output terminal of the Step block to the positive input of the Sum input.
Another option is to click on the Step block and then Ctrl-Click on the Sum block to connect the
two togther. You should see the following.
The resulting line should have a filled arrowhead. If the arrowhead is open and red, as shown
below, it means it is not connected to anything.
You can continue the partial line you just drew by treating the open arrowhead as an output
terminal and drawing just as before. Alternatively, if you want to redraw the line, or if the line
connected to the wrong terminal, you should delete the line and redraw it. To delete a line (or
any other object), simply click on it to select it, and hit the delete key.
Draw a line connecting the Sum block output to the Gain input. Also draw a line from the Gain
to the PI Controller, a line from the PI Controller to the Plant, and a line from the Plant to the
Scope. You should now have the following.
The line remaining to be drawn is the feedback signal connecting the output of the Plant to the
negative input of the Sum block. This line is different in two ways. First, since this line loops
around and does not simply follow the shortest (right-angled) route so it needs to be drawn in
several stages. Second, there is no output terminal to start from, so the line has to tap off of an
existing line.
Drag a line off the negative portion of the Sum block straight down and release the mouse so the
line is incomplete. From the endpoint of this line, click and drag to the line between the Plant
and the Scope. The model should now appear as follows.
Finally, labels will be placed in the model to identify the signals. To place a label anywhere in
the model, double-click at the point you want the label to be. Start by double-clicking above the
line leading from the Step block. You will get a blank text box with an editing cursor as shown
below.
Type an r in this box, labeling the reference signal and click outside it to end editing.
Label the error (e) signal, the control (u) signal, and the output (y) signal in the same manner.
Your final model should appear as:
To save your model, select Save As in the File menu and type in any desired model name. The
completed model can be downloaded by right-clicking here and then selecting Save link as ....
Simulation
Now that the model is complete, you can simulate the model. Select Run from the Simulation menu to
run the simulation. Double-click on the _Scope_block to view its output and you should see the
following:
In some cases, parameters, such as gain, may be calculated in MATLAB to be used in a Simulink
model. If this is the case, it is not necessary to enter the result of the MATLAB calculation directly into
Simulink. For example, suppose we calculated the gain in MATLAB in the variable K. Emulate this by
entering the following command at the MATLAB command prompt.
K = 2.5
This variable can now be used in the Simulink Gain block. In your Simulink model, double-click on the
Gain block and enter the following the Gain field.
K
Close this dialog box. Notice now that the Gain block in the Simulink model shows the variable K rather
than a number.
Now, you can re-run the simulation and view the output on the Scope. The result should be the same as
before.
Now, if any calculations are done in MATLAB to change any of the variables used in the Simulink
model, the simulation will use the new values the next time it is run. To try this, in MATLAB, change
the gain, K, by entering the following at the command prompt.
K = 5
Start the Simulink simulation again and open the Scope window. You will see the following output
which reflects the new, higher gain.
Besides variables and signals, even entire systems can be exchanged between MATLAB and Simulink.
Introduction: PID Controller Design
In this tutorial we will introduce a simple, yet versatile, feedback compensator structure: the
Proportional-Integral-Derivative (PID) controller. The PID controller is widely employed because it is
very understandable and because it is quite effective. One attraction of the PID controller is that all
engineers understand conceptually differentiation and integration, so they can implement the control
system even without a deep understanding of control theory. Further, even though the compensator is
simple, it is quite sophisticated in that it captures the history of the system (through integration) and
anticipates the future behavior of the system (through differentiation). We will discuss the effect of each
of the PID parameters on the dynamics of a closed-loop system and will demonstrate how to use a PID
controller to improve a system's performance.
Key MATLAB commands used in this tutorial are: tf , step , pid , feedback , pidtune
Contents
PID Overview
The Characteristics of the P, I, and D Terms
Example Problem
Open-Loop Step Response
Proportional Control
Proportional-Derivative Control
Proportional-Integral Control
Proportional-Integral-Derivative Control
General Tips for Designing a PID Controller
Automatic PID Tuning
PID Overview
The output of a PID controller, which is equal to the control input to the plant, is calculated in the time
domain from the feedback error as follows:
(1)
First, let's take a look at how the PID controller works in a closed-loop system using the schematic
shown above. The variable ( ) represents the tracking error, the difference between the desired output (
) and the actual output ( ). This error signal ( ) is fed to the PID controller, and the controller computes
both the derivative and the integral of this error signal with respect to time. The control signal ( ) to the
plant is equal to the proportional gain ( ) times the magnitude of the error plus the integral gain ( )
times the integral of the error plus the derivative gain ( ) times the derivative of the error.
This control signal ( ) is fed to the plant and the new output ( ) is obtained. The new output ( ) is then
fed back and compared to the reference to find the new error signal ( ). The controller takes this new
error signal and computes an update of the control input. This process continues while the controller is
in effect.
The transfer function of a PID controller is found by taking the Laplace transform of Equation (1).
(2)
We can define a PID controller in MATLAB using a transfer function model directly, for example:
Kp = 1;
Ki = 1;
Kd = 1;
s = tf('s');
C = Kp + Ki/s + Kd*s
C =
s^2 + s + 1
-----------
s
Alternatively, we may use MATLAB's pid object to generate an equivalent continuous-time controller
as follows:
C = pid(Kp,Ki,Kd)
C =
1
Kp + Ki * --- + Kd * s
s
with Kp = 1, Ki = 1, Kd = 1
tf(C)
ans =
s^2 + s + 1
-----------
s
Increasing the proportional gain ( ) has the effect of proportionally increasing the control signal for
the same level of error. The fact that the controller will "push" harder for a given level of error tends to
cause the closed-loop system to react more quickly, but also to overshoot more. Another effect of
increasing is that it tends to reduce, but not eliminate, the steady-state error.
The addition of a derivative term to the controller ( ) adds the ability of the controller to "anticipate"
error. With simple proportional control, if is fixed, the only way that the control will increase is if the
error increases. With derivative control, the control signal can become large if the error begins sloping
upward, even while the magnitude of the error is still relatively small. This anticipation tends to add
damping to the system, thereby decreasing overshoot. The addition of a derivative term, however, has
no effect on the steady-state error.
The addition of an integral term to the controller ( ) tends to help reduce steady-state error. If there is a
persistent, steady error, the integrator builds and builds, thereby increasing the control signal and
driving the error down. A drawback of the integral term, however, is that it can make the system more
sluggish (and oscillatory) since when the error signal changes sign, it may take a while for the integrator
to "unwind."
The general effects of each controller parameter ( , , ) on a closed-loop system are summarized
in the table below. Note, these guidelines hold in many cases, but not all. If you truly want to know the
effect of tuning the individual gains, you will have to do more analysis, or will have to perform testing
on the actual system.
Example Problem
(3)
(4)
The transfer function between the input force and the output displacement then becomes
(5)
Let
m = 1 kg
b = 10 N s/m
k = 20 N/m
F = 1 N
(6)
The goal of this problem is to show how each of the terms, , , and , contributes to obtaining the
common goals of:
Let's first view the open-loop step response. Create a new m-file and run the following code:
s = tf('s');
P = 1/(s^2 + 10*s + 20);
step(P)
The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output to a unit step
input. This corresponds to a steady-state error of 0.95, which is quite large. Furthermore, the rise time is
about one second, and the settling time is about 1.5 seconds. Let's design a controller that will reduce
the rise time, reduce the settling time, and eliminate the steady-state error.
Proportional Control
From the table shown above, we see that the proportional controller ( ) reduces the rise time, increases
the overshoot, and reduces the steady-state error.
The closed-loop transfer function of our unity-feedback system with a proportional controller is the
following, where is our output (equals ) and our reference is the input:
(7)
Let the proportional gain ( ) equal 300 and change the m-file to the following:
Kp = 300;
C = pid(Kp)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
Kp = 300
P-only controller.
T =
300
----------------
s^2 + 10 s + 320
The above plot shows that the proportional controller reduced both the rise time and the steady-state
error, increased the overshoot, and decreased the settling time by a small amount.
Proportional-Derivative Control
Now, let's take a look at PD control. From the table shown above, we see that the addition of derivative
control ( ) tends to reduce both the overshoot and the settling time. The closed-loop transfer function
of the given system with a PD controller is:
(8)
Let equal 300 as before and let equal 10. Enter the following commands into an m-file and run it
in the MATLAB command window.
Kp = 300;
Kd = 10;
C = pid(Kp,0,Kd)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
Kp + Kd * s
with Kp = 300, Kd = 10
T =
10 s + 300
----------------
s^2 + 20 s + 320
Proportional-Integral Control
Before proceeding to PID control, let's investigate PI control. From the table, we see that the addition of
integral control ( ) tends to decrease the rise time, increase both the overshoot and the settling time,
and reduces the steady-state error. For the given system, the closed-loop transfer function with a PI
controller is:
(9)
Let's reduce to 30, and let equal 70. Create a new m-file and enter the following commands.
Kp = 30;
Ki = 70;
C = pid(Kp,Ki)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
1
Kp + Ki * ---
s
with Kp = 30, Ki = 70
T =
30 s + 70
------------------------
s^3 + 10 s^2 + 50 s + 70
Run this m-file in the MATLAB command window and you should generate the above plot. We have
reduced the proportional gain ( ) because the integral controller also reduces the rise time and
increases the overshoot as the proportional controller does (double effect). The above response shows
that the integral controller eliminated the steady-state error in this case.
Proportional-Integral-Derivative Control
Now, let's examine PID control. The closed-loop transfer function of the given system with a PID
controller is:
(10)
After several iterations of tuning, the gains = 350, = 300, and = 50 provided the desired
response. To confirm, enter the following commands to an m-file and run it in the command window.
You should obtain the following step response.
Kp = 350;
Ki = 300;
Kd = 50;
C = pid(Kp,Ki,Kd)
T = feedback(C*P,1);
t = 0:0.01:2;
step(T,t)
C =
1
Kp + Ki * --- + Kd * s
s
Now, we have designed a closed-loop system with no overshoot, fast rise time, and no steady-state
error.
General Tips for Designing a PID Controller
When you are designing a PID controller for a given system, follow the steps shown below to obtain a
desired response.
Lastly, please keep in mind that you do not need to implement all three controllers (proportional,
derivative, and integral) into a single system, if not necessary. For example, if a PI controller meets the
given requirements (like the above example), then you don't need to implement a derivative controller
on the system. Keep the controller as simple as possible.
An example of tuning a PI controller on an actual physical system can be found at the following link.
This example also begins to illustrate some challenges of implementing control, including: control
saturation, integrator wind-up, and noise amplification.
MATLAB provides tools for automatically choosing optimal PID gains which makes the trial and error
process described above unnecessary. You can access the tuning algorithm directly using pidtune or
through a nice graphical user interface (GUI) using pidTuner.
The MATLAB automated tuning algorithm chooses PID gains to balance performance (response time,
bandwidth) and robustness (stability margins). By default, the algorithm designs for a 60-degree phase
margin.
Let's explore these automated tools by first generating a proportional controller for the mass-spring-
damper system by entering the command shown below. In the shown syntax, P is the previously
generated plant model, and 'p' specifies that the tuner employ a proportional controller.
pidTuner(P,'p')
The pidTuner GUI window, like that shown below, should appear.
Notice that the step response shown is slower than the proportional controller we designed by hand.
Now click on the Show Parameters button on the top right. As expected, the proportional gain, , is
smaller than the one we employed, = 94.86 < 300.
We can now interactively tune the controller parameters and immediately see the resulting response in
the GUI window. Try dragging the Response Time slider to the right to 0.14 s, as shown in the figure
below. This causes the response to indeed speed up, and we can see is now closer to the manually
chosen value. We can also see other performance and robustness parameters for the system. Note that
before we adjusted the slider, the target phase margin was 60 degrees. This is the default for the
pidTuner and generally provides a good balance between robustness and performance.
Now let's try designing a PID controller for our system. By specifying the previously designed or
(baseline) controller, C, as the second parameter, pidTuner will design another PID controller (instead
of P or PI) and will compare the response of the system with the automated controller with that of the
baseline.
pidTuner(P,C)
We see in the output window that the automated controller responds slower and exhibits more overshoot
than the baseline. Now choose the Domain: Frequency option from the toolstrip, which reveals
frequency domain tuning parameters.
Now type in 32 rad/s for Bandwidth and 90 deg for Phase Margin, to generate a controller similar in
performance to the baseline. Keep in mind that a higher closed-loop bandwidth results in a faster rise
time, and a larger phase margin reduces the overshoot and improves the system stability.
Finally, we note that we can generate the same controller using the command line tool pidtune instead
of the pidTuner GUI employing the following syntax.
opts = pidtuneOptions('CrossoverFrequency',32,'PhaseMargin',90);
[C, info] = pidtune(P, 'pid', opts)
C =
1
Kp + Ki * --- + Kd * s
s
info =
struct with fields:
Stable: 1
CrossoverFrequency: 32
PhaseMargin: 90
Introduction: Simulink Control
Contents
In the Introduction: Simulink Modeling page we demonstrated how Simulink can be employed to
simulate a physical system. More generally, Simulink can also simulate the complete control system,
including the control algorithm in addition to the physical plant. As mentioned previously, Simulink is
especially useful for generating the approximate solutions of mathematical models that may be
prohibitively difficult to solve "by hand." For example, consider that you have a nonlinear plant. A
common approach is to generate a linear approximation of the plant and then use the linearized model to
design a controller using analytical techniques. Simulink can then be employed to simulate the
performance of your controller when applied to the full nonlinear model. Simulink can be employed for
generating the linearized model and MATLAB can be employed for designing the controller as
described in the other Introduction pages. Various control design facilities of MATLAB can also be
accessed directly from within Simulink. We will demonstrate both approaches in this page.
Recall the Simulink model of the toy train system derived in the Introduction: Simulink Modeling page
and pictured below.
You can generate this model yourself, or you can download the completed model by right-clicking here
and then selecting Save link as .... Assuming that the train only travels in one dimension (along the
track), we want to apply control to the train engine so that it starts and comes to rest smoothly, and so
that it can track a constant speed command with minimal error in steady state.
Let us first create the structure for simulating the train system in unity feedback with a PID controller.
In order to make our Simulink model more understandable, we will first save the train model into its
own subsystem block. To accomplish this, delete the three scope blocks and replace each one by an
Out1 block from the Sinks library. Label each Out1 block with the corresponding variable name,
"x1_dot", "x1", and "x2". Then delete the Signal Generator block and replace it with an In1 block from
the Sources library. Label this input "F" for the force generated between the train engine and the
railroad track. Your model should now appear as follows.
Next select all of the blocks in your model (Ctrl A) and select Create Subsystem from Selection after
right-clicking on the model window. With a little rearranging and relabeling, your model will appear as
shown below.
Now we can add a controller to our system. We will employ a PID controller which can be implemented
using a PID Controller block from the Continuous library. Placing this block in series with the train
subsystem, your model will appear as follows. In the following, we model the controller as generating
the force "F" directly. This neglects the dynamics with which the train engine generates the torque
applied to the wheels, and subsequently neglects the dynamics of how the force is generated at the
wheel/track interface. This simplified approach is taken at this point since we only wish to introduce the
basic functionality of Simulink for controller design and analysis.
Double-clicking on the PID Controller block, we will initially set the Integral (I) gain field equal to 0
and will leave the Proportional (P) and Derivative (D) gains as their defaults of 1 and 0, respectively.
Next add a Sum block from the Math Operations library. Double-click on this block and modify the List
of signs field to "|+-". Since we wish to control the velocity of the toy train engine, we will feed back
the engine's velocity. This is accomplished by tapping a line off of the "x1_dot" signal and connecting it
to the negative sign of the Sum block. The output of the Sum block will be the velocity error for the
train engine and should be connected to the input of the PID Controller block. Connecting the blocks as
described and adding labels, your model should appear as follows.
Next add a Signal Builder block from the Sources library to represent the velocity commanded to the
train. Since we wish to design a controller to bring the train smoothly up to speed and smoothly to rest,
we will test the system with a velocity command that steps up to 1 m/s followed by a step back down to
0 m/s (recall that our system is a toy train). To generate this type of command signal, double-click on
the Signal Builder block. Then choose Change time range from the Axes menu at the top of the block's
dialog window. Set the Max time field to "300" seconds. Next, set the step up to occur at 10 seconds
and the step down to occur at 150 seconds. This is accomplished by clicking on the corresponding
portions of the signal graph (left and right vertical lines) and either dragging the line to the desired
position, or entering the desired time in the T field at the bottom of the window. When done, your signal
should appear as follows.
Also add a Scope block from the Sinks library and use it to replace the Out1 block for the train's
velocity. Relabeling the blocks, your model will appear as follows.
We are now ready to run the closed-loop simulation. If you wish to skip the above steps, you may
download the completed model with control by right-clicking here and then selecting Save link as ....
Before running the model, we need to assign numerical values to each of the variables used in the
model. For the train system, we will employ the following values.
= 1 kg
= 0.5 kg
= 1 N/sec
=1N
= 0.02 sec/m
= 9.8 m/s^2
M1 = 1;
M2 = 0.5;
k = 1;
F = 1;
mu = 0.02;
g = 9.8;
Execute your m-file in the MATLAB command window to define these values. Simulink will recognize
these MATLAB variables for use in the model. Next we need to set the time for which our simulation
will run to match the time range of the command from the Signal Builder block. This is accomplished
by selecting Model Configuration Parameters from the Simulation menu at the top of the model
window and changing the Stop Time field to "300". Now, run the simulation and open the "x1_dot"
scope to examine the velocity output. The result as shown below demonstrates that the closed-loop
system is stable for this controller.
Since the performance achieved above is unsatisfactory because of steady-state error, we will show how
to redesign our controller. We will first demonstrate how to extract a model from Simulink into
MATLAB for analysis and design. Then we will demonstrate how to design the control from directly
within Simulink.
The Simulink Control Design toolbox offers the functionality to extract a model from Simulink into the
MATLAB workspace. This is especially useful for complicated, or nonlinear simulation models. This is
also useful for generating discrete-time (sampled) models. For this example, let us extract a continous-
time model of our train subsystem. First we need to identify the inputs and outputs of the model we
wish to extract. The input to the train system is the force . We can designate this fact by right-clicking
on the signal representing "F" (output of the PID block) and choosing Linear Analysis Points > Open-
loop Input from the resulting menu. Likewise, we can designate the output of the train system by right-
clicking on the "x1_dot" signal and choosing Linear Analysis Points > Open-loop Output from the
resulting menu. These inputs and outputs will now be indicated by small arrow symbols as shown in the
following figure. Since we wish to extract a model of the train by itself, without control, we need to
further delete the feedback signal, otherwise we will extract the closed-loop model from to . Your
model should now appear as follows.
We can now extract the model by opening the Linear Analysis Tool. This is accomplished by selecting
Control Design > Linear Analysis from under the Analysis menu at the top of the model window.
Following these steps will open the window shown below.
This tool generates an LTI object from a (possibly nonlinear) Simulink model and allows you to specify
the point about which the linearization is performed. Since our Simulink model is already linear, our
choice of operating point will have no effect and we can leave it as the default Model Initial
Condition. In order to generate the linearized model, select the Step button in the above figure, which
is indicated by a small green triangle. The Linear Analysis Tool window should now appear as shown
below.
Inspecting the above, the step response of the linearized model was automatically generated. Comparing
this step response to the one generated by the simulation of the open-loop train system in the
Introduction: Simulink Modeling page, you can see that the responses are identical. This makes sense
since the simulation model was already linear. Additionally, the linearization process generated the
object linsys1 shown in the Linear Analysis Workspace above. This LTI object can be exported for
use within MATLAB by simply dragging the object into the MATLAB Workspace portion of the
Linear Analysis Tool window.
Having extracted this model, we can now employ all of the facilities that MATLAB offers for controller
design. For example, let us employ the following commands to generate and analyze the closed-loop
system reflecting the Simulink model created above.
sys_cl = feedback(linsys1,1);
p = pole(sys_cl)
z = zero(sys_cl)
p =
-0.9237 + 0.0000i
0.0000 + 0.0000i
-0.2342 + 1.6574i
-0.2342 - 1.6574i
z =
-0.0980 + 1.4108i
-0.0980 - 1.4108i
0.0000 + 0.0000i
Inspection of the above shows there is a pole-zero cancellation at the origin. Further, the remaining
poles have negative real part and the two "slowest" poles are complex. This demonstrates that the
closed-loop system in its current form is stable and the dominant poles are underdamped. This agrees
with the result of our closed-loop simulation from above. We could then employ MATLAB to design a
new controller in order to, for example, dampen out the oscillation in the response. In this case, we will
rather demonstrate how to access some of MATLAB's functionality from directly within Simulink.
We can launch interactive tools to tune our controller from within Simulink. One manner in which this
can be done is to double-click on the PID Controller in the model and select the Tune button to launch
the PID Tuner tool. Rather than doing this, we will launch the more general Control System Designer
tool by selecting Control Design > Control System Designer from under the Analysis menu located at
the top of the model window. Following these steps will open the window shown below.
The first thing that needs to be done is to identify the controller block that is to be tuned. This is
accomplished by first clicking on the Add Blocks button, and then selecting the PID Controller block
from the resulting window as shown below. Next click the OK button. Note that controllers represented
by other types of blocks (Transfer Function, State Space, etc.) can also be tuned.
Before we proceed to tune our controller, we must first identify the inputs and outputs of the closed-
loop system we wish to analyze. This is done in a similar manner to how we extracted the linearized
model into MATLAB. Specifically, right-click on the velocity command signal (output of the Signal
Builder block) and choose Linear Analysis Points > Input Perturbation from the resulting menu to
identify the input of our closed-loop system. Next, right-click on the train engine velocity signal
("x1_dot") and select Linear Analysis Points > Output Measurement from the menu to choose the
output of our system. Your model should now appear as follows where the small arrow symbols identify
the input and output of the model.
Now that we have identified the block to tune and our input and output signals, we can now commence
with tuning the controller. Select the OK button in the Edit Architecture window. We should now be
able to see the window shown below.
Clicking the Tuning Methods button, we will choose the design plots we wish to employ for designing
our controller. In this example, we will employ a root locus design approach and hence will select the
Root Locus Editor under Graphical Tuning as shown above. The root locus approach to design
employs a plot that shows all possible closed-loop poles as a parameter (the loop gain) is varied from
zero to infinity. Specifically, we make the following selection in the Select Response to Edit window
and select Plot.
We then should obtain a root locus plot as shown below, which displays all possible closed-loop pole
locations of the closed-loop train system under simple proportional control. Examining the plot, one can
see that all values of loop gain will place the closed-loop poles in the left-half plane indicating a stable
response.
If we decrease the loop gain sufficiently, we can move the closed-loop poles further into the left-half
plane and we can change the performance of our system. This can be accomplished graphically by
"grabbing" the pink boxes marking the closed-loop pole locations and dragging them toward the open-
loop pole locations (marked by x's). We can examine the corresponding closed-loop step response by
clicking on New Plot under the CONTROL SYSTEM tab and selecting New Step. When the
following window appears, we then select New Input-Output Transfer Response from the Select
Response to Plot dropdown menu as shown.
Then we specify the input and output signals within the New Step to plot window as shown below.
Then click the Plot button. From the resulting closed-loop step response we can see that the response is
stable, but with some steady-state error.
Recall that adding integral control is one way to reduce the steady-state error of a closed-loop system. In
this case, adding an integrator via the controller will make the system type 1, where type 1 systems can
track step references with zero steady-state error. Recall the following form of a PI controller.
(1)
Inspection of this equation demonstrates that a PI controller will add an integrator and a zero to our
open-loop system. The integrator can be added to the system by right-clicking in the field of the root
locus plot and selecting Add Pole/Zero > Integrator from the resulting menu. Similarly, the zero can
be added by right-clicking on the root locus plot and selecting Add Pole/Zero > Real Zero from the
resulting menu. Then click on the real axis where you wish to place the zero. You can move the zero by
clicking on it and dragging it to a new location. The compensator can also be edited by directly typing
in pole and zero locations. This can be achieved by right-clicking on the root locus plot and choosing
Edit Compensator from the resulting menu. The window that opens is shown below. We will place an
integrator, a real zero at -0.15, and will choose a loop gain equal to 0.05.
The resulting closed-loop step response plot is shown below demonstrating that the train engine is
brought to rest smoothly and with zero steady-state error for a constant speed command.
The control gains that have been chosen can then be applied to the Simulink model by clicking the
Update Blocks button within the CONTROL SYSTEM tab as shown above. The simulation can then
be run with this newly tuned controller. Clicking on the Scope block for the train engine's velocity will
produce a plot like the one shown below.
Overall, this response seems to meet our goals of bringing the train up to speed and back to rest
smoothly, while maintaining minimal steady-state error. This response matches the result generated
with the Control System Designer above because that analysis and the Simulink model used the exact
same linear model.