You are on page 1of 248

Introduction: System Modeling

The first step in the control design process is to develop appropriate mathematical models of the system 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 enter these models into MATLAB for further analysis.

Key MATLAB commands used in this tutorial are: ss , tf

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, is the state vector, a set of variables representing the configuration of the system

at time . For instance in a simple mechanical mass-spring-damper system, the two state variables could be the

position and velocity of the mass. is the vector of control inputs at time , representing the externally applied

"forces" on the system, and is a possibly nonlinear function giving the time derivative (rate of change) of the

state vector, for a particular state, input, and time.

The state at any future time, , may be determined exactly given knowledge of the initial state, , and

the time history of the inputs, , between and by integrating Eq.(1). Though the state variables

themselves are not unique, there is a minimum number of state variables, , required in a given system for the
above to hold true. 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 Eq.(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 betime invariant. This is often a very reasonable assumption, since 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 control input, 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, 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 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), is the feedforward matrix (qxp).

The output equation, Eq.(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 often there is no direct feedforward 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 Eq.(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 at the same frequency but in general with different magnitude and phase. These magnitude
and phase differences as a function of frequency are 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 output/input 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.
The Laplace transform of a time domain function, , is defined below:

(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). It is much more common to
look up the transform of the function you are interested in in a table such as the one found here: Laplace

Transform Table
The Laplace transform of the nth derivative of a function is particularly important:

(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 follows:

(6)
The Laplace transform of this equation is given below:

(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 the so called zero-pole-

gain form:

(9)
The zeros of the transfer function, , are the roots of the numerator polynomial, i.e. the values of s such

that . The poles of the transfer function, , are the roots of the denominator polynomial, i.e. the

values of s 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 form 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, Eq. (11),

states that the sum of the forces acting on a body equals its mass times acceleration. Newton's third law, for

our purposes, states that if two bodies are connected, then they experience the same magnitude force acting in

opposite directions.

(11)
When applying this equation, it is best to construct a free body diagram (FBD) of the sysetm showing all applied

forces.
Example: Mass-Spring-Damper System

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, Eq. (11), in each direction of the
problem. 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

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)

Note also that these state variables correspond to the potential energy in the spring and the kinetic energy of the

mass respectively. Often when choosing state variables it is helpful to consider the independent energy storage
elements in the system.

The state equation in this case is as follows:

(14)

If, for instance, we are interested in controlling the position of the mass, then the output equation is as follows:

(15)
Entering State-Space Models into MATLAB
Now we will show you how to enter the equations derived above into a m-file for MATLAB. Let's assign

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

Continuous-time state-space model.

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)
Entering Transfer Function Models into MATLAB
Now we will show how to enter the transfer function derived above into 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 =

---------------

s^2 + 0.2 s + 1
Continuous-time transfer function.

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 =

---------------

s^2 + 0.2 s + 1

Continuous-time transfer function.

Electrical Systems
Like Newton’s laws in mechanical systems, Kirchoff’s circuit laws are the basic analytical tool in electrical
systems. Kirchoff’s current law (KCL) states that the sum of the electrical currents entering and exiting a node
in a circuit must be equal. Kirchoff’s voltage law (KVL) states that the sum of voltage differences around any
closed loop in the circuit is zero. When applying KVL, the source voltages are typically taken as positive and the

load voltages taken as negative.


Example: RLC Circuit
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 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 to mass, the resistance to viscous damping, and the inverse

capacitance 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 and current as the state variables.

(19)

where,

(20)
The state equation is therefore:
(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 fcuntion 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

known. 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.

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 if the other form of representation is required.

If you need to learn how to convert from one representation to the other, see the Introduction: System

Conversions page.
Introduction: System Analysis
Once appropriate mathematical models of a system have been obtained, either in state-space or transfer function

form, we may then analyze these models to predict how the system will respond in both the time and frequency

domains. To put this in context, control systems are often designed to improve stability, speed of response,

steady-state error, or prevent oscillations. In this section, we will show how to determine these dynamic
properties from the system models.

Key MATLAB commands used in this tutorial are: tf , ssdata , pole , eig , step , pzmap , bode , ltiview

Contents

 Time Response Overview

 Frequency Response Overview

 Stability

 System Order

 First Order Systems

 Second Order Systems


Time Response Overview
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 input forces, 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 free (homogeneous or zero input) and the forced (inhomogeneous or non-zero input) solutions of the

governing differential equations respectively.


Frequency Response Overview
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 as a function of frequency comprise
the frequency response of the system.
The frequency response of a system can be found from the 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 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 s at 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 oscillate. The poles of a LTI system model can easily
be found in MATLAB using the pole command, an example if which is shown below:

s = tf('s');

G = 1/(s^2+2*s+5)

pole(G)

G =

-------------

s^2 + 2 s + 5

Continuous-time transfer function.

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 A. 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 s 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 cruise control
systems and RC circuits.

The general form of the first order differential equation is as follows

(1)
The first order transfer function is

(2)
DC Gain

The DC gain, , is the ratio of the magnitude of the steady-state step response to the magnitude of the step
input. From the Final Value Theorem, for stable transfer functions the DC gain is the value of the transfer

function when s=0. For first order systems equal to .


Time Constant

The time constant is the time it takes for the system to reach 63% of the steady-state value for

a step response or to decrease to 37% of the inital value for an impulse response. More generally, it represents
the time scale for which the dynamics of the system are significant.

Poles/Zeros

There is a single real pole at . Therefore, the system is stable if is positive and unstable if is negative.
There are no zeros.

Step Response

We can calculate the system time response to a step input of magnitude u using the MATLAB following
commands:

k_dc = 5;

Tc = 10;

u = 2;

s = tf('s');

G = k_dc/(Tc*s+1)

step(u*G)

G =

--------

10 s + 1

Continuous-time transfer function.


Note: MATLAB also provides a powerful GUI (LTI Viewer) for analyzing LTI systems which can be accessed
using, ltiview('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 ouput to fall within a certain percentage (i.e. 2%) of the

steady state value for a step input or equivalently to decrease to a certain percentage of the initial value for an

impulse input. The settling times for 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 tolerance,
as expected.

10% 5% 2% 1%

Ts=2.3/a=2.3Tc Ts=3/a=3Tc Ts=3.9/a=3.9Tc Ts=4.6/a=4.6Tc

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

The Bode Plots show the magnitude and phase of the system frequency response, . We can generate
the Bode plots in MATLAB using the bode(G) command.

bode(G)

Again the same results could be obtained using the LTI viewer GUI, ltiview('bode',G)

The Bode plots use 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)

Like frequency, 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 systems are combined or controllers are added, transfer functions

are often multiplied together. Using the dB scale, we may simply add the magnitudes of the transfer functions.
Note, we may also add the phase angles though these are not shown on a log scale.

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 at 20dB for every factor of
ten increase in frequency (-20dB/decade). The phase plot is asymptotic to 0 degrees at low frequency, and
asymptotic to -90 degrees at high frequency. 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. In fact many real higher order systems are modeled as second order to facilitate analysis.
Examples include mass-spring-damper systems and RLC circuits.

The general form of the first order differential equation is as follows

(4)
The first order transfer function is

(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 second order systems,

(6)
Damping Ratio

The damping ratio is a dimensionless quantity charaterizing the energy losses in the system due to such effects
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

The second order transfer function has two poles at:

(9)
Under Damped System

If , then the system is under damped. Both poles are complex valued with negative real parts; therefore

the system is stable but oscillates while approaching the steady-state value.
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 or equivalently to decrease to a certain percentage of the initial value for an impulse
input. For a 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%

Ts=2.3/(zeta*wn) Ts=3/(zeta*w_n) Ts=3.9/(zeta*w_n) Ts=4.6/(zeta*w_n)

Percent Overshoot

The percent overshoot is the percent by which a system exceeds its final steady-state value. For a second order
under damped system, the percent overshoot is diretly related to the damping ratio by the following equation:

(11)

For second order under damped systems, the 2% settling time, , rise time, , and percent overshoot, %OS,
are related to the damping and natural frequency as shown below.
(12)

(13)

(14)
Over Damped Systems

If , then the system is over damped. 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 over damped system are calculated below:

zeta = 1.2;

G2 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2);

pzmap(G2)

axis([-20 1 -1 1])
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, .

Critically damped systems approach steady-state quickest without oscillating. Now change the value of the

damping to 1, and replot the step response and pole-zero map.

zeta = 1;

G3 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2);

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 oscillates indefinitely.

zeta = 0;

G4 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2);

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('under damped: zeta < 1','over damped: zeta > 1','critically damped: zeta =

1','undamped: zeta = 0')


The magnitude of the bode plot of a second order system drops off at -40dB per decade, while the relative phase

changes from 0 to -180 degrees at -90 degrees per decade. For the under damped systems, we also see a

resonance peak near the natural frequency, = 10 rad/s. The 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: PID Controller Design
In this tutorial we will introduce a simple yet versatile feedback compensator structure, the Proportional-Integral-

Derivative (PID) controller. We will discuss the effect of each of the pid parameters on the closed-loop dynamics
and demonstrate how to use a PID controller to improve the system performance.

Key MATLAB commands used in this tutorial are: tf , step , pid , feedback , pidtool , pidtune

Contents

 PID Overview

 The Characteristics of P, I, and D Controllers

 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
In this tutorial, we will consider the following unity feedback system:

The output of a PID controller, equal to the control input to the plant, in the time-domain is 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 input value ( ) and the actual

output ( ). This error signal ( ) will be sent to the PID controller, and the controller computes both the derivative

and the integral of this error signal. 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 sent 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 its derivative and its integral again, ad infinitum.

The transfer function of a PID controller is found by taking the Laplace transform of Eq.(1).

(2)

= Proportional gain = Integral gain = Derivative gain

We can define a PID controller in MATLAB using the transfer function directly, for example:

Kp = 1;

Ki = 1;

Kd = 1;

s = tf('s');

C = Kp + Ki/s + Kd*s

C =

s^2 + s + 1

-----------

Continuous-time transfer function.


Alternatively, we may use MATLAB's pid controller object to generate an equivalent continuous-time controller

as follows:

C = pid(Kp,Ki,Kd)

C =

Kp + Ki * --- + Kd * s

with Kp = 1, Ki = 1, Kd = 1

Continuous-time PID controller in parallel form.

Let's convert the pid object to a transfer function to see that it yields the same result as above:

tf(C)

ans =

s^2 + s + 1

-----------

s
Continuous-time transfer function.

The Characteristics of P, I, and D Controllers


A proportional controller ( ) will have the effect of reducing the rise time and will reduce but never eliminate
the steady-state error. An integral control ( ) will have the effect of eliminating the steady-state error for a

constant or step input, but it may make the transient response slower. A derivative control ( ) will have the

effect of increasing the stability of the system, reducing the overshoot, and improving the transient response.

The effects of each of controller parameters, , , and on a closed-loop system are summarized in the
table below.

CL RESPONSE RISE TIME OVERSHOOT SETTLING TIME S-S ERROR

Kp Decrease Increase Small Change Decrease

Ki Decrease Increase Increase Eliminate

Kd Small Change Decrease Decrease No Change

Note that these correlations may not be exactly accurate, because , , and are dependent on each

other. In fact, changing one of these variables can change the effect of the other two. For this reason, the table
should only be used as a reference when you are determining the values for , and .

Example Problem
Suppose we have a simple mass, spring, and damper problem.

The modeling equation of this system is

(3)
Taking the Laplace transform of the modeling equation, we get
(4)
The transfer function between the displacement and the input then becomes

(5)

Let

M = 1 kg

b = 10 N s/m

k = 20 N/m

F = 1 N

Plug these values into the above transfer function

(6)

The goal of this problem is to show you how each of , and contributes to obtain

Fast rise time

Minimum overshoot

No steady-state error

Open-Loop Step Response


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 an unit step input.

This corresponds to the steady-state error of 0.95, quite large indeed. 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 (Kp) reduces the rise time, increases the
overshoot, and reduces the steady-state error.

The closed-loop transfer function of the above system with a proportional controller is:

(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

Continuous-time transfer function.


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 small amount.

Proportional-Derivative Control
Now, let's take a look at a PD control. From the table shown above, we see that the derivative controller (Kd)

reduces 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

Continuous-time PD controller in parallel form.

T =

10 s + 300

----------------

s^2 + 20 s + 320
Continuous-time transfer function.

This plot shows that the derivative controller reduced both the overshoot and the settling time, and had a small
effect on the rise time and the steady-state error.

Proportional-Integral Control
Before going into a PID control, let's take a look at a PI control. From the table, we see that an integral controller

(Ki) decreases the rise time, increases both the overshoot and the settling time, and eliminates the steady-state
error. For the given system, the closed-loop transfer function with a PI control is:

(9)

Let's reduce the to 30, and let equal 70. Create an 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 =

Kp + Ki * ---

with Kp = 30, Ki = 70

Continuous-time PI controller in parallel form.

T =
30 s + 70

------------------------

s^3 + 10 s^2 + 50 s + 70

Continuous-time transfer function.

Run this m-file in the MATLAB command window, and you should get the following plot. We have reduced the

proportional gain (Kp) 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.

Proportional-Integral-Derivative Control
Now, let's take a look at a PID controller. The closed-loop transfer function of the given system with a PID
controller is:
(10)

After several trial and error runs, 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 get 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 =

Kp + Ki * --- + Kd * s

with Kp = 350, Ki = 300, Kd = 50


Continuous-time PID controller in parallel form.

Now, we have obtained 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.

1. Obtain an open-loop response and determine what needs to be improved

2. Add a proportional control to improve the rise time

3. Add a derivative control to improve the overshoot

4. Add an integral control to eliminate the steady-state error

5. Adjust each of Kp, Ki, and Kd until you obtain a desired overall response. You can always refer to the table
shown in this "PID Tutorial" page to find out which controller controls what characteristics.

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 gives a good enough response
(like the above example), then you don't need to implement a derivative controller on the system. Keep the
controller as simple as possible.

Automatic PID Tuning


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 pidtool.

The MATLAB automated tuning algorithm chooses PID gains to balance performance (response time,
bandwidth) and robustness (stability margins). By default the algorthm 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 following commands:

pidtool(P,'p')

The pidtool 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 constant, Kp, is lower than

the one we used, Kp = 94.85 < 300.

We can now interactively tune the controller parameters and immediately see the resulting response int he GUI
window. Try dragging the resposne time slider to the right to 0.14s, as shown in the figure below. The response

does indeeed speed up, and we can see Kp is now closer to the manual value. We can also see all the other
performance and robustness parameters for the system. Note that the phase margin is 60 degrees, the default
for pidtool and generally a good balance of 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, pidtool 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.
pidtool(P,C)

We see in the output window that the automated controller responds slower and exhibits more overshoot than
the baseline. Now choose the Design Mode: Extendedoption at the top, which reveals more tuning parameters.
Now type in Bandwidth: 32 rad/s and Phase Margin: 90 deg to generate a controller similar in performance to

the baseline. Keep in mind that a higher bandwidth (0 dB crossover of the open-loop) results in a faster rise time,

and a higher 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

pidtool GUI
opts = pidtuneOptions('CrossoverFrequency',32,'PhaseMargin',90);

[C, info] = pidtune(P, 'pid', opts)

C =

Kp + Ki * --- + Kd * s

with Kp = 320, Ki = 169, Kd = 31.5

Continuous-time PID controller in parallel form.

info =

Stable: 1

CrossoverFrequency: 32

PhaseMargin: 90
Introduction: Root Locus Controller
Design
In this tutorial we will introduce the root locus, show how to create it using MATlAB, and demonstrate how to
design feedback controllers using the root locus that satisfy certain performance criteria.

Key MATLAB commands used in this tutorial are: feedback , rlocus , step , sisotool

Contents

 Closed-Loop Poles

 Plotting the Root Locus of a Transfer Function


 Choosing a Value of K from the Root Locus

 Closed-Loop Response

 Using SISOTOOL for Root Locus Design


Closed-Loop Poles
The root locus of an (open-loop) transfer function is a plot of the locations (locus) of all possible closed-
loop poles with proportional gain K and unity feedback.

The closed-loop transfer function is:

(1)

and thus the poles of the closed-loop poles of the closed-loop system are values of such that .

If we write , then this equation has the form:

(2)

(3)

Let = order of and = order of (the order of a polynomial is the highest power of that appears in
it).

We will consider all positive values of K. In the limit as , the poles of the closed-loop system

are or the poles of . In the limit as , the poles of the closed-loop system are or

the zeros of .
No matter what we pick K to be, the closed-loop system must always have poles, where is the number

of poles of . The root locus must have branches, each branch starts at a pole of and goes to a

zero of . If has more poles than zeros (as is often the case), and we say
that has zeros at infinity. In this case, the limit of as is zero. The number of zeros at infinity

is , the number of poles minus the number of zeros, and is the number of branches of the root locus that

go to infinity (asymptotes).

Since the root locus is actually the locations of all possible closed-loop poles, from the root locus we can select

a gain such that our closed-loop system will perform the way we want. If any of the selected poles are on the

right half plane, the closed-loop system will be unstable. The poles that are closest to the imaginary axis have

the greatest influence on the closed-loop response, so even though the system has three or four poles, it may
still act like a second or even first order system depending on the location(s) of the dominant pole(s).

Plotting the Root Locus of a Transfer Function


Consider an open-loop system which has a transfer function of

(4)

How do we design a feedback controller for the system by using the root locus method? Say our design criteria
are 5% overshoot and 1 second rise time. Make a MATLAB file called rl.m. Enter the transfer function, and the

command to plot the root locus:

s = tf('s');

sys = (s + 7)/(s*(s + 5)*(s + 15)*(s + 20));

rlocus(sys)

axis([-22 3 -15 15])


Choosing a Value of K from the Root Locus
The plot above shows all possible closed-loop pole locations for a pure proportional controller. Obviously not all

of those closed-loop poles will satisfy our design criteria, To determine what part of the locus is acceptable, we
can use the command sgrid(Zeta,Wn) to plot lines of constant damping ratio and natural frequency. Its two

arguments are the damping ratio ( ) and natural frequency ( ) [these may be vectors if you want to look at a

range of acceptable values]. In our problem, we need an overshoot less than 5% (which means a damping

ratio of greater than 0.7) and a rise time of 1 second (which means a natural frequency greater than 1.8).

Enter the following in the MATLAB command window:

Zeta = 0.7;

Wn = 1.8;

sgrid(Zeta,Wn)
On the plot above, the two dotted lines at about a 45 degree angle indicate pole locations with = 0.7; in between

these lines, the poles will have > 0.7 and outside of the lines < 0.7. The semicircle indicates pole locations
with a natural frequency = 1.8; inside the circle, < 1.8 and outside the circle > 1.8.

Going back to our problem, to make the overshoot less than 5%, the poles have to be in between the two white

dotted lines, and to make the rise time shorter than 1 second, the poles have to be outside of the white dotted

semicircle. So now we know only the part of the locus outside of the semicircle and in betwen the two lines are
acceptable. All the poles in this location are in the left-half plane, so the closed-loop system will be stable.

From the plot above we see that there is part of the root locus inside the desired region. So in this case, we need
only a proportional controller to move the poles to the desired region. You can use the rlocfind command in

MATLAB to choose the desired poles on the locus:


[k,poles] = rlocfind(sys)

Click on the plot the point where you want the closed-loop pole to be. You may want to select the points indicated
in the plot below to satisfy the design criteria.
Note that since the root locus may have more than one branch, when you select a pole, you may want to find

out where the other pole (poles) are. Remember they will affect the response too. From the plot above, we see

that all the poles selected (all the "+" signs) are at reasonable positions. We can go ahead and use the
chosen K as our proportional controller.
Closed-Loop Response
In order to find the step response, you need to know the closed-loop transfer function. You could compute this
using the rules of block diagrams, or let MATLAB do it for you (there is no need to enter a value for K if

the rlocfind command was used):

K = 350;

sys_cl = feedback(K*sys,1)

sys_cl =
350 s + 2450

--------------------------------------

s^4 + 40 s^3 + 475 s^2 + 1850 s + 2450

Continuous-time transfer function.

The two arguments to the function feedback are the numerator and denominator of the open-loop system. You

need to include the proportional gain that you have chosen. Unity feedback is assumed.
If you have a non-unity feedback situation, look at the help file for the MATLAB function feedback, which can

find the closed-loop transfer function with a gain in the feedback loop.
Check out the step response of your closed-loop system:

step(sys_cl)
As we expected, this response has an overshoot less than 5% and a rise time less than 1 second.

Using SISOTOOL for Root Locus Design


Another way to complete what was done above is to use the interactive MATLAB GUI called sisotool. Using

the same model as above, first define the plant, .

s = tf('s');

plant = (s + 7)/(s*(s + 5)*(s + 15)*(s + 20));

The sisotool function can be used for analysis and design. In this case, we will focus on using the Root Locus

as the design method to improve the step response of the plant. To begin, type the following into the MATLAB

command window:
sisotool(plant)

The following window should appear. To start, select the tab labeled Graphical Tuning. Within this window, turn

off Plot 2 and make sure Plot 1 is the Root Locus and verify that Open Loop 1 is selected. Finally, click the

button labeled Show Design Plot to bring up the tunable Root Locus plot.
In the same fashion, select the tab labeled Analysis Plots. Within this window, for Plot 1, select Step. In

the Contents of Plots subwindow, select Closed Loop r to y for Plot 1. If the window does not automatically

pop up, click the button labeled Show Analysis Plot.


The next thing to do is to add the design requirements to the Root Locus plot. This is done directly on the plot
by right-clicking and selecting Design Requirements, New. Design requirements can be set for the Settling

Time, the Percent Overshoot, the Damping Ratio, the Natural Frequency, or a Region Constraint. There is no

direct requirement for Rise Time, but the natural frequency can be used for this.

Here, we will set the design requirements for the damping ratio and the natural frequency just like was done
with sgrid. Recall that the requirements call for = 0.7 and = 1.8. Set these within the design requirements.

On the plot, any area which is still white, is an acceptable region for the poles.
Zoom into the Root Locus by right-clicking on the axis and select Properties, then click the label Limits. Change
the real axis to -25 to 5 and the imaginary to -2.5 to 2.5.

Also, we can see the current values of some key parameters in the response. In the Step response, right-click
on the plot and go to Characteristics and select Peak Response. Do the same for the Rise Time. There should

now be two large dots on the screen indicating the location of these parameters. Click each of these dots to bring

up a screen with information.


Both plots should appear as shown here:

As the characteristics show on the Step response, the overshoot is acceptable, but the rise time is incredibly off.

To fix this, we need to choose a new value for the gain K. Similarly to the rlocfind command, the gain of the

controller can be changed directly on the root locus plot. Click and drag the pink box on the origin to the

acceptable area where the poles have an imaginary component as shown below.
At the bottom of the plot, it can be seen that the loop gain has been changed to 361. Looking at the Step response,
both of the values are acceptable for our requirements.
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 behavior in closed-loop.

Key MATLAB commands used in this tutorial


are: bode , nyquist , margin , lsim , step , feedback , sisotool

Contents

 Gain and Phase Margin

 Nyquist Diagram

 The Cauchy Criterion

 Closed-Loop Performance from Bode Plots

 Closed-Loop Stability from the Nyquist Diagram


Gain and Phase Margin
Consider the following unity feedback system:

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 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 a 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 where the phase shift is 180 deg), the system will become

unstable in closed-loop. The time delay, can be thought of as an extra block in the forward path of the block
diagram that adds phase 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 (in radians/second).
For now, we won't worry about where all this comes from and will concentrate on identifying the gain and phase
margins on a Bode plot.

The phase margin is the difference in phase between the phase curve and -180 degrees at the point

corresponding to the frequency that gives us a gain of 0 dB (the gain crossover frequency, ). Likewise, the

gain margin is the difference between the magnitude curve and 0 dB at the point corresponding to the frequency
that gives us a phase of -180 degrees (the phase crossover frequency, ).

One nice thing about the phase margin is that you don't need to replot the Bode in order to find the new phase

margin when changing the gains. If you recall, adding gain only shifts the magnitude plot up. This is equivalent

to changing the y-axis on the magnitude plot. Finding the phase margin is simply a matter of finding the new

cross-over frequency and reading off the phase margin. For example, suppose you entered the
command bode(sys). You will get the following bode plot:

s = tf('s');

sys = 50/(s^3 + 9*s^2 + 30*s +40);

bode(sys)

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*sys). You should get the following plot:

bode(100*sys)

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 90 degrees, the same as the second Bode plot.

We can have MATLAB calculate and display the gain and phase margins using the margin(sys) command.

This command returns the gain and phase margins, the gain and phase cross over frequencies, and a graphical
representation of these on the Bode plot. Let's check it out:

margin(100*sys)
Bandwidth Frequency

The bandwidth frequency is defined as the frequency at which the closed-loop magnitude response is equal to -

3 dB. 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 Wbw (the bandwidth

frequency) are tracked "reasonably well" by the system. Sinusoidal inputs with frequency greater than Wbw 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)

sys = 1/(s^2 + 0.5*s + 1);

bode(sys)
Since this is the closed-loop transfer function, our bandwidth frequency will be the frequency corresponding to a

gain of -3 dB. Looking at the plot, we find that it is approximately 1.4 rad/s. We can also read off the plot that for

an input frequency of 0.3 radians, the output sinusoid should have a magnitude about one and the phase should

be shifted by perhaps a few degrees (behind the input). For an input frequency of 3 rad/sec, the output magnitude

should be about -20 dB (or 1/10 as large as the input) and the phase should be nearly -180 (almost exactly out-
of-phase). We can use the lsim command to simulate the response of the system to sinusoidal inputs.
First, consider a sinusoidal input with a frequency lower than Wbw. 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).

sys = 1/(s^2 + 0.5*s + 1);

w = 0.3;

t = 0:0.1:100;

u = sin(w*t);

[y,t] = lsim(sys,u,t);
plot(t,y,t,u)

axis([50 100 -2 2])

Note that the output (blue) tracks the input (green) fairly well; it is perhaps a few degrees behind the input as
expected. However, if we set the frequency of the input higher than the bandwidth frequency for the system,

we get a very distorted response (with respect to the input):

sys = 1/(s^2 + 0.5*s + 1);

w = 3;

t = 0:0.1:100;

u = sin(w*t);

[y,t] = lsim(sys,u,t);

plot(t,y,t,u)
axis([90 100 -1 1])

Again, note that the magnitude is about 1/10 that of the input, as predicted, and that it is almost exactly out of

phase (180 degrees behind) the input. Feel free to experiment and view the response for several different
frequencies , and see if they match the Bode plot.

Nyquist Diagram
The Nyquist plot 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 in the jw-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 jw-

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 and
negative frequencies (from zero to infinity) 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 jw axis, will not be defined at those points, and we
must loop around them when we are plotting the contour. Such a contour would look as follows:

Please note that the contour loops around the pole on the jw axis. As we mentioned before, the
MATLAB nyquist command does not take poles or zeros on the jw 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 jw axis, we have
to use nyquist1. If there are no poles or zeros on the jw-axis, or if we have pole-zero cancellation, we can use

either the nyquist command or nyquist1.m.


The Cauchy Criterion
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 controls, we are not as interested in as in 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');

sys = 0.5/(s - 0.5);

nyquist(sys)

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.

sys = (s + 2)/(s^2);

nyquist(sys)

nyquist1(sys)
lnyquist(sys)
Note that the nyquist plot is not the correct one, the nyquist1 plot is correct, but it's hard to see what happens

close to the -1 point, and the lnyquist plot is correct and has an appropriate scale.
Closed-Loop Performance from Bode Plots
In order to predict closed-loop performance from open-loop frequency response, we need to have several
concepts clear:

 The system must be stable in open-loop if we are going to design via Bode plots.

 If the gain crossover frequency is less than the phase crossover frequency (i.e. ), then the

closed-loop system will be stable.

 For 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 second-order systems, a relationship between damping ratio, bandwidth frequency, and settling time is

given by an equation described on the Extras: Bandwidthpage.


 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)

The design must meet the following specifications:

 Zero steady state error.

 Maximum overshoot must be less than 40%.

 Settling time must be less than 2 seconds.

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 a m-

file with the following code:

sys = 10/(1.25*s + 1);

bode(sys)
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 = 1.8 seconds.

This is a rough estimate, so we will say the rise time is about 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 constant ( , , or ) is found from the intersection of the low frequency asymptote with
the w = 1 line. Just extend the low frequency line to the w = 1 line. The magnitude at this point is the constant.

Since the Bode plot of this system is a horizontal line at low frequencies (slope = 0), we know this system is of

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+Kp) = 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(sys,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 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 given by:

(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 specs.

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 or affects our response. Change your m-file to look like the

following (this adds an integral term but no proportional term):

plant = 10/(1.25*s + 1);

contr = 1/s;

bode(contr*plant, 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:

plant = 10/(1.25*s + 1);

contr = (s + 1)/s;

bode(contr*plant, 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.

plant = 10/(1.25*s + 1);

contr = 5 * ((s + 1)/s);

bode(contr*plant, 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(contr*plant,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.

Closed-Loop Stability from the Nyquist Diagram


Consider the negative feedback system:

Remember from the Cauchy criterion that the number N of times that the plot of G(s)H(s) encircles -1 is equal

to the number Z of zeros of 1 + G(s)H(s) enclosed by the frequency contour minus the number P of poles

of 1 + G(s)H(s) enclosed by the frequency contour (N = Z - P). Keeping careful track of open- and closed-

loop transfer functions, as well as numerators and denominators, you should convince yourself that:
 The zeros of 1 + G(s)H(s) are the poles of the closed-loop transfer function.

 The poles of 1 + G(s)H(s) are the poles of the open-loop transfer function.

The Nyquist criterion then states that:

 P = the number of open-loop (unstable) poles of G(s)H(s).

 N = the number of times the Nyquist diagram encircles -1.

 clockwise encirclements of -1 count as positive encirclements.

 counter-clockwise encirclements of -1 count as negative encirclements.

 Z = the number of right-half-plane (positive, real) poles of the closed-loop system.

The important equation whih relates these three quantities is:

(7)
Note: This is only one convention for the Nyquist criterion. Another convention states that a
positive N counts the counter-clockwise 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 diagram encircles -
1. Therefore, we will go into some detail to help you visualize this. You can view this movie as an example.

Another way of looking at it 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 anti-clockwise, N is negative.

Knowing the number of right-half plane (unstable) poles in 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 G(s) is:

(8)
This system has a gain K 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 =

The poles of the open-loop transfer function are both positive. Therefore, we need two anti-clockwise (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 anti-clockwise, our system will be unstable.
Let's look at our Nyquist diagram for a gain of 1:

sys = (s^2 + 10*s + 24)/(s^2 - 8*s + 15);

nyquist(sys)
There are two anti-clockwise encirclements of -1. Therefore, the system is stable for a gain of 1. Now we will see
how the system behaves if we increase the gain to 20:

nyquist(20*sys)
The diagram expanded. Therefore, we know that the 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*sys)
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
steps 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 system unstable. 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 and therefore 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 180 degree phase

shift occurs...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 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)

and that the Nyquist diagram can be viewed by typing:

sys = 50/(s^3 + 9*s^2 + 30*s + 20);

nyquist(sys)
As we discussed before, all that we need to do to find the gain margin is find a, as defined in the preceding figure.

To do this, we need to find the point where there is exactly 180 degrees of phase shift. This means that the

transfer function at this point is real (has no imaginary part). The numerator is already real, so we just need to
look at the denominator. When s = jw, the only terms in the denominator that will have imaginary parts are

those which are odd powers of s. Therefore, for G(jw) to be real, we must have:

(14)
which means w = 0 (this is the rightmost point in the Nyquist diagram) or w = sqrt(30). We can then find
the value of G(jw) 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 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 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 zero 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 Modelingsection. For a SISO LTI system, the state-

space form is given below:

(1)

(2)
where x is a n by 1 vector representing the state (commonly position and velocity variable in mechanical
systems), u is a scalar representing the input (commonly a force or torque in mechanical systems), and y is a

scalar representing the output. The matrices A (n by n), B (n by 1), and C (1 by n) determine the relationships

between the state and input and output variables. Note that there are n first-order differential equations. State
space representation can also be used for systems with multiple inputs and outputs (MIMO), but we will only use

single-input, single-output (SISO) systems in these tutorials.

To introduce the state space 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 midair. 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 h is the vertical position of the ball, i is the current through the electromagnet, V is the applied

voltage, M is the mass of the ball, g is gravity, L is the inductance, R is the resistance, and K is a coefficient that

determines the magnetic force exerted on the ball. For simplicity, we will choose values M = 0.05 Kg, K =

0.0001, L = 0.01 H,R = 1 Ohm, g = 9.81 m/sec^2. The system is at equilibrium (the ball is suspended

in midair) whenever h = K i^2/Mg (at which point dh/dt = 0). We linearize the equations about the point h

= 0.01 m (where the nominal current is about 7 amp) and get the state space equations:

(5)

(6)
where:

(7)
is the set of state variables for the system (a 3x1 vector), u is the input voltage (delta V), and y (the output), is

delta h. Enter the system matricies into a m-file.

A = [ 0 1 0

980 0 -2.8
0 0 -100 ];

B = [ 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, A, (equivalent to

the poles of the transfer fucntion) determine the stability. The eigenvalues of the A matrix are the values
of s wheredet(sI - A) = 0.

poles = eig(A)

poles =

31.3050

-31.3050

-100.0000

One of the poles is in the right-half plane, (i.e. has positive real part which means that the system is unstable in
open-loop.

To check out what happens to this unstable system when there is a nonzero initial condition, add the following
lines to your m-file and 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).

Controllability and Observability


A system is controllable if there exists a control input, u(t), that transfers any state of the system to zero in finite

time. It can be shown that an LTI system is controllable if and only if its controllabilty matrix, CO, has full rank

(i.e. if rank(CO) = n where n is the number of states ). 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 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 neccesary to estimatethe values of the unknown internal state variables

using only the available system outputs. A system is observable if the initial state, x(t_0), can be determined

from the system output, y(t), over some finite time t_0 < t < t_f. For LTI systems, the system is observable if and

only if the observability matrix, OB, has full rank (i.e. if rank(OB) = n where n is the number of states). 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 (A,B) is controllable if and only if a system

(A',C,B',D) is observable. This fact will be useful when designing an observer, as we shall see below.
Control Design Using Pole Placement
Let's build a controller for this system using pole placement. 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 instance

in this system, we would need a sensor measuring the ball position, another measuring velocity, and a third
measuring current in the electro-magnet.
For simplicity, let's assume the reference is zero, R=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 poles (eigenvalues) of the matrix (A-BK). Since the matrices A and B*K are both 3 by 3 matrices,

there will be 3 poles for the system. By choosing an appropriate K matrix we can place these closed-loop poles
anywhere we want. We can use the MATLAB function place to find the control matrix, K, which will give the

desired poles.

Before attempting this method, we have to decide where we want the closed-loop poles to be. 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 zeta = 0.7 or 45 degrees with sigma = 10 > 4.6*2). The third pole we might

place at -50 to start, and we can change it later depending on what the closed-loop behavior is. 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)')


The overshoot is too large (there are also zeros in the transfer function which can increase the overshoot; you

do not 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 (K) in both cases. In general, the farther you move the poles, the more control
effort it takes.

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 works similarly to place:

K = acker(A,B,[p1 p2 p3])
Introducing the Reference Input
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 K, and then subtract this result from the reference. There is no reason to expect that

K*x will be equal to the desired output. To eliminate this problem, we can scale the reference input to make it
equal to K*x steadystate. This scale factor is often called Nbar; it is introduced as shown in the following

schematic:

We can get Nbar from MATLAB by using 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 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 introduction
of the reference, we simply note the fact that the input is multiplied by this new factor, Nbar:

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.

Observer Design
When we can't measure all the states x (often the case in practice), we can build an observer to estimate them,

while measuring only the output y = C x. For the magnetic ball example, we will add three new, estimated

states 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 y to the estimated output y_hat; this will cause the estimated

states \hat{x} to approach the values of the actual states x. The error dynamics of the observer are given by the

poles of (A-LC).

First, we need to choose the observer gain L. 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, but replacing the matrix B by the matrix C and taking the transposes of each matrix
L = place(A',C',[op1 op2 op3])';

The equations in the block diagram above are given for \hat{x}. It is conventional to write the combined equations
for the system plus observer using the original state x plus the error state: e = x - \hat{x}. We use as state

feedback u = -K \hat{x}. After a little bit of algebra (consult your textbook for more details), we arrive at the

combined state and error equations with the full-state feedback and 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 looks to a nonzero initial condition with no reference input, add the following lines into
your m-file. We typically assume that the observer begins with zero initial condition, \hat{x} = 0. This gives us

that the initial condition for the error is equal to the initial condition of the state.

sys = ss(At,Bt,Ct,0);

lsim(sys,zeros(size(t)),t,[x0 x0]);

title('Linear Simulation Results (with observer)')

xlabel('Time (sec)')

ylabel('Ball Position (m)')


Responses of all the states are plotted below. Recall that lsim gives us x and e; to get \hat{x}, we need to

compute x - e.

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;
% Save state variables explicitly to aid in plotting

h = x(:,1); h_dot = x(:,2); i = x(:,3);

h_est = x_est(:,1); h_dot_est = x_est(:,2); i_est = x_est(:,3);

plot(t,h,'-r',t,h_est,':r',t,h_dot,'-b',t,h_dot_est,':b',t,i,'-g',t,i_est,':g')

xlabel('Time (sec)')

We can see that the observer estimates the states quickly and tracks the states well in the steady-state.
Introduction: Digital Controller Design
In this section we will discuss converting continuous time models into discrete time (or difference equation)

models. We will also introduce the z-transform and show how to use it to analyze and design controllers for
discrete time systems.

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 feedback system that we have been considering so far in this
tutorial. Almost all of the continuous controllers can be built using analog electronics.

The continuous controller, enclosed in the dashed square, can be replaced by a digital controller, shown below,

that performs same control task as the continuous controller. The basic difference between these controllers is

that the digital system operates on discrete signals (or samples of the sensed signal) rather than on continuous
signals.
Different types of signals in the above digital schematic can be represented by the following plots.

The purpose of this Digital Control Tutorial is to show you 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 digital control system contains both discrete

and the continuous portions. When designing a digital control system, we need to find the discrete equivalent of
the continuous portion so that we only need to deal with discrete functions.

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 T 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 Hzoh(z) have only

samples u(k) to work on and produce only samples of output y(k); thus, Hzoh(z) can be realized as a discrete

function.

The philosophy of the design is the following. We want to find a discrete function Hzoh(z) so that for a piecewise

constant input to the continuous system H(s), the sampled output of the continuous system equals the discrete

output. Suppose the signal u(k) represents a sample of the input signal. There are techniques for taking this

sample u(k) and holding it to produce a continuous signal uhat(t). The sketch below shows that the uhat(t) is
held constant at u(k) over the interval kT to (k+1)T. This operation of holding uhat(t) constant over the sampling
time is called zero-order hold.
The zero-order held signal uhat(t) goes through H2(s) and A/D to produce the output y(k) that will be the
piecewise same signal as if the discrete signal u(k) goes through Hzoh(z) to produce the discrete output y(k).

Now we will redraw the schematic, placing Hzoh(z) in place of the continuous portion.

By placing Hzoh(z), we can design digital control systems dealing with only discrete functions.

Note: There are certain cases where the discrete response does not match the continuous response due to a

hold circuit implemented in digital control systems. For information, see Lagging effect associated with the hold.
Conversion Using c2d
There is a MATLAB function called 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 command
for this in MATLAB is sys_d = c2d(sys,Ts,'zoh')

The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth
frequency.

Example: Mass-Spring-Damper
Transfer Function

Suppose you have the following continuous transfer function

(1)

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 an 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


Sample time: 0.01 seconds

Discrete-time transfer function.

State-Space

The continuous time state-space model is as follows:

(2)

(3)

All constants are the same as before. The following m-file converts the above continuous state-space to discrete
state-space.

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

Sample time: 0.01 seconds

Discrete-time state-space model.

From these matrices, the discrete state-space can be written as

(4)

(5)

Now you have the discrete time state-space model.

Stability and Transient Response


For continuous systems, we know that certain behaviors results 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 expression

(6)
 T = Sampling time (sec/sample)

 s = Location in the s-plane

 z = Location in the z-plane

The figure below shows the mapping of lines of constant damping ratio (zeta) and natural frequency (Wn) from
the s-plane to the z-plane using the expression shown above.
If you noticed in the z-plane, the stability boundary is no longer imaginary axis, but is the unit circle z=1. The

system is stable when all poles are located inside the unit circle and unstable when any pole is located outside.

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,

 zeta = Damping ratio

 Wn = Natural frequency (rad/sec)

 Ts = Settling time

 Tr = Rise time

 Mp = Maximum overshoot

Important: The natural frequency (Wn) in z-plane has the unit of rad/sample, but when you use the equations
shown above, the Wn must be in the unit of rad/sec.

Suppose we have the following discrete transfer function


(10)

Create an 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 poles are located approximately at the natural frequency of 9pi/20T (rad/sample) and the

damping ratio of 0.25. Assuming that we have a sampling time of 1/20 sec (which leads to Wn = 28.2 rad/sec)

and using three equations shown above, we can determine that this system should have the rise time of 0.06
sec, a settling time of 0.65 sec and a maximum overshoot of 45% (0.45 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 get the following step response.

sys = tf(numDz,denDz,1/20);

step(sys,2.5);

As you can see from the plot, the rise time, settling time and overshoot came out to be what we expected. This

shows how you can use the locations of poles and the above three equations to analyze the transient response
of the system.

Discrete Root Locus


The root-locus is the locus of points where roots of characteristic equation can be found as a single gain is varied
from zero to infinity. The characteristic equation of an unity feedback system is

(11)

where G(z) is the compensator implemented in the digital controller and Hzoh(z) is the plant transfer function in
z.
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 called sgrid to find the root-locus region that

gives an acceptable gain (K). For the discrete root-locus analysis, we will use the function zgrid that has the

same characteristics as sgrid. The command zgrid(zeta, Wn) draws lines of constant damping ratio (zeta) and
natural frequency (Wn).

Suppose we have the following discrete transfer function

(12)

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 design requirements, sampling time (sec/sample) and 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 an new m-file and enter the following commands. Running this m-file should give you
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, you should realize that the system is stable because all poles are located inside the unit circle.

Also, you see two dotted lines of 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, we do have the root-locus drawn in the desired region. Therefore, a gain (K) chosen from one of
the loci in the desired region should give you the response that satisfies design requirements.
Introduction: Simulink Modeling
In Simulink, it is very straightforward to represent and then simulate a mathematical model representing a

physical system. Models are represented graphically in Simulink as block diagrams. A wide array of blocks are

available to the user in provided libraries for representing various phenomena and models in a range of formats.

One of the primary advantages of employing Simulink (and simulation in general) for the analysis of dynamic

systems is that it allows us to quickly analyze the response of complicated systems that may be prohibitively

difficult to analyze analytically. Simulink is able to numerically approximate the solutions to mathematical models
that we are unable to, or don't wish to, solve "by hand."

In general, the mathematical equations representing a given system that serve as the basis for a Simulink model

can be derived from physical laws. In this page we will demonstrate how to derive a mathematical model and
then implement that model in Simulink. This model is then employed in the Introduction: Simulink Control page

in order to demonstrate how to employ Simulink to design and simulate the control for a system.

Contents

 Train system

 Free-body diagram and Newton's second law

 Constructing the Simulink model

 Running the model


Train system
In this example, we will consider a toy train consisting of an engine and a car. Assuming that the train only travels

in one dimension (along the track), we want to apply control to the train 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.

The mass of the engine and the car will be represented by and , respectively. Furthermore, the engine

and car are connected via a coupling with stiffness . In other words, the coupling is modeled as a spring with a

spring constant . The force represents the force generated between the wheels of the engine and the track,
while represents the coefficient of rolling friction.

Free-body diagram and Newton's second law


The first step in deriving the mathematical equations that govern a physical system is to draw the free-body
diagram(s) representing the system. This is done below for our train system.
From Newton's second law, we know that the sum of the forces acting on a body is equal to the product of the

mass of the body and its acceleration. In this case, the forces acting on the engine in the horizontal

direction are the spring force, the rolling resistance, and the force generated at the wheel/track interface. The

forces acting on the train car in the horizontal direction are the spring force and the rolling resistance. In

the vertical direction, the weight forces are balanced by the normal forces applied by the ground .
Therefore, there will be no acceleration in the vertical direction.

We will model the spring as generating a force that is linearly proportional to the deformation of the

spring, , where and are the displacements of the engine and car, respectively. Here it is

assumed that the spring is undeformed when and equal zero. The rolling resistance forces are modeled

as being linearly proportional to the product of the corresponding velocities and normal forces (which are equal
to the weight forces).

Applying Newton's second law in the horizontal direction based on the above free-body diagrams leads to the
following governing equations for the train system.

(1)

(2)
Constructing the Simulink model
This set of system equations can now be represented graphically without further manipulation. Specifically, we

will construct two copies (one for each mass) of the general expression or . First,

open Simulink and open a new model window. Then drag two Sum blocks (from the Math Operations library)
into your model window and place them approximately as shown in the figure below.
The outputs of each of these Sum blocks represents the sum of the forces acting on each mass. Multiplying each

output signal by will give us the corresponding acceleration of each mass. Now drag two Gain blocks (from

the Math Operations Library) into your model and attach each one with a line from the output of one of the Sum

blocks. Label these two signals as "Sum_F1" and "Sum_F2" in order to make your model more clear. This is
accomplished by double-clicking in the space above each of the two signal lines and entering the desired label.
These Gain blocks should contain for each of the masses. We will define the variables M1 and M2 in the

MATLAB workspace, so we can just enter the corresponding variable names in each of the Gain blocks. Double-
click on the upper Gain block and enter "1/M1" into the Gain field. Similarly, enter "1/M2" in the Gain field of the

second Gain block.


You will notice that the gains did not appear in the image of the Gain blocks, rather the blocks display a value
of -K-. This is because the blocks are too small on the screen to show the full variable name inside the triangle.

The blocks can be resized so that the actual gain value can be seen. To resize a block, select it by clicking on it

once. Small squares will appear at the corners. Drag one of these squares to stretch the block. Your model

should appear as below.


The outputs of these gain blocks are the accelerations of each of the masses (the train engine and car). The

governing equations we derived above depend on the velocities and displacements of the masses. Since velocity

can be determined by integrating acceleration, and position can be determined by integrating velocity, we can

generate these signals employing integrator blocks. Drag a total of four Integrator blocks from the Continuous
library into your model, two for each of our two accelerations. Connect these blocks and label the signals as

shown below. Specifically, the first inegrator takes the acceleration of mass 1 ("x1_ddot") as an input and

generates the velocity of mass 1 ("x1_dot"). The second integrator then takes this velocity and outputs the
displacement of the first mass ("x1"). The same pattern holds for the integrators for the second mass.
Now, drag two Scopes from the Sinks library into your model and connect them to the outputs of these integrators.
Label them "x1" and "x2".
Now we are ready to add the forces acting on each mass. First, we need to adjust the inputs on each Sum block

to represent the proper number of forces (we will worry about the signs later). Since there is a total of three
forces acting on mass 1, double-click on the corresponding Sum block and change the List of signs field to

"|+++". The symbol "|" serves as a spacer. There are only 2 forces acting on mass 2, therefore, we can leave
that Sum block alone for now.
The first force acting on mass 1 is just the input force, . Drag a Signal Generator block from the Sources library
and connect it to the uppermost input of the corresponding Sum block. Label this signal as "F".
The next force acting on mass 1 is the rolling resistance force. Recall that this force is modeled as follows.

(3)

To generate this force, we can tap off the velocity signal and multiply by an appropriate gain. Drag a Gain block

into your model window. Tap off the "x1_dot" signal and connect it to the input of this new Gain block (draw this

line in several steps if necessary). Connect the output of the Gain block to the second input of the Sum block.
Double-click the Gain block and enter "mu*g*M1" into the Gain field. The rolling resistance force, however, acts

in the negative direction. Therefore, change the list of signs of the Sum block to "|+-+". Next, resize the Gain

block to display the full gain and label the output of the Gain block "Frr1". Your model should now appear as

follows.
The last force acting on mass 1 is the spring force. Recall that this force is equal to the following.

(4)

Therefore, we need to generate a signal which we can then be multiplied by a gain to create the

force. Drag a Subtraction block (or a Sum block or an Addition block) below the rest of your model. In order to
change the direction of this block, right-click on the block and choose Format > Flip block from the resulting

menu. Alternatively, you can select the block then hit Ctrl-I. Now, tap off the "x2" signal and connect it to the

negative input of the Subtract block. Also, tap off the "x1" signal and connect it to the positive input. This will

cause signal lines to cross. Lines may cross, but they are only actually connected where a small block appears

(such as at a tap point).


Now, we can multiply this difference by the spring constant to generate the spring force. Drag a Gain block into

your model to the left of the Subtraction block. Change the value of the Gain block to "k" and connect the output

of the Subtract block to its input. Then connect the output of the Gain block to the third input of the Sum block

for mass 1 and label the signal "Fs". Since the spring force acts on mass 1 in the negative direction, it is necessary
to again change the list of signs of the Sum block to "|+--". Your model should appear as follows.
We can now apply forces to mass 2. For the first force, we will use the same spring force we just generated,

except that it is applied to mass 2 in the positive direction. Simply tap off the spring force signal "Fs" and connect
it to the first input of the Sum block for mass 2.
The last force applied to mass 2 is its rolling resistance force. This force is generated in an analogous manner

to the rolling resistance force applied to mass 1. Tap off the signal "x2_dot" and multiply it by a Gain block with

value "mu*g*M2". Then connect the output of the Gain block to the second input of the corresponding Sum block

and label the signal "Frr2". Changing the second input of the Sum block to be negative will lead to the following
model.
Now the model is complete. We simply need to supply the proper input and define the output of interest. The

input to the system is the force generated by the engine. Within the Simulink model, we have already defined

the force to be the output of a Signal Generator block. The output of the system, which we will observe and

ultimately try to control, will be the velocity of the train engine. Add another Scope block to your model from the
Sinks library. Tap a line from the "x1_dot" signal and connect it to the Scope block. Label this scope as "x1_dot"
and your model should appear as in the following.
Now, the model is complete and should be saved. You can also download the completed model here.
Running the model
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

Create a new m-file and enter the following commands.

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.

Now, we need to give an appropriate input to the engine. Double-click on the Signal Generator block (outputs
"F"). Select square from the Wave form drop-down menu and set the Frequency field to equal "0.001". You

may leave the Units as the default Hertz. Also enter "-1" into the Amplitude field (positive amplitude steps

negative before stepping positive).

The last step before running the simulation is to select an appropriate simulation time. To view one cycle of the
0.001 Hz square wave, we should simulate the model for 1000 seconds. Select Parameters from

the Simulation menu at the top of the model window and change the Stop Time field to "1000". Close the dialog

box.
Now, run the simulation and open the "x1_dot" scope to examine the velocity output (hit autoscale). The input

was a square wave with two steps, one positive and one negative. Physically, this means that the engine first
went forward, then backward. The velocity output reflects this.

In this page we derived the mathematical model for the train system from first principles and then represented

the derived equations within Simulink. An alternative option that is now available is to represent a dynamic

system employing the physical modeling tool Simscape. Simscape is an addition to Simulink that allows a user

to model a system employing blocks that represent physical quantities (and objects) such as inertias and joints,

or resistances and inductors. Employing Simscape allows the user to simulate a physical system without deriving
the governing mathematical equations.

Continuing on to the Introduction: Simulink Control page, we will employ the model derived in this page to
demonstrate how to use Simulink to design the control for our train system.
Introduction: Simulink Control

Contents

 The open-loop plant model

 Implementing a PID controller in Simulink

 Running the closed-loop model

 Extracting a model into MATLAB

 Controller design within Simulink


The open-loop plant model
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 here. 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.
Implementing a PID controller in Simulink
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 the Edit menu at the top

of 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 feedback the engine's velocity. This is accomplished
by 4ring 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 toytrain). 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 theT 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 here.


Running the closed-loop model
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

Create a new m-file and enter the following commands.

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 Parameters from theSimulation 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 (hit

autoscale). The result as shown below demonstrates that the closed-loop system is unstable for this controller.

Since the performance achieved above is unsatisfactory, we need 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.

Extracting a model into MATLAB


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 Linearization Points > Input Point from the resulting menu. Likewise, we can

designate the output of the train system by right-clicking on the "x1_dot" signal and choosing Linearization

Points > Output Point 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 Tools 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 Linearize button in the above figure, which is indicated by the 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 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);

pole(sys_cl)

ans =

-1.5261

0.0000

0.0670 + 1.1977i

0.0670 - 1.1977i

Examination of the above demonstrates that the closed-loop system in its current state has poles with positive

real part and, therefore, is unstable. This agrees with the result of our closed-loop simulation from above. We

can then employ MATLAB to design a new controller. Instead, we will demonstrate how to access some of
MATLAB's functionality from directly within Simulink.

Controller design within Simulink


Rather than performing the controller design in MATLAB, we can also 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 GUI. Rather than do this, will launch the more

general Simulink Control Design GUI by selecting Linear Analysis > Compensator Design from under

the Tools menu located at the top of the model window. Following these steps will open the Control and

Estimation Tools Manager 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 Select 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 in 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 the same manner we did when extracting a model into MATLAB. Specifically,
right-click on the velocity command signal (output of the Signal Builder block) and choose Linearization Points

> Input Point from the resulting menu to identify the input of our closed-loop system. Similarly, right-click on the

train engine velocity signal ("x1_dot") and select Linearization Points > Output Point 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 Tune Blocks button in theControl and Estimation Tools Manager window.

This will open the Design Configuration Window shown below which provides some introduction on how to

employ the interactive design tool. In essence, this GUI is the SISO Design Tool that is available from within

MATLAB as well.
Clicking the Next 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 choose a Plot Type of Root

Locus for Plot 1 as shown below. Since the root locus approach to design employs a plot from the open-loop

system for placing the closed-loop poles, we will leave the choice of Open/Closed Loops as Open Loop 1 (this

is our only choice!).


Clicking the Next button again will allow us to choose our analysis plots. We use the step response plot to assess

how well we are able to meet our goal of bringing the train up to speed smoothly with minimal steady-state error
to a constant speed command. Therefore, we will choose a Plot Type of Step from the drop-down menu

underPlot 1 as shown below. We will also select Plot 1 under the Plot Contents portion of the window for the

only system that is defined. There is only one system available because we have defined only a single input and

single output for our system.


Selecting the Next Button will then open the SISO Design Tool with a root locus plot and a step response plot.

The root locus plot shown below displays the closed-loop pole locations of the train system plant under simple

proportional control. Examining the plot, one can see that many values of loop gain will place closed-loop poles

in the right-half plane leading to an unstable response.


If we decrease the loop gain sufficiently, we can move the closed-loop poles into the left-half plane and we can

stabilize 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). A loop gain of

approximately 0.1 will stabilize the system. Examining the corresponding step response, which will change
automatically in response to the gain change if the Real-Time Update box is checked in the LTI Viewer window,

you can see that while the response is stable its steady-state error is quite large.
Recall that adding integral control is one way to reduce the steady-state error for a 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)

Therefore, 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 where along the real axis you wish to place the zero.

We will place the zero just to the right of the plant pole on the real axis. You can move the zero by clicking on it

and dragging it to a new location. Once you have placed the zero, then grab the pink boxes representing the

closed-loop poles and attempt to line the three dominant poles up so that they have the same real part. There

also will be a real closed-loop pole to the left that is "faster" than the rest, and a closed-loop pole at the origin
that is cancelled by a closed-loop zero at the origin. The resulting root locus plot is shown below.
The compensator can also be edited by directly typing in pole and zero locations. This can be done by
choosing Edit Compensator from the Design menu located at the top of the SISO Design Task window. The

window that opens is shown below. We will more precisely place the zero at -0.15 and will choose a loop gain

equal to 0.01.
The resulting closed-loop step response plot is shown below demonstrating that the train engine is brought to
speed 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

Simulink Block Parameters button under the Compensator Editor tab of the Control and Estimation Tools

Manager window (see above). The simulation can then be run with this newly tuned controller. Clicking on the

Scope block for the train engine's velocity and selecting autoscale 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 to rest smoothly, while
maintaining minimal steady-state error. This response matches the result generated with the SISO Design

Tool above because that analysis and the Simulink model used the exact same linear model.
Cruise Control: Simulink Modeling

Contents

 Physical setup and system equations

 Building the model

 Open-loop response
Physical setup and system equations
The model of the cruise control system is relatively simple. If it is assumed that rolling resistance and air drag

are proportional to the car's speed, then the problem is reduced to the simple mass and damper system shown
below.

Using Newton's 2nd law, the governing equation for this system becomes:

(1)

where u is the force generated between the road/tire interface and can be controlled directly. For this example,
let's assume that

m = 1000 kg

b = 50 N.sec/m

u = 500 N

Building the model


This system will be modeled by summing the forces acting on the mass and integrating the acceleration to give
the velocity. Open Simulink and open a new model window. First, we will model the integral of acceleration.
(2)

 Insert an Integrator Block (from the Continuous library) and draw lines to and from its input and output
terminals.

 Label the input line "vdot" and the output line "v" as shown below. To add such a label, double click in the
empty space just above the line.

Since the acceleration (dv/dt) is equal to the sum of the forces divided by mass, we will divide the incoming signal
by the mass.

 Insert a Gain block (from the Math Operations library) connected to the Integrator block input line and draw
a line leading to the input of the Gain block.

 Edit the Gain block by double-clicking on it and change its value to "1/m".

 Change the label of the Gain block to "inertia" by clicking on the word "Gain" underneath the block.
Now, we will add in the forces which are represented in Equation (1). First, we will add in the damping force.

 Attach a Sum block (from the Math Operations library) to the line leading to the inertia Gain block.

 Change the signs of the Sum block to "+-".

 Insert a Gain block below the Inertia block, select it by single-clicking on it, and select Flip from the Format
menu (or type Ctrl-F) to flip it left-to-right.

 Set the block's value to "b" and rename this block to "damping".

 Tap a line (hold Ctrl while drawing) off the Integrator block's output and connect it to the input of the damping
Gain block.

 Draw a line from the damping Gain block output to the negative input of the Sum Block.
The second force acting on the mass is the control input, u. We will apply a step input.

 Insert a Step block (from the Sources library) and connect it with a line to the positive input of the Sum Block.

 To view the output velocity, insert a Scope block (from the Sinks library) connected to the output of the
Integrator.
 To provide an appropriate step input of 500 at time equal zero, double-click the Step block and set the Step
Time to "0" and the Final Value to "u".
You can download a model file for the complete system here, ccmodel.mdl.
Open-loop response
To simulate this system, first, an appropriate simulation time must be set.

 Select Parameters from the Simulation menu and enter "120" in the Stop Time field. 120 seconds is long
enough to view the open-loop response.
The physical parameters must now be set. Run the following commands at the MATLAB prompt:

m = 1000;

b = 50;

u = 500;
Run the simulation (hit Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-
click on the Scope and hit its autoscale button. You should see the following output.

Observing the above, we would like to improve the response of the cruise control system. The model created

here will be employed for controller design and analysis within Simulink in the Cruise Control: Simulink Controller

Design page.
Cruise Control: System Analysis
Key MATLAB commands used in this tutorial are: ss , step

Contents

 System model and parameters

 Performance specifications

 Open-loop step response

 Open-loop poles/zeros

 Open-loop Bode plot


System model and parameters
The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System

Modeling page for the derivation.

(1)

The parameters used in this example are as follows:

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(u) nominal control force 500 N

Performance specifications
The next step is to come up with some design criteria that the compensated system should achieve. When the

engine gives a 500 Newton force, the car will reach a maximum velocity of 10 m/s (22 mph), see open-loop step

response section below. An automobile should be able to accelerate up to that speed in less than 5 seconds. In

this application, a 10% overshoot and 2% steady-state error on the velocity are sufficient.
Keeping the above in mind, we have proposed the following design criteria for this problem:

 Rise time < 5 s

 Overshoot < 10%

 Steady-state error < 2%


Open-loop step response
The open-loop response of the system, without any feedback control, to a step input force of 500 Newtons is

simulated in MATLAB as follows:


m = 1000;

b = 50;

u = 500;

s = tf('s');

P_cruise = 1/(m*s+b);

step(u*P_cruise)

We see that the open-loop system exhibits no overshoot or oscillations (characteristic of first-order systems),

and does reach the desired steady-state speed of 10 m/s; however, the rise time is much too slow, ~60 s.

Therefore we need to design a feedback controller which speeds up the response significantly without negatively
affecting the other dynamic performance metrics.

Open-loop poles/zeros
The cruise control system has a single pole at s = -b/m which we can see plotted on the s-plane using the
following MATLAB commands:

pzmap(P_cruise)

axis([-1 1 -1 1])

We observe that the open-loop system is stable and does not oscillate since the pole is real and negative.
Furthermore, the speed of response is determined by the magnitude of this pole, b/m: the larger the magnitude,

the quicker the system approaches the steady-state value. Since we're typically not able to change the system

parameters to change the dynamic response of the system, we must instead design controllers which alter the
poles and zeros of the closed-loop system to meet the desired performance specifications.
Open-loop Bode plot
We are also interested in the open-loop frequency response of the system which we find using the following
MATLAB command:

bode(P_cruise)
We see that the Bode plots exhibit the definitive features of first-order systems, including a -3 dB magnitude and

-45 deg phase at the corner frequency of w = b/m = 0.05 rad/s and -20 dB/dec roll-off at high frequencies.
Cruise Control: PID Controller Design
Key MATLAB commands used in this tutorial are: tf , step , feedback

Contents

 System model and parameters

 Performance specifications

 PID overview

 Proportional control

 PI control

 PID control
System model and parameters
The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System

Modeling page for the derivation.

(1)

The parameters used in this example are as follows:

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(r) reference speed 10 m/s

Performance specifications
 Rise time < 5 s

 Overshoot < 10%

 Steady-state error < 2%


PID overview
The block diagram of a typical unity feedback system is shown below.
Recall from the Introduction: PID Controller Design page, the transfer function of a PID controller is

(2)

We can define a PID controller in MATLAB using the transfer function directly:

Kp = 1;

Ki = 1;

Kd = 1;

s = tf('s');

C = Kp + Ki/s + Kd*s

C =

s^2 + s + 1

-----------

Continuous-time transfer function.

Alternatively, we may use MATLAB's pid controller object to generate an equivalent continuous time controller

as follows:

C = pid(Kp,Ki,Kd)

C =
1

Kp + Ki * --- + Kd * s

with Kp = 1, Ki = 1, Kd = 1

Continuous-time PID controller in parallel form.

Proportional control
The first thing to do in this problem is to find a closed-loop transfer function with a proportional control (C = Kp)
added.

By reducing the unity feedback block diagram, the closed-loop transfer function with a proportional controller
becomes:

(3)

Recall from the Introduction: PID Controller Design page, a proportional controller, Kp, decreases the rise time,

which is desirable in this case.

For now, use Kp equal 100 and a reference speed of 10 m/s. Create a new m-file and enter the following

commands.

m = 1000;

b = 50;

r = 10;
s = tf('s');

P_cruise = 1/(m*s + b);

Kp = 100;

C = pid(Kp);

T = feedback(C*P_cruise,1)

t = 0:0.1:20;

step(r*T,t)

axis([0 20 0 10])

T =

100

------------

1000 s + 150

Continuous-time transfer function.


Note that we have used the MATLAB feedback command to simplify the block diagram reduction of the closed-

loop system. Please verify for yourself that the result agrees with the closed-loop transfer function, T, derived

above.

Running the m-file in MATLAB should give you the step response above. As you can see from the plot, neither
the steady-state error nor the rise time satisfy our design criteria.

You can increase the proportional gain, Kp, to reduce the rise time and the steady-state error. Change the
existing m-file so that Kp equals 5000 and rerun it in the MATLAB command window. You should see the
following plot.

Kp = 5000;

C = pid(Kp);

T = feedback(C*P_cruise,1);

step(r*T,t)
axis([0 20 0 10])

The steady-state error is now essentially zero, and the rise time has been reduced substantially. However, this

response is unrealistic because a real cruise control system generally can not change the speed of the vehicle
from 0 to 10 m/s in less than 0.5 seconds due to power limitations of the engine and drivetrain.

Actuator limitations are very frequently encountered in practice in control systems engineering, and

consequently, the required control action must always be considered when proposing a new controller. We will

discuss this issue much more in subsequent tutorials.

The solution to this problem in this case is to choose a lower proportional gain, Kp, that will give a reasonable
rise time, and add an integral controller to eliminate the steady-state error.

PI control
The closed-loop transfer function of this cruise control system with a PI controller (C = Kp + Ki/s) is:

(4)
Recall from the Introduction: PID Controller Design page, an addition of an integral controller to the system

eliminates the steady-state error. For now, let Kp equal 600 and Ki equal 1 and see what happens to the response.

Change your m-file to the following.

Kp = 600;

Ki = 1;

C = pid(Kp,Ki);

T = feedback(C*P_cruise,1);

step(r*T,t)

axis([0 20 0 10])
Now adjust both the proportional gain, Kp, and the integral gain, Ki, to obtain the desired response. When you

adjust the integral gain, Ki, we suggest you to start with a small value since a large Ki can destabilize the
response. When Kp equals 800 and Ki equals 40, the step response will look like the following:

Kp = 800;

Ki = 40;

C = pid(Kp,Ki);

T = feedback(C*P_cruise,1);

step(r*T,t)

axis([0 20 0 10])
PID control
For this particular example, no implementation of a derivative controller was needed to obtain the required output.

However, you might want to see how to work with a PID control for the future reference. The closed-loop transfer
function for this cruise control system with a PID controller (C = Kp + Ki/s + Kd*s) is:

(5)

Let Kp equal 1, Ki equal 1, and Kd equal 1 and enter the following commands into an new m-file.

Kp = 1;

Ki = 1;

Kd = 1;

C = pid(Kp,Ki,Kd);
T = feedback(C*P_cruise,1);

Plot the step response and adjust all of Kp, Kd, and Ki until you obtain satisfactory results. We will leave this as
an exercise for you to work on.

Suggestion: Usually choosing appropriate gains requires a trial and error process. The best way to attack this

tedious process is to adjust one variable (Kp, Ki, or Kd) at a time and observe how changing one variable

influences the system output. The characteristics of Kp, Ki, and Kd are summarized in the Introduction: PID

Controller Designpage.
Cruise Control: Root Locus Controller
Design
Key MATLAB commands used in this tutorial are: tf , rlocus , feedback , step

Contents

 System model

 System parameters

 Performance specifications

 Proportional control

 Lag controller
System model
The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System

Modeling page for the derivation.

(1)
System parameters
For this example, let's assume that the parameters of the system are

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(r) reference speed 10 m/s

and the block diagram of a typical unity feedback system is shown below.

Performance specifications
 Rise time < 5 sec

 Overshoot < 10%


 Steady-state error < 2%
Proportional control
Recall from the Introduction: Root Locus Controller Design page, the root-locus plot shows the locations of all

possible closed-loop poles when a single gain is varied from zero to infinity. Thus, only a proportional controller,

K_P, will be considered to solve this problem. The closed-loop transfer function becomes:

(2)

Also, from the Introduction: Root Locus Controller Design page, we know that the MATLAB
command sgrid can be used to display an acceptable region of the root-locus plot. To use the sgrid, both the

damping ratio, zeta, and the natural frequency, Wn, need to be determined first. The following two equations will

be used to find the damping ratio and the natural frequency:

(3)

(4)

where

 wn = Natural Frequency [rad\s]

 zeta = Damping Ratio

 Tr = Rise time [s]

 Mp = Maximum Overshoot

One of our design criteria is to have a rise time of less than 5 seconds. From the first equation, we see that the

natural frequency must be greater than 0.36. Also using the second equation, we see that the damping ratio
must be greater than 0.6, since the maximum overshoot must be less than 10%.

Now, we are ready to generate a root-locus plot and use the sgrid to find an acceptable region on the root-

locus. Create a new m-file and enter the following commands.

m = 1000;

b = 50;

r = 10;

s = tf('s');
P_cruise = 1/(m*s+b);
rlocus(P_cruise)

axis([-0.6 0 -0.6 0.6]);

sgrid(0.6, 0.36)

The two dotted lines in an angle indicate the locations of constant damping ratio (zeta=0.6); the damping ratio is

greater than 0.6 in between these lines and less than 0.6 outside the lines. The semi-ellipse indicates the

locations of constant natural frequency (Wn=0.36); the natural frequency is greater than 0.36 outside the semi-
ellipse, and smaller than 0.36 inside.

We can then find a gain to place the closed-loop poles in the desired region by employing
the rlocfind command. Add the code [Kp,poles]=rlocfind(P_cruise)onto the end of your m-file to

help you choose a specific loop gain. After running in the command window, you should see a prompt asking

you to pick a point on the root-locus plot. Since you want to pick a point in between dotted lines (zeta>0.6) and

outside the semi-ellipse (Wn>0.36), click on the real axis just outside the semi-ellipse (around -0.4) as indicated

by the cross mark in the following figure.


After doing this, you should see the following output in the MATLAB command window.

Select a point in the graphics window

selected_point =

-0.4002 + 0.0019i

Kp =

350.2419
poles =

-0.4002

Note that the value returned from your MATLAB command window may not be exactly the same, but should at

least have the same order of magnitude. This returned value can be used as the gain for the compensator and
the closed-loop step response can be generated as follows.

Kp = 350.2419;

sys_cl = feedback(Kp*P_cruise,1);

t = 0:0.1:20;

step(r*sys_cl,t)
With the gain Kp you just chose, the rise time and the overshoot criteria have been met; however, a steady-state
error of more than 10% remains.

Lag controller
To reduce the steady-state error, a lag controller will be added to the system. The transfer function of the lag
controller is:

(5)

The closed-loop transfer function (not including Kp) now becomes:

(6)

Finally, including the loop gain Kp, the closed-loop transfer function becomes:

(7)

If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in the Lead and Lag Compensator

Design page, the pole and the zero of a lag controller need to be placed close together. Also, it states that the

steady-state error will be reduced by a factor of zo/po. For these reasons, let zo equal 0.3 and po equal 0.03.
Create a new m-file, and enter the following commands.

zo = 0.3;

po = 0.03;

s = tf('s');

C_lag = (s+zo)/(s+po);

rlocus(C_lag*P_cruise);

axis([-0.6 0 -0.4 0.4])

sgrid(0.6,0.36);
Using the rlocfind command again, we can choose a new loop gain Kp. Enter the

code [Kp,poles]=rlocfind(C_lag*P_cruise) into the command window and click on the real axis

around -0.4 as shown in the following figure.


After doing this, you should see the following output in the MATLAB command window.

Select a point in the graphics window

selected_point =

-0.4002 - 0.0012i

Kp =

1.2936e+03
poles =

-0.9733

-0.4003

We can then generate the new closed-loop step response as follows.

Kp = 1293.6;

sys_cl = feedback(Kp*C_lag*P_cruise,1);

t = 0:0.1:20;

step(r*sys_cl,t)

axis([0 20 0 12])
As you can see, the steady-state error has been reduced to near zero. The overshoot is a result of the zero

added in the lag controller. For now all of the design criteria have been met and no further iterations are needed;

however, you should experiment with different zo and po values to see what their effect is on the closed-loop
system response.
Cruise Control: Frequency Domain
Methods for Controller Design
Key MATLAB commands used in this tutorial are: tf , feedback , step

Contents

 System model

 System parameters

 Performance specifications

 Bode plot and open-loop response

 Proportional controller

 Lag compensator
System model
The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System

Modeling page for the derivation.

(1)
System parameters
For this example, let's assume that the parameters of the system are

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(r) reference speed 10 m/s

(u) nominal control force 500 N

and the block diagram of an typical unity feedback system is shown below.

Performance specifications
 Rise time < 5 sec

 Overshoot < 10%

 Steady-state error < 2%


Bode plot and open-loop response
The first step in solving this problem using frequency response is to determine what open-loop transfer function

to use. Just like for the Root-Locus design method, we will only use a proportional controller to solve the problem.
The block diagram and the open-loop transfer function are shown below.

(2)

In order to use a Bode plot, the open-loop response must be stable. Let Kp equal 1 for now and see how the

open-loop response looks like. Create a new m-file and enter the following commands.

m = 1000;

b = 50;

u = 500;

Kp = 1;

s = tf('s');

P_cruise = 1/(m*s+b);

C = Kp;

step(u*C*P_cruise)
As you can see, the open-loop system is stable; thus, we can go ahead and generate the Bode plot. Change the
above m-file by deleting the step command and adding in the following command.

bode(C*P_cruise);
Proportional controller
Refer to the Introduction: Frequency Domain Methods for Controller Design page, and let's see what system

characteristics we can determine from the above Bode plot.


The steady-state error can be found from the following equation:

(3)

For this system, the low frequency gain is -34dB = 0.02; therefore, the steady-state error should be 98%. We
can confirm this by generating a closed-loop step response as follows.

r = 10;

sys_cl = feedback(C*P_cruise,1);

step(r*sys_cl);
We need to increase the low frequency gain in order to improve the steady-state error. Specifically, the error

needs to be < 2%; therefore, 1/(1+M_{w=0}) < 0.02 -> M_{w=0} > 49 = 33.8 dB. So to reach the desired steady-

state error using proportional control only requires a Kp > 67.8 dB = 2455. Let's look at the Bode diagram of the
compensated open-loop system.

Kp = 2500;

C = Kp;

bode(C*P_cruise);
As you can see from the Bode plot above, the low frequency magnitude is now, 34 dB. Now let's simulate the
step response of the closed loop system with this gain.

sys_cl = feedback(C*P_cruise,1);

step(r*sys_cl);
The steady-state error meets the requirements; however, the rise time is much shorter than is needed and is

unreasonable in this case since the car can not accelerate to 10 m/s in 2 sec. Therefore, we will try using a

smaller proportional gain to reduce the control action required along with a lag compensator to reduce the steady-
state error.

Lag compensator
If you take a look at the "Lag or Phase-Lag Compensator using Frequency Response" section of the Lead and

Lag Compensator Design page, the lag compensator adds gain at the low frequencies while keeping the

bandwidth frequency at the same place. This is actually what we need: Larger low frequency gain to reduce the

steady-state error and keep the same bandwidth frequency to maintain the desired rise time. The transfer

function of the lag controller is:

(4)

If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in Lead and Lag Compensator

Design page, the pole and the zero of a lag controller need to be placed close together. Also, it states that the

steady-state error will be reduce by a factor of zo/po. For these reasons, let zo equal 0.1 and po equal 0.02. The

proportional gain, Kp = 1000 was chosen by trial-and-error.

Kp = 1000;
zo = 0.1;

po = 0.02;

C_lag = (s+zo)/(s+po);

bode(Kp*C_lag*P_cruise);

Let's confirm the performance by generating a closed-loop step response.

sys_cl = feedback(Kp*C_lag*P_cruise,1);

t = 0:0.1:20;

step(r*sys_cl,t);
As you can see, there is a very slight overshoot, the steady state error is close to zero, and the rise time is under
5 seconds. The system has now met all of the design requirements. No more iteration is needed.
Cruise Control: State-Space Methods for
Controller Design
In this tutorial we will design a controller and observer for the cruise control system using the state-space model.

Key MATLAB commands used in this tutorial are: ss , feedback

Contents

 State-space equations

 Design requirements

 Control design using pole placement


 Reference input
State-space equations
The equations of motion in state-space form are as follows:

(1)

(2)
where

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(u) nominal control force 500 N

(v) vehicle velocity where y=v is the system output

Design requirements
 Rise time < 5 s

 Overshoot < 10%

 Steady-state error < 2%

To see the original problem setup, see the Cruise Control: System Modeling page.
Control design using pole placement
The schematic of a full state-feedback system is shown below.
(3)

where

 K = state-feedback gain matrix

 u = r-K.v = control input

Recall from the State-Space Tutorial page, we can use a "pole placement" technique to obtain the desired output.

Poles of a closed-loop system can be found from the characteristic equation: the determinant of the [sI-(A-B*K)]

matrix. If the poles the system can be placed in the desired location by designing an appropriate control matrix

(K), then the desired output can be obtained. In this tutorial, poles will be chosen first, then we will use MATLAB

to find the corresponding control matrix (K).

Now, we need to determine where to place poles for our system. Since our [sI-(A-B*K)] matrix is 1x1, we have

only one pole to place. Let the pole be at -1.5 (arbitrary). Just as in the State-Space Tutorial, the MATLAB
command place will be used to find the control matrix K. Create a new m-file and enter the following commands.

Running the m-file in the MATLAB command window should give you the control matrix and step response

shown below.

m = 1000;

b = 50;

t = 0:0.1:10;

u = 500*ones(size(t));

A = [-b/m];
B = [1/m];

C = [1];

D = [0];

sys = ss(A,B,C,D);

x0 = [0];

p1 = -1.5;

K = place(A,B,[p1])

sys_cl = ss(A-B*K,B,C,D);

lsim(sys_cl,u,t,x0);

axis([0 10 0 0.35])

K =

1450
As you can see, the rise time is satisfactory, but the steady-state error is too large.

Reference input
Once again from the State-Space Tutorial page, a scaling factor called Nbar (the schematic is shown below) can

be used to eliminate the steady-state error. We can use the rscale function to compute the scaling factor.

Download it here, rscale.m. The input is already multiplied by 500, and we want the steady-state speed to be 10

m/sec, so we need to account for these factors as well.

Copy the following commands to an m-file and run it in the MATLAB command window. You should get the step
response shown below.

Nbar = rscale(sys,K)*10/500;
sys_cl = ss(A-B*K,B*Nbar,C,D);

lsim(sys_cl,u,t,x0);

axis([0 10 0 11])

As you can see, the steady-state error has been eliminated. The rise time is less than 5 seconds and the
overshoot is, in fact, zero. All the design requirements are satisfied.
Cruise Control: Digital Controller Design
In this digital control version of the cruise control problem, we are going to use the root-locus design method to
design the digital controller.

Key MATLAB commands used in this tutorial are: tf , c2d , rlocus , zgrid , feedback , step

Contents

 System model

 System parameters

 Performance specifications

 Discrete-time transfer function

 Root locus in the z-Plane

 Compensation using a digital controller


System model
The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System

Modeling page for the derivation.

(1)
System parameters
For this example, let's assume that parameters of the system are

(m) vehicle mass 1000 kg

(b) damping coefficient 50 N.s/m

(r) reference speed 10 m/s

(u) nominal control force 500 N

Performance specifications
We need to design a controller to meet the following design criteria for this problem:

 Rise time < 5 s

 Overshoot < 10%

 Steady-state error < 2%


Discrete-time transfer function
The first step in performing a discrete analysis of a system is to find the discrete-time equivalent transfer function

of the continuous portion. We will convert the above transfer function (Y(s)/U(s)) to a discrete-time transfer
function using the MATLAB function c2d. To use this function, you need to specify three arguments: system,

sampling time (Ts), and the 'method'. You should already be familiar with how to enter num and den matrices.

The sampling time (Ts), in the unit of sec/sample, should be smaller than 1/(30*BW), where BW is the closed-
loop bandwidth frequency. For the method, we will use the zero-order hold ('zoh').

Let the sampling time equal 1/50 sec; this is sufficiently fast assuming that the bandwidth frequency is 1 rad/sec.

Now enter the following commands into an m-file and run it in the command window.

m = 1000;

b = 50;

u = 500;

s = tf('s');

P_cruise = 1/(m*s+b);

Ts = 1/50;

dP_cruise = c2d(P_cruise,Ts,'zoh')

dP_cruise =

1.999e-05

---------

z - 0.999

Sample time: 0.02 seconds


Discrete-time transfer function.

Root locus in the z-Plane


Recall from the Digital Control Tutorial page, the MATLAB function zgrid can be used to find an acceptable

region of the discrete root-locus that gives the desired gain (K). The zgrid command requires two arguments:

Natural frequency (Wn) and damping ratio (zeta). These two arguments can be found from the rise time and the

overshoot requirements and the following two equations.

(2)

(3)

where,

 zeta = Damping ratio

 Wn = Natural frequency (rad/sec)

 Tr = Rise time

 %OS = Maximum overshoot

Since our rise time and overshoot requirements are 5 seconds and 10%, respectively, we can determine that

the natural frequency (Wn) must be greater than 0.36 rad/sec and the damping ratio (zeta) must be greater than
0.6.

Let's generate the root-locus and use the zgrid command to find the acceptable region of the root-locus. But

before doing that, if you refer to the Digital Control Tutorial, the natural frequency argument for zgrid needs to

be in units of rad/sample, so let Wn = 0.36*Ts = 0.0072 rad/sample. Now add the following commands to the
above m-file and rerun it. You should get the following plot.

Wn = 0.0072;

zeta = 0.6;

rlocus(dP_cruise)

zgrid(zeta, Wn)
axis ([-1 1 -1 1])

The region of the complex plane which interests us is that which is near the point (1,0), so you should zoom in
on this point. Rerun using the following axis command and your figure should now appear as shown below.

axis ([0.95 1 -.1 .1])


The dotted line on the right, indicates the locations of constant natural frequency (Wn), and the natural frequency

is greater than 0.0072 outside the line. The other dotted line indicates the locations of constant damping ratio

(zeta), and the damping ratio is greater than 0.6 inside the line. The jagged vertical line is a portion of the unit
circle which is calculated at low resolution (hence the jaggedness).

In the above plot, you can see that part of the root locus is inside the desired region. Let's find a specific gain (K)
using the MATLAB function rlocfind and then obtain the corresponding step response. Running the

command [K,poles] = rlocfind(dP_cruise) in the MATLAB command window will generate a prompt

directing you to select a point on the root locus. Remember that if you choose a pole which is too far inside the

unit circle, then the step response will be too fast indicating a physically unreasonable acceleration. Therefore,

you should choose the pole which is near the intersection of the constant natural frequency and the real axis.

Select the point near 0.99 as shown by the cross mark on the following root locus graph.
After selecting a point, you should see the following output in the MATLAB command window indicating the point

you chose, the point on the root locus closest to that point (the pole), and the gain K that places the closed-loop
pole at that location.

Select a point in the graphics window

selected_point =

0.9900 - 0.0003i

K =

451.1104
poles =

0.9900

Then to see the closed-loop step response, add the following code to your m-file.

K = 451.1104;

sys_cl = feedback(K*dP_cruise,1);

r = 10;

figure

step(r*sys_cl,10);
This response satisfies the rise time and overshoot requirements. But the steady-state error is about 11%. To
obtain the desired steady-state error, we will modify the digital controller.

Compensation using a digital controller


Recall from the continuous Cruise Control: Root Locus page, a lag compensator was added to the system to

obtain the desired response. In this digital control version of the cruise control problem, we will modify the existing

digital controller by adding additional lag compensation of the form shown below.

(4)

There is a guideline for the design digital lead and lag compensators and a guideline for the design of continuous-

time lead and lag compensators. The discrete design method described says that the zero of the lag

compensator should be chosen to (approximately) cancel one of the plant poles, as long as it is stable. Thus,

we choose the zero to be at zo = 0.999.

To reduce the steady-state error, we note that the low frequency gain of the discrete-time control system with a

lag compensator is increased by a factor of (1 - zo)/(1 - po). To reduce the steady-state error by a factor of 5, we

choose po = 0.9998. To have a gain of 1 at zero frequency, the numerator is multiplied by Kd = (1 - zp)/(1 - zo)

= 0.2 before using the root locus. Note that the whole compensator is multiplied by the loop gain determined
from the root locus.

Now we have the discrete-time compensator transfer function. Let's generate the root-locus and obtain the step
response. First create a new m-file and enter the following commands.

m = 1000;

b = 50;

u = 500;

s = tf('s');

P_cruise = 1/(m*s+b);

Ts = 1/50;

dP_cruise = c2d(P_cruise,Ts,'zoh');

z = tf('z',Ts);
C = 0.2*(z - 0.999)/(z - 0.9998);
Wn = 0.0072;

zeta = 0.6;

rlocus(C*dP_cruise)

zgrid(zeta, Wn)

axis([0.98 1 -0.01 0.01])

Then enter the command [K,poles] = rlocfind(C*dP_cruise) at the command line and again choose

a pole location near 0.99 as indicated by the cross marks in the following figure.
After selecting a point, you should see the following output in the MATLAB command window indicating the point

you chose, the point on the root locus closest to that point (the pole), and the gain K that places the closed-loop
pole at that location.

Select a point in the graphics window

selected_point =

0.9900 - 0.0000i

K =

2.4454e+03
poles =

0.9900

0.9900

Finally, to see the closed-loop step response, add the following code to your m-file.

K = 2.4454e+03;

sys_cl = feedback(K*C*dP_cruise,1);

r = 10;

step(r*sys_cl,10);
This response rises about as fast as before, but the steady-state error has been reduced to 2%. This system
satisfies all of the design requirements while requiring a reasonable amount of control effort.

Note: A design problem does not necessarily have a unique answer. For practice, you may try other

compensators to obtain a better response than the one shown above.


Cruise Control: Simulink Modeling

Contents

 Physical setup and system equations

 Building the model

 Open-loop response
Physical setup and system equations
The model of the cruise control system is relatively simple. If it is assumed that rolling resistance and air drag

are proportional to the car's speed, then the problem is reduced to the simple mass and damper system shown
below.

Using Newton's 2nd law, the governing equation for this system becomes:

(1)

where u is the force generated between the road/tire interface and can be controlled directly. For this example,
let's assume that

m = 1000 kg

b = 50 N.sec/m

u = 500 N

Building the model


This system will be modeled by summing the forces acting on the mass and integrating the acceleration to give
the velocity. Open Simulink and open a new model window. First, we will model the integral of acceleration.
(2)

 Insert an Integrator Block (from the Continuous library) and draw lines to and from its input and output
terminals.

 Label the input line "vdot" and the output line "v" as shown below. To add such a label, double click in the
empty space just above the line.

Since the acceleration (dv/dt) is equal to the sum of the forces divided by mass, we will divide the incoming signal
by the mass.

 Insert a Gain block (from the Math Operations library) connected to the Integrator block input line and draw
a line leading to the input of the Gain block.

 Edit the Gain block by double-clicking on it and change its value to "1/m".

 Change the label of the Gain block to "inertia" by clicking on the word "Gain" underneath the block.
Now, we will add in the forces which are represented in Equation (1). First, we will add in the damping force.

 Attach a Sum block (from the Math Operations library) to the line leading to the inertia Gain block.

 Change the signs of the Sum block to "+-".

 Insert a Gain block below the Inertia block, select it by single-clicking on it, and select Flip from the Format
menu (or type Ctrl-F) to flip it left-to-right.

 Set the block's value to "b" and rename this block to "damping".

 Tap a line (hold Ctrl while drawing) off the Integrator block's output and connect it to the input of the damping
Gain block.

 Draw a line from the damping Gain block output to the negative input of the Sum Block.
The second force acting on the mass is the control input, u. We will apply a step input.

 Insert a Step block (from the Sources library) and connect it with a line to the positive input of the Sum Block.

 To view the output velocity, insert a Scope block (from the Sinks library) connected to the output of the
Integrator.
 To provide an appropriate step input of 500 at time equal zero, double-click the Step block and set the Step
Time to "0" and the Final Value to "u".
You can download a model file for the complete system here, ccmodel.mdl.
Open-loop response
To simulate this system, first, an appropriate simulation time must be set.

 Select Parameters from the Simulation menu and enter "120" in the Stop Time field. 120 seconds is long
enough to view the open-loop response.
The physical parameters must now be set. Run the following commands at the MATLAB prompt:

m = 1000;

b = 50;

u = 500;
Run the simulation (hit Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-
click on the Scope and hit its autoscale button. You should see the following output.

Observing the above, we would like to improve the response of the cruise control system. The model created

here will be employed for controller design and analysis within Simulink in the Cruise Control: Simulink Controller

Design page.
Cruise Control: Simulink Controller Design

Contents

 Extracting a linear model into MATLAB

 Implementing PI control

 Closed-loop response

In the Cruise Control: Simulink Modeling page we created a Simulink model of the cruise control system. You

can recreate the model or download it here. In this section, we will show how to implement a feedback controller

in Simulink to meet the performance specifications for the system.


Extracting a linear model into MATLAB
A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model
into MATLAB. This is done through the use of In1 and Out1 blocks and the MATLAB function linmod.

 Replace the Step Block and Scope Block with an In1 and an Out1 block, respectively (these blocks can be

found in the Ports & Subsystems library). This defines the input and output of the system for the extraction
process.

Save your file as "ccmodel.mdl" (select Save As from the File menu). MATLAB will extract the linear model from
the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:
m = 1000;

b = 50;

u = 500;

[A,B,C,D] = linmod('ccmodel')

cruise_ss = ss(A,B,C,D);

A =

-0.0500

B =

1.0000e-03

C =

D =

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in

MATLAB. We will multiply the numerator by 500 to simulate a step input of 500 N. Enter the following command
in MATLAB.

step(u*cruise_ss)
Implementing PI control
In the Cruise Control: PID Control page a PI controller was designed with Kp = 800 and Ki = 40 to give the

desired response. We will implement this in Simulink by first containing the open-loop system from earlier in this

page in a Subsystem block.


 Create a new model window.

 Drag a Subsystem block from the Ports & Subsystems library into your new model window.
 Double-click on this block. You will see a blank window representing the contents of the subsystem (which
is currently empty).

 Open your previously saved model of the cruise control system, ccmodel.mdl.
 Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C).

 Select the blank subsystem window from your new model and select Paste from the Edit menu (or Ctrl-V).
You should see your original system in this new subsystem window. Close this window.

 You should now see input and output terminals on the Subsystem block. Name this block "plant model".
Now, we will build a PI controller around the plant model. First, we will feed back the plant output.

 Draw a line extending from the plant output.

 Insert a Sum block and assign "+-" to it's inputs.

 Tap a line of the output line and draw it to the negative input of the Sum block.
The output of the Sum block will provide the error signal. From this, we will generate proportional and integral
components.

 Insert an Integrator block after the Sum block and connect them with a line.

 Insert and connect a Gain block after the Integrator block to provide the integral gain.

 Label this Integrator "Ki" and assign it a value of "Ki".

 Insert a new Gain block and connect it with a line tapped off the output of the Sum block.

 Label this gain "Kp" and assign it a value of "Kp".


Now we will add the proportional and integral components and apply the sum to the plant.

 Insert a Sum block between the Ki block and the plant model and connect the outputs of the two Gain blocks
to the Sum block inputs.

 Connect the Sum block output to the input of the plant block.
Finally, we will apply a step input and view the output with a Scope block.

 Attach a Step block to the free input of the feedback Sum block.

 Attach a Scope block to the plant output.

 Double-click the Step block and set the Step Time to "0" and the Final Value to "u". This allows the input
magnitude to be changed outside of Simulink.
You can download our version of the closed-loop system model here.

In this example, we constructed a PI controller from fundamental blocks. As an alternative, we could have used
a Transfer Function block (from the Continuous library) to implement this in one step, as shown below.

You can download this model here.


Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation

menu and enter "10" in the Stop Time field. The design requirements included a rise time of less than 5 sec, so

we simulate for 10 seconds to view the output. The physical parameters must now be set. Run the following
commands at the MATLAB prompt:

m = 1000;

b = 50;

u = 10;

Kp = 800;

Ki = 40;

Run the simulation (hit Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-
click on the scope and hit its autoscale button. You should see the following output.
DC Motor Speed: Simulink Controller
Design

Contents

 Extracting a linear model into MATLAB

 Open-loop response

 Closed-loop response with lag compensator

 Closed-loop response with lead compensator

From the DC Motor Speed: Simulink Modeling page we generated two different DC motor models in Simulink.

We will now employ these models within Simulink to simulate the system response and design different

approaches to control.
Extracting a linear model into MATLAB
A linear model of the system can be extracted from the Simulink model into the MATLAB workspace. This can
accomplished employing the MATLAB command linmod or from directly within Simulink as we will do here. We

will specifically use the base Simulink model developed from first principles shown below. You can download

this model here, or you can refer to the DC Motor Speed: Simulink Modeling page to recreate the model yourself.

Recall that the physical parameters have to be set if they have not previously been defined in the workspace.
J = 0.01;

b = 0.1;

K = 0.01;

R = 1;

L = 0.5;

We then need to identify the inputs and outputs of the model we wish to extract. First right-click on the signal
representing the Voltage input in the Simulink model. Then choose Linearization > Input Point from the

resulting menu. Similarly, right-click on the signal representing the Speed output and select Linearization >

Output Point from the resulting menu. The input and output signals should now be identified on your model by

arrow symbols as shown in the figure below.

In order to perform the extraction, select from the menus at the top of the model window Tools > Control Design

> Linear Analysis. This will cause the Linear Analysis Toolto open. Within the Linear Analysis Tool window,
the Operating Point to be linearized about can remain the default, Model Initial Condition. In order to

perform the linearization, next click the Linearize button identified by the green triangle. The result of this
linearization is the linsys1 object which now appears in the Linear Analysis Workspace as shown below.

Furthermore, the open-loop step response of the linearized system was also generated automatically.

The open-loop step response above is consistent with the response generated in the DC Motor Speed: System

Analysis page The reason the responses match so closely is because this Simulink model uses only linear
components. Note that this process can be used extract linear approximations of models with nonlinear elements

too.
We will further verify the model extraction by looking at the model itself. The linearized model can be exported
by simply dragging the object into the MATLAB Workspace. This object can then be used within MATLAB in

the same manner as an object created directly from the MATLAB command line. Specifically, entering the
commandzpk(linsys1) in the MATLAB command window demonstrates that the resulting model has the

following form.

(1)

This model matches the one generated in the DC Motor Speed: System Modeling page. This can be seen by

repeating the MATLAB commands given below.

s = tf('s');

P_motor = K/((J*s+b)*(L*s+R)+K^2);

zpk(P_motor)

ans =

-------------------

(s+9.997) (s+2.003)

Continuous-time zero/pole/gain model.

Open-loop response
The open-loop step response can also be generated directly within Simulink, without extracting any models to

the MATLAB workspace. In order to simulate the step response, the details of the simulation must first be set.
This can be accomplished by selecting Configuration Parameters from the Simulation menu. Within the

resulting menu, define the length for which the simulation is to run in the Stop time field. We will enter "3" since

3 seconds will be long enough for the step response to reach steady state. Within this window you can also

specify various aspects of the numerical solver, but we will just use the default values for this example.
Next we need to add an input signal and a means for displaying the output of our simulation. This is done by
doing the following:

 Remove the In1 and Out1 blocks.

 Insert a Step block from the Simulink/Sources library and connect it with a line to the Voltage input of the
motor subsystem.

 To view the Speed output, insert a Scope from the Simulink/Sinks library and connect it to the Speed output
of the motor subsystem.

 To provide a appropriate unit step input at t=0, double-click the Step block and set the Step time to "0".

The final model should appear as shown in the following figure.

Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation is finished,

double-click on the scope and hit its autoscale button. You should see the following output.
This response is identical to that obtained by MATLAB above using the extracted model. This is again to be
expected because this Simulink model includes only linear blocks.

Closed-loop response with lag compensator


In the DC Motor Speed: Root Locus Controller Design page a lag compensator was designed with the following

transfer function.

(2)

To generate the closed-loop step response with this compensator in Simulink, we will begin with the

"Motor_Model.mdl" file described above. We will then put the lag compensator in series with the motor subsystem
and will feed back the motor's speed for comparison to a desired reference.

More specifically, follow the steps given below:

 Remove the Input and Output ports of the model.

 Insert a Sum block from the Simulink/Math Operations library. Then double-click on the block and enter "|+-
" for its List of signs where the symbol "|" serves as a spacer between ports of the block.

 Insert a Transfer Function block from the Simulink/Continuous library. Then double-click on the block and
edit the Numerator coefficients field to "[44 44]" and theDenominator coefficients field to "[1 0.01]".

 Insert a Step block from the Simulink/Sources library. Then double-click on the block and set the Step

time to "0".

 Insert a Scope block from the Simulink/Sinks library.

Then connect and label the components as shown in the following figure
You can download our version of the closed-loop system model here, Motor_Model_lag.mdl.
Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation is finished,

double-click on the scope and hit its autoscale button. You should see the following output.

This step response matches exactly the closed-loop performance observed in the DC Motor Speed: Root Locus

Controller Design page where the lag compensator was originally designed. Note that while we used the physics-
based Simulink model developed in the DC Motor Speed: Simulink Modeling page for simulating the closed-loop

system, we could have equivalently used the Simscape version of the DC motor model.
Closed-loop response with lead compensator
We have shown in the above and in other pages of this example that the lag compensator we have designed

meets all of the given design requirements. Instead of a lag compensator, we could have also designed a lead

compensator to meet the given requirements. More specifically, we could have designed a lead compensator to

achieve a similar DC gain and phase margin to that achieved by the lag compensator, but with a larger gain

crossover frequency. You can refer back to the DC Motor Speed: Frequency Domain Methods for Controller

Design page for more details on the design of the lag compensator, but the fact that the DC gains and phase

margins are similar indicate that the responses under lag and lead control would have similar amounts of error

in steady state and similar amounts of overshoot. The difference in response would come in that the larger gain

crossover frequency provided by the lead compensator would make the system response faster than with the

lag compensator. We will specifically use the following lead compensator.

(3)

To see the precise effect of the lead compensator as compared to our lag compensator, let's modify our Simulink
model from above as follows:

 Disconnect the Step block and Scope block from the rest of the model.

 Copy the blocks forming the closed-loop of the model: the Sum block, the Transfer Function block, and the
DC Motor subsystem. Then paste a copy of this loop below the original blocks.

 Double-click on the Transfer Function block and edit the Numerator coefficients field to "[160000 5.6e6]"

and the Denominator coefficients field to "[1 1035]".

 Insert a Mux block from the Simulink\Signal Routing library and connect the outputs of the two Motor
subsystem blocks to the inputs of the Mux and connect the output of the Mux to the Scope.

 Connect the Step block to the Sum block of the original feedback system. Then branch off from this line and
connect it to the Sum block of the lead compensated system as well.

The Mux block serves to bundle the two signals into a single line, this way the Scope will plot both speed signals
on the same set of axes. When you are done, your model should appear as follows.
Running the simulation and observing the output produced by the scope, you will see that both responses have

a steady-state error that approaches zero. Zooming in on the graphs you can generate a figure like the one

shown below. Comparing the two graphs, the purple response belonging to the lead compensated system has

a much smaller settle time and slightly larger, but similar, overshoot as compared to the yellow response
produced by the lag compensated system.
It is generally preferred that a system respond to a command quickly. Why then might we prefer to use the lag

compensator even though it is slower than the lead compensator? The advantage of the lag compensator in this

case is that by responding more slowly it requires less control effort than the lead compensator. Less control

effort means that less power is consumed and that the various components can be sized smaller since they do
not have to supply as much energy or withstand the higher voltages and current required of the lead compensator.

We will now modify our simulation to explicitly observe the control effort requirements of our two feedback

systems. We will do this by sending our various signals to the workspace for plotting and further manipulation if

desired. Specifically, delete the Scope and Mux blocks from your Simulink model. Then insert four To Workspace
blocks from the Simulink\Sinks library. Double-click on each of the blocks and change their Save
format from Structure to Array. Also provide a Variable name within each block that will make sense to you.

You can then connect the blocks to the existing model and label them as shown below. You can download our

version of this Simulink model here, Motor_Model_leadlag.mdl.


Then change the simulation stop time to 1 second and run the model. The act of running the simulation will send

to the MATLAB workspace a series of arrays corresponding to the variables set-up in your model with the To

Workspace blocks. Furthermore, the time vector used by that run of the simulation is stored in the default
variable tout. You can now plot the results of your simulation from the workspace. Enter the following code to

see how to specifically plot the control effort variables.

subplot(2,1,1)

plot(tout,ulag);

xlabel('time (seconds)')
ylabel('control effort (volts)')

title('Control Effort Under Lag Compensation')


subplot(2,1,2)

plot(tout,ulead);

xlabel('time (seconds)')

ylabel('control effort (volts)')

title('Control Effort Under Lead Compensation')

Examination of the above shows that the control effort required by the lead compensator is above 150,000 Volts,

which is well above anything that could be supplied or withstood by a typical DC motor. This exemplifies the

tradeoff inherent between achieving small tracking error and keeping the amount of control effort required small.

Optimal control techniques have been developed to achieve an optimal balance between competing goals. One

such technique is explored in the Aircraft Pitch: State Space Methods for Controller Design page.

You might also like