matlab basics

381
ANALISIS SISTEM FISIS JURUSAN FISIKA FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM UNIVERSITAS NEGERI SEMARANG TAHUN 2010-2011 Mata Kuliah Analisis Sistem Fisis Disalin oleh angkatan 2008 - Instrumentasi Fisika Unnes dari : www.engin.umich.edu

Upload: kevin-maulana

Post on 23-Jan-2016

41 views

Category:

Documents


1 download

DESCRIPTION

Matlab

TRANSCRIPT

Page 1: Matlab Basics

ANALISIS SISTEM FISIS JURUSAN FISIKA FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM

UNIVERSITAS NEGERI SEMARANG TAHUN 2010 -2011

Mata Kuliah Analisis Sistem Fisis

Disalin oleh angkatan 2008 - Instrumentasi Fisika Unnes dari :

www.engin.umich.edu

Page 2: Matlab Basics

DAFTAR ISI

I. Matlab Basics

1.1 Vectors

1.2 Functions

1.2 Plotting

1.4 Polynomials

1.5 Matrices

Using M-files in Matlab

Getting help in Matlab

II. Modeling

2.1 Train system

2.2 Free body diagram and Newton's law

2.3 State-variable and output equations

2.4 Matlab representation

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

III. PID

3.1 Introduction

3.2 The three-term controller

3.3 Characteristics of P, I, and D controllers

3.4 Open-loop step response

3.5 Proportional control

3.1 PD control

Page 3: Matlab Basics

3.2 PI control

3.3 PID control

3.9 General tips for designing a PID controller

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

IV. Root Locus

4.1 Closed-loop poles

4.2 Plotting the root locus of a transfer function

4.3 Choosing a value of K

4.4 Closed-loop response

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

V. Frequency Response

5.1 Bode plot

5.2 Gain and phase margin

5.3 Bandwidth frequency

5.4 Closed-loop performance

5.5 Nyquist diagram

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

VI. State Space

6.1 State-space equations

6.2 Control design using pole placement

Page 4: Matlab Basics

6.3 Introducing the reference input

6.4 Observer design

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

VII. Digital Control

7.1 Introduction

7.2 Zero-order hold equivalence

7.3 Conversion using c2dm

7.4 Stability and transient response

7.5 Discrete root-locus

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted

Pendulum, Pitch Controller, Ball & Beam

Page 5: Matlab Basics

Matlab Basics

1.1 Vectors

Let's start off by creating something simple, like a vector. Enter each element of the vector

(separated by a space) between brackets, and set it equal to a variable. For example, to create the

vector a, enter into the Matlab command window (you can "copy" and "paste" from your

browser into Matlab to make it easy):

a = [1 2 3 4 5 6 9 8 7]

Matlab should return:

a =

1 2 3 4 5 6 9 8 7

Let's say you want to create a vector with elements between 0 and 20 evenly spaced in

increments of 2 (this method is frequently used to create a time vector):

t = 0:2:20

t =

0 2 4 6 8 10 12 14 16 18 20

Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2

to each of the elements in vector 'a'. The equation for that looks like:

b = a + 2

b =

3 4 5 6 7 8 11 10 9

Now suppose, you would like to add two vectors together. If the two vectors are the same length,

it is easy. Simply add the two as shown below:

c = a + b

c =

4 6 8 10 12 14 20 18 16

Page 6: Matlab Basics

Subtraction of vectors of the same length works exactly the same way.

1.2 Functions

To make life easier, Matlab includes many standard functions. Each function is a block of code

that accomplishes a specific task. Matlab contains all of the standard functions such as sin, cos,

log, exp, sqrt, as well as many others. Commonly used constants such as pi, and i or j for the

square root of -1, are also incorporated into Matlab.

sin(pi/4)

ans =

0.7071

To determine the usage of any function, type help [function name] at the Matlab command

window.

Matlab even allows you to write your own functions with the function command; follow the link

to learn how to write your own functions and see a listing of the functions we created for this

tutorial.

1.3 Plotting

It is also easy to create plots in Matlab. Suppose you wanted to plot a sine wave as a function of

time. First make a time vector (the semicolon after each statement tells Matlab we don't want to

see all the values) and then compute the sin value at each time.

t=0:0.25:7;

y = sin(t);

plot(t,y)

Page 7: Matlab Basics

The plot contains approximately one period of a sine wave. Basic plotting is very easy in Matlab,

and the plot command has extensive add-on capabilities. I would recommend you visit the

plotting page to learn more about it.

1.4 Polynomials

In Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply

enter each coefficient of the polynomial into the vector in descending order. For instance, let's

say you have the following polynomial:

To enter this into Matlab, just enter it as a vector in the following manner

x = [1 3 -15 -2 9]

x =

1 3 -15 -2 9

Matlab can interpret a vector of length n+1 as an nth order polynomial. Thus, if your polynomial

is missing any coefficients, you must enter zeros in the appropriate place in the vector. For

example,

would be represented in Matlab as:

Page 8: Matlab Basics

y = [1 0 0 0 1]

You can find the value of a polynomial using the polyval function. For example, to find the

value of the above polynomial at s=2,

z = polyval([1 0 0 0 1],2)

z =

17

You can also extract the roots of a polynomial. This is useful when you have a high-order

polynomial such as

Finding the roots would be as easy as entering the following command;

roots([1 3 -15 -2 9])

ans =

-5.5745

2.5836

-0.7951

0.7860

Let's say you want to multiply two polynomials together. The product of two polynomials is

found by taking the convolution of their coefficients. Matlab's function conv that will do this for

you.

x = [1 2];

y = [1 4 8];

z = conv(x,y)

Page 9: Matlab Basics

z =

1 6 16 16

Dividing two polynomials is just as easy. The deconv function will return the remainder as well

as the result. Let's divide z by y and see if we get x.

[xx, R] = deconv(z,y)

xx =

1 2

R =

0 0 0 0

As you can see, this is just the polynomial/vector x from before. If y had not gone into z evenly,

the remainder vector would have been something other than zero.

If you want to add two polynomials together which have the same order, a simple z=x+y will

work (the vectors x and y must have the same length). In the general case, the user-defined

function, polyadd can be used. To use polyadd, copy the function into an m-file, and then use

it just as you would any other function in the Matlab toolbox. Assuming you had the polyadd

function stored as a m-file, and you wanted to add the two uneven polynomials, x and y, you

could accomplish this by entering the command:

z = polyadd(x,y)

x =

1 2

y =

1 4 8

z =

1 5 10

Page 10: Matlab Basics

1.5 Matrices

Entering matrices into Matlab is the same as entering a vector, except each row of elements is

separated by a semicolon (;) or a return:

B = [1 2 3 4;5 6 7 8;9 10 11 12]

B =

1 2 3 4

5 6 7 8

9 10 11 12

B = [ 1 2 3 4

5 6 7 8

9 10 11 12]

B =

1 2 3 4

5 6 7 8

9 10 11 12

Matrices in Matlab can be manipulated in many ways. For one, you can find the transpose of a

matrix using the apostrophe key:

C = B'

C =

1 5 9

2 6 10

3 7 11

4 8 12

It should be noted that if C had been complex, the apostrophe would have actually given the

complex conjugate transpose. To get the transpose, use .' (the two commands are the same if the

matix is not complex).

Page 11: Matlab Basics

Now you can multiply the two matrices B and C together. Remember that order matters when

multiplying matrices.

D = B * C

D =

30 70 110

70 174 278

110 278 446

D = C * B

D =

107 122 137 152

122 140 158 176

137 158 179 200

152 176 200 224

Another option for matrix manipulation is that you can multiply the corresponding elements of

two matrices using the .* operator (the matrices must be the same size to do this).

E = [1 2;3 4]

F = [2 3;4 5]

G = E .* F

E =

1 2

3 4

F =

2 3

4 5

G =

2 6

Page 12: Matlab Basics

12 20

If you have a square matrix, like E, you can also multiply it by itself as many times as you like

by raising it to a given power.

E^3

ans =

37 54

81 118

If wanted to cube each element in the matrix, just use the element-by-element cubing.

E.^3

ans =

1 8

27 64

You can also find the inverse of a matrix:

X = inv(E)

X =

-2.0000 1.0000

1.5000 -0.5000

or its eigenvalues:

eig(E)

ans =

-0.3723

5.3723

Page 13: Matlab Basics

There is even a function to find the coefficients of the characteristic polynomial of a matrix. The

"poly" function creates a vector that includes the coefficients of the characteristic polynomial.

p = poly(E)

p =

1.0000 -5.0000 -2.0000

Remember that the eigenvalues of a matrix are the same as the roots of its characteristic

polynomial:

roots(p)

ans =

5.3723

-0.3723

Using M-files in Matlab

There are slightly different things you need to know for each platform.

Macintosh

There is a built-in editor for m-files; choose "New M-file" from the File menu. You can

also use any other editor you like (but be sure to save the files in text format and load

them when you start Matlab).

Windows

Running Matlab from Windows is very similar to running it on a Macintosh. However,

you need to know that your m-file will be saved in the clipboard. Therefore, you must

make sure that it is saved as filename.m

Unix

You will need to run an editor separately from Matlab. The best strategy is to make a

directory for all your m-files, then cd to that directory before running both Matlab and the

editor. To start Matlab from your Xterm window, simply type: matlab.

Page 14: Matlab Basics

You can either type commands directly into matlab, or put all of the commands that you will

need together in an m-file, and just run the file. If you put all of your m-files in the same

directory that you run matlab from, then matlab will always find them.

Getting help in Matlab

Matlab has a fairly good on-line help; type

help commandname

for more information on any given command. You do need to know the name of the command

that you are looking for; a list of the all the ones used in these tutorials is given in the command

listing; a link to this page can be found at the bottom of every tutorial and example page.

Here are a few notes to end this tutorial.

You can get the value of a particular variable at any time by typing its name.

B

B =

1 2 3

4 5 6

7 8 9

You can also have more that one statement on a single line, so long as you separate them with

either a semicolon or comma.

Also, you may have noticed that so long as you don't assign a variable a specific operation or

result, Matlab with store it in a temporary variable called "ans".

Page 15: Matlab Basics

Modeling

2.1 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 direction, we want to apply control to the train so that it has a smooth

start-up and stop, along with a constant-speed ride.

The mass of the engine and the car will be represented by M1 and M2, respectively. The two are

held together by a spring, which has the stiffness coefficient of k. F represents the force applied

by the engine, and the Greek letter, mu (which will also be represented by the letter u), represents

the coefficient of rolling friction.

Photo courtesy: Dr. Howard Blackburn

2.2 Free body diagram and Newton's law

The system can be represented by following Free Body Diagrams.

From Newton's law, you know that the sum of forces acting on a mass equals the mass times its

acceleration. In this case, the forces acting on M1 are the spring, the friction and the force

applied by the engine. The forces acting on M2 are the spring and the friction. In the vertical

direction, the gravitational force is canceled by the normal force applied by the ground, so that

there will be no acceleration in the vertical direction. The equations of motion in the horizontal

direction are the followings:

Page 16: Matlab Basics

2.3 State-variable and output equations

This set of system equations can now be manipulated into state-variable form. Knowing state-

variables are X1 and X2 and the input is F, state-variable equations will look like the following:

Let the output of the system be the velocity of the engine. Then the output equation will become:

1. Transfer function

To find the transfer funciton of the system, first, take Laplace transforms of above state-variable

and output equations.

Using these equations, derive the transfer function Y(s)/F(s) in terms of constants. When finding

the transfer function, zero initial conditions must be assumed. The transfer function should

look like the one shown below.

2. State-space

Another method to solve the problem is to use the state-space form. Four matrices A, B, C, and

D characterize the system behavior, and will be used to solve the problem. The state-space form

that were manipulated from the state-variable and the output equations is shown below.

Page 17: Matlab Basics

2.4 Matlab representation

Now we will show you how to enter the equations derived above into an m-file for Matlab. Since

Matlab can not manipulate symbolic variables, let's assign numerical values to each of the

variables. Let

M1 = 1 kg

M2 = 0.5 kg

k = 1 N/sec

F= 1 N

u = 0.002 sec/m

g = 9.8 m/s^2

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

M1=1;

M2=0.5;

k=1;

F=1;

u=0.002;

g=9.8;

Page 18: Matlab Basics

Now you have one of two choices: 1) Use the transfer function, or 2) Use the state-space form to

solve the problem. If you choose to use the transfer function, add the following commands onto

the end of the m-file which you have just created.

num=[M2 M2*u*g 1];

den=[M1*M2 2*M1*M2*u*g M1*k+M1*M2*u*u*g*g+M2*k M1*k*u*g+M2*k*u*g];

If you choose to use the state-space form, add the following commands at the end of the m-file,

instead of num and den matrices shown above.

A=[ 0 1 0 0;

-k/M1 -u*g k/M1 0;

0 0 0 1;

k/M2 0 -k/M2 -u*g];

B=[ 0;

1/M1;

0;

0];

C=[0 1 0 0];

D=[0];

See the Matlab basics tutorial to learn more about entering matrices.

Example: Modeling a Cruise Control System

Physical setup and system equations

The model of the cruise control system is relatively simple. If the inertia of the wheels is

neglected, and it is assumed that friction (which is proportional to the car's speed) is what is

opposing the motion of the car, then the problem is reduced to the simple mass and damper

system shown below.

Page 19: Matlab Basics

Using Newton's law, modeling equations for this system becomes:

(1)

where u is the force from the engine. For this example, let's assume that

m = 1000kg

b = 50Nsec/m

u = 500N

Design requirements

The next step in modeling this system is to come up with some design criteria. When the engine

gives a 500 Newton force, the car will reach a maximum velocity of 10 m/s (22 mph). An

automobile should be able to accelerate up to that speed in less than 5 seconds. Since this is only

a cruise control system, a 10% overshoot on the velocity will not do much damage. A 2% steady-

state error is also acceptable for the same reason.

Keeping the above in mind, we have proposed the following design criteria for this problem:

Rise time < 5 sec

Overshoot < 10%

Steady state error < 2%

Page 20: Matlab Basics

Matlab representation

1. Transfer Function

To find the transfer function of the above system, we need to take the Laplace transform of the

modeling equations (1). When finding the transfer function, zero initial conditions must be

assumed. Laplace transforms of the two equations are shown below.

Since our output is the velocity, let's substitute V(s) in terms of Y(s)

The transfer function of the system becomes

To solve this problem using Matlab, copy the following commands into an new m-file:

m=1000;

b=50;

u=500;

num=[1];

den=[m b];

These commands will later be used to find the open-loop response of the system to a step input.

But before getting into that, let's take a look at another representation, the state-space.

2. State-Space

We can rewrite the first-order modeling equation (1) as the state-space model.

To use Matlab to solve this problem, create an new m-file and copy the following commands:

m = 1000;

b = 50;

u = 500;

A = [-b/m];

Page 21: Matlab Basics

B = [1/m];

C = [1];

D = 0;

Note: It is possible to convert from the state-space representation to the transfer function or vise

versa using Matlab. To learn more about the conversion, click Conversion

Open-loop response

Now let's see how the open-loop system responds to a step input. Add the following command

onto the end of the m-file written for the tranfer function (the m-file with num and den matrices)

and run it in the Matlab command window:

step (u*num,den)

You should get the following plot:

To use the m-file written for the state-space (the m-file with A, B, C, D matrices), add the

following command at the end of the m-file and run it in the Matlab command window:

step (A,u*B,C,D)

You should get the same plot as the one shown above.

From the plot, we see that the vehicle takes more than 100 seconds to reach the steady-state

speed of 10 m/s. This does not satisfy our rise time criterion of less than 5 seconds.

Page 22: Matlab Basics

Closed-loop transfer function

To solve this problem, a unity feedback controller will be added to improve the system

performance. The figure shown below is the block diagram of a typical unity feedback system.

The transfer function in the plant is the transfer function derived above {Y(s)/U(s)=1/ms+b}.

The controller will to be designed to satisfy all design criteria. Four different methods to design

the controller are listed at the bottom of this page. You may choose on PID, Root-locus,

Frequency response, or State-space.

Example: DC Motor Speed Modeling

Photo courtesy: Pope Electric Motors Pty Limited

Physical setup and system equations

A common actuator in control systems is the DC motor. It directly provides rotary motion and,

coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of

the armature and the free body diagram of the rotor are shown in the following figure:

Page 23: Matlab Basics

For this example, we will assume the following values for the physical parameters. These values

were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls

lab.

* moment of inertia of the rotor (J) = 0.01 kg.m^2/s^2

* damping ratio of the mechanical system (b) = 0.1 Nms

* electromotive force constant (K=Ke=Kt) = 0.01 Nm/Amp

* electric resistance (R) = 1 ohm

* electric inductance (L) = 0.5 H

* input (V): Source Voltage

* output (theta): position of shaft

* The rotor and shaft are assumed to be rigid

The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf,

e, is related to the rotational velocity by the following equations:

In SI units (which we will use), Kt (armature constant) is equal to Ke (motor constant).

From the figure above we can write the following equations based on Newton's law combined

with Kirchhoff's law:

Page 24: Matlab Basics

1. Transfer Function

Using Laplace Transforms, the above modeling equations can be expressed in terms of s.

By eliminating I(s) we can get the following open-loop transfer function, where the rotational

speed is the output and the voltage is the input.

2. State-Space

In the state-space form, the equations above can be expressed by choosing the rotational speed

and electric current as the state variables and the voltage as an input. The output is chosen to be

the rotational speed.

Design requirements

First, our uncompensated motor can only rotate at 0.1 rad/sec with an input voltage of 1 Volt

(this will be demonstrated later when the open-loop response is simulated). Since the most basic

requirement of a motor is that it should rotate at the desired speed, the steady-state error of the

Page 25: Matlab Basics

motor speed should be less than 1%. The other performance requirement is that the motor must

accelerate to its steady-state speed as soon as it turns on. In this case, we want it to have a

settling time of 2 seconds. Since a speed faster than the reference may damage the equipment, we

want to have an overshoot of less than 5%.

If we simulate the reference input (r) by an unit step input, then the motor speed output should

have:

Settling time less than 2 seconds

Overshoot less than 5%

Steady-state error less than 1%

Matlab representation and open-loop response

1. Transfer Function

We can represent the above transfer function into Matlab by defining the numerator and

denominator matrices as follows:

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

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Now let's see how the original open-loop system performs. Add the following commands onto

the end of the m-file and run it in the Matlab command window:

step(num,den,0:0.1:3)

Page 26: Matlab Basics

title('Step Response for the Open Loop System')

You should get the following plot:

From the plot we see that when 1 volt is applied to the system, the motor can only achieve a

maximum speed of 0.1 rad/sec, ten times smaller than our desired speed. Also, it takes the motor

3 seconds to reach its steady-state speed; this does not satisfy our 2 seconds settling time

criterion.

2. State-Space

We can also represent the system using the state-space equations. Try the following commands

in a new m-file.

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

A=[-b/J K/J

-K/L -R/L];

Page 27: Matlab Basics

B=[0

1/L];

C=[1 0];

D=0;

step(A, B, C, D)

Run this m-file in the Matlab command window, and you should get the same output as the one

shown above.

Example: Modeling DC Motor Position

Physical Setup

A common actuator in control systems is the DC motor. It directly provides rotary motion and,

coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of

the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values

were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls

lab.

* moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2

* damping ratio of the mechanical system (b) = 3.5077E-6 Nms

Page 28: Matlab Basics

* electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp

* electric resistance (R) = 4 ohm

* electric inductance (L) = 2.75E-6 H

* input (V): Source Voltage

* output (theta): position of shaft

* The rotor and shaft are assumed to be rigid

System Equations

The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf,

e, is related to the rotational velocity by the following equations:

In SI units (which we will use), Kt (armature constant) is equal to Ke (motor constant).

From the figure above we can write the following equations based on Newton's law combined

with Kirchhoff's law:

1. Transfer Function

Using Laplace Transforms the above equations can be expressed in terms of s.

By eliminating I(s) we can get the following transfer function, where the rotating speed is the

output and the voltage is an input.

Page 29: Matlab Basics

However during this example we will be looking at the position, as being the output. We can

obtain the position by integrating Theta Dot, therefore we just need to divide the transfer

function by s.

2. State Space

These equations can also be represented in state-space form. If we choose motor position, motor

speed, and armature current as our state variables, we can write the equations as follows:

Design requirements

We will want to be able to position the motor very precisely, thus the steady-state error of the

motor position should be zero. We will also want the steady-state error due to a disturbance, to

be zero as well. The other performance requirement is that the motor reaches its final position

very quickly. In this case, we want it to have a settling time of 40ms. We also want to have an

overshoot smaller than 16%.

If we simulate the reference input (R) by a unit step input, then the motor speed output should

have:

Settling time less than 40 milliseconds

Overshoot less than 16%

No steady-state error

Page 30: Matlab Basics

No steady-state error due to a disturbance

Matlab representation and open-loop response

1. Transfer Function

We can put the transfer function into Matlab by defining the numerator and denominator as

vectors:

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

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Now let's see how the original open-loop system performs. Add the following command onto the

end of the m-file and run it in the Matlab command window:

step(num,den,0:0.001:0.2)

You should get the following plot:

Page 31: Matlab Basics

From the plot we see that when 1 volt is applied to the system, the motor position changes by 6

radians, six times greater than our desired position. For a 1 volt step input the motor should spin

through 1 radian. Also, the motor doesn't reach a steady state which does not satisfy our design

criteria

2. State Space

We can put the state space equations into Matlab by defining the system's matrices as follows:

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

A=[0 1 0

0 -b/J K/J

0 -K/L -R/L];

B=[0 ; 0 ; 1/L];

C=[1 0 0];

D=[0];

The step response is obtained using the command

step(A,B,C,D)

Unfortunately, Matlab responds with

Warning: Divide by zero

??? Index exceeds matrix dimensions.

Error in ==> /usr/local/lib/matlab/toolbox/control/step.m

On line 84 ==> dt = t(2)-t(1);

Page 32: Matlab Basics

There are numerical scaling problems with this representation of the dynamic equations. To fix

the problem, we scale time by tscale = 1000. Now the output time will be in milliseconds rather

than in seconds. The equations are given by

tscale = 1000;

J=3.2284E-6*tscale^2;

b=3.5077E-6*tscale;

K=0.0274*tscale;

R=4*tscale;

L=2.75E-6*tscale^2;

A=[0 1 0

0 -b/J K/J

0 -K/L -R/L];

B=[0 ; 0 ; 1/L];

C=[1 0 0];

D=[0];

The output appears the same as when obtained through the transfer function, but the time vector

must be divided by tscale.

[y,x,t]=step(A,B,C,D);

plot(t/tscale,y)

ylabel('Amplitude')

xlabel('Time (sec)')

Example: Modeling a Bus Suspension System using Transfer Function

Physical setup

Designing an automatic suspension system for a bus turns out to be an interesting control

problem. When the suspension system is designed, a 1/4 bus model (one of the four wheels) is

used to simplify the problem to a one dimensional spring-damper system. A diagram of this

system is shown below:

Page 33: Matlab Basics

Design requirements

A good bus suspension system should have satisfactory road holding ability, while still providing

comfort when riding over bumps and holes in the road. When the bus is experiencing any road

disturbance (i.e. pot holes, cracks, and uneven pavement),the bus body should not have large

oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very

difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the

distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an

estimation.

The road disturbance (W) in this problem will be simulated by a step input. This step could

represent the bus coming out of a pothole. We want to design a feedback controller so that the

output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For

example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of

+/- 5 mm and return to a smooth ride within 5 seconds.

Equations of motion

From the picture above and Newton's law, we can obtain the dynamic equations as the following:

Page 34: Matlab Basics

Transfer Function Equation

Assume that all of the initial condition are zeroes, so these equations represent the situation when

the bus's wheel go up a bump. The dynamic equations above can be expressed in a form of

transfer functions by taking Laplace Transform of the above equations. The derivation from

above equations of the Transfer Functions G1(s) and G2(s) of output,X1-X2, and two inputs,U

and W, are as follows.

Find the inverse of matrix A and then multiple with inputs U(s)and W(s) on the right hand side

as the following:

Page 35: Matlab Basics

When we want to consider input U(s) only, we set W(s) = 0. Thus we get the transfer function

G1(s) as the following:

When we want to consider input W(s) only, we set U(s) = 0. Thus we get the transfer function

G2(s) as the following:

Also we can express and derive the above equations in state-space form. Even though this

approach will express the first two equations above in the standard form of matrix, it will

simplify the transfer function without going through any algebra, because we can use a function

ss2tf to transform from state-space form to transfer function form for both inputs

Entering equations into Matlab

We can put the above Transfer Function equations into Matlab by defining the numerator and

denominator of Transfer Functions in the form, nump/denp for actuated force input and

num1/den1 for disturbance input, of the standard transfer function G1(s) and G2(s):

G1(s) = nump/denp

G2(s) = num1/den1

Now, let's create a new m-file and enter the following code:

m1=2500;

m2=320;

k1=80000;

k2=500000;

b1 = 350;

b2 = 15020;

nump=[(m1+m2) b2 k2];

Page 36: Matlab Basics

denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2];

'G(s)1'

printsys(nump,denp)

num1=[-(m1*b2) -(m1*k2) 0 0];

den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2];

'G(s)2'

printsys(0.1*num1,den1)

Open-loop response

We can use Matlab to display how the original open-loop system performs (without any

feedback control). Add the following commands into the m-file and run it in the Matlab

command window to see the response of unit step actuated force input and unit step disturbance

input.Note that the step command will generate the unit step inputs for each input.

step(nump,denp)

From this graph of the open-loop response for a unit step actuated force, we can see that the

system is under-damped. People sitting in the bus will feel very small amount of oscillation and

the steady-state error is about 0.013 mm. Moreover, the bus takes very unacceptably long time

Page 37: Matlab Basics

for it to reach the steady state or the settling time is very large. The solution to this problem is to

add a feedback controller into the system's block diagram.

step(0.1*num1,den1)

To see some details, you can change the axis:

axis([0 10 -.1 .1])

Page 38: Matlab Basics

From this graph of open-loop response for 0.1 m step disturbance, we can see that when the bus

passes a 10 cm high bump on the road, the bus body will oscillate for an unacceptably long

time(100 seconds) with larger amplitude, 13 cm, than the initial impact. People sitting in the bus

will not be comfortable with such an oscillation. The big overshoot (from the impact itself) and

the slow settling time will cause damage to the suspension system. The solution to this problem

is to add a feedback controller into the system to improve the performance. The schematic of the

closed-loop system is the following:

From the above transfer functions and schematic, we can draw the bus-system block diagram as

the following:

From the schematic above we see that:

Plant = nump/denp

F * Plant=num1/den1

so that

Page 39: Matlab Basics

F=num1/(den1*Plant)

Example: Modeling an Inverted Pendulum

Problem setup and design requirements

he cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F.

Determine the dynamic equations of motion for the system, and linearize about the pendulum's

angle, theta = Pi (in other words, assume that pendulum does not move more than a few degrees

away from the vertical, chosen to be at an angle of Pi). Find a controller to satisfy all of the

design requirements given below.

For this example, let's assume that

M mass of the cart 0.5 kg

m mass of the pendulum 0.5 kg

b friction of the cart 0.1 N/m/sec

l length to pendulum center of mass 0.3 m

I inertia of the pendulum 0.006 kg*m^2

F force applied to the cart

x cart position coordinate

theta pendulum angle from vertical

Page 40: Matlab Basics

For the PID, root locus, and frequency response sections of this problem we will be only

interested in the control of the pendulums position. This is because the techniques used in these

tutorials can only be applied for a single-input-single-output (SISO) system. Therefore, none of

the design criteria deal with the cart's position. For these sections we will assume that the system

starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its

upright position within 5 seconds, and never move more than 0.05 radians away from the

vertical.

The design requirements for this system are:

Settling time of less than 5 seconds.

Pendulum angle never more than 0.05 radians from the vertical.

However, with the state-space method we are more readily able to deal with a multi-output

system. Therefore, for this section of the Inverted Pendulum example we will attempt to control

both the pendulum's angle and the cart's position. To make the design more challenging we will

be applying a step input to the cart. The cart should achieve it's desired position within 5 seconds

and have a rise time under 0.5 seconds. We will also limit the pendulum's overshoot to 20

degrees (0.35 radians), and it should also settle in under 5 seconds.

The design requirements for the Inverted Pendulum state-space example are:

Settling time for x and theta of less than 5 seconds.

Rise time for x of less than 0.5 seconds.

Overshoot of theta less than 20 degrees (0.35 radians).

Force analysis and system equations

Below are the two Free Body Diagrams of the system.

Page 41: Matlab Basics

Summing the forces in the Free Body Diagram of the cart in the horizontal direction, you get the

following equation of motion:

Note that you could also sum the forces in the vertical direction, but no useful information would

be gained.

Summing the forces in the Free Body Diagram of the pendulum in the horizontal direction, you

can get an equation for N:

If you substitute this equation into the first equation, you get the first equation of motion for this

system:

(1)

To get the second equation of motion, sum the forces perpendicular to the pendulum. Solving the

system along this axis ends up saving you a lot of algebra. You should get the following

equation:

To get rid of the P and N terms in the equation above, sum the moments around the centroid of

the pendulum to get the following equation:

Combining these last two equations, you get the second dynamic equation:

(2)

Since Matlab can only work with linear functions, this set of equations should be linearized

about theta = Pi. Assume that theta = Pi + ø (ø represents a small angle from the vertical upward

direction). Therefore, cos(theta) = -1, sin(theta) = -ø, and (d(theta)/dt)^2 = 0. After linearization

the two equations of motion become (where u represents the input):

1. Transfer Function

To obtain the transfer function of the linearized system equations analytically, we must first take

the Laplace transform of the system equations. The Laplace transforms are:

Page 42: Matlab Basics

NOTE: When finding the transfer function initial conditions are assumed to be zero.

Since we will be looking at the angle Phi as the output of interest, solve the first equation for

X(s),

then substituting into the second equation:

Re-arranging, the transfer function is:

where,

From the transfer function above it can be seen that there is both a pole and a zero at the origin.

These can be canceled and the transfer function becomes:

Page 43: Matlab Basics

2. State-Space

After a little algebra, the linearized system equations equations can also be represented in state-

space form:

The C matrix is 2 by 4, because both the cart's position and the pendulum's position are part of

the output. For the state-space design problem we will be controlling a multi-output system so

we will be observing the cart's position from the first row of output and the pendulum's with the

second row.

Matlab representation and the open-loop response

1. Transfer Function

The transfer function found from the Laplace transforms can be set up using Matlab by inputting

the numerator and denominator as vectors. Create an m-file and copy the following text to model

the transfer function:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

Page 44: Matlab Basics

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0]

den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]

Your output should be:

num =

4.5455 0

den =

1.0000 0.1818 -31.1818 -4.4545

To observe the system's velocity response to an impulse force applied to the cart add the

following lines at the end of your m-file:

t=0:0.01:5;

impulse(num,den,t)

axis([0 1 0 60])

Note: Matlab commands from the control system toolbox are highlighted in red.

You should get the following velocity response plot:

As you can see from the plot, the response is entirely unsatisfactory. It is not stable in open loop.

You can change the axis to see more of the response if you need to convince yourself that the

system is unstable.

Page 45: Matlab Basics

1. State-Space

Below, we show how the problem would be set up using Matlab for the state-space model. If you

copy the following text into a m-file (or into a '.m' file located in the same directory as Matlab)

and run it, Matlab will give you the A, B, C, and D matrices for the state-space model and a plot

of the response of the cart's position and pendulum angle to a step input of 0.2 m applied to the

cart.

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies

A = [0 1 0 0;

0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;

0 0 0 1;

0 -(m*l*b)/p m*g*l*(M+m)/p 0]

B = [ 0;

(i+m*l^2)/p;

0;

m*l/p]

C = [1 0 0 0;

0 0 1 0]

D = [0;

0]

T=0:0.05:10;

U=0.2*ones(size(T));

[Y,X]=lsim(A,B,C,D,U,T);

plot(T,Y)

axis([0 2 0 100])

You should see the following output after running the m-file:

A =

Page 46: Matlab Basics

0 1.0000 0 0

0 -0.1818 2.6727 0

0 0 0 1.0000

0 -0.4545 31.1818 0

B =

0

1.8182

0

4.5455

C =

1 0 0 0

0 0 1 0

D =

0

0

The blue line represents the cart's position and the green line represents the pendulum's angle. It

is obvious from this plot and the one above that some sort of control will have to be designed to

improve the dynamics of the system. Four example controllers are included with these tutorials:

PID, root locus, frequency response, and state space. Select from below the one you would like

to use.

Note: The solutions shown in the PID, root locus and frequency response examples may not

yield a workable controller for the inverted pendulum problem. As stated previously, when we

put this problem into the single-input, single-output framework, we ignored the x position of the

cart. The pendulum can be stabilized in an inverted position if the x position is constant or if the

cart moves at a constant velocity (no acceleration). Where possible in these examples, we will

show what happens to the cart's position when our controller is implemented on the system. We

Page 47: Matlab Basics

emphasize that the purpose of these examples is to demonstrate design and analysis techniques

using Matlab; not to actually control an inverted pendulum.

Example: Modeling a Pitch Controller

Photo courtesy: Boeing

Physical setup and system equations

The equations governing the motion of an aircraft are a very complicated set of six non-linear

coupled differential equations. However, under certain assumptions, they can be decoupled and

linearized into the longitudinal and lateral equations. Pitch control is a longitudinal problem, and

in this example, we will design an autopilot that controls the pitch of an aircraft.

The basic coordinate axes and forces acting on an aircraft are shown in the figure below:

Assume that the aircraft is in steady-cruise at constant altitude and velocity; thus, the thrust and

drag cancel out and the lift and weight balance out each other. Also, assume that change in pitch

angle does not change the speed of an aircraft under any circumstance (unrealistic but simplifies

the problem a bit). Under these assumptions, the longitudinal equations of motion of an aircraft

can be written as:

Page 48: Matlab Basics

(1)

Please refer to any aircraft-related textbooks for the explanation of how to derive these

equations. Also, click Variables to see what each variable represents.

For this system, the input will be the elevator deflection angle, and the output will be the pitch

angle.

Design requirements

The next step is to set some design criteria. We want to design a feedback controller so that the

output has an overshoot of less than 10%, rise time of less than 2 seconds, settling time of less

than 10 seconds, and the steady-state error of less than 2%. For example, if the input is 0.2 rad

(11 degress), then the pitch angle will not exceed 0.22 rad, reaches 0.2 rad within 2 seconds,

settles 2% of the steady-state within 10 seconds, and stays within 0.196 to 0.204 rad at the

steady-state.

Overshoot: Less than 10%

Rise time: Less than 2 seconds

Settling time: Less than 10 seconds

Steady-state error: Less than 2%

Transfer function and the state-space

Before finding transfer function and the state-space model, let's plug in some numerical values to

simplify the modeling equations (1) shown above.

(2)

These values are taken from the data from one of the Boeing's commercial aircraft.

1. Transfer function

To find the transfer function of the above system, we need to take the Laplace transform of the

above modeling equations (2). Recall from your control textbook, when finding a transfer

Page 49: Matlab Basics

function, zero initial conditions must be assumed. The Laplace transform of the above

equations are shown below.

After few steps of algebra, you should obtain the following transfer function.

2. State-space

Knowing the fact that the modeling equations (2) are already in the state-variable form, we can

rewrite them into the state-space model.

Since our output is the pitch angle, the output equation is:

Matlab representation and open-loop response

Now, we are ready to observe the system characteristics using Matlab. First, let's obtain an open-

loop system to a step input and determine which system characteristics need improvement. Let

the input (delta e) be 0.2 rad (11 degrees). Create an new m-file and enter the following

commands.

de=0.2;

num=[1.151 0.1774];

den=[1 0.739 0.921 0];

step (de*num,den)

Page 50: Matlab Basics

Running this m-file in the Matlab command window should give you the following plot.

From the plot, we see that the open-loop response does not satisfy the design criteria at all. In

fact the open-loop response is unstable.

If you noticed, the above m-file uses the numerical values from the transfer function. To use the

state-space model, enter the following commands into a new m-file (instead of the one shown

above) and run it in the command window.

de=0.2;

A=[-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0];

B=[0.232; 0.0203; 0];

C=[0 0 1];

D=[0];

step(A,B*de,C,D)

You should get the same response as the one shown above.

Note: It is possible to convert from the state-space to transfer function, or vice versa using

Matlab. To learn more about conversions, see Conversions

Closed-loop transfer function

Page 51: Matlab Basics

To solve this problem, a feedback controller will be added to improve the system performance.

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

A controller needs to be designed so that the step response satisfies all design requirements. Four

different methods to design a controller are listed at the bottom of this page. You may choose:

PID, Root-locus, Frequency response, or State-space.

Example: Modeling the Ball and Beam Experiment

Problem Setup

A ball is placed on a beam, see figure below, where it is allowed to roll with 1 degree of freedom

along the length of the beam. A lever arm is attached to the beam at one end and a servo gear at

the other. As the servo gear turns by an angle theta, the lever changes the angle of the beam by

alpha. When the angle is changed from the vertical position, gravity causes the ball to roll along

the beam. A controller will be designed for this system so that the ball's position can be

manipulated.

Page 52: Matlab Basics

For this problem, we will assume that the ball rolls without slipping and friction between the

beam and ball is negligible. The constants and variables for this example are defined as follows:

M mass of the ball 0.11 kg

R radius of the ball 0.015 m

d lever arm offset 0.03 m

g gravitational acceleration 9.8 m/s^2

L length of the beam 1.0 m

J ball's moment of inertia 9.99e-6 kgm^2

r ball position coordinate

alpha beam angle coordinate

theta servo gear angle

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

System Equations

The Lagrangian equation of motion for the ball is given by the following:

Linearization of this equation about the beam angle, alpha = 0, gives us the following linear

approximation of the system:

The equation which relates the beam angle to the angle of the gear can be approximated as linear

by the equation below:

Page 53: Matlab Basics

Substituting this into the previous equation, we get:

1. Transfer Function

Taking the Laplace transform of the equation above, the following equation is found:

NOTE: When taking the Laplace transform to find the transfer function initial conditions are

assumed to be zero.

Rearranging we find the transfer function from the gear angle (theta(s)) to the ball position

(R(s)).

It should be noted that the above plant transfer function is a double integrator. As such it is

marginally stable and will provide a challenging control problem.

2. State-Space

The linearized system equations can also be represented in state-space form. This can be done by

selecting the ball's position (r) and velocity (rdot) as the state variables and the gear angle (theta)

as the input. The state-space representation is shown below:

Page 54: Matlab Basics

However, for our state-space example we will be using a slightly different model. The same

equation for the ball still applies but instead of controlling the position through the gear angle,

theta, we will control alpha-doubledot. This is essentially controlling the torque of the beam.

Below is the representation of this system:

Note: For this system the gear and lever arm would not be used, instead a motor at the center of

the beam will apply torque to the beam, to control the ball's position.

Matlab Representation and Open-Loop Response

1. Transfer Function

The transfer function found from the Laplace transform can be implemented in Matlab by

inputting the numerator and denominator as vectors. To do this we must create an m-file and

copy the following text into it:

Page 55: Matlab Basics

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

printsys(num,den)

Your output should be:

num/den =

0.21

----------

s^2

Now, we would like to observe the ball's response to a step input of 0.25 m. To do this you will

need to add the following line to your m-file:

step(0.25*num,den)

NOTE: Matlab commands from the control system toolbox are highlighted in red.

You should see the following plot showing the balls position as a function of time:

Page 56: Matlab Basics

From this plot it is clear that the system is unstable in open-loop causing the ball to roll right off

the end of the beam. Therefore, some method of controlling the ball's position in this system is

required. Three examples of controller design are listed below for the transfer function problem.

You may select from PID, Root Locus, and Frequency Response.

2. State-Space

The state-space equations can be represented in Matlab with the following commands (these

equations are for the torque control model).

m = 0.111;

R = 0.015;

g = -9.8;

J = 9.99e-6;

H = -m*g/(J/(R^2)+m);

A=[0 1 0 0

0 0 H 0

0 0 0 1

0 0 0 0];

B=[0;0;0;1];

C=[1 0 0 0];

D=[0];

The step response to a 0.25m desired position can be viewed by running the command below:

step(A,B*.25,C,D)

Your output should look like the following:

Page 57: Matlab Basics

Like the plot for the transfer function this plot shows that the system is unstable and the ball will

roll right off the end of the beam. Therefore, we will require some method of controlling the

ball's position in this system. The State-Space example below shows how to implement a

controller for this type of system.

If you are interested in knowing how to convert state-space representations to transfer function

representations, and vice versa, please refer to the Conversions page.

Page 58: Matlab Basics

PID

3.1 Introduction

This tutorial will show you the characteristics of the each of proportional (P), the integral (I), and

the derivative (D) controls, and how to use them to obtain a desired response. In this tutorial, we

will consider the following unity feedback system:

Plant: A system to be controlled

Controller: Provides the excitation for the plant; Designed to control the overall system

behavior

3.2 The three-term controller

The transfer function of the PID controller looks like the following:

Kp = Proportional gain

KI = Integral gain

Kd = Derivative gain

First, let's take a look at how the PID controller works in a closed-loop system using the

schematic shown above. The variable (e) represents the tracking error, the difference between the

desired input value (R) and the actual output (Y). This error signal (e) will be sent to the PID

controller, and the controller computes both the derivative and the integral of this error signal.

The signal (u) just past the controller is now equal to the proportional gain (Kp) times the

magnitude of the error plus the integral gain (Ki) times the integral of the error plus the

derivative gain (Kd) times the derivative of the error.

Page 59: Matlab Basics

This signal (u) will be sent to the plant, and the new output (Y) will be obtained. This new output

(Y) will be sent back to the sensor again to find the new error signal (e). The controller takes this

new error signal and computes its derivative and its integral again. This process goes on and on.

3.3 Characteristics of P, I, and D controllers

A proportional controller (Kp) will have the effect of reducing the rise time and will reduce ,but

never eliminate, the steady-state error. An integral control (Ki) will have the effect of eliminating

the steady-state error, but it may make the transient response worse. A derivative control (Kd)

will have the effect of increasing the stability of the system, reducing the overshoot, and

improving the transient response. Effects of each of controllers Kp, Kd, and Ki on a closed-loop

system are summarized in the table shown 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 Small Change

Note that these correlations may not be exactly accurate, because Kp, Ki, and Kd are dependent

of 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

Ki, Kp and Kd.

Example Problem

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

Page 60: Matlab Basics

The modeling equation of this system is

(1)

Taking the Laplace transform of the modeling equation (1)

The transfer function between the displacement X(s) and the input F(s) then becomes

Let

M = 1kg

b = 10 N.s/m

k = 20 N/m

F(s) = 1

Plug these values into the above transfer function

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

Fast rise time

Minimum overshoot

No steady-state error

3.4 Open-loop step response

Let's first view the open-loop step response. Create a new m-file and add in the following code:

num=1;

Page 61: Matlab Basics

den=[1 10 20];

step(num,den)

Running this m-file in the Matlab command window should give you the plot shown below.

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 eliminates the

steady-state error.

3.5 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:

Let the proportional gain (Kp) equals 300 and change the m-file to the following:

Page 62: Matlab Basics

Kp=300;

num=[Kp];

den=[1 10 20+Kp];

t=0:0.01:2;

step(num,den,t)

Running this m-file in the Matlab command window should gives you the following plot.

Note: The Matlab function called cloop can be used to obtain a closed-loop transfer function

directly from the open-loop transfer function (instead of obtaining closed-loop transfer function

by hand). The following m-file uses the cloop command that should give you the identical plot as

the one shown above.

num=1;

den=[1 10 20];

Kp=300;

[numCL,denCL]=cloop(Kp*num,den);

t=0:0.01:2;

step(numCL, denCL,t)

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.

Page 63: Matlab Basics

3.6 PD 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:

Let Kp equals to 300 as before and let Kd equals 10. Enter the following commands into an m-

file and run it in the Matlab command window.

Kp=300;

Kd=10;

num=[Kd Kp];

den=[1 10+Kd 20+Kp];

t=0:0.01:2;

step(num,den,t)

This plot shows that the derivative controller reduced both the overshoot and the settling time,

and had small effect on the rise time and the steady-state error.

Page 64: Matlab Basics

3.7 PI 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:

Let's reduce the Kp to 30, and let Ki equals to 70. Create an new m-file and enter the following

commands.

Kp=30;

Ki=70;

num=[Kp Ki];

den=[1 10 20+Kp Ki];

t=0:0.01:2;

step(num,den,t)

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.

Page 65: Matlab Basics

3.8 PID 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:

After several trial and error runs, the gains Kp=350, Ki=300, and Kd=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;

num=[Kd Kp Ki];

den=[1 10+Kd 20+Kp Ki];

t=0:0.01:2;

step(num,den,t)

Now, we have obtained the system with no overshoot, fast rise time, and no steady-state error.

Page 66: Matlab Basics

3.9 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

derivative controller to the system. Keep the controller as simple as possible.

Example: Solution to the Cruise Control Problem Using PID control

The transfer function for this cruise control problem is the following,

m = 1000

b = 50

U(s) = 10

Y(s) = velocity output

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

Page 67: Matlab Basics

The design criteria for this problem are:

Rise time < 5 sec

Overshoot < 10%

Steady state error < 2%

To see the original problem setup, see Cruise Control Modeling page.

Recall from the PID tutorial page, the transfer function of a PID controller is

Let's first take a look at the proportional control.

Proportional control

The first thing to do in this problem is to find a closed-loop transfer function with a proportional

control (Kp) added. By reducing the block diagram, the closed-loop transfer function with a

proportional controller becomes:

Recall from the PID tutorial page, a proportional controller (Kp) decreases the rise time. This is

what we need, if you refer to the Cruise Control Modeling page.

For now, let Kp equals 100 and see what happens to the response. Create an new m-file and enter

the following commands.

kp=100;

m=1000;

b=50;

u=10;

num=[kp];

den=[m b+kp];

t=0:0.1:20;

Page 68: Matlab Basics

step(u*num,den,t)

axis([0 20 0 10])

Running this m-file in the Matlab command window should give you the following step

response.

Note: You can use the Matlab command cloop to find the closed-loop response directly from the

open-loop transfer function. If you choose to do so, change the m-file to the following and run it

in the command window. You should get the same plot as the one shown above.

kp=100;

m=1000;

b=50;

u=10;

num=[1];

den=[m b];

[numc,denc]=cloop(kp*num,den,-1);

t = 0:0.1:20;

step (u*numc,denc,t)

axis([0 20 0 10])

As you can see from the plot, both the steady-state error and the rise time do not satisfy our

design criteria. You can increase the proportional gain (Kp) to improve the system output.

Page 69: Matlab Basics

Change the existing m-file so that Kp equal 10000 and rerun it in the Matlab command window.

You should see the following plot.

The steady-state error has dropped to near zero and the rise time has decreased to less than 0.5

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

The solution to this problem is to choose a 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 is:

Recall from the PID tutrial page, an addition of an integral controller to the system eliminates the

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

response. Change your m-file to the following.

kp = 600;

ki = 1;

m=1000;

b=50;

u=10;

Page 70: Matlab Basics

num=[kp ki];

den=[m b+kp ki];

t=0:0.1:20;

step(u*num,den,t)

axis([0 20 0 10])

Note: If you choose to obtain the closed-loop response directly from the open-loop transfer

function, enter the following commands instead of the ones shown above:

kp=600;

ki=1;

m=1000;

b=50;

u=10;

num=[1];

den=[m b];

num1=[kp ki];

den1=[1 0];

num2=conv(num,num1);

den2=conv(den,den1);

[numc,denc]=cloop(num2,den2,-1);

t=0:0.1:20;

step(u*numc,denc,t)

axis([0 20 0 10])

Whichever the m-file you run, you should get the following output:

Page 71: Matlab Basics

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

large (Ki) most likely unstabilize the response.

With Kp equals 800 and Ki equals 40, the step response will look like the following:

As you can see, this step response meets all design criteria.

PID control

For this particular example, no implementation of a derivative controller was needed to obtain a

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

is.

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

file.

kp=1;

ki=1;

Page 72: Matlab Basics

kd=1;

m=1000;

b=50;

u=10;

num=[kd kp ki];

den=[m+kd b+kp ki];

t=0:0.1:20;

step(u*num,den,t)

axis([0 20 0 10])

Running this m-file should give you the step response of the system with PID controller. 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 require trial and error processes. The best way to

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

changing one variable influences the system output. The characteristics of Kp, Kd, and Ki are

summarized in the PID Tutorial page.

Example: PID Design Method for DC Motor Speed Control

From the main problem, the dynamic equations and the open-loop transfer function of the DC

Motor are:

and the system schematic looks like:

Page 73: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec step input, the design criteria are:

Settling time less than 2 seconds

Overshoot less than 5%

Steady-stage error less than 1%

Now let's design a PID controller and add it into the system. First create a new m-file and type in

the following commands (refer to the Modeling page for the details of getting these commands).

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Recall that the transfer function for a PID controller is:

Proportional control

Let's first try using a proportional controller with a gain of 100. Add the following code to the

end of your m-file:

Kp=100;

numa=Kp*num;

dena=den;

To determine the closed-loop transfer function, we use the cloop command. Add the following

line to your m-file:

[numac,denac]=cloop(numa,dena);

Page 74: Matlab Basics

Note that numac and denac are the numerator and the denominator of the overall closed-loop

transfer function.

Now let's see how the step response looks, add the following to the end of your m-file, and run it

in the command window:

t=0:0.01:5;

step(numac,denac,t)

title('Step response with Proportion Control')

You should get the following plot:

PID control

From the plot above we see that both the steady-state error and the overshoot are too large.

Recall from the PID tutorial page that adding an integral term will eliminate the steady-state

error and a derivative term will reduce the overshoot. Let's try a PID controller with small Ki and

Kd. Change your m-file so it looks like the following. Running this new m-file gives you the

following plot.

J=0.01;

b=0.1;

Page 75: Matlab Basics

K=0.01;

R=1;

L=0.5;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Kp=100;

Ki=1;

Kd=1;

numc=[Kd, Kp, Ki];

denc=[1 0];

numa=conv(num,numc);

dena=conv(den,denc);

[numac,denac]=cloop(numa,dena);

step(numac,denac)

title('PID Control with small Ki and Kd')

Tuning the gains

Now the settling time is too long. Let's increase Ki to reduce the settling time. Go back to your

m-file and change Ki to 200. Rerun the file and you should get the plot like this:

Page 76: Matlab Basics

Now we see that the response is much faster than before, but the large Ki has worsened the

transient response (big overshoot). Let's increase Kd to reduce the overshoot. Go back to the m-

file and change Kd to 10. Rerun it and you should get this plot:

So now we know that if we use a PID controller with

Page 77: Matlab Basics

Kp=100,

Ki=200,

Kd=10,

all of our design requirements will be satisfied.

Example: PID Design Method for the DC Motor Position

From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec step reference, the design criteria are:

Settling time less than 0.04 seconds

Overshoot less than 16%

No steady-state error

No steady-state error due to a disturbance

Now let's design a PID controller and add it into the system. First create a new m-file and type in

the following commands(refer to main problem for the details of getting those commands).

J=3.2284E-6;

b=3.5077E-6;

Page 78: Matlab Basics

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Recall that the transfer function for a PID controller is:

Proportional control

Let's first try using a proportional controller with a gain of 1.7. Add the following code to the

end of your m-file:

Kp=1.7;

numcf=[Kp];

dencf=[1];

numf=conv(numcf,num);

denf=conv(dencf,den);

To determine the closed-loop transfer function, we use the cloop command. Add the following

line to your m-file:

[numc,denc]=cloop(numf,denf);

Note that numc and denc are the numerator and the denominator of the overall closed-loop

transfer function.

Now let's see how the step response looks. Add the following to the end of your m-file, and run it

in the command window:

t=0:0.001:0.2;

step(numc,denc,t)

Page 79: Matlab Basics

You should get the following plot:

Now lets take a look at the step disturbance response. Add the following to the end of your m-

file, and run it in the command window:

numdcl=conv(numc,1);

dendcl=conv(denc,Kp);

step(numdcl,dendcl,t);

You should get the following plot:

Page 80: Matlab Basics

PID control

From the plots above we see that although the steady-state error looks good the settling time is

too large, as is the overshoot. We also see that the steady-state error to a disturbance is large.

Recall from PID tutorial page that adding an integral term will eliminate the steady-state error

and a derivative term will reduce the overshoot. Let's first try a PI controller to get rid of the

disturbance steady state error. Change your m-file so it looks like:

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Kp=1.7;

Ki=20;

numcf=[Kp Ki];

dencf=[1 0];

numf=conv(numcf,num);

denf=conv(dencf,den);

[numc,denc]=cloop(numf,denf,-1);

t=0:0.001:0.4;

step(numc,denc,t)

You should get the following step response:

Page 81: Matlab Basics

Lets see what happened to the step disturbance response, add the following to your m-file:

figure

numdcl=conv(numc,dencf);

dendcl=conv(denc,numcf);

step(numdcl,dendcl,t);

You should get the following plot:

Tuning the gains

The settling time is still too long. Let's increase the gains in order to speed up the response. Go

back to your m-file and change Ki to 200 and Kp to 17. Rerun the file and you should get plots

like these:

Page 82: Matlab Basics

Now we see that the response is faster than before, but the large Ki has worsened the transient

response (big overshoot). Let's now try a PID controller to reduce the overshoot. Go back to the

m-file and make the following changes to look at the step response.

Kp=17;

Ki=200;

Kd=0.15;

numcf=[Kd Kp Ki];

dencf=[1 0];

numf=conv(numcf,num);

denf=conv(dencf,den);

[numc,denc]=cloop(numf,denf,-1);

t=0:0.001:0.1;

step(numc,denc,t)

Rerun it and you should get this plot:

Page 83: Matlab Basics

Your step disturbance plot should look like this:

We now see that our step response looks really good, it has less than 16% overshoot and the

settling time is roughly 40ms, and there is no steady-state error. However the step disturbance

response is now really slow. Lets increase Ki to speed up the disturbance response. Change Ki to

600 in your m-file and rerun the file. You should get the following plots:

Page 84: Matlab Basics

We now can see that the step response has a settling time of roughly 40ms, it has less than 16%

overshoot, and it has no steady state error. The step disturbance response also has no steady state

error. So now we know that if we use a PID controller with

Kp=17,

Ki=600,

Kd=.15,

Page 85: Matlab Basics

all of our design requirements will be satisfied.

Example:PID Design Method for the Bus Suspension System

From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations and schematic, please

refer to the bus modeling page.

We want to design a feedback controller so that when the road disturbance (W) is simulated by a

unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less

than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate

within a range of +/- 5 mm and will stop oscillating within 5 seconds.

The system model can be represented in Matlab by creating a new m-file and entering the

following commands (refer to main problem for the details of getting those commands).

Page 86: Matlab Basics

m1=2500;

m2=320;

k1 = 80000;

k2 = 500000;

b1 = 350;

b2 = 15020;

nump=[(m1+m2) b2 k2]

denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

num1=[-(m1*b2) -(m1*k2) 0 0]

den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

numf=num1;

denf=nump;

Adding a PID controller

Recall that the transfer function for a PID controller is:

where KP is the proportional gain, KI is the integral gain, and KD is the derivative gain. Let's

assume that we will need all three of these gains in our controller. To begin, we might start with

guessing a gain for each: KP=208025, KI=832100 and KD=624075. This can be implemented

into Matlab by adding the following code into your m-file:

KD=208025;

KP=832100;

KI=624075;

numc=[KD,KP,KI];

denc=[1 0];

Page 87: Matlab Basics

Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the

road. From the schematic above we can find the transfer function from the road disturbance W to

the output(X1-X2):

This transfer function can be modeled in Matlab by adding the following code into your m-file:

numa=conv(conv(numf,nump),denc);

dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Note that the function "polyadd" is not a standard function in Matlab; you will need to copy it to

a new m-file to use it. Click here for more information on defining new functions in Matlab.

Refer to the bus modeling page, nump = denf as we can see in the matlab command above. Thus

we can simplified this transfer function to be the following:

numa=conv(numf,denc);

dena=polyadd(conv(denp,denc),conv(nump,numc));

Plotting the closed-loop response

Now we have created the closed-loop transfer function in Matlab that will represent the plant, the

disturbance, as well as the controller. Let's see what the closed-loop step response for this system

looks like before we begin the control process. Keep in mind that we are going to use a 0.1 m

high step as our disturbance, to simulate this, all we need to do is to multiply numa by 0.1. Add

the following code into your m-file:

t=0:0.05:5;

step(0.1*numa,dena,t)

Page 88: Matlab Basics

title('closed-loop response to 0.1m high step w/ pid controller')

you should see the response (X1-X2) to a step W like this:

From the graph, the percent overshoot = 9%, which is 4% larger than the requirement, but the

settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable

output from the beginning, we start with choosing a pole and two zeros for PID controller. A

pole of this controller must be at zero and one of the zeros has to be very close to the pole at the

origin, at 1. The other zero, we will put further from the first zero, at 3, actually we can adjust the

second-zero's position to get the system to fulfill the requirement. Add the following command

in the m-file, so you can adjust the second-zero's location and choose the gain to have a rough

idea what gain you should use for KD,KP, and KI.

z1=1;

z2=3;

p1=0;

numc=conv([1 z1],[1 z2])

denc=[1 p1]

num2=conv(nump,numc);

Page 89: Matlab Basics

den2=conv(denp,denc);

rlocus(num2,den2)

title('root locus with PID controller')

[K,p]=rlocfind(num2,den2)

you should see the closed-loop poles and zeros on the s-plane like this and you can choose the

gain and dominant poles on the graph by yourself:

We will explain root locus method in more detail in the "Root Locus" page.

Choosing the gains for the PID controller

Now that we have the closed-loop transfer function, controlling the system is simply a matter of

changing the KD,KP,and KI variables. From the figure above, we can see that the system has

larger damping than required, but the settling time is very short. This response still doesn't satisfy

the 5% overshoot requirement. As mentioned before, this can be rectified by adjusting the KD,

KP and KI variables to find better response. Let's increase KP,KI,KD by 2 times to see what

will happen. Go back to your m-file and multiply KP,KI,KD by 2 and then rerun the program,

you should get the following plot:

Page 90: Matlab Basics

To compare this graph with the graph of low-gain PID controller, you can change the axis:

axis([0 5 -.01 .01])

Page 91: Matlab Basics

Now we see that the percent overshoot and settling time meet the requirements of the system.

The percent overshoot is about 5% of the input's amplitude and settling time is 2 seconds less

than 5 seconds from requirement.

For this problem, it turns out that the PID design method adequately controls the system. This

can been seen by looking at the root locus plot. Such a task can be achieved by simply changing

only the gains of a PID controller. Feel free to play around with all three of the

parameters,KD,KP and KI, as we suggested, but you will most likely get the response to have

either large percent overshoot or very long settling time. But if you do find a good PID design,

please email us with your results! We are always interested in different ways to solve our

examples; we may include your solution in a future version of these tutorials.

Example: Solution to the Inverted Pendulum Problem Using PID Control

The transfer function of the plant for this problem is given below:

where,

The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:

Settling time of less than 5 seconds.

Pendulum should not move more than 0.05 radians away from the vertical.

To see how this problem was originally set up, consult the inverted pendulum modeling page.

Page 92: Matlab Basics

Open-loop Representation

The first thing to do when using PID control in Matlab is to find the transfer function of the

system and to check to see if it makes sense. The transfer function found from the Laplace

transforms for the output Phi (the pendulum's angle) can be set up using Matlab by inputting the

numerator and denominator as vectors. Create an m-file (or a '.m' file located in the same

directory as Matlab) and copy the following text to model the transfer function:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0]

den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]

Your output should be:

num =

4.5455 0

den =

1.0000 0.1818 -31.1818 -4.4545

Closed-loop transfer function

The control of this problem is a little different than the standard control problems you may be

used to. Since we are trying to control the pendulum's position, which should return to the

vertical after the initial disturbance, the reference signal we are tracking should be zero. The

force applied to the cart can be added as an impulse disturbance. The schematic for this problem

should look like the following.

Page 93: Matlab Basics

It will be easier to determine the appropriate transfer function to enter into Matlab if we first

rearrange the schematic as follows:

Now, we can find the closed-loop transfer function.

Adding the PID controller

This closed-loop transfer function can be modeled in Matlab by copying the following code to

the end of your m-file (whether your using the transfer function from the Laplace transforms or

from the state-space representation):

kd = 1;

k = 1;

ki = 1;

numPID = [kd k ki];

denPID = [1 0];

numc = conv(num,denPID)

denc = polyadd(conv(denPID,den),conv(numPID,num))

Note: Non-standard Matlab commands used in this example are highlighted in green.

The function polyadd is not in the Matlab toolbox. You will have to copy it to a new m-file to

use it. This transfer function assumes that both derivative and integral control will be needed

Page 94: Matlab Basics

along with proportional control. This does not have to be the case. If you wanted to start with PI

control, just remove the kd term from numPID. If you wanted to start with PD control, just

remove the ki term from numPID and change denPID to equal [1]. Assuming you do not change

the PID control, you should get the following closed-loop numerator and denominator in the

Matlab command window:

numc =

4.5455 0 0

denc =

1.0000 4.7273 -26.6363 0.0910 0

Now we can begin the actual control of this system. First let's see what the impulse response

looks like with the numbers we already have. Enter the following code to the end of your m-file:

t=0:0.01:5;

impulse(numc,denc,t)

axis([0 1.5 0 40])

You should get the following velocity response plot from the impulse disturbance:

Page 95: Matlab Basics

This response is still not stable. Let's start by increasing the proportional control to the system.

Increase the k variable to see what effect it has on the response. If you set k=100, and set the axis

to axis([0, 2.5, -0.2, 0.2]), you should get the following velocity response plot:

The settling time is acceptable at about 2 seconds. Since the steady-state error has already been

reduced to zero, no more integral control is needed. You can remove the integral gain constant to

see for yourself that the small integral control is needed. The overshoot is too high, so that must

be fixed. To alleviate this problem, increase the kd variable. With kd=20, you should get a

satisfactory result. You should now see the following velocity response plot:

Page 96: Matlab Basics

As you can see, the overshoot has been reduced so that the pendulum does not move more than

0.05 radians away from the vertical. All of the design criteria have been met, so no further

iteration is needed.

What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given. The

diagram was not entirely complete. The block representing the the position was left out because

that variable was not being controlled. It is interesting though, to see what is happening to the

cart's position when the controller for the pendulum's angle is in place. To see this we need to

consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

The feedback loop represents the controller we have designed for the pendulum's. The transfer

function from the cart's position to the impulse force, with the PID feedback controller which we

designed, is given as follows:

Page 97: Matlab Basics

Recall that den1=den2 if the pole/zero at the origin that was cancelled is added back in. So the

transfer function from X to F can be simplified to:

Now that we have the transfer function for the entire system, let's take a look at the response.

First we need the transfer function for the cart's position. To get this we need to go back to the

laplace transforms of the system equations and find the transfer function from X(s) to U(s).

Below is this transfer function:

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page.

The pole/zero at the origin cancelled out of the transfer function for Phi, has been put back in. So

that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the

command window:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

Page 98: Matlab Basics

num1 = [m*l/q 0 0];

den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];

den2 = den1

kd = 20;

k = 100;

ki = 1;

numPID = [kd k ki];

denPID = [1 0];

numc = conv(num2,denPID);

denc = polyadd(conv(denPID,den2),conv(numPID,num1));

t=0:0.01:5;

impulse(numc,denc,t)

As you can see, the cart moves in the negative direction with a constant velocity. So although the

PID controller stabilizes the angle of the pendulum, this design would not be feasible to

implement on an actual physical system.

Page 99: Matlab Basics

Example: PID Design method for the Pitch Controller

In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the

pitch angle (theta).

The design requirements are

Overshoot: Less than 10%

Rise time: Less than 2 seconds

Settling time: Less than 10 seconds

Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page.

Recall from the PID Tutorial page, the transfer function of a PID controller is:

We will implement combinations of proportional (Kp), integral (Ki), and derivative (Kd)

controllers in an unity feedback system shown below to study the system output.

Let's first take a look at a proportional control.,

Proportional control

The first thing in solving this problem using PID control is to find a closed-loop transfer function

with a proportional control (Kp). A closed-loop transfer function can be obtained either by hand

Page 100: Matlab Basics

or using the Matlab function called cloop. Either way, you should obtain the closed-loop transfer

function as:

Note: Matlab cannot manipulate symbolic variables. To use the function cloop, enter the

following commands to an m-file and run it in the Matlab command window. You should obtain

the numerical coefficients of numerator and denominator of the closed-loop transfer function.

Kp=[1]; %Enter any numerical value for the proportional gain

num=[1.151 0.1774];

num1=conv(Kp,num);

den1=[1 0.739 0.921 0];

[numc,denc]=cloop (num1,den1)

For now, let the proportional gain (Kp) equal 2 and observe the system behavior. We will use the

closed-loop transfer function derived by hand. Enter the following commands into a new m-file

and run it in the Matlab command window. You should obtain the step response similar to the

one shown below:

de=0.2;

Kp=2;

numc=Kp*[1.151 0.1774];

denc=[1 0.739 1.151*Kp+0.921 0.1774*Kp];

t=0:0.01:30;

step (de*numc,denc,t)

Page 101: Matlab Basics

As you see, both the overshoot and the settling time need some improvement.

Proportional-Derivative control

Recall from the PID Tutorial page, the derivative controller will reduce both the overshoot and

the settling time. Let's try a PD controller. The closed-loop transfer function of the system with a

PD controller is:

Using the commands shown below and with several trial-and-error runs, a proportional gain (Kp)

of 9 and a derivative gain (Kd) of 4 provided the reasonable response. To comfirm this, change

your m-file to the following and run it in the Matlab command window. You should obtain the

step response similar to the one shown below:

de=0.2;

Kp=9;

Kd=4;

numc=[1.151*Kd 1.151*Kp+0.1774*Kd 0.1774*Kp];

denc=[1 0.739+1.151*Kd 0.921+1.151*Kp+0.1774*Kd 0.1774*Kp];

t=0:0.01:10;

step (de*numc,denc,t)

Page 102: Matlab Basics

This step response shows the rise time of less than 2 seconds, the overshoot of less than 10%, the

settling time of less than 10 seconds, and the steady-state error of less than 2%. All design

requirements are satisfied.

Proportional-Integral-Derivative control

Even though all design requirements were satisfied with the PD controller, the integral controller

(Ki) can be added to reduce the sharp peak and obtain smoother response. After several trial-and-

error runs, the proportional gain (Kp) of 2, the integral gain (Ki) of 4, and the derivative gain

(Kd) of 3 provided smoother step response that still satisfies all design requirements. To cofirm

this, enter the following commands to an m-file and run it in the command window. You should

obtain the step response shown below:

Note: This time we are going to use the function cloop to find the closed-loop transfer function,

and then obtain the step response.

de=0.2;

Kp=2;

Kd=3;

Ki=4;

numo=[1.151 0.1774];

deno=[1 0.739 0.921 0];

numpid=[Kd Kp Ki];

Page 103: Matlab Basics

denpid=[1 0];

num1=conv(numo,numpid);

den1=conv(deno,denpid);

[numc,denc] = cloop(num1,den1);

t=0:0.01:10;

step (de*numc,denc,t)

Example: Solution to the Ball & Beam Problem Using PID Control

The open-loop transfer function of the plant for the ball and beam experiment is given below:

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

To see the derivation of the equations for this problem refer to the ball and beam modeling page.

Page 104: Matlab Basics

Closed-loop Representation

The block diagram for this example with a controller and unity feedback of the ball's position is

shown below:

First, we will study the response of the system shown above when a proportional controller is

used. Then, derivative and/or integral control will be added if necessary.

Recall, that the transfer function for a PID controller is:

Proportional Control

The closed-loop transfer function for proportional control with a proportional gain (kp) equal to

100, can be modeled by copying the following lines of Matlab code into an m-file (or a '.m' file

located in the same directory as Matlab)

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

kp = 1;

numP = kp*num;

[numc, denc] = cloop(numP, den)

NOTE: Matlab commands from the control system toolbox are highlighted in red.

Page 105: Matlab Basics

You numerator and denominator should be:

numc =

0 0 0.2100

denc =

1.0000 0 0.2100

Now, we can model the system's response to a step input of 0.25 m. Add the following line of

code to your m-file and run it:

step(0.25*numc,denc)

You should get the following output:

As, you can see the addition of proportional gain does not make the system stable. Try changing

the value of kp and note that the system remains unstable.

Proportional-Derivative Control

Now, we will add a derivative term to the controller. Copy the following lines of code to an m-

file and run it to view the system's response to this control method.

Page 106: Matlab Basics

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

kp = 10;

kd = 10;

numPD = [kd kp];

numh = conv(num, numPD);

[numc, denc] = cloop(numh, den);

t=0:0.01:5;

step(0.25*numc,denc,t)

Your plot should be similar to the following:

Now the system is stable but the overshoot is much too high and the settling time needs to go

down a bit. From the PID tutorial page in the section on characteristics of P, I, and D controllers,

Page 107: Matlab Basics

we see that by increasing kd we can lower overshoot and decrease the settling time slightly.

Therefore, make kd = 20 in your m-file and run it again. Your output should be:

The overshoot criterion is met but the settling time needs to come down a bit. To decrease the

settling time we may try increasing the kp slightly to increase the rise time. The derivative gain

(kd) can also be increased to take off some of the overshoot that increasing kp will cause. After

playing with the gains a bit, the following step response plot can be achieved with kp = 15 and

kd = 40:

Page 108: Matlab Basics

As you can see from the above plot all the control objectives have been met without the use of an

integral controller (settling time for this example is considered achieved when the response is

less than 2% of it's final value). Remember, that for a control problem there is more than one

solution for the problem.

For other methods of controlling the ball and beam example, see the root locus, frequency

response, and state-space links below.

Page 109: Matlab Basics

Root Locus

Key Matlab commands used in this tutorial: cloop, rlocfind, rlocus, sgrid, step

Matlab commands from the control system toolbox are highlighted in red.

4.1 Closed-loop poles

The root locus of an (open-loop) transfer function H(s) 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:

and thus the poles of the closed loop system are values of s such that 1 + K H(s) = 0.

If we write H(s) = b(s)/a(s), then this equation has the form:

Let n = order of a(s) and m = order of b(s) [the order of a polynomial is the highest power of s

that appears in it].

We will consider all positive values of k. In the limit as k -> 0, the poles of the closed-loop

system are a(s) = 0 or the poles of H(s). In the limit as k -> infinity, the poles of the closed-loop

system are b(s) = 0 or the zeros of H(s).

Page 110: Matlab Basics

No matter what we pick k to be, the closed-loop system must always have n poles, where n is

the number of poles of H(s). The root locus must have n branches, each branch starts at a pole

of H(s) and goes to a zero of H(s). If H(s) has more poles than zeros (as is often the case), m < n

and we say that H(s) has zeros at infinity. In this case, the limit of H(s) as s -> infinity is zero.

The number of zeros at infinity is n-m, 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).

4.2 Plotting the root locus of a transfer function

Consider an open loop system which has a transfer function of

How do we design a feed-back 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:

num=[1 7];

den=conv(conv([1 0],[1 5]),conv([1 15],[1 20]));

rlocus(num,den)

axis([-22 3 -15 15])

Page 111: Matlab Basics

4.3 Choosing a value of K from the root loocus

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 (Zeta) and natural

frequency (Wn) [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 Zeta of greater than

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

the Matlab command window:

zeta=0.7;

Wn=1.8;

sgrid(zeta, Wn)

Page 112: Matlab Basics

On the plot above, the two white dotted lines at about a 45 degree angle indicate pole locations

with Zeta = 0.7; in between these lines, the poles will have Zeta > 0.7 and outside of the lines

Zeta < 0.7. The semicircle indicates pole locations with a natural frequency Wn = 1.8; inside the

circle, Wn < 1.8 and outside the circle Wn > 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 between 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

rlocfind command in Matlab to choose the desired poles on the locus:

[kd,poles] = rlocfind(num,den)

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.

Page 113: Matlab Basics

Note that since the root locus may has 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 white "+") are at reasonable

positions. We can go ahead and use the chosen kd as our proportional controller.

4.4 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:

[numCL, denCL] = cloop((kd)*num, den)

The two arguments to the function cloop 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(numCL,denCL)

Page 114: Matlab Basics

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

Example: Solution to the Cruise Control Problem Using Root Locus Method

The open-loop transfer function for this problem is:

where

m=1000

b=50

U(s)=10

Y(s)=velocity output

The design criteria are:

Rise time < 5 sec

Overshoot < 10%

Steady state error < 2%

To see the original problem setup, refer to Cruise Control Modeling page.

Page 115: Matlab Basics

Proportional Controller

Recall from the Root-Locus Tutorial 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 (Kp) will be considered to solve this problem. Then, the closed-loop transfer function

becomes:

Also, from the Root-Locus Tutorial, we know that the Matlab command called sgrid should be

used to find 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:

Where:

Wn=Natural frequency

zeta=Damping ratio

Tr=Rise time

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 an new m-file and enter the following commands.

Page 116: Matlab Basics

hold off;

m = 1000;

b = 50;

u = 10;

numo=[1];

deno=[m b];

figure

hold;

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

rlocus (numo,deno)

sgrid(0.6, 0.36)

[Kp, poles]=rlocfind(numo,deno)

figure

hold;

numc=[Kp];

denc=[m (b+Kp)];

t=0:0.1:20;

step (u*numc,denc,t)

axis ([0 20 0 10])

Running this m-file should give you the following root-locus plot.

Page 117: Matlab Basics

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.

If you look at the Matlab 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).

You should see the gain value (Kp) and pole locations in the Matlab command window. Also

you should see the closed-loop step response similar to the one shown below.

With the specified gain Kp (the one you just picked), the rise time and the overshoot criteria have

been met; however, the steady-state error of more than 10% remained.

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:

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

Page 118: Matlab Basics

Finally, the closed-loop transfer function becomes:

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

Compensator 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 equals -0.3 and Po equals -0.03.

Create an new m-file, and enter the following commands.

hold off;

m = 1000;

b = 50;

u = 10;

Zo=0.3;

Po=0.03;

numo=[1 Zo];

deno=[m b+m*Po b*Po];

figure

hold;

axis ([-0.6 0 -0.4 0.4])

rlocus(numo,deno)

sgrid(0.6,0.36)

[Kp, poles]=rlocfind(numo,deno)

figure

t=0:0.1:20;

numc=[Kp Kp*Zo];

denc=[m b+m*Po+Kp b*Po+Kp*Zo];

axis ([0 20 0 12])

Page 119: Matlab Basics

step (u*numc,denc,t)

Running this m-file should give you the root-locus plot similar to the following:

In the Matlab command window, you should see the prompt asking you to select a point on the

root-locus plot. Once again, click on the real axis around -0.4. You should have the following

response.

As you can see, the steady-state error has been reduced to near zero. Slight overshoot is a result

of the zero added in the lag controller.

Page 120: Matlab Basics

Now all of the design criteria have been met and no further iterations will be needed.

Example: Root Locus Design Method for DC Motor Speed Control

From the main problem, the dynamic equations and the open-loop transfer function of DC Motor

Speed are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec step reference, the design criteria are:

Settling time less than 2 seconds

Overshoot less than 5%

Steady-state error less than 1%

Now let's design a controller using the root locus method.

Create a new m-file and type in the following commands (refer to main problem for the details of

getting those commands).

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

Page 121: Matlab Basics

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Drawing the open-loop root locus

The main idea of root locus design is to find the closed-loop response from the open-loop root

locus plot. Then by adding zeros and/or poles to the original plant, the closed-loop response can

be modified. Let's first view the root locus for the plant. Add the following commands at the end

of your m-file.

rlocus(num,den)

sgrid(.8,0)

sigrid(2.3)

title('Root Locus without a controller')

The command sigrid is the user-defined function. You need to copy the sigrid.m file to your

directly before using it. For more information on how to use functions, refer to functions.

Two arguments in the sgrid command are the damping ratio (zeta) term (0.8 corresponds to a

overshoot of 5%), and the natural frequency (Wn) term (= 0 corresponds to no rise time

criterion) respectively. The single argument in the sigrid command is the sigma term (4.6/2

seconds = 2.3). After you have saved sigma.m file to your directly, run the above m-file in the

command window. You should get the root locus plot shown below:

Page 122: Matlab Basics

Finding the gain using the rlocfind command

If you recall, we need the settling time and the overshoot to be as small as possible. Large

damping corresponds to points on the root locus near the real axis. A fast response corresponds

to points on the root locus far to the left of the imaginary axis. To find the gain corresponding to

a point on the root locus, we can use the rlocfind command. We can find the gain and plot the

step response using this gain all at once. To do this, enter the following commands at the end of

your m-file and rerun it.

[k,poles] = rlocfind(num,den)

[numc,denc]=cloop(k*num,den,-1);

t=0:0.01:3;

step(numc,denc,t)

title('Step response with gain')

Go to the plot and select a point on the root locus half-way between the real axis and the

damping requirement, say at -6+2.5i. Matlab should return the output similar to the following.

selected_point =

-5.9596 + 2.0513i

k =

10.0934

poles =

-6.0000 + 2.0511i

-6.0000 - 2.0511i

Note that the values returned in your Matlab command window may not be exactly the same, but

should at least have the same order of magnitude. You should also get the following plot:

Page 123: Matlab Basics

As you can see, the system is overdamped and the settling time is about one second, so the

overshoot and settling time requirements are satisfied. The only problem we can see from this

plot is the steady- state error of about 50%. If we increase the gain to reduce the steady-state

error, the overshoot becomes too large (Try this yourself). We need to add a lag controller to

reduce the steady-state error.

Adding a lag controller

From the plot we see that this is a very simple root locus. The damping and settling time criteria

were met with the proportional controller. The steady-state error is the only criterion not met

with the proportional controller. A lag compensator can reduce the steady-state error. By doing

this, we might however increase our settling time. Try the following lag controller first:

This can be done by changing your m-file to look like the following:

J=0.01;

b=0.1;

K=0.01;

R=1;

Page 124: Matlab Basics

L=0.5;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

z1=1;

p1=0.01;

numa = [1 z1];

dena = [1 p1];

numb=conv(num,numa);

denb=conv(den,dena);

rlocus(numb,denb)

sgrid(.8,0)

sigrid(2.3)

title('Root Locus with a lag controller')

numa and dena are the numerator and denominator of the controller, and numb and denb are

the numerator and denominator of the overall open-loop transfer function.

You should get the following root locus, which looks very similar to the original one:

Page 125: Matlab Basics

Plotting the closed-loop response

Now let's close the loop and see the closed-loop step response Enter the following code at the

end of your m-file:

[k,poles]=rlocfind(numb,denb)

[numc,denc]=cloop(k*numb,denb,-1);

t=0:0.01:3;

step(numc,denc,t)

title('Step response with a lag controller')

Rerun this m-file in the Matlab command window. When prompted to select a point, pick one

that is near the damping requirement (diagonal dotted line). You should get the a plot similar to

the following:

Your gain should be about 20. As you can see the response is not quite satisfactory. You may

also note that even though the gain was selected to correlate with a position close to the damping

criterion, the overshoot is not even close to five percent. This is due to the effect of the lag

controller kicking in at a later time than the plant. (its pole is slower). What this means is that we

can go beyond the dotted lines that represent the limit, and get the higher gains without worrying

about the overshoot . Rerun your m-file, place the gain just above the white, dotted line. Keep

Page 126: Matlab Basics

trying until you get a satisfactory response. It should look similar to the following (we used a

gain of around 50):

The steady-state error is smaller than 1%, and the settling time and overshoot requirements have

been met. As you can see, the design process for root locus is very much a trial and error process.

That is why it is nice to plot the root locus, pick the gain, and plot the response all in one step. If

we had not been able to get a satisfactory response by choosing the gains, we could have tried a

different lag controller, or even added a lead controller.

Example: Root Locus Design Method for DC Motor Position Control

From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

Page 127: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec step reference, the design criteria are:

Settling time less than 0.04 seconds

Overshoot less than 16%

No steady-state error

No steady-state error due to a disturbance

Now let's design a controller using the root locus method.

Create a new m-file and type in the following commands (refer to main problem for the details of

getting those commands).

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Drawing the open-loop root locus

The main idea of root locus design is to find the closed-loop response from the open-loop root

locus plot. Then by adding zeros and/or poles to the original plant, the closed-loop response will

be modified. Let's first view the root locus for the plant. Add the following commands at the end

of your m-file.

rlocus(num,den)

sgrid(.5,0)

sigrid(100)

Page 128: Matlab Basics

The commands sgrid and sigrid are functions. Sgrid is a function in the Matlab tool box, but

sigrid is not. You need to copy the sigrid.m file to your directly. Click here to see how to copy

sigrid.m into an m-file. The variables in the sgrid command are the zeta term (0.5 corresponds to

a overshoot of 16%), and the wn term (no rise time criteria) respectively. The variable in the

sigrid command is the sigma term (4/0.04 seconds = 100). Run the above m-file and you should

get the root locus plot below:

If you look at the axis scales on this plot, one open-loop pole is very far to the left (further than -

1x10^6). This pole does not affect the closed loop dynamics unless very large gains are used,

where the system becomes unstable. We will ignore it by changing the axes to zoom in to just the

lower-gain portion of the root locus (Click here for more info.) Add the following command to

your m-file and re-run it.

axis([-400 100 -200 200])

You should obtain the following plot in which you can see that the closed-loop system will be

stable for small gains.

Page 129: Matlab Basics

We can see from this plot that the closed-loop poles are never fast enough to meet the settling

time requirement (that is, they never move to the left of the sigma=100 vertical line). Also, recall

that we need an integrator in the controller (not just in the system) to remove steady-state error

due to a disturbance.

Integral Control

Now, let's try using integral control to remove steady-state error to a disturbance. Modify your

m-file so it looks like:

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

numcf=[1];

dencf=[1 0];

numf=conv(numcf,num);

denf=conv(dencf,den);

rlocus(numf,denf)

Page 130: Matlab Basics

sgrid(.5,0)

sigrid(100)

axis([-400 100 -200 200])

Note that this adds a 1/s term to the forward loop. In this m-file, we are ignoring the fast pole,

and just zooming in to the low-gain portion of the root locus. Run this m-file and you will obtain

the following plot.

From this root locus we can see that the closed-loop system under integral control is never stable,

and another controller must be used.

Proportional plus Integral Control

Now, let's modify the integral controller to a PI controller. Using PI instead of I control adds a

zero to the open-loop system. We'll place this zero at s=-20. The zero must lie between the open-

loop poles of the system in this case so that the closed-loop will be stable. Change the lines

defining the controller (numcf and dencf) in your m-file to the following.

\

numcf=[1 20];

dencf=[1 0];

Re-run your m-file and obtain the following plot.

Page 131: Matlab Basics

Now, we have managed to stabilize the system with zero steady-state error to a disturbance, but

the system will still not be fast enough.

Proportional plus Integral plus Derivative Control

In order to pull the root locus further to the left, to make it faster, we need to place a second

open-loop zero, resulting in a PID controller. After some experimentation, we can place the two

PID zeros at s=-60 and s=-70. Change the lines defining the controller (numcf and dencf) in your

m-file to the following.

numcf=conv([1 60],[1 70]);

dencf=[1 0];

Re-run your m-file and obtain the following plot.

Page 132: Matlab Basics

Now, we can see that two of the closed-loop poles loop around well within both the settling time

and percent overshoot requirements. The third closed loop pole moves from the open-loop pole

at s=-59.2 to the open loop zero at s=-60. This closed-loop pole nearly cancels with the zero

(which remains in the closed loop transfer function) because it is so close. Therefore, we can

ignore it's effect. However, the other open-loop zero also remains in the closed-loop, and will

affect the response, slowing it down, and adding overshoot. Therefore, we have to be

conservative in picking where on the root locus we want the closed-loop poles to lie.

Finding the gain using the rlocfind command and plotting the closed-loop response

If you recall, we need the settling time and the overshoot to be as small as possible, particularly

because of the effect of the extra zero. Large damping corresponds to points on the root locus

near the real axis. A fast response corresponds to points on the root locus far to the left of the

imaginary axis. To find the gain corresponding to a point on the root locus, we can use the

rlocfind command. We can find the gain and plot the step response using this gain all at once. To

do this, enter the following commands at the end of your m-file and rerun it.

[k,poles] = rlocfind(numf,denf)

[numc,denc]=cloop(k*numf,denf,-1);

t=0:0.001:.1;

step(numc,denc,t)

Go to the plot and select a point on the root locus on left side of the loop, close to the real axis as

shown below with the small black + marks. This will ensure that the response will be as fast as

possible with as little overshoot as possible. These pole locations would indicate that the

response would have almost no overshoot, but you must remember that the zero will add some

overshoot.

Page 133: Matlab Basics

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

selected_point =

-1.3943e+02+ 1.8502e+01i

k =

0.1309

poles =

1.0e+06 *

-1.4542

-0.0001 + 0.0000i

-0.0001 - 0.0000i

-0.0001

Note that the values returned in your Matlab command window may not be exactly the same, but

should at least have the same order of magnitude. You should also get the following step

response plot:

Page 134: Matlab Basics

As you can see, the system has an overshoot of approximately 15%, a settling time of

approximately 0.04 seconds, and no steady-state error.

Let's now look at the disturbance response by computing the closed-loop disturbance transfer

function and plotting its step response. Add the following lines to your m-file:

numdcl=conv(numc,dencf);

dendcl=conv(denc,numcf);

step(numdcl,dendcl,t);

Re-run your m-file, selecting the same point on the root locus, and you will get the following

plot.

Page 135: Matlab Basics

You can see that the disturbance reaches a steady-state value of zero, and in fact, stays within

0.02 (or 2%) afte 0.04 seconds. Therefore, all the design requirements have been met.

In this example, we placed zeros on the root locus to shape it the way we wanted. While some

trial-and error is necessary to place the zeros, it is helpful to understand how the root locus is

drawn, and Matlab is used to verify and refine the placement.

Example: Root Locus Design Method for the Bus Suspension System

From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations and schematic, please

refer to the bus modeling page.

If you are interested in running an animation of this example based on the control techniques

used in the root locus tutorial please go to the bus suspension animation page after completing

this tutorial.

Page 136: Matlab Basics

We want to design a feedback controller so that when the road disturbance (W) is simulated by a

unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less

than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate

within a range of +/- 5 mm and will stop oscillating within 5 seconds.

The system model can be represented in Matlab by creating a new m-file and entering the

following commands (refer to main problem for the details of getting those commands).

m1=2500;

m2=320;

k1 = 80000;

k2 = 500000;

b1 = 350;

b2 = 15020;

nump=[(m1+m2) b2 k2]

denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

num1=[-(m1*b2) -(m1*k2) 0 0]

den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

numf=num1;

denf=nump;

We are now ready to design a controller using the root locus design method.

First let's see what the open loop poles of the system are:

R=roots(denp)

Matlab should return:

R =

-23.9758 +35.1869i

-23.9758 -35.1869i

-0.1098 + 5.2504i

-0.1098 - 5.2504i

Page 137: Matlab Basics

Therefore the dominant poles are the roots -0.1098+/-5.2504i, which are close to the complex

axis with a small damping ratio.

Plotting the root locus

The main idea of root locus design is to estimate the closed-loop response from the open-loop

root locus plot. By adding zeros and/or poles to the original system (adding a compensator), the

root locus and thus the closed-loop response will be modified. Let's first view the root locus for

the plant. In your m-file, add the following command and then run the file, you should get the

root locus plot below:

rlocus(nump,denp)

z=-log(0.05)/sqrt(pi^2+(log(0.05)^2))

sgrid(z,0)

Note from the specification, we required the overshoot, %OS, to be less than 5% and damping

ratio, zeta, can be find from approximation damping ratio equation, z = -

log(%OS/100)/sqrt(pi^2+[log(%OS/100)^2]). The commandsgrid is used to overlay desired

percent overshoot line on the close-up root locus, you can find more information from

commands list.

From the plot above, we see that there are two pair of the poles and zeros that are very close

together. These pair of poles and zeros are almost on the imaginary axis, they might make the

Page 138: Matlab Basics

bus system marginally stable, which might cause a problem. We have to make all of the poles

and zeros move into the left-half plane as far as possible to avoid an unstable system. We have to

put two zeros very close to the two poles on the imaginary axis of uncompensated system for

pole-and-zero cancellation. Moreover, we put another two poles further on the real axis to get

fast response.

Adding a notch filter

We will probably need two zeros near the two poles on the complex axis to draw the root locus,

leaving those poles to the compensator zeros instead of to the plant zeros on the imaginary axis.

We'll also need two poles placed far to the left to pull the locus to the left. It seems that a notch

filter (2-lead controller) will probably do the job. Let's try putting the zeros at 30 and 60 and the

poles at 3+/-3.5i. In your m-file add the following lines of code:

z1=3+3.5i;

z2=3-3.5i;

p1=30;

p2=60;

numc=conv([1 z1],[1 z2]);

denc=conv([1 p1],[1 p2]);

rlocus(conv(nump,numc),conv(denp,denc))

Add % in front of the rlocus(nump,denp) command (Matlab treats any text after a % sign as a

comment) and rerun the m-file, you should get a new root locus plot looking like this:

Page 139: Matlab Basics

Now let's change axis to see the details of the root locus.

rlocus(conv(nump,numc),conv(denp,denc))

axis([-40 10 -30 30])

z=-log(0.05)/sqrt(pi^2+(log(0.05)^2))

sgrid(z,0)

:

Finding the gain from the root locus

Now that we have moved the root locus across the 5% damping ratio line, we can choose a gain

that will satisfy the design requirements. Recall that we want the settling time and the overshoot

to be as small as possible. Generally, to get a small overshoot and a fast response, we need to

select a gain corresponding to a point on the root locus near the real axis and far from the

complex axis or the point that the root locus crosses the desired damping ratio line. But in this

case, we need the cancellation of poles and zeros near the imaginary axis, so we need to select a

gain corresponding to a point on the root locus near zeros and percent overshoot line. There is a

method to do this with the rlocfind command in matlab. Enter the following command into the

Matlab command window:

Page 140: Matlab Basics

[k,poles]=rlocfind(conv(nump,numc),conv(denp,denc))

Go to the plot and select the point at the position mentioned above (indicated by the white cross

on the plot below:

You should see something similar to the following:

selected_point =

-2.9428 -13.0435i

K =

1.0678e+08

poles =

1.0e+02 *

-0.6322 + 6.1536i

-0.6322 - 6.1536i

Page 141: Matlab Basics

-0.0294 + 0.1306i

-0.0294 - 0.1306i

-0.0292 + 0.0367i

-0.0292 - 0.0367i

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. Add this gain to the system:

numc=k*numc;

Recall that the schematic of the system is the following:

and the closed-loop transfer function can be derived as following:

To obtain the closed-loop transfer function from W to X1-X2, add the following to your m-file:

numa=conv(conv(numf,nump),denc);

dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Page 142: Matlab Basics

Note that the function "polyadd" is not a Matlab standard function. You will need to copy it to a

new m-file to use it. Click here for more information.

Plotting closed-loop response

Let's see what the closed-loop step response looks like with this compensator. Keep in mind that

we are going to use a 0.1 m high step as the disturbance. To simulate this, simply multiply numa

by 0.1. Add the following commands into the m-file and put % marks in front of all rlocus and

rlocfind commands.

step(0.1*numa,dena)

title('closed-loop response to 0.1m high step w/ notch filter')

and you should see the following plot:

From this plot we see that when the bus encounters a 0.1 m step on the road, the maximum

deviation of the bus body from the wheel (or the road) is about 3.75 mm, and the oscillations

settle in 2 seconds. Thus this response is satisfactory.

Note: A design problem does not necessarily have an unique answer. Using the root locus

method (or any other method) may result in many different compensators. For practice, you may

Page 143: Matlab Basics

want to go back to the original open-loop root locus and try to find other good ways to add zeros

and poles to get a better response.

If you are interested in running an animation of the bus suspension example based on the control

techniques used in this tutorial please go to the Bus Suspension Animation Page.

Example: Solution to the inverted pendulum problem using Root Locus method

The transfer function of the plant for this problem is given below:

where,

The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:

Settling time of less than 5 seconds.

Pendulum should not move more than 0.05 radians away from the vertical.

To see how this problem was originally set up, consult the inverted pendulum modeling page.

Transfer functions

The rlocus command in Matlab can find the root locus for a system described by state-space

equations or by a transfer function. For this problem it will be easier in the long run to use a

transfer function (the reason for this will become clear later). The transfer function found from

the Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab by

inputting the numerator and denominator as vectors. Create an m-file (or a '.m' file located in the

same directory as Matlab) and copy the following text to model the transfer function:

Page 144: Matlab Basics

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0]

den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]

Your output should be:

num =

4.5455 0

den =

1.0000 0.1818 -31.1818 -4.4545

Block Diagram

The control of this problem is a little different than the standard control problems you may be

used to. Since we are trying to control the pendulum's position, which should return to the

vertical after the initial disturbance, the reference signal we are tracking should be zero. The

force applied to the cart can be added as an impulse disturbance. The schematic for this problem

should look like the following.

It will be easier to determine the appropriate transfer function to enter into Matlab if we first

rearrange the schematic as follows:

Page 145: Matlab Basics

Now, we can find the closed-loop transfer function.

Root locus design

This closed-loop transfer function can be modeled in Matlab. The first thing to do is to look at

the root locus for the plant by itself, with no compensator. To pick the gain for the proportional

control, remember that the settling time has to be less than 5 seconds. This implies that sigma

should be more than 4.6/5=0.92. We can put this criteria right on the root locus using the sigrid

function, which has to be copied to a m-file. To do this, copy the following code to the end of

your m-file (whether your using the transfer function from the Laplace transforms or from the

state-space representation):

rlocus(num,den)

sigrid(0.92)

axis([-6 6 -6 6])

Note: Matlab commands from the control system toolbox are highlighted in red.

Note: Non-standard Matlab commands used in this example are highlighted in green.

You should see the following rlocus plot:

Page 146: Matlab Basics

As you can see, one of the roots of the closed-loop transfer function is in the right-half-plane.

This means that the system will be unstable. Look back at the root locus to see why. Part of the

root locus lies between the origin and the pole in the right-half-plane. No matter what gain you

chose, you will always have a closed-loop pole in this region, making your impulse response

unstable. To solve this problem, we need to add another pole at the origin so all the zeros and

poles at the origin will cancel each other out and multiple roots will be created in this right-half-

plane region. The multiple roots can then be drawn into the left-half-plane to complete the

design. Add the following to your m-file:

p1 = 0;

dentemp = [1 p1];

num2 = num;

den2 = conv(den, dentemp);

rlocus(num2,den2)

sigrid(0.92)

axis([-10 10 -10 10])

You should get the following root locus plot with multiple roots in the right-half-plane:

Page 147: Matlab Basics

Now we can begin trying to draw the branches of the root locus into the left half plane. Enter the

following two commands into the command window:

roots(num2)

roots(den2)

If you are using the laplace transform transfer function, you should get the following output in

the Matlab command window,

ans =

0

ans =

0

-5.6041

5.5651

-0.1428

As you can see there are four poles and only one zero. This means there will be three

asymptotes: one along the real axis in the negative direction, and the other two at 120 degree

angles from this. For the state-space transfer function there will be one extra pole and zero.

This configuration will never have the multiple roots in the left-half-plane. We must reduce the

number of asymptotes from three to two by adding one more zero than pole the controller. If just

Page 148: Matlab Basics

a zero is added, then the intersection of the asymptotes (alpha) would be [(-5.6041+5.5651-

0.1428+0+0)-(0+0+z2)]/2. This means the two asymptotes will leave the real axis at roughly -

0.1-(1/2)z2. Making z2 as small as possible (assume near the origin) will not pull the multiple

roots far enough into the left-half-plane to meet the design requirements (asymptotes will leave

at about -0.1).

Lead-lag controller

The solution to this problem is to add another pole far to the left of the other poles and zeros. To

keep the right number of asymptotes, another zero should be added as well. The placement of the

added pole and zeros is not important except that the pole should be relatively large and the zeros

should be relatively small.

Try the m-file below to see what effect the poles and zeros have on the root locus. The polyadd

function needs to be copied to the directory you are running Matlab in.

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0];

den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q];

z1 = 3;

p1 = 0;

z2 = 4;

p2 = 50;

numlag = [1 z1];

denlag = [1 p1];

numlead = [1 z2];

denlead = [1 p2];

Page 149: Matlab Basics

num3 = conv(conv(num, numlead), numlag);

den3 = conv(conv(den, denlead), denlag);

rlocus(num3,den3)

sigrid(0.92)

axis([-50 50 -50 50])

figure

rlocus(num3,den3)

sigrid(0.92)

axis([-10 10 -10 10])

[k,poles]=rlocfind(num3,den3)

figure

numc = conv(conv(num,denlead),denlag);

denc = polyadd(k*num3,den3);

impulse(numc,denc)

axis([0 2 -0.05 0.05])

The poles and zeros were found by trial and error. The only things to keep in mind was that one

pole had to be at the origin, the other had to be far to the left, and the two zeros had to be small.

Furthermore, I found that if the two zeros were close together and to the right of the farthest left

plant pole, the response was better. You should see first the following root locus plot with the

zeros and poles listed above:

Page 150: Matlab Basics

The second plot should be of the same root locus magnified a little so that the root locus around

the origin can be seen.

When prompted to pick a location on the root locus, chose a spot on the multiple roots just

before they return to the real axis. Your velocity response to the impulse disturbance should look

similar to the following:

Page 151: Matlab Basics

The response now meets all of the requirements, so no further iteration is needed.

What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given. The

diagram was not entirely complete. The block representing the the position was left out because

that part was not being controlled. It would be interesting though, to see what is happening to the

cart's position when the controller for the pendulum's angle is in place. To see this, we need to

consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

The feedback loop represents the controller we have designed for the pendulum. The transfer

function from the cart's position to the impulse force, with the feedback controller which we

designed, is given as follows:

Page 152: Matlab Basics

Recall that den1=den2 if the pole/zero at the origin that was canceled is added back in. So the

transfer function from X to F can be simplified to:

Transfer Function

Now that we have the transfer function for the entire system, let's take a look at the response.

First we need the transfer function for the cart's position. To get this we need to go back to the

laplace transforms of the system equations and find the transfer function from X(s) to U(s).

Below is this transfer function:

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page.

The pole/zero at the origin canceled out of the transfer function for Phi, has been put back in. So

that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the

command window:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

Page 153: Matlab Basics

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];

den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];

den2 = den1

z1 = 3;

p1 = 0;

z2 = 4;

p2 = 50;

numlag = [1 z1];

denlag = [1 p1];

numlead = [1 z2];

denlead = [1 p2];

num3 = conv(conv(num1, numlead), numlag);

den3 = conv(conv(den1, denlead), denlag);

subplot(1,1,1);rlocus(num3,den3)

axis([-10 10 -10 10])

[k,poles]=rlocfind(num3,den3)

numc = conv(conv(num1,denlead),denlag);

denc = polyadd(k*num3,den3);

t=0:0.01:6;

subplot(2,1,1);

impulse(numc,denc,t)

axis([0 6 -0.05 0.05])

num4 = conv(num2,den3);

den4 = polyadd(conv(den1,den3),k*conv(den1,num3));

subplot(2,1,2);

impulse(num4,den4,t)

axis([0 6 -0.1 0.1])

Page 154: Matlab Basics

If you select the point on the root locus you selected before (near the real axis), you should see

the following plot:

The top curve represents the pendulum's angle, and the bottom curve represents the cart's

position. As you can see, the cart moves, is stabilized at near zero for almost five seconds, and

then goes unstable. It is possible that friction (which was neglected in the modeling of this

problem) will actually cause the cart's position to be stabilized. Keep in mind that if this is in fact

true, it is due more to luck than to anything else, since the cart's position was not included in the

control design.

Example: Root-Locus Design method for the Pitch Controller

In the Pitch Controller Modeling page, the transfer function was derived as

Page 155: Matlab Basics

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the

pitch angle (theta).

The design requirements are

Overshoot: Less than 10%

Rise time: Less than 2 seconds

Settling time: Less than 10 seconds

Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page.

Original root-locus plot

Recall from the Root-Locus Tutorial page, a root-locus plot shows all possible closed-loop pole

locations for a pure proportional controller. Since not all poles are acceptable, the Matlab

function called sgrid should be used to find an acceptable region of the locus. This sgrid function

requires two arguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments

can be determined from the rise time, the settling time, and the overshoot requirements and three

equations shown below.

where,

Wn=Natural frequency

zeta=Damping ratio

Ts=Settling time

Tr=Rise time

Mp=Maximum overshoot

From these three equations, we can determine that the natural frequency (Wn) must be greater

than 0.9 and the damping ratio (zeta) must be greater than 0.52.

Page 156: Matlab Basics

Let's generate a root-locus plot and use the sgrid to find the acceptable region of the locus. Create

a new m-file and enter the following commands:

num=[1.151 0.1774];

den=[1 0.739 0.921 0];

Wn=0.9;

zeta=0.52;

rlocus (num,den)

sgrid (zeta,Wn)

axis ([-1 0 -2.5 2.5])

Run this m-file in the Matlab command window. You should see the root-locus plot similar to

the one shown below:

The two dotted lines in an angle indicate the locations of constant damping ratio, and the

damping ratio is greater than 0.52 in between these lines. The dotted semi-ellipse indicates the

locations of constant natural frequency, and the natural frequency is greater than 0.9 outside the

semi-ellipse. As you may noticed, there is no root-locus plotted in our desired region. We need to

bring the root-locus in between two dotted lines and outside the semi-ellipse by modifying the

controller.

Page 157: Matlab Basics

Lead compensator

We need to shift the root-locus more toward the left to get it inside our desired region. If you

refer to the Designing Lead and Lag Compensators page, you will notice that the lead

compensator can move the root locus to the left. The transfer function of a typical lead

compensator is:

Zo=zero

Po=pole

Zo < Po

In general, the zero is placed in the neighborhood of the natural frequency criterion, and the pole

is placed at a distance 3 to 20 times the value of the zero location. Let's place the zero (Zo) at 0.9

and the pole (Zo) at 20.

This time, let Matlab functions conv and cloop determine the closed-loop transfer function with

the lead compensator. Enter the following commands to an new m-file and run it in the Matlab

command window. You should obtain the following root-locus plot:

num1=[1.151 0.1774];

den1=[1 0.739 0.921 0];

num2=[1 0.9];

den2=[1 20];

num=conv(num1,num2);

den=conv(den1,den2);

Wn=0.9; zeta=0.52;

rlocus (num,den)

axis ([-3 0 -2 2])

sgrid (zeta,Wn)

Page 158: Matlab Basics

The root-locus has been generated in our desired region. Now, we are ready to pick a gain (K)

and generate the step response corresponding to that gain. Add the following commands to the

m-file shown above and run it in the command window. You should see a prompt asking you to

pick a point on the root-locus plot. Pick a point close to the zero on the natural frequency

criterion, say around -1 on real axis. This point should give you the gain around 200. You should

see a step response similar to the one shown below.

[K, poles]=rlocfind (num,den)

de=0.2;

[numc,denc]=cloop (K*num,den,-1);

step (de*numc,denc)

Page 159: Matlab Basics

This response satisfies all the design requirements.

Quick summary

1. Obtain a root-locus plot with the sgrid using the original plant transfer function.

2. Add a lead (or lag) compensator to bring the root-locus to the desired region, if

necessary.

3. Pick a point on the root-locus and obtain the corresponding gain (K).

4. Generate the step response with the chosen gain (K).

5. Determine what needs to be changed from the step response.

6. Add or modify the lead (or lag or lead-lag) compensator.

7. Obtain new root-locus plot with the sgrid command active.

8. Repeat steps 3 to 7 until you obtain a satisfactory result.

Example: Solution to the Ball & Beam Problem Using Root Locus Method

The open-loop transfer function of the plant for the ball and beam experiment is given below:

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

To see the derivation of the equations for this problem refer to the ball and beam modeling page.

A schematic of the closed loop system with a controller is given below:

Page 160: Matlab Basics

Open-loop Root Locus

The main idea of the root locus design is to estimate the closed-loop response from the open-loop

root locus plot. By adding zeroes and/or poles to the original system (adding a compensator), the

root locus and thus the closed-loop response will be modified. Let us first view the root locus for

the plant in open loop. Create an m-file with the following Matlab code in order to model the

plant and plot the root locus.

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

rlocus(num,den)

NOTE: Matlab commands from the control system toolbox are highlighted in red.

Now, run the m-file and you should see the following root locus plot:

Page 161: Matlab Basics

As you can see the system has two poles at the origin which go off to infinity along the

imaginary axes.

The design criteria can also be plotted onto the root locus using the sgrid command. This

command generates a grid of constant damping ratio and natural frequency. The damping ratio

and natural frequency were found using the following equation, which relates the them to our

percent overshoot (PO) and settling time (Ts) requirements:

Note, that the equation with Ts is found by assuming settled is when the response remains within

2% of it's final value. From these equation damping ratio and natural frequency were found to be

0.7 and 1.9 respectively.

sgrid(0.70, 1.9)

axis([-5 5 -2 2])

Page 162: Matlab Basics

The area between the two dotted diagnol lines represents locations where the percent overshoot

is less than 5%. The area outside the curved line represents locations where the setttling time is

less than 3 seconds. Note that no region of the plot falls within the design criteria shown be these

lines. To remedy this and bring the root locus into the left-hand plane for stability we will try

adding a lead-compensator to the system.

Lead Controller

A first order lead compensator tends to shift the root locus into the left-hand plane. For a more

detailed description of lead compensators refer to the Lead & Lag Compensator Design page. A

lead compensator has the form given below:

where, the magnitude of zo is less than the magnitude of po.

Now, let us add the controller to the plant and view the root locus. We will position the zero near

the origin to cancel out one of the poles. The pole of our compensator will be placed to the left of

the origin to pull the root locus further into the left-hand plane. Add the following lines of

Matlab code to your m-file.

zo = 0.01;

po = 5;

numlead = [1 zo];

denlead = [1 po];

numl = conv(num,numlead);

denl = conv(den,denlead);

rlocus(numl,denl)

sgrid(0.70, 1.9)

Run your m-file in the Matlab command window and you should see the following:

Page 163: Matlab Basics

Now, the branches of the root locus are within our design criteria.

Selecting a Gain

Now that we have moved the root locus into the left-hand plane, we may select a gain that will

satisfy our design requirements. We can use the rlocfind command to help us do this. Add the

following onto the end of your m-file.

[kc,poles]=rlocfind(numl,denl)

Go to the plot and select a point near those indicated by the cross mark on the plot below:

Page 164: Matlab Basics

You should see in the Matlab command window something similar to the following (your

numbers will be slightly different):

selected_point =

-2.4988+ 1.2493i

kc =

37.3131

poles =

-2.4950+ 1.2493i

-2.4950- 1.2493i

-0.0101

Now, we can plot the response with this gain.

Plotting the Closed-loop Response

This value of kc can be put into the system and the closed-loop response to a step input of 0.25 m

can be obtained. Add the following lines to your m-file to perform this analysis.

numl2 = kc*numl;

[numcl,dencl] = cloop(numl2,denl);

t=0:0.01:5;

figure

step(0.25*numcl,dencl,t)

Run your m-file and you select a point on the root locus similar to the selected point above. The

step response should look like the following:

Page 165: Matlab Basics

From this plot we see that when a 0.25m step input is given to the system both the settling time

and percent overshoot design criteria are met.

Note: A design problem does not necessarily have a unique answer. Using this method (or any

other) may result in many different compensators. Try running your m-file several more times

selecting a different point each time and study the effect this has on the step response. For

practice you may also want to go back to the original open-loop root locus and try to find other

ways to add zeros and poles to get a better response.

Page 166: Matlab Basics

Frequency Response

The frequency response method 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.

The frequency response of a system can be viewed two different ways: via the Bode plot or via

the Nyquist diagram. Both methods display the same information; the difference lies in the way

the information is presented. We will study both methods in this tutorial.

The frequency response is a representation of the system's response to sinusoidal inputs at

varying frequencies. The output of a linear system to a sinusoidal input is a sinusoid of the same

frequency but with a different magnitude and phase. The frequency response is defined as the

magnitude and phase differences between the input and output sinusoids. 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.

To plot the frequency response, we create a vector of frequencies (varying between zero or "DC"

and infinity) and compute the value of the plant transfer function at those frequencies. If G(s) is

the open loop transfer function of a system and w is the frequency vector, we then plot G(j*w)

vs. w. Since G(j*w) is a complex number, we can plot both its magnitude and phase (the Bode

plot) or its position in the complex plane (the Nyquist plot). More information is available on

plotting the frequency response.

5.1 Bode plot

As noted above, a Bode plot is the representation of the magnitude and phase of G(j*w) (where

the frequency vector w contains only positive frequencies). To see the Bode plot of a transfer

function, you can use the Matlab bode command. For example,

bode(50,[1 9 30 40])

displays the Bode plots for the transfer function:

50

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

s^3 + 9 s^2 + 30 s + 40

Page 167: Matlab Basics

Please note the axes of the figure. The frequency is on a logarithmic scale, the phase is given in

degrees, and the magnitude is given as the gain in decibels.

Note: a decibel is defined as 20*log10 ( |G(j*w| )

5.2 Gain and phase margin

Let's say that we have the following system:

where K is a variable (constant) gain and G(s) 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.

Keep in mind that unity gain in magnitude is equal to a gain of zero in dB.

The phase margin is defined as the change in open loop phase shift required to make a closed

loop system unstable.

Page 168: Matlab Basics

The phase margin also measures the system's tolerance to time delay. If there is a time delay

greater than 180/Wpc in the loop (where Wpc 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 the

gain. That is, a time delay can be represented as a block with magnitude of 1 and phase

w*time_delay (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 deg at the point

corresponding to the frequency that gives us a gain of 0dB (the gain cross over frequency, Wgc).

Likewise, the gain margin is the difference between the magnitude curve and 0dB at the point

corresponding to the frequency that gives us a phase of -180 deg (the phase cross over frequency,

Wpc).

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 the equivalent of changing the y-axis on the magnitude plot. Finding

the phase margin is simply the matter of finding the new cross-over frequency and reading off

Page 169: Matlab Basics

the phase margin. For example, suppose you entered the command bode(50,[1 9 30 40]). You

will get the following bode plot:

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*50,[1 9 30 40]). You should get the following plot

(note I changed the axis so the scale would be the same as the plot above, your bode plot may not

be exactly the same shape, depending on the scale used):

As you can see the phase plot is exactly the same as before, and the magnitude plot is shifted up

by 40dB (gain of 100). The phase margin is now about -60 degrees. This same result could be

Page 170: Matlab Basics

achieved if the y-axis of the magnitude plot was shifted down 40dB. Try this, look at the first

Bode plot, find where the curve crosses the -40dB line, and read off the phase margin. It should

be about -60 degrees, the same as the second Bode plot.

We can find the gain and phase margins for a system directly, by using Matlab. Just enter the

margin 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(50,[1 9 30 40])

5.3 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.5dB, assuming the open loop

phase response is between -135 deg and -225 deg. For a complete derivation of this

approximation, consult your textbook.

Page 171: Matlab Basics

If you would like to see how the bandwidth of a system can be found mathematically from the

closed-loop damping ratio and natural frequency, the relevant equations as well as some plots

and Matlab code are given on our Bandwidth Frequency page.

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 that we have the following closed-loop transfer function representing a system:

1

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

s^2 + 0.5 s + 1

First of all, let's find the bandwidth frequency by looking at the Bode plot:

bode (1, [1 0.5 1 ])

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

Page 172: Matlab Basics

(behind the input). For an input frequency of 3 rad/sec, the output magnitude should be about -

20dB (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).

w= 0.3;

num = 1;

den = [1 0.5 1 ];

t=0:0.1:100;

u = sin(w*t);

[y,x] = lsim(num,den,u,t);

plot(t,y,t,u)

axis([50,100,-2,2])

Note that the output (blue) tracks the input (purple) 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):

w = 3;

num = 1;

den = [1 0.5 1 ];

t=0:0.1:100;

u = sin(w*t);

Page 173: Matlab Basics

[y,x] = lsim(num,den,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 w, and see if they match the Bode plot.

5.4 Closed-loop performance

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 cross over frequency is less than the phase cross over frequency(i.e. Wgc <

Wpc), 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 deg. We can use

this concept with caution if the phase margin is greater than 60 deg.

For second-order systems, a relationship between damping ratio, bandwidth frequency

and settling time is given by an equation described on the bandwidth page.

A very rough estimate that you can use is that the bandwidth is approximately equal to

the natural frequency.

Let's use these concepts to design a controller for the following system:

Where Gc(s) is the controller and G(s) is:

Page 174: Matlab Basics

10

----------

1.25s + 1

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

There are two ways of solving this problem: one is graphical and the other is numerical. Within

Matlab, the graphical approach is best, so that is the approach we will use. First, let's look at the

Bode plot. Create an m-file with the following code:

num = 10;

den = [1.25,1];

bode(num, den)

There are several 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 second 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. This corresponds to a damping of

PM/100=95/100=0.95. Plugging in this value into the equation relating overshoot and the

Page 175: Matlab Basics

damping ratio (or consulting a plot of this relation), we find that the damping ratio corresponding

to this overshoot is approximately 1%. The system will be close to being overdamped.

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 (Kp, Kv, or Ka) are located at 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 20dB (magnitude 10). What this means is that the constant for the error

function it 10. Click here to see the table of system types and error functions. 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.

[numc,denc] = cloop(num,den,-1);

step(numc,denc)

Page 176: Matlab Basics

As you can see, our predictions were very good. The system has a rise time of about 2 seconds, is

overdamped, 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:

K*(s+a)

Gc(s) = -------

s

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 approximately 30 degrees. From our

Ts*Wbw vs damping ratio plot, we find that Ts*Wbw ~ 21. 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):

Page 177: Matlab Basics

num = [10];

den = [1.25, 1];

numPI = [1];

denPI = [1 0];

newnum = conv(num,numPI);

newden = conv(den,denPI);

bode(newnum, newden, 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:

num = [10];

den = [1.25, 1];

numPI = [1 1];

denPI = [1 0];

newnum = conv(num,numPI);

newden = conv(den,denPI);

bode(newnum, newden, logspace(0,2))

Page 178: Matlab Basics

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.

num = [10];

den = [1.25, 1];

numPI = 5*[1 1];

denPI = [1 0];

newnum = conv(num,numPI);

newden = conv(den,denPI);

bode(newnum, newden, logspace(0,2))

Page 179: Matlab Basics

That looks really good. Let's look at our step response and verify our results. Add the following

two lines to your m-file:

[clnum,clden] =cloop(newnum,newden,-1);

step(clnum,clden)

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.

This tutorial is continued on the Nyquist page (the link is after the feedback form).

5.5 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

Page 180: Matlab Basics

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. The following movie will help you visualize

the relationship between the Bode plot and the Nyquist diagram.

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 take into account poles

and zeros on the jw-axis.

The Nyquist diagram is basically a plot of G(j* w) where G(s) is the open-loop transfer function

and w 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):

In order to see how the frequency vector contributes to the Nyquist diagram more clearly, you

can view our movie.

However, if we have open-loop poles or zeros on the jw axis, G(s) 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:

Page 181: Matlab Basics

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 G(s), the number of times that the

plot of G(s) encircles the origin is equal to the number of zeros of G(s) enclosed by the frequency

contour minus the number of poles of G(s) 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 G(s) as in the closed-loop transfer

function:

G(s)

---------

1 + G(s)

If 1+ G(s) encircles the origin, then G(s) 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 1 + G(s)) 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 lnyquist1.m function to your files.

The lnyquist1.m command plots the Nyquist diagram using a logarithmic scale and preserves the

characteristics of the -1 point.

Page 182: Matlab Basics

To view a simple Nyquist plot using Matlab, we will define the following transfer function and

view the Nyquist plot:

0.5

-------

s - 0.5

nyquist (0.5,[1 -0.5])

Now we will look at the Nyquist diagram for the following transfer function:

s + 2

-----

s^2

Note that this function has a pole at the origin. We will see the difference between using the

nyquist, nyquist1, and lnyquist1 commands with this particular function.

nyquist([1 2], [1 0 0])

nyquist1([1 2], [1 0 0])

Page 183: Matlab Basics

lnyquist1([1 2], [1 0 0])

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 lnyquist1 plot is correct and has an appropriate scale.

Closed Loop Stability

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

Page 184: Matlab Basics

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 (or anti-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 which relates these three quantities is:

Z = P + N

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:

Page 185: Matlab Basics

where G(s) is :

s^2 + 10 s + 24

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

s^2 - 8 s + 15

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 =

5

3

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:

nyquist([ 1 10 24], [ 1 -8 15])

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*[ 1 10 24], [ 1 -8 15])

Page 186: Matlab Basics

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*[ 1 10 24], [ 1 -8 15])

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.

If you are having trouble counting the Nyquist encirclements, we suggest you try using nyquist1.

The number of anti-clockwise encirclements will be displayed on your screen (remember that

this number actually represents negative N, i.e. if nyquist1 shows 2, N = -2) as well as the

number of open and closed-loop positive real poles.

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

Page 187: Matlab Basics

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 :

50

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

s^3 + 9 s^2 + 30 s + 40

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:

G(jw) = -1/a = a*G(jw) = a* -1/a => a*G(jw) = -1

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 :

GM = 20*log10(a) [dB]

We will now find the gain margin of the stable, open-loop transfer function we viewed before.

Recall that the function is:

Page 188: Matlab Basics

50

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

s^3 + 9 s^2 + 30 s + 40

and that the Nyquist diagram can be viewed by typing:

nyquist (50, [1 9 30 40 ])

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 = j*w, the

only terms in the denominator that will have imaginary parts are those which are odd powers of

s. Therefore, for G(j*w) to be real, we must have:

-j w^3 + 30 j w = 0

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(j*w) at this point using polyval:

polyval(50,j*w)/polyval([1 9 30 40],j*w)

Our 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 canproceed to find the gain margin.

Page 189: Matlab Basics

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,

-1/a = -0.2174

=> a = 4.6

=> GM = 20*log10( 4.6) = 13.26 dB

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*50,[1 9 30 40])

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.

Page 190: Matlab Basics

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

Example: Solution to the Cruise Control Problem Using Frequency Response

The open-loop transfer function for this problem is :

m=1000

b=50

U(s)=10

Y(s)=Velocity output

The design criteria are:

Rise time < 5 sec

Overshoot < 10%

Steady state error < 2%

To see the original problem set, see the Cruise Control Modeling page.

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.

Page 191: Matlab Basics

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

see how the open-loop response looks like. Create an new m-file and enter the following

commands.

m = 1000;

b = 50;

u = 500;

Kp=1;

numo=[Kp];

deno=[m b];

step (u*numo,deno)

Running this m-file in the Matlab command window should give you the following plot.

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 step command and add in the following command.

bode(numo,deno)

Page 192: Matlab Basics

Running this new m-file should give you the following Bode plot.

Proportional controller

Let's see what system characteristics we can determine from the above Bode plot. Recall from

the Root-Locus Tutorial, the bandwidth frequency (BW) (the frequency at the gain M(dB)=-6~-

7.5dB) is roughly equals to the natural frequency (Wn). Using the equation,

the rise time (Tr) for our system can be determined to be extremely long since the gain shown

above do not reach -6~-7.5dB. Moreover, we see from the Root-Locus Tutorial that the damping

ratio is roughly equals to the phase margin (in degrees) divided by 100.

Since our phase margin is approximately 155 degrees, the damping ratio will be 1.55. Thus, we

know that the system is overdamped (since the damping ratio is greater than 1). Finally, the

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

Page 193: Matlab Basics

For our system, since the low frequency gain M(dB) is approximately -35dB, the steady-state

error should be 98%. We can confirm these by generating a closed-loop step response.

In terms of the Bode plot, if we can shift the gain upward so that both the bandwidth frequency

and the low frequency gain increase, then both the rise time and the steady-state error will

improve. We can do that by increasing the proportional gain (Kp). Let's increase the proportional

gain (Kp) to ,say, 100 and see what happens. Change the m-file to the following.

m = 1000;

b = 50;

u = 10;

Kp=100;

numo=[Kp];

deno=[m b];

bode(numo,deno)

Running this m-file in the Matlab command window should give you the following Bode plot.

Page 194: Matlab Basics

Now, the low frequency gain is about 6dB (magnitude 2) which predicts the steady-state error of

33%. The bandwidth frequency is about 0.1 rad/sec so that the rise time should be around 18

seconds. Let's take a look at the closed-loop step response and confirm these.

As we predicted, both the steady-state error and the rise time have improved. Let's increase the

proportional gain even higher and see what happens. Change the m-file to the following and

rerun it. You should get the following Bode plot.

m = 1000;

b = 50;

u = 10;

Page 195: Matlab Basics

Kp=100;

numo=[Kp/b];

deno=[m/b 1];

bode(numo,deno)

Now, the low frequency gain is approximately 20dB (magnitude 10) that predicts the steady-state

error of 9%, and the bandwidth frequency is around 0.6 that predicts the rise time of 3 sec

(within the desired value). Thus, both the steady-state error and the rise time should have been

improved. Again, let's confirm these by generating the closed-loop step response.

Page 196: Matlab Basics

If you noticed, the steady-state error will eventually reach the desired value by increasing the

proportional gain even higher. However, by the time the steady-state error reaches the desired

value, the rise time becomes too fast (unrealistic for the real physical system). Thus, let's leave

the Kp as it is and implement a lag controller to handle the steady-state error problem.

Lag controller

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

the Lead and Lag Compensator page, the lag controller adds gain at the low freqencies 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 shown below.

Now, we need to choose a value for a and T. Recall from the "Lag or Phase-Lag Compensator

using Frequency Response" page, the steady-state error will decrease by a factor of a. The value

T should be chosen so that two corner frequencies will not be placed close together because

transient response gets worse. So let a equals 0.05 and T equals 700 and see what happens. Copy

the following m-file and run it in the Matlab command window. You should see the following

Bode plot.

m = 1000;

b = 50;

u = 10;

Kp=600;

numo=[Kp/b];

deno=[m/b 1];

a = 0.05;

T=700;

numlag = [a*T 1];

Page 197: Matlab Basics

denlag = a*[T 1];

newnum = conv(numo,numlag);

newden = conv(deno,denlag);

bode(newnum,newden)

figure

[numc,denc]=cloop(newnum,newden);

step (u*numc,denc)

Since the low frequency gain has increased while the bandwidth frequency stayed the same, the

steady-state error should be reduced and the rise time should stay the same. Let's confirm this by

generating a closed-loop step response

Page 198: Matlab Basics

It may be hard to see, but there should be a green, dotted line across just below 10. This line

shows the steady-state value of the step, and we can see that the steady-state error has been met.

However, the settling time is too long. To fix this, raise the proportional gain to Kp=1500. This

gain was chosen from trial-and-error that will not be described here in the interest of length.

With this change made, the following Bode and step response plots can be generated.

As you can see, the overshoot is in fact zero, the steady state error is close to zero, the rise time is

about 2 seconds, and the settling time is less than 3.5 seconds. The system has now met all of the

design requirements. No more iteration is needed.

Page 199: Matlab Basics

Example: Frequency Design Method for DC Motor Speed Control

From the main problem, the dynamic equations and the open-loop transfer function of DC Motor

Speed are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With the 1 rad/sec step input, the design criteria are:

Settling time less than 2 seconds

Overshoot less than 5%

Steady-state error less than 1%

Create a new m-file and type in the following commands (refer to the main problem for the

details of getting those commands).

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Page 200: Matlab Basics

Drawing the original Bode plot

The main idea of frequency-based design is to use the Bode plot of the open-loop transfer

function to estimate the closed-loop response. Adding a controller to the system changes the

open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the Bode plot

for the original open-loop transfer function. Add the following code to the end of your m-file,

and then run it in the Matlab command window.

bode(num,den)

You should get the following Bode plot:

Adding proportional gain

From the bode plot above, we see that the phase margin can be greater than about 60 degrees if w

is less than 10 rad/sec. Let's add gain to the system so the bandwidth frequency is 10 rad/sec,

which will give us a phase margin of about 60 degrees. To find the gain at 10 rad/sec, you can try

to read it off the Bode plot (it looks to be slightly more than -40 dB, or 0.01 in magnitude). The

bode command, invoked with left-hand arguments, can also be used to give you the exact

magnitude:

[mag,phase,w] = bode(num,den,10)

Page 201: Matlab Basics

mag =

0.0139

To have a gain of 1 at 10 rad/sec, multiply the numerator by 1/0.0139 or approximately 72.

num = 70*num

and rerun your m-file. You should have the following Bode plot:

Plotting the closed-loop response

From the plot above we see that the phase margin is now quite large. Let's see what the closed-

loop response look like. Add a % in front of the bode commands and add the following code to

the end of your m-file:

[numc,denc]=cloop(num, den, -1);

t=0:0.01:10;

step(numc,denc,t)

You will see the following plot:

Page 202: Matlab Basics

The settling time is fast enough, but the overshoot and the steady-state error are too high. The

overshoot can be reduced by reducing the gain a bit to get a higher phase margin, but this would

cause the steady-state error to increase. A lag controller is probably needed.

Adding a lag controller

We can add a lag controller to reduce the steady-state error. At the same time, we should try to

reduce the overshoot by reducing the gain. Let's reduce the gain to 50, and try a lag controller of

which should reduce the steady-state error by a factor of 1/0.01 = 100 (but could increase the

settling time). Go back and change your m-file so it looks like the following:

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

num=50*K;

z=1;

Page 203: Matlab Basics

p=0.1;

numa=[1 z];

dena=[1 p];

numb=conv(num,numa);

denb=conv(den,dena);

bode(numb,denb)

Rerun the file and you will get this plot:

The phase margin looks good. The steady-state error is predicted to be about 1/40dB or 1%, as

desired. Close the loop and look at the step response. Add the following lines of code to the end

of you m-file and rerun.

[numc,denc]=cloop(numb, denb, -1);

t=0:0.01:10;

step(numc,denc,t)

Page 204: Matlab Basics

Now you have a step response that meets the design requirements. The steady-state error is less

than 1%, the overshoot is about 5%, and the settling time is about 2 seconds.

Example: Frequency Design Method for DC Motor Position Control

From the main problem, the dynamic equations in transfer-function form are the following:

and the system schematic looks like:

Page 205: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec step reference, the design criteria are:

Settling time less than 40 milliseconds

Overshoot less than 16%

No steady-state error

No steady-state error due to a step disturbance

Drawing the original Bode plot

Create a new m-file and type in the following commands (refer to the main problem for the

details of getting those commands).

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

The main idea of frequency-based design is to use the Bode plot of the open-loop transfer

function to estimate the closed-loop response. Adding a controller to the system changes the

open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the Bode plot

for the original open-loop transfer function. Add the following code to the end of your m-file,

and then run the m-file.

w=logspace(0,4,101);

bode(num,den,w)

You should get the following Bode plot:

Page 206: Matlab Basics

Adding an integrator

Now lets add an integrator for zero steady-state error in response to a step disturbance. Add the

following lines to your m-file:

numi=1;

deni=[1 0];

numiol=conv(num,numi);

deniol=conv(den,deni);

bode(numiol,deniol,w)

You should get the following plot:

Page 207: Matlab Basics

Gain and phase margin specification and controller design

We want less than a 16% overshoot, so lets compute the damping ratio based on a 16%

overshoot. Also the corresponding phase margin is 100 times the damping ratio. From the

settling time requirement, we are able to compute the desired bandwidth frequency. Add the

following lines to your m-file:

zeta=-log(.16)/sqrt(pi^2+(log(.16))^2);

PM=100*zeta;

wbw=(4/(0.04*zeta))*sqrt((1-2*zeta^2)+sqrt(4*zeta^4-4*zeta^2+2));

We want to have at least 50 degrees of phase margin, therefore the gain should fall between -6

and -7.5 dB at some frequency after 250 rad/sec. From the Bode plot we see that we must add

about 80 degrees of phase and 60 dB of gain at a frequency of 250 rad/sec. The gain plot will

then lie between -6 and -7.5 dB and after 244 rad/sec. From the Bode phase plot we can see that

there is a pole near 60 rad/sec. We will use a PI controller to put a zero at s=60 to flatten out the

phase curve. Add the following lines to your m-file:

numpi=[1 60];

denpi=[1 0];

numpiol=conv(numpi,num);

denpiol=conv(denpi,den);

bode(numpiol,denpiol,w)

You should see the following plot:

Page 208: Matlab Basics

From the bode plot we can see that we need 50 more degrees of phase at a frequency of 250

rad/sec. Lets now try a lead compensator to add exactly 50 degrees of phase. Add the following

lines to your m-file:

a=(1 - sin(PM*pi/180))/(1 + sin(PM*pi/180));

T=1/(wbw*sqrt(a));

numpil = conv([1 60],[T 1]);

denpil = conv([1 0],[a*T 1]);

numpilol = conv(numpil,num);

denpilol = conv(denpil,den);

w = logspace(2,3,101);

bode(numpilol,denpilol,w)

This new Bode plot now shows that the phase margin is about right at 250 rad/sec, but the gain is

too small by about 20 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up by

20 dB we will multiply by a gain of 10. Add the following lines to your m-file:

kpid = 10;

bode(kpid*numpilol,denpilol,w)

You should get the following plot:

Page 209: Matlab Basics

Lets now check the step response of the closed loop system. Add the following lines to youe m-

file:

[numpilcl,denpilcl] = cloop(kpid*numpilol,denpilol,-1);

t = 0:0.001:0.1;

step(numpilcl,denpilcl)

You should get the following plot:

Page 210: Matlab Basics

The overshoot is now too large, however the settling time is better than expected. So let's try

another design where the phase margin is larger, say around 70 degrees. Add the following lines

to your m-file:

PM=70;

a=(1 - sin(PM*pi/180))/(1 + sin(PM*pi/180));

T=1/(wbw*sqrt(a));

numpil=conv([1 60],[T 1]);

denpil=conv([1 0],[a*T 1]);

numpilol=conv(numpil,num);

denpilol=conv(denpil,den);

w=logspace(2,3,101);

bode(numpilol,denpilol,w)

You should get the following plot:

This new bode plot shows that the phase margin is good at around 250 rad/sec, but the gain is too

small by about 14 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up we

will multiply by a gain of 5. Add the following lines to your m-file:

kpid = 5;

bode(kpid*numpilol,denpilol,w)

You should get the following plot:

Page 211: Matlab Basics

Now lets check the step response again. Add the following lines to your m-file:

[numpilcl,denpilcl] = cloop(kpid*numpilol,denpilol,-1);

t = 0:0.001:0.1;

step(numpilcl,denpilcl)

You should get the following step response:

Page 212: Matlab Basics

From the step response we now see that the overshoot is fine, but the settling time is too long.

Let's try a slightly higher bandwidth. Add the following lines to your m-file:

wbw=300;

a=(1-sin(PM*pi/180))/(1+sin(PM*pi/180));

T=1/(wbw*sqrt(a));

numpil=conv([1 60],[T 1]);

denpil=conv([1 0],[a*T 1]);

numpilol=conv(numpil,num);

denpilol=conv(denpil,den);

w=logspace(2,3,101);

bode(numpilol,denpilol,w)

You should get the following plot:

This new bode plot shows that the phase margin is about right at a frequency of 250 rad/sec, but

the gain is too small by about 18 dB. The gain crossover must occur at 240 rad/sec. To bring the

gain up we will multiply by a gain of 8. Add the following lines to your m-file:

kpid=8;

bode(kpid*numpilol,denpilol,w);

You should get the following plot:

Page 213: Matlab Basics

Now let's check the step response of the closed loop system. Add the following lines to your m-

file:

[numpilcl,denpilcl]=cloop(kpid*numpilol,denpilol,-1);

t=0:0.001:0.1;

step(numpilcl,denpilcl)

You should get the following step response:

Page 214: Matlab Basics

Now everything looks good. We have less than 16% overshoot and a settling time of about 40

milliseconds.

Note: As you noticed, the frequency response method for this particular problem requires

substantial amount of trial and error runs. The m-file below is the simplified version of what was

done above. After you run this m-file, you will get the last two plots shown above.

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

num=K;

den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

PM=70;

wbw=300;

a=(1-sin(PM*pi/180))/(1+sin(PM*pi/180));

T=1/(wbw*sqrt(a));

numpil=conv([1 60],[T 1]);

denpil=conv([1 0],[a*T 1]);

numpilol=conv(numpil,num);

denpilol=conv(denpil,den);

kpid=8;

w=logspace(2,3,101);

bode(kpid*numpilol,denpilol,w)

figure

[numpilcl,denpilcl]=cloop(kpid*numpilol,denpilol,-1);

t=0:0.001:0.1;

step(numpilcl,denpilcl)

Page 215: Matlab Basics

Example: Frequency Design Method for the Bus Suspension System

From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic is:

For the original problem and the derivation of the above equations and schematic, please refer to

the bus modeling page.

We want to design a feedback controller so that when the road disturbance (W) is simulated by a

unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less

than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate

within a range of +/- 5 mm and will stop oscillating within 5 seconds.

The system model can be represented in Matlab by creating a new m-file and entering the

following commands (refer to the main problem for the details of getting those commands).

m1=2500;

m2=320;

k1 = 80000;

k2 = 500000;

b1 = 350;

b2 = 15020;

Page 216: Matlab Basics

nump=[(m1+m2) b2 k2]

denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

num1=[-(m1*b2) -(m1*k2) 0 0]

den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2)

(b1*k2)+(b2*k1) k1*k2]

numf=num1;

denf=nump;

Plotting the frequency response using the bode command

The main idea of frequency-based design is to use the Bode plot of the open-loop transfer

function to estimate the closed-loop response. Adding a controller to the system changes the

open-loop Bode plot so that the closed-loop response will also change. Let's first draw the Bode

plot for the original open-loop transfer function. Add the following line of code to your m-file

and rerun:

w = logspace(-1,2);

bode(nump,denp,w)

You should get the following bode plot:

Page 217: Matlab Basics

For convenience in representing systems with different natural frequencies of the system, we

normalize and scale our finding before plotting the Bode plot, so that the low-frequency

asymptote of each term is at 0 dB. This normalization by adjusting the gain, K, makes it easier to

add the components of the Bode plot. The effect of K is move the magnitude curve up

(increasing K) or down (decreasing K) by an amount 20*logK, but the gain, K, has no effect on

the phase curve. Therefore from the previous plot, K must be equal to 100 dB or 100,000 to

move the magnitude curve up to 0 dB at 0.1 rad/s. Go back to your m-file and add the following

line of code to your m-file before the bode command and rerun:

nump=100000*nump

You should get the following bode plot:

Adding a two-lead controller

From the Bode plot above, we see that the phase curve is concave at about 5 rad/sec. First, we

will try to add positive phase around this region, so that the phase will remain above the -180

degree line. Since a large phase margin leads to a small overshoot, we will want to add at least

140 degrees of positive phase at the area near 5 rad/sec. Since one lead controller can add no

more than +90 degrees, we will use a two-lead controller:

Page 218: Matlab Basics

To obtain T and a, the following steps can be used:

1: Determine the positive phase needed :

Since we want 140 degrees total, we will need 70 degrees from each controller.

2: Determine the frequency where the phase should be added:

In our case this frequency should be 5.0 rad/sec.

3: Determine the constant a from the equation below, this determines the required space

between the zero and the pole for the desired maximum phase added.

4: Determine T and aT from the following equations, these determine the corner frequencies

so that the maximum phase will be added at the desired frequency.

Now let's put our 2-Lead controller into the system and see what the Bode plot looks like. Add

the following code to your m-file, and add a % in front of the previous bode command (if there is

one):

numc=conv([1.13426 1], [1.13426 1]);

denc=conv([0.035265 1], [0.035265 1]);

margin(conv(nump,numc),conv(denp,denc))

You should get the following Bode plot:

Page 219: Matlab Basics

Since the Bode plot has a limited phase range (-360-0), the above plot is a little deceiving. The

plot is equivalent to the following:

w=logspace(-4,4);

[mag,phase,w] = bode(conv(nump,numc),conv(denp,denc),w);

subplot(2,1,1);

semilogx(w,20*log10(mag));

grid

title('Bode plot of system with notch filter')

xlabel('Frequency (rad/s)')

ylabel('20logM')

subplot(2,1,2);

semilogx(w,phase);

axis([1e-4, 1e4, -180, 360])

grid

xlabel('Frequency (rad/s)')

ylabel('Phase (degrees)')

Page 220: Matlab Basics

From this plot we see that the concave portion of the phase plot is above -180 degrees now, and

the phase margin is large enough for the design criteria. Let's see how the output (the distance

X1-X2) responds to a bump on the road (W). Recall that the schematic of the system is:

and the closed-loop transfer function can be derived as follows:

Page 221: Matlab Basics

To obtain the closed-loop transfer function from W to X1-X2, the following commands can be

added into the m-file:

numa=conv(conv(numf,nump),denc);

dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Note that the function "polyadd" is not a Matlab standard function. You will need to copy it to a

new m-file to use it. Click here for more information.

Refer to the bus modeling page, nump = denf as we can see in the matlab command above. Thus

we can simplified this transfer function to be the following:

numa=conv(numf,denc);

dena=polyadd(conv(denp,denc),conv(nump,numc));

Plotting the closed-loop response

Let's see what the step response looks like now. Keep in mind that we are using a 0.1 m high step

as the disturbance. To simulate this, simply multiply numa by 0.1. Add the following code into

the m-file and rerun it. Don't forget to put % mark in front of all bode and margin

commands!

t=0:0.01:5;

step(0.1*numa,dena,t)

axis([0 5 -.01 .01])

and you should see the following plot:

Page 222: Matlab Basics

The amplitude of response is a lot smaller than the percent overshoot requirement and the

settling time also is less than 5 seconds. Since we can see that an amplitude of the output's

response less than 0.0001 m or 1% of input magnitude after 4 seconds. Therefore we can say that

the settling time is 4 seconds from the above plot. From the Bode plot above, we see that

increasing the gain will increase the crossover frequency and thus make the response faster. We

will increase the gain and see if we can get a better response. Go back to your m-file and change

numc to numc=4*conv([3.1483 1],[3.1483 1]). Rerun the m-file and you should get the

following plot:

Page 223: Matlab Basics

From this plot we can see that the percent overshoot is about 0.15 mm less than the previous

plot's and the settling time also less than 5 seconds. This response is now satisfactory and no

more design iteration is needed.

Example: Solution to the Inverted Pendulum Problem Using Frequency Response Method

The transfer function of the plant for this problem is given below:

where,

Note: There is a pole/zero cancellation in this transfer function. In previous examples these were

removed from the transfer function. However, in this example they will be left in for reasons that

will become clear later.

The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:

Settling time of less than 5 seconds.

Pendulum should not move more than 0.05 radians away from the vertical.

To see how this problem was originally set up, consult the inverted pendulum modeling page.

Note: Before trying to work through this problem, it should be noted that this is a complicated

problem to solve with the frequency response method. As you will soon find out, this problem has

a pole in the right-half-plane, making it unstable. The frequency response method works best

when the system is stable in the open loop. For this reason I would not suggest trying to follow

Page 224: Matlab Basics

this example if you are trying to learn how to use frequency response. This problem is for people

who want to learn how to solve frequency response problems that are more complicated.

Open-loop Representation

The frequency response method uses the bode command in Matlab to find the frequency

response for a system described by a transfer function in bode form.

The transfer function found from the Laplace transforms for the output Phi (the pendulum's

angle) can be set up using Matlab by inputting the numerator and denominator as vectors. Create

an m-file (or a '.m' file located in the same directory as Matlab) and copy the following text to

model the transfer function:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0 0]

den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0]

Your output should be:

num =

4.5455 0 0

den =

1.0000 0.1818 -31.1818 -4.4545 0

Page 225: Matlab Basics

Closed-loop response with no compensation

We will now design an inverted pendulum controller for an impulse force using Nyquist

diagrams (we cannot use Bode plots because the system is unstable in open loop). Let's begin by

looking at the block diagram for this system:

If you try to model this system in Matlab, you will have all sorts of problems. The best way to

use Matlab to solve this problem is to first change the schematic to something that can be

modeled much more easily. Rearranging the picture, you can get the following new schematic:

Now we can begin our design. First, we will look at the poles and zeros of this function:

x = roots(num)

y = roots(den)

x =

0

0

y =

0

-5.6041

5.5651

-0.1428

As you already know, we have a pole-zero cancellation at the origin, as well as one positive, real

pole in the right-half plane. This means that we will need one anti-clockwise encirclement of -1

in order to have a stable closed-loop system (Z = P + N; P = 1, N = -1). The following m-file will

Page 226: Matlab Basics

be very useful for designing our controller. Please note that you will need the function polyadd.m

to run this m-file. Copy and paste the function from your browser to a m-file in your directory

(make sure the function command starts in the first column of the m-file).

Note: Non-standard Matlab commands used in this example are highlighted in green.

function[ ] = pend()

clf

figure(1)

clf

%define TF

num = [4.5455 0 0];

den =[1.0000 0.1818 -31.1818 -4.4545 0];

figure(1)

%ask user for controller

numc = input('numc?.........');

denc = input('denc?.........');

k = input('K?............');

%view compensated system bode

bode(k*conv(numc,num), conv(denc,den))

%view compensated system nyquist

figure(2)

subplot (2,1,1)

nyquist(k*conv(numc,num), conv(denc,den))

%view compensated CL system impulse response

subplot(2,1,2)

clnum = conv(num,denc);

temp1 = k*conv(numc,num);

temp2 = conv(denc, den);

clden = polyadd(temp1,temp2);

impulse (clnum,clden)

Page 227: Matlab Basics

Note: Matlab commands from the control system toolbox are highlighted in red.

With this m-file we will now view the uncompensated system's Nyquist diagram by setting the

controller numerator, denominator and gain equal to one. Enter pend at the command prompt,

and enter 1 for numc, denc, and K. You should see the following plots in your screen:

numc?.........1

denc?.........1

K?............1

Page 228: Matlab Basics

Closed-loop response with compensation

The system is unstable in closed loop (no encirclements of -1). Our first step will be to add an

integrator to cancel the extra zero at the origin (we will then have two poles and two zeros at the

origin). Use the pend command again.

numc?.........1

denc?.........[1 0]

K?............1

Page 229: Matlab Basics

Notice that the nyquist diagram encircles the -1 point in a clockwise fashion. Now we have two

poles in the right-half plane (Z= P + N = 1 + 1). We need to add phase in order to get an anti-

clockwise encirclement. We will do this by adding a zero to our controller. For starters, we will

place this zero at -1.

numc?.........[1 1]

denc?.........[1 0]

K?............1

Page 230: Matlab Basics

as you can see, this wasn't enough phase. The encirclement around -1 is still clockwise. We are

going to need to add a second zero.

numc?.........conv([1 1],[1 1])

denc?.........[1 0]

K?............1

Page 231: Matlab Basics

We still have one clockwise encirclement of the -1 point. However, if we add some gain, we can

make the system stable by shifting the nyquist plot to the left, moving the anti-clockwise circle

around -1, so that N = -1. help impulse

numc?.........conv([1 1],[1 1])

denc?.........[1 0]

K?............10

Page 232: Matlab Basics

As you can see, the system is now stable. We can now concentrate on improving the response.

We can modify the poles of the controller in order to do this. We have to keep in mind that small

poles (close to the origin) will affect the response at small frequencies, while larger poles (farther

from the origin) will affect the response at high frequencies. When designing via frequency

response, we are interested in obtaining simple plots, since they will be easier to manipulate in

order to achieve our design goal. Therefore, we will use these concepts to 'flatten' the frequency

response (bode plot). At the same time, you will notice that the nyquist diagram will take an oval

shape.

If we try different combinations of poles and gains, we can get a very reasonable response.

(Enter the command axis([0 5 -0.05 0.1]) after the pend command.)

numc?.........conv([1 1.1],[1 5])

denc?.........[1 0]

K?............10

Page 233: Matlab Basics

Our response has met our design goals. Feel free to vary the parameters and observe what

happens.

What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given. The

diagram was not entirely complete. The block representing the the position was left out because

that variable was not being controlled. It is interesting though, to see what is happening to the

cart's position when the controller for the pendulum's angle is in place. To see this we need to

consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

Page 234: Matlab Basics

The feedback loop represents the controller we have designed for the pendulum. The transfer

function from the cart's position to the impulse force, with the frequency response feedback

controller which we designed, is given as follows:

Recall that den1=den2 (the two transfer functions G1 and G2 differ in numerator alone), so the

transfer function from X to F can be simplified to:

Transfer Function

Now that we have the transfer function for the entire system, let's take a look at the response.

First we need the transfer function for the cart's position. To get this we need to go back to the

laplace transforms of the system equations and find the transfer function from X(s) to U(s).

Below is this transfer function:

Page 235: Matlab Basics

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page.

The pole/zero at the origin canceled out of the transfer function for Phi, has been put back in. So

that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the

command window:

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];

den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];

den2 = den1;

k = 10;

numcontroller = conv([1 1.1],[1 5]);

dencontroller = [1 0];

numc = conv(numx,dencontroller);

denc = polyadd(conv(dencontroller,den),k*conv(numcontroller,num));

t=0:0.01:100;

impulse(numc,denc,t)

Page 236: Matlab Basics

As you can see, the cart moves in the negative direction and stabilizes at about -0.18 meters. This

design might work pretty well for the actual controller, assuming that the cart had that much

room to move in. Keep in mind, that this was pure luck. We were not trying to design to stabilize

the cart's position, and the fact that we have is a fortunate side effect.

Example: Frequency Response Design method for the Pitch Controller

In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the

pitch angle (theta).

The design requirements are

Overshoot: Less than 10%

Rise time: Less than 5 seconds

Settling time: Less than 10 seconds

Page 237: Matlab Basics

Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page.

Open-loop response

Recall from your control textbook that the frequency response design method is most effective

for systems with stable open-loop. To check the open-loop stability of our system, create a new

m-file, and enter the following commands. Running this m-file in the Matlab command window

should give you the step response shown below:

de=0.2;

num=[1.151 0.1774];

den=[1 0.739 0.921 0];

step (de*num,den)

Unfortunately, our system is unstable in open-loop; however, we can still design the feedback

system via frequency response method (even though this might not be the easiest way). First,

let's generate the open-loop Bode plot and see what it looks like. Change the m-file to the

following and re-run it in the Matlab command window. You should see a Bode plot similar to

the one shown below:

Page 238: Matlab Basics

num=[1.151 0.1774];

den=[1 0.739 0.921 0];

bode (num,den)

From our design requirements, we can determine that the natural frequency (Wn) must be greater

than 0.9 and the damping ratio (zeta) must be greater than 0.52 (please refer to the Pitch

Controller: Root-Locus method for details). Using two equations shown below, we see that the

bandwidth frequency and the phase margin must be greater than 0.9 and 52 degrees,

respectively.

Currently, we have the bandwidth frequency of 1 rad/sec and the phase margin of 80 degrees.

These values are within our desired region. Let's plot the closed-loop step response and see what

Page 239: Matlab Basics

it looks like. Delete the bode command from the above m-file and add the following commands.

Running this new m-file should give you the following closed-loop step response:

[numc,denc]=cloop(num,den,-1);

de=0.2;

t=0:0.01:10;

step (de*numc,denc,t)

As you can see, the transient response is worse that results in long settling time. We will

implement a lead compensator to improve the system response.

Lead compensator

Referring to the "Lead or phase-lead compensator using frequency response" section of Lead and

Lag Compensator page, a lead compensator will add a positive phase to the system. An

additional positive phase increases the phase margin; thus, increase damping. The settling time

should decrease as a result of this increased damping.

The transfer function of a typical first-order lead compensator is

Page 240: Matlab Basics

We need to find alead, Tlead and Klead. First, the phase margin requirement and the following

equation can be used to find alead

Since we are required to have the phase margin of greater than 52 degrees, the alead must be

greater than 8.43. Using this alead, the bandwidth frequency requirement of greater than 0.9 and

the following equation leads us to have the Tlead of smaller than 0.382.

Let the Klead equal 0.1, alead equal 10, and Tlead equal 0.3 for now and enter the following

commands to an new m-file.

num=[1 151 0.1774];

den=[1 0.739 0.921 0];

alead=10;

Tlead=0.3;

aleadtlead=alead*Tlead;

k=0.1;

numlead=k*[aleadtlead 1];

denlead=[Tlead 1];

num1=conv(num,numlead);

den1=conv(den,denlead);

bode(num1,den1)

[numc,denc]=cloop(num1,den1,-1);

de=0.2;

t=0:0.01:10;

figure

Page 241: Matlab Basics

step (de*numc,denc,t)

Running this m-file in the Matlab command window gives you the following Bode and step

response plots.

Page 242: Matlab Basics

Although both bandwidth frequency and phase margin increased, the response still does not

satisfy the design requirements. Let's increase alead and decrease Tlead. After several trial and

error runs, an alead of 200, Tlead of 0.0025, and Klead of 0.05 , were found which gave the

following lead compensator,

provided the desired transient response. To see the step response and the corresponding Bode

plot, enter the following commands to an m-file and run it in the command window. You should

see both the Bode plot and the step response shown below:

num=[1 151 0.1774];

den=[1 0.739 0.921 0];

alead=200;

Tlead=0.0025;

aleadtlead=alead*Tlead;

k=0.05;

numlead=k*[aleadtlead 1];

denlead=[Tlead 1];

num1=conv(num,numlead);

den1=conv(den,denlead);

bode(num1,den1)

[numc,denc]=cloop(num1,den1,-1);

de=0.2;

t=0:0.01:10;

figure

step (de*numc,denc,t)

Page 243: Matlab Basics

If you compare the above Bode plot to the original Bode plot, you see both the phase margin and

the bandwidth frequency have increased. Increasing both of them improves the rise time, the

overshoot, and the settling time, as seen in the above step response plot. To improve the steady-

state error, we will add a lag compensator to the system.

Lag compensator

Referring to the "Lag or phase-lag Compensator using frequency response" section of Lead and

Lag Compensator page, a lag compensator reduces the steady-state error. The typical first-order

transfer function of a lead compensator is

Page 244: Matlab Basics

The steady-state error will be reduced by a factor of alag. From the above step response, we see

that the steady-state error is roughly 10%. Thus, alag needs to be approximately 0.1. The Tlag

should be greater than alag*Tlag because this compensator must not greatly change the transient

response.

After several trial and error runs, an alag of 0.1, Tlag of 20, and Klag of 1.5, were found which

gave the following lag compensator,

provided the desired response. To see the step response and the corresponding Bode plot, enter

the following commands to an new m-file. Running this m-file in the command window should

give you the two plots shown below:

num=[1 151 0.1774];

den=[1 0.739 0.921 0];

alead=200;

Tlead=0.0025;

aleadtlead=alead*Tlead;

k=0.05;

numlead=k*[aleadtlead 1];

denlead=[Tlead 1];

num1=conv(num,numlead);

den1=conv(den,denlead);

Tlag=20;

alag=0.1;

at=alag*Tlag;

k2=1.5;

numlag=k2/alag*[at 1];

denlag=[Tlag 1];

Page 245: Matlab Basics

num2=conv(num1,numlag);

den2=conv(den1,denlag);

bode (num2,den2)

[numc2,denc2]=cloop(num2,den2,-1);

figure

step (0.2*numc2,denc2,t)

If you see the Bode plot, the low frequency gain has increased while keeping the bandwidth

frequency the same. This tells us that steady-state error has reduced while keeping the same rise

Page 246: Matlab Basics

time. The above step response shows that the steady-state error got eliminated. Now all design

requirements are satisfied.

Example: Solution to the Ball & Beam Problem Using Frequency Response Method

The open-loop transfer function of the plant for the ball and beam experiment is given below:

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

To see the derivation of the equations for this problem refer to the ball and beam modeling page.

A schematic of the closed loop system with a controller is given below:

Open-loop Bode Plot

The main idea of frequency based design is to use the Bode plot of the open-loop transfer

function to estimate the closed-loop response. Adding a controller to the system changes the

open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the bode plot

for the original open-loop transfer function. Create an m-file with the following code and then

run it in the Matlab command window:

m = 0.111;

Page 247: Matlab Basics

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

bode(num,den)

NOTE: Matlab commands from the control system toolbox are highlighted in red.

You should get the following Bode plot:

From this plot we see that the phase margin is zero. Since the phase margin is defined as the

change in open-loop phase shift necessary to make a closed-loop system stable this means that

our zero phase margin indicates our system is unstable. We want to increase the phase margin

Page 248: Matlab Basics

and we can use a lead compensator controller to do this. For more information on Phase and

Gain margins please refer to the Frequency Response Tutorial.

Phase-Lead Controller

A first order phase-lead compensator has the form given below:

The phase-lead compensator will add positive phase to our system over the frequency range 1/aT

and 1/T, which are called the corner frequencies. The maximum added phase for one lead

compensator is 90 degrees. For our controller design we need a percent overshoot of 5, which

corresponds to a zeta of 0.7. Generally zeta * 100 will give you the minimum phase margin

needed to obtain your desired overshoot. Therefore we require a phase margin greater than 70

degrees.

To obtain "T" and "a", the following steps can be used.

1. Determine the positive phase needed:

We need at least 70 degrees from our controller.

2. Determine the frequency where the phase should be added (center frequency):

In our case this is difficult to determine because the phase vs. frequency graph in the

bode plot is a flat line. However, we have a relation between bandwidth frequency (wbw)

and settling time (refer to the Bandwidth Frequency page for this equation) which tells us

that wbw is approximately 1.92 rad/s. Therefore we want a center frequency just before

this. For now we will choose 1.

3. Determine the constant "a" from the equation below, this determines the required space

between the zero and the pole for the maximum phase added.

Page 249: Matlab Basics

where phi refers to the desired phase margin. For 70 degrees, a = 0.0311.

4. Determine "T" and "aT" from the following equations:

For 70 degrees and center frequency (w) = 1, aT = 0.176 and T = 5.67

Now, we can add our lead controller to the system and view the bode plot. Remove the bode

command from your m-file and add the following:

k=1;

numlead = k*[5.67 1];

denlead = [0.176 1];

numl = conv(num,numlead);

denl = conv(den,denlead);

bode(numl,denl)

You should get the following bode plot:

Page 250: Matlab Basics

You can see that our phase margin is now 70 degrees. Let's check the closed-loop response to a

step input of 0.25m. Add the following to your m-file:

[numcl,dencl] = cloop(numl,denl);

t=0:0.01:5;

step(0.25*numcl,dencl,t)

You should get the following plot:

Although the system is now stable and the overshoot is only slightly over 5%, the settling time is

not satisfactory. Increasing the gain will increase the crossover frequency and make the response

faster. Make k = 5, your response should look like:

Page 251: Matlab Basics

The response is faster, however, the overshoot is much too high. Increasing the gain further will

just make the overshoot worse.

Adding More Phase

We can increase our phase-lead compensator to decrease the overshoot. In order to make the

iterative process easier use the following program. Create an m-file and copy the function from

your web-browser into it (make sure the function command starts in the first column of the m-

file).

function[ ] = phaseball()

%define TF

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

Page 252: Matlab Basics

num = [-K];

den = [1 0 0];

%ask user for controller information

pm = input('Phase Margin?.......');

w = input('Center Frequency?...');

k = input('Gain?...............');

%view compensated system bode plot

pmr = pm*pi/180;

a = (1 - sin(pmr))/(1+sin(pmr));

T = sqrt(a)/w;

aT = 1/(w*sqrt(a));

numlead = k*[aT 1];

denlead = [T 1];

numl=conv(num,numlead);

denl=conv(den,denlead);

figure

bode(numl,denl)

%view step response

[numcl,dencl]=cloop(numl,denl);

t=0:0.01:5;

figure

step(0.25*numcl,dencl,t)

With this m-file you can choose the phase margin, center frequency, and gain. Run your m-file

with the following values and you should see the plots below on your screen.

Phase Margin?.......80

Center Frequency?...1

Gain?...............1

Page 253: Matlab Basics

The overshoot is fine but the settling time is just a bit long. Try different numbers and see what

happens.

Using the following values the design criteria was met.

Phase Margin?.......85

Center Frequency?...1.9

Gain?...............2

Page 254: Matlab Basics

Note: A design problem does not necessarily have a unique answer. Using this method (or any

other) may result in many different compensators. For practice you may want to go back and

change the added phase, gain, or center frequency.

Page 255: Matlab Basics

State Space

6.1 State-space equations

There are several different ways to describe a system of linear differential equations. The state-

space representation is given by the equations:

where x is an n by 1 vector representing the state (commonly position and velocity variables 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:

Page 256: Matlab Basics

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:

where: 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 matrices into a m-file.

A = [ 0 1 0

980 0 -2.8

0 0 -100];

B = [0

0

100];

C = [1 0 0];

One of the first things you want to do with the state equations is find the poles of the system;

these are the values of s where det(sI - A) = 0, or the eigenvalues of the A matrix:

poles = eig(A)

You should get the following three poles:

poles =

Page 257: Matlab Basics

31.3050

-31.3050

-100.0000

One of the poles is in the right-half plane, 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,

t = 0:0.01:2;

u = 0*t;

x0 = [0.005 0 0];

[y,x] = lsim(A,B,C,0,u,t,x0);

h = x(:,2); %Delta-h is the output of interest

plot(t,h)

and run the file again.

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

Page 258: Matlab Basics

6.2 Control design using pole placement

Let's build a controller for this system. The schematic of a full-state feedback system is the

following:

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-

BK)). Since the matrices A and B*K are both 3 by 3 matrices, there will be 3 _poles for the

system. By using full-state feedback we can place the poles anywhere we want. We could 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]);

lsim(A-B*K,B,C,0,u,t,x0);

Page 259: Matlab Basics

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]);

lsim(A-B*K,B,C,0,u,t,x0);

Page 260: Matlab Basics

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

6.3 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));

lsim(A-B*K,B,C,0,u,t)

The system does not track the step well at all; not only is the magnitude not one, but i t 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

Page 261: Matlab Basics

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(A,B,C,0,K)

Note that this function is not standard in Matlab. You will need to copy it to a new m-file to use

it. Click here for more information on using functions in Matlab. 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(A-B*K,B*Nbar,C,0,u,t)

Page 262: Matlab Basics

and now a step can be tracked reasonably well.

6.4 Observer design

When we can't measure all the states x (as is commonly the case), 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 ; this will cause the estimated states to approach the values of the actual states x.

The error dynamics of the observer are given by the poles of (A-L*C).

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 (consult your text book for the derivation):

L = place(A',C',[op1 op2 op3])';

Page 263: Matlab Basics

The equations in the block diagram above are given for . It is conventional to write the

combined equations for the system plus observer using the original state x plus the error state: e

= x - . We use as state feedback u = -K . 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, =0. This gives us that the initial condition for the error is equal to the initial

condition of the state.

lsim(At,Bt,Ct,0,zeros(size(t)),t,[x0 x0])

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

to compute x-e.

Page 264: Matlab Basics

Zoom in to see some detail:

The blue solid line is the response of the ball position , the blue dotted line is the estimated

state ;

The green solid line is the response of the ball speed , the green dotted line is the estimated

state ;

The red solid line is the response of the current , the red dotted line is the estimated state .

We can see that the observer estimates the states quickly and tracks the states reasonably well in

the steady-state.

The plot above can be obtained by using the plot command.

Page 265: Matlab Basics

Example: Solution to the Cruise Control Problem Using State Space

The state equations for this problem are:

where

m=1000 kg

b=50 N*sec/kg

u=500 N

v=velocity

y=output

The design criteria are:

Rise time < 5 sec

Overshoot < 10%

Steady state error < 2%

To see the original problem setup , see Cruise Control Modeling page.

Control design using pole placement

The schematic of a full-state feedback system is shown below.

where

K=Control matrix

U=-Kv=input

R=Reference

Recall from the State-Space Tutorial page, we should use the technique called "pole placement"

to obtain the desired output. Poles of a closed-loop system can be found from the characteristic

Page 266: Matlab Basics

equation: the determinate of [sI-(A-B*K)] matrix. If desired poles can be placed into the system

by designing right control matrix (K), then the desired output can be obtained. In this tutorial,

poles will be chosen first, then 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 to be at -1.5 (arbitrary). Just as in the State-

Space Tutorial, the Matlab function called place will be used to find the control matrix K . Create

an new m-file and enter the following commands.

m=1000;

b=50;

t=0:0.1:10;

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

A=[-b/m];

B=[1/m];

C=[1];

D=[0];

x0=[0];

p1=-1.5;

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

A1=A-B*K;

lsim(A1,B,C,D,u,t,x0);

Running this m-file in the Matlab command window should give you the control matrix and the

following step response.

Page 267: Matlab Basics

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, scaling factor called Nbar (the schematic is shown

below) should be used to eliminate the steady-state error. Unlike the example in the Tutorial, the

command rscale is not applicable for our system. Nbar needs to be determined manually.

After several trial-and-error runs, the Nbar equals 30 provided the desired step response. Copy

the following commands to an m-file and run it in the Matlab command window. You should get

the step response shown below.

m=1000;

b=50;

t=0:0.1:10;

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

A=[-b/m];

B=[1/m];

Page 268: Matlab Basics

C=[1];

D=[0];

x0=[0];

p1=-1.5

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

Nbar=30;

A1=A-B*K;

lsim(A1,B*Nbar,C,D,u,t,x0);

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.

Example: A State-Space Controller for DC Motor Speed

From the main problem, the dynamic equations in state-space form are the following:

Page 269: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling a DC Motor page.

With a 1 rad/sec reference added to the system, the design criteria are:

Settling time less than 2 seconds

Overshoot less than 5%

Steady-state error less than 1%

Create a new m-file and type in the following commands (refer to the main problem for the

details of getting these commands).

J=0.01;

b=0.1;

K=0.01;

R=1;

L=0.5;

A=[-b/J K/J

-K/L -R/L];

B=[0

1/L];

C=[1 0];

D=0;

Designing the full-state feedback controller

Since both of the state variables in our problem are very easy to measure (simply add an

ammeter for current and a tachometer for the speed), we can design a full-state feedback

controller for the system without worrying about having to add an observer. The schematic for a

full-state feedback system is:

Page 270: Matlab Basics

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-

BK)) where s is the Laplace variable. Since the matrices A and B*K are both 2x2 matrices, there

should be 2 _poles for the system. By designing a full-state feedback controller, we can move

these two poles anywhere we want them. We shall first try to place them at -5 + i and -5-i (note

that this corresponds to a zeta = 0.98 which gives 0.1% overshoot and a sigma = 5 which leads to

a 1 sec settling time). Once we come up with the poles we want, Matlab will find the controller

matrix,K, for us. Simply add the following code to the end of your m-file :

p1 = -5 + i;

p2 = -5 - i;

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

Now look at the schematic above again. We see that after adding the K matrix into the system,

the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-

file:

t=0:0.01:3;

step(A-B*K,B,C,D,1,t)

Run your m-file in the command window, You should see the following plot:

Page 271: Matlab Basics

Adding a reference input

From this plot we see that the steady-state error is too large. In contrast to the other design

methods, where we feed back the output and compare it to the reference to compute an error,

here we are feeding back both states. We need to compute what the steady-state value of the

states should be, multiply that by the chosen gain K, and use this new value as our reference for

computing the input. This can be done in one step by adding a constant gain Nbar after the

reference:

We can find this Nbar factor by using the Matlab command rscale:

Page 272: Matlab Basics

Nbar=rscale(A,B,C,D,K)

Note that the function rscale is not a standard function in Matlab. You will have to copy it before you use

it. Click here for more information. Now we can plot the step response by adding the following line of

code to your m-file:

t=0:0.01:10;

step(A-B*K,B*Nbar,C,D,1,t)

title('Step Response with K Controller and Nbar')

This time, the steady-state error is much less than 1%, and all the other design criteria have been

met as well.

Example: A State-Space Controller for DC Motor Position Control

From the main problem, the dynamic equations in state-space form are the following:

Page 273: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling DC Motor Position page.

With a 1 rad reference added to the system, the design criteria are:

Settling time less than 0.04 seconds

Overshoot less than 16%

Zero steady-state error to a step input

Zero steady-state error to a step disturbance

Create a new m-file and type in the following commands (refer to the main problem for the

details of getting those commands).

J=3.2284E-6;

b=3.5077E-6;

K=0.0274;

R=4;

L=2.75E-6;

A=[0 1 0

0 -b/J K/J

0 -K/L -R/L];

B=[0 ; 0 ; 1/L];

C=[1 0 0];

D=[0];

Designing the full-state feedback controller

Since all of the state variables in our problem are very easy to measure (simply add an ammeter

for current, a tachometer for speed, and a potentiometer for position), we can design a full-state

feedback controller for the system without worrying about having to add an observer. The

schematic for a full-state feedback system is:

Page 274: Matlab Basics

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-

BKc)) where s is the Laplace variable. Since the matrices A and B*Kc are both 3x3 matrices,

there should be 3 poles for the system. By designing a full-state feedback controller, we can

move these three poles anywhere we want them. We shall first try to place them at -100 + 100i

and -100-100i (note that this corresponds to a zeta = 0.5 which gives 0.16% overshoot and a

sigma = 100 which leads to a .04 sec settling time). Once we come up with the poles we want,

Matlab will find the controller matrix,Kc, for us. Simply add the following code to the end of

your m-file :

p1=-100+100i;

p2=-100-100i;

p3=-200;

Kc=place(A,B,[p1,p2,p3]);

Now look at the schematic above again. We see that after adding the K matrix into the system,

the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-

file:

Page 275: Matlab Basics

t=0:0.001:.05;

step(A-B*Kc,B,C,D,1,t)

Run your m-file in the command window, You should see the following plot:

Disturbance Response

In order to get the disturbance response, we must provide the proper input to the system.

Physically, a disturbance is a torque which acts on the inertia of the motor. A torque acts as an

additive term in the second state equation (which gets divided by J, as do all the other terms in

this equation). We can simulate this simply by modifying our closed loop input matrix, B, to

have a 1/J in the second row. Add the following line to your m-file and re-run.

step(A-B*Kc,[0;1/J;0],C,D,1,t)

Page 276: Matlab Basics

This is not a zero steady-state error to a disturbance, and we will have to compensate for this.

Adding Integral Action

We know that if we put an extra integrator in series with the plant it can remove steady-state

error to an input. If the integrator comes before the injection of the disturbance, it will cancel the

disturbance in steady state. This changes our control structure so it now resembles the following:

We can model the integrator by augmenting our state equations with an extra state which is the

integral of the output. This adds an extra equation which states that the derivative of the integral

of theta is theta. This equation will be placed at the top of our matrices. The input, r, now enters

Page 277: Matlab Basics

the system before the integrator, so it appears in the newly added top equation. The output of the

system remains the same.

These equations represent the dynamics of the system before the loop is closed. We will refer to

the matrices in this equation as Aa, Ba, Ca, and Da. We will refer to the state vector of the

augmented system as xa. Note that the reference, r, does not affect the states (except the

integrator state) or the output of the plant - this is expected, since there is no path from the

reference to the plant input, u, without implementing the feedback matrix, Kc.

In order to find the closed loop equations, we have to look at how the input, u, affects the plant.

In this case, it is exactly the same as in the unaugmented equations. Therefore, there is a vector,

call it Bau, which replaces Ba when we are treating u as the input. This is just our old B vector

with an extra zero added as a first row. Since u=Kc*xa is the input to the plant for the closed

loop, but r is the input to the closed loop system, the closed loop equations will depend on both

Bau and Ba. The closed loop equations will become:

Now, the integral of the output will be fed back, and will be used by the controller to remove

steady state error to a disturbance. We can now redesign our controller. Since we need to place

one closed-loop pole for each pole in the plant, we will place another pole at -300, which will be

faster than the rest of the poles. Since the closed-loop system matrix depends on Bau, we will use

Bau in the place command rather that Ba. Add the following to your m-file:

Aa=[0 1 0 0

Page 278: Matlab Basics

0 0 1 0

0 0 -b/J K/J

0 0 -K/L -R/L];

Ba=[ -1 ; 0 ; 0 ; 0];

Bau=[0 ; 0 ; 0 ; 1/L ];

Ca=[0 1 0 0];

Da=[0];

p4=-300;

Kc=place(Aa,Bau,[p1,p2,p3,p4]);

t=0:0.001:.05;

step(Aa-Bau*Kc,Ba,Ca,Da,1,t)

Run your m-file (or just these new lines) and you will get the following output.

To look at the disturbance response, we apply a similar B matrix as we did previously when

simulating the disturbance response.

step(Aa-Bau*Kc,[0 ; 0 ; 1/J ; 0] ,Ca,Da,1,t)

Page 279: Matlab Basics

We can see that all of the design specifications have been met by this controller.

Example: A State-space Controller for a Bus Suspension System

From the main problem, the dynamic equations in state-space form are the following:

Page 280: Matlab Basics

For the original problem setup and the derivation of the above equations, please refer to the

Modeling page.

We want to design a feedback controller so that when the road disturbance (W) is simulated by a

unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less

than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate

within a range of +/- 5 mm and will stop oscillating within 5 seconds.

The system model can be represented in Matlab by creating a new m-file and entering the

following commands (refer to main problem for the details of getting those commands). We need

to define the A, B, C, D matrices by entering the following into the m-file:

m1=2500;

m2=320;

k1 = 80000;

k2 = 500000;

b1 = 350;

b2 = 15020;

A=[0 1 0

0

-(b1*b2)/(m1*m2) 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1)

-(b1/m1)

b2/m2 0 -((b1/m1)+(b1/m2)+(b2/m2))

1

k2/m2 0 -((k1/m1)+(k1/m2)+(k2/m2))

0];

B=[0 0

1/m1 (b1*b2)/(m1*m2)

0 -(b2/m2)

(1/m1)+(1/m2) -(k2/m2)];

C=[0 0 1 0];

D=[0 0];

Page 281: Matlab Basics

Designing the full-state feedback controller

First, let's design a full-state feedback controller for the system. Assuming for now that all the

states can be measured (this assumption is probably not true but is sufficient for this problem),

the schematic of the system should be:

The characteristic polynomial for this closed-loop system is the determinant of (sI-(A-

B[1,0]'K)). Note that it's not sI-(A-BK) because the controller K can only control the force input

u but not the road disturbance W. Recall that our B matrix is a 4 x 2 matrix, and we only need

the first column of B to control u.

For this example, we have to use integral action to achieve zero steady-state error, so we add an

extra state which is . Since in reality the bus will eventually reach an equilibrium

that yields a zero steady-state error. New states become X1, Y1, and Y2. Also the state-space

matrices, A,B,and C, after adding extra state change to be the following:

Aa=[0 1 0

0 0

-(b1*b2)/(m1*m2) 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1)

-(b1/m1) 0

b2/m2 0 -((b1/m1)+(b1/m2)+(b2/m2))

1 0

Page 282: Matlab Basics

k2/m2 0 -((k1/m1)+(k1/m2)+(k2/m2))

0 0

0 0 1

0 0];

Ba=[0 0

1/m1 (b1*b2)/(m1*m2)

0 -(b2/m2)

(1/m1)+(1/m2) -(k2/m2)

0 0];

Ca=[0 0 1 0 0];

Da=[0 0];

Actually, there is a shortcut for matlab to achieve the same result.

Aa = [[A,[0 0 0 0]'];[C, 0]];

Ba = [B;[0 0]];

Ca = [C,0];

Da = D;

Add the above matlab code into the m-file. In this case, we treated the problem like PID

controller design. The integral control is obtained from the new state. The proportional control is

obtained from a gain on Y1 or X1-X2. The direct derivative control for the output isn't possible,

since derivative of Y1 or X1-X2 isn't a state. Instead we use the derivative of X1 , which is

available for feedback. (While X1 maybe hard to measure, could be obtained by integrating the

output of an accelerometer mounted on the bus.) It is similar to adding more damping to velocity

of oscillation of the bus. Add the following matlab code for controller K in the m-file:

K = [0 2.3e6 5e8 0 8e6]

We arrive with this value of matrix with trial and error by adjusting gain for derivative of X1,Y1

and integral of Y1, as we previously mentioned.

Plotting the closed-loop response

Looking at the schematic above again, we see that after adding the K matrix into the system, the

state-space equations become:

Page 283: Matlab Basics

We can now obtain the closed-loop response by simply adding the following code into your m-

file. Note that we need to multiply B matrix by 0.1 to simulate 0.1 m high step disturbance:

t=0:0.01:2;

step(Aa-Ba(:,1)*K,-0.1*Ba,Ca,Da,2,t)

title('Closed-loop response to a 0.1 m step')

Running the m-file in the command window, you should see the following plot:

From the plot we see that the percent overshoot and settling time requirements are satisfied.

Moreover the steady-state error approaches zero as well. Therefore, we will determine that the

response is satisfactory. Feel free to play around with the gain for matrix K. But you will most

likely get the response to have either large percent overshoot or very long settling time. But if

you do find a better response, please email us with your results! We are always interested in

Page 284: Matlab Basics

different ways to solve our examples; we may include your solution in a future version of these

tutorials.

Example: State-space design for the inverted pendulum

The state equations for this problem are:

The design criteria for this system with the cart receiving a 0.2 m step input are as follows:

Settling time for x and theta of less than 5 seconds.

Rise time for x of less than 1 second.

Overshoot of theta less than 20 degrees (0.35 radians).

Steady-state error within 2%.

As you may have noticed if you went through some of the other inverted pendulum examples the

design criteria for this example are different. In the other other examples we were dealing with

an impulse and not a step input. Also, we were only concerned with the pendulums angle and

disregarded the cart's position in the design of the controller. However, for an inverted pendulum

it is unrealistic to consider just the single output system. Using state-space methods it is

relatively simple to work with a multi-output system, so in this example we will design a

controller with both the pendulum angle and the cart position in mind.

To see how this problem was originally set up, consult the inverted pendulum modeling page.

This problem can be solved using full state feedback. The schematic of this type of control

system is shown below:

Page 285: Matlab Basics

If you are interested in running an animation of this example based on the control techniques

used in the state-space tutorial please go to the Inverted Pendulum Animation Page after

completing this tutorial.

Open-loop poles

In this problem R represents the commanded step input to the cart. The 4 states represent the

position and velocity of the cart and the angle and angular velocity of the pendulum. The output

y contains both the position of the cart and the angle of the pendulum. We want to design a

controller so that when an step input is given to the system, the pendulum should be displaced,

but eventually return to zero (i.e. the vertical) and the cart should move to it's new commanded

position. To view the system's open-loop response please refer to the inverted pendulum

modeling Page

The first step in designing this type of controller is to determine the open-loop poles of the

system. Enter the following lines of code into a m-file (or a '.m' file located in the same directory

as Matlab):

M = 0.5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

p = i*(M+m)+M*m*l^2; %denominator

A = [0 1 0 0;

Page 286: Matlab Basics

0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;

0 0 0 1;

0 -(m*l*b)/p m*g*l*(M+m)/p 0];

B = [0; (i+m*l^2)/p; 0; m*l/p];

C = [1 0 0 0;

0 0 1 0];

D = [0;0];

p = eig(A)

The Matlab command window should output the following text as a result:

p =

0

-0.1428

5.5651

-5.6041

As you can see, there is one right-half-plane pole at 5.5651. This should confirm your intuition

that the system is unstable in open loop.

LQR design

The next step in the design process is to assume that we have full-state feedback (i.e. that we can

measure all four states), and find the vector K which determines the feedback control law. This

can be done in a number of ways. If you know the desired closed-loop poles, you can use the

place or acker command. Another option is to use the lqr function; this will give you the optimal

controller (under certain assumptions; consult your textbook for more details). The lqr function

allows you to choose two parameters, R and Q, which will balance the relative importance of the

input and state in the cost function that you are trying to optimize. The simplest case is to assume

R=1, and Q=C'*C. You may notice that we are using both outputs (the pendulum's angle and the

cart's position). Essentially, the lqr method allows for the control of both outputs. In this case, it

is pretty easy to do. The controller can be tuned by changing the nonzero elements in the Q

matrix to get a desirable response.

Page 287: Matlab Basics

Note: Matlab commands from the control system toolbox are highlighted in red.

To find the structure of Q, enter the following into the Matlab command window:

C'*C

You should see the following in the command window:

ans =

1 0 0 0

0 0 0 0

0 0 1 0

0 0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the

3,3 position will be used to weight the pendulum's angle. The input weighting R will remain at 1.

Now that we know what the Q matrix should look like we can experiment to find the K matrix

that will give us a good controller. We will go ahead and find the K matrix and plot the response

all in one step so that changes can be made in the control and be seen automatically in the

response. Enter the following text into your m-file:

x=1;

y=1;

Q=[x 0 0 0;

0 0 0 0;

0 0 y 0;

0 0 0 0];

R = 1;

K = lqr(A,B,Q,R)

Ac = [(A-B*K)];

Bc = [B];

Cc = [C];

Dc = [D];

T=0:0.01:5;

Page 288: Matlab Basics

U=0.2*ones(size(T));

[Y,X]=lsim(Ac,Bc,Cc,Dc,U,T);

plot(T,Y)

legend('Cart','Pendulum')

You should get the following value for K and a response plot:

K =

-1.0000 -1.6567 18.6854 3.4594

The curve in green represents the pendulum's angle, in radians and the curve in blue represents

the cart's position in meters. As you can see, this plot is not satisfactory. The pendulum and cart's

overshoot appear fine, but their settling times need improvement and the cart's rise time needs to

go down. As I'm sure you have noticed the cart is not near the desired location but has in fact

moved in the other direction. This error will be dealt with in the next section and right now we

will focus on the settling and rise times. Go back to your m-file and change the x and y variables

to see if you can get a better response. You will find that increasing x makes the settling and rise

times go down, and lowers the angle the pendulum moves. Using x=5000 and y=100, the

following value of K and step response were found:

Page 289: Matlab Basics

K =

-70.7107 -37.8345 105.5298 20.9238

You may have noted that if you increased x and y even higher, you could improve the response

even more. The reason this plot was chosen was because it satisfied the design requirements

while keeping x and y as small as possible. In this problem, x and y have been used to describe

the relative weight of the tracking error in the cart's position and pendulum's angle versus the

control effort. The higher x and y are, the more control effort is used, but the smaller the tracking

error. The system response has a settling time under 2 seconds.

Adding the reference Input

Now we want to get rid of the steady-state error. In contrast to the other design methods, where

we feedback the output and compare it to the reference input to compute an error, with a full-

state feedback controller we are feeding back all the states. We need to compute what the steady-

state value of the states should be, multiply that by the chosen gain K, and use a new value as our

Page 290: Matlab Basics

reference for computing the input. This can be done by adding a constant gain Nbar after the

reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your m-file

is in). Delete the lsim line and copy the following to your m-file and run it to view the step

response with Nbar added.

Cn=[1 0 0 0];

Nbar=rscale(A,B,Cn,0,K)

Bcn=[Nbar*B];

[Y,X]=lsim(Ac,Bcn,Cc,Dc,U,T);

plot(T,Y)

legend('Cart','Pendulum')

Note: Non-standard matlab commands are highlighted in green.

A different C had to be used because the rscale function will not work for multiple outputs.

However, the Nbar found is correct, as you can see from the output below:

Nbar =

-70.7107

Page 291: Matlab Basics

Now, the steady-state error is within our limits, the rise and settling times are met and the

pendulum's overshoot is within range of the design criteria.

Observer design

This response is good, but was found assuming full-state feedback, which most likely will not be

a valid assumption. To compensate for this, we will next design a full-order estimator to estimate

those states that are not measured. A schematic of this kind of system is shown below, without

Nbar:

To begin, we must first find the controller poles. To do this copy the following code to the end of

your m-file:

Page 292: Matlab Basics

p = eig(Ac)

If you changed the weighting factors x and y above to x=5000 and y=100, you should see the

following poles in the Matlab command window:

p =

-8.4910 + 7.9283i

-8.4910 - 7.9283i

-4.7592 + 0.8309i

-4.7592 - 0.8309i

We want to design estimator poles that are about 4-10 times as fast as slowest pole, say at -40.

We will use the place command in Matlab to find the L vector (note that acker would also work).

Remember that the place command cannot have all the desired poles at the same location. Delete

from the lsim command on and enter the following text to the end of your m-file to find the L

matrix:

P = [-40 -41 -42 -43];

L = place(A',C',P)'

We are using both outputs (the angle of the pendulum and the position of the cart) to design the

observer. The system is not observable using only the angle of the pendulum as output; you can

check this in Matlab by computing rank(obsv(A,C(2,:))). This should make sense to you: if you

can only measure the angle of the pendulum, you cannot determine what the position of the cart

will be.

You should see the following in the Matlab window:

L =

1.0e+03 *

0.0826 -0.0010

Page 293: Matlab Basics

1.6992 -0.0402

-0.0014 0.0832

-0.0762 1.7604

Now we will combine the control-law design with the estimator design to get the compensator.

The response should be similar to the one from the control-law design. To set up the

compensator copy the following code to the end of your m-file:

Ace = [A-B*K B*K;

zeros(size(A)) (A-L*C)];

Bce = [ B*Nbar;

zeros(size(B))];

Cce = [Cc zeros(size(Cc))];

Dce = [0;0];

T = 0:0.01:5;

U = 0.2*ones(size(T));

[Y,X] = lsim(Ace,Bce,Cce,Dce,U,T);

plot(T,Y)

legend('Cart','Pendulum')

After running this m-file, you should output the following step response simulation plot:

Page 294: Matlab Basics

This response is about the same as before. All of the design requirements have been met with the

minimum amount of control effort, so no more iteration is needed.

As you can see, it is much easier to control multi-input or multi-output systems with the state

space method than with any other of the methods.

If you are interested in running an animation of the inverted pendulum example based on the

control techniques used in this tutorial please go to the Inverted Pendulum Animation Page.

Example: State-space method for the Pitch Controller

In the Pitch Controller Modeling page, the state-space model was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the

pitch angle (theta).

The design requirements are

Overshoot: Less than 10%

Rise time: Less than 5 seconds

Settling time: Less than 10 seconds

Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page.

If you are interested in running an animation of this example based on the control techniques

used in the state-space tutorial please go to the Pitch Controller Animation page after completing

this tutorial.

Page 295: Matlab Basics

Controllability and Observability

The first thing to do in designing a system via state-space method is to check the controllability

and observability of the system. For the system to be completely state controllable, the

controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or

columns). In the same token, for the system to be completely state observable, the observability

matrix

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3,

the rank for both matrices must be 3. The Matlab command rank can give you the ranks of both

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

A=[-0.313 56.7 0;

-0.0139 -0.426 0;

0 56.7 0];

B=[0.232;

0.0203;

0];

C=[0 0 1];

D=[0];

Page 296: Matlab Basics

co=ctrb (A,B);

ob=obsv (A,C);

Controllability=rank(co)

Observability=rank(ob)

If you run this m-file in the Matlab command window, you should see

Controllability =

3

Observability =

3

This proves that our system is both completely state controllable and completely state

observable.

Control design via pole placement

The schematic of a full-state feedback system is shown below:

where

K=Control matrix

x=State matrix (alpha, q, theta)

de=-Kx=input

Page 297: Matlab Basics

R=Reference

Recall from the State-Space Tutorial page, the "pole placement" technique should be used to find

the control matrix (K). Since the determinate of [sI-(A-BK)] matrix is a third-order polynomial,

there are three poles we can place.

In the State-Space Tutorial, the dominant second-order pole placement method was introduced.

However for this example, we will use another method called Linear Quadratic Regulator

(LQR) method. This method allows you to find the optimal control matrix that results in some

balance between system errors and control effort. Please consult your control textbook for

details. To use this LQR method, we need to find three parameters: performance index matrix

(R), state-cost matrix (Q), and weighting factor (p). For simplicity, we will choose the

performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to C' x C. The

weighting factor (p) will be varied as we see the step response. To see the structure of the Q

matrix, type in the following commands to an m-file and run it in the Matlab command window

(or you can simply type them directly into the command window).

C=[0 0 1];

Q=C'*C

You should see the following Q matrix in the command window:

Q =

0 0 0

0 0 0

0 0 1

Page 298: Matlab Basics

Now we are ready to find the control matrix (K) and see the response of the system. First, let the

weighting factor (p) equal 50. Enter the following commands to a new m-file and run it in the

Matlab command window.

t=0:0.1:10;

de=0.2*ones(size(t));

yo=[0 0 0];

A=[-0.313 56.7 0;

-0.0139 -0.426 0;

0 56.7 0];

B=[0.232;

0.0203;

0];

C=[0 0 1];

D=[0];

p=50;

Q=[0 0 0;

0 0 0;

0 0 p];

[K]= lqr (A,B,Q,1)

lsim (A-B*K,B,C,D,de,t,yo)

After you run this m-file, you should see the step response similar to the one shown below:

Page 299: Matlab Basics

The rise time, overshoot, and settling time looks satisfactory. However, there is a large steady-

state error. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference input

Unlike other design methods, the full-state feedback system does not compare the output to the

reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference

(see the schematic shown above). Thus, we should not expect to see the output equal to the input.

To obtain the desired output, we need to scale the reference input so that the output equals the

reference. This can be easily done by introducing a feed-forwarding scaling factor called Nbar.

The basic schematic with the scaling factor (Nbar) is shown below:

We can easily find Nbar from the Matlab function rscale. Since this rscale is a user-defined

function, you need to copy and save the rscale m-file to your directory. For further assistance in

using user-defined functions, refer to Function. After you have saved the rscale m-file to your

Page 300: Matlab Basics

directory, enter the following commands to a new m-file and run it in the Matlab command

window. You should see the response shown below:

t=0:0.1:10;

de=0.2*ones(size(t));

yo=[0 0 0];

A=[-0.313 56.7 0;

-0.0139 -0.426 0;

0 56.7 0];

B=[0.232;

0.0203;

0];

C=[0 0 1];

D=[0];

x=50;

Q=[0 0 0;

0 0 0;

0 0 x];

[K]= lqr (A,B,Q,1)

Nbar = rscale(A,B,C,D,K)

lsim (A-B*K,B*Nbar,C,D,de,t,yo)

Page 301: Matlab Basics

Now the steady-state error has been eliminated and all design requirements are satisfied.

If you are interested in running an animation of the pitch controller example based on the control

techniques used in this tutorial please go to the Pitch Controller Animation page.

Example: Solution to the Ball & Beam Problem Using the State-space Design Method

The state-space representation of the ball and beam example is given below:

Remember, unlike the previous examples where we controlled the gear's angle to control the

beam and ball, here we are controlling alpha-doubledot. By doing this we are essentially

controlling a torque applied at the center of the beam by a motor. Therefore, we do not need a

gear and lever system.

Page 302: Matlab Basics

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

To see the derivation of the state-space equations for this problem refer to the ball and beam

modeling page.

If you are interested in running an animation of this example based on the control techniques

used in the state-space tutorial please go to the Ball & Beam Animation Page after completing

this tutorial.

Full-State Feedback Controller

We will design a controller for this physical system that utilizes full-state feedback control. A

schematic of this type of system is shown below:

Recall, that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-

BK)), where s is the Laplace variable. For our system the A and B*K matrices are both 4x4.

Hence, there should be four poles for our system. In designing our full-state feedback controller

we can move these poles anywhere we want.

For our design we desire an overshoot of less than 5% which corresponds to a zeta of 0.7 (please

refer to your textbook for the relationship between overshoot and damping ratio). On a root locus

this criterion is represented as a 45 degree line emanating from the origin and extending out into

the left-half plane. We want to place our poles on or beneath this line. Our next criterion is a

settling time less than 3 seconds, which corresponds to a sigma = 4.6/Ts = 4.6/3 = 1.53,

represented by a vertical line at -1.53 on the root locus. Anything beyond this line in the left-half

plane is a suitable place for our poles. Therefore we will place our poles at -2+2i and -2-2i. We

will place the other poles far to the left for now, so that they will not affect the response too

Page 303: Matlab Basics

much. To start with place them at -20 and -80. Now that we have our poles we can use Matlab to

find the controller (K matrix) by using the place command. Copy the following code to an m-file

to model the system and find the K matrix:

NOTE: Matlab commands from the control system toolbox are highlighted in red.

m = 0.111;

R = 0.015;

g = -9.8;

J = 9.99e-6;

H = -m*g/(J/(R^2)+m);

A=[0 1 0 0

0 0 H 0

0 0 0 1

0 0 0 0];

B=[0;0;0;1];

C=[1 0 0 0];

D=[0];

p1=-2+2i;

p2=-2-2i;

p3=-20;

p4=-80;

K=place(A,B,[p1,p2,p3,p4])

Run your m-file and you should get the following output for the K matrix:

place: ndigits= 15

K =

1.0e+03 *

Page 304: Matlab Basics

1.8286 1.0286 2.0080 0.1040

After adding the K matrix, the state space equations now become:

We can now simulate the closed-loop response to a 0.25m step input by using the lsim command.

Add the following to your m-file:

T = 0:0.01:5;

U = 0.25*ones(size(T));

[Y,X]=lsim(A-B*K,B,C,D,U,T);

plot(T,Y)

Run your m-file and you should get the following plot:

Page 305: Matlab Basics

From this plot we see that there is a large steady state error for which we will need to add

reference input (explained in next section). However, the overshoot and settling time criteria are

met. If we wanted to reduce the overshoot further than we would make the imaginary part of the

pole smaller than the real part. Also, if we wanted a faster settling time we would move the poles

further in the left-half plane. Feel free to experiment with the pole positions to see these trends.

Reference Input

Now we want to get rid of the steady-state error. In contrast to the other design methods, where

we feedback the output and compare it to the reference input to compute an error, with a full-

state feedback controller we are feeding back both states. We need to compute what the steady-

state value of the states should be, multiply that by the chosen gain K, and use a new value as our

reference for computing the input. This can be done by adding a constant gain Nbar after the

reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your m-file

is in). Copy the following to your m-file and run it to view the step response with Nbar added.

Nbar=rscale(A,B,C,D,K)

T = 0:0.01:5;

U = 0.25*ones(size(T));

[Y,X]=lsim(A-B*K,B*Nbar,C,D,U,T);

plot(T,Y)

Page 306: Matlab Basics

Note: Non-standard Matlab commands used in this example are highlighted in green.

Your output should be:

place: ndigits= 15

Nbar =

1.8286e+03

Now the steady-state error is gone and all the design criteria are satisfied.

Note: A design problem does not necessarily have a unique answer. Using this method (or any

other) may result in many different compensators. For practice you may want to go back and try

to change the pole positions to see how the system responds.

If you are interested in running an animation of the ball & beam example based on the control

techniques used in this tutorial please go to the Ball & Beam Animation Page.

Page 307: Matlab Basics

Digital Control

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

Page 308: Matlab Basics

The purpose of this Digital Control Tutorial is to show you how to work with discrete functions

either in transfer function or state-space form to design digital control systems.

7.2 Zero-order 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.

Page 309: Matlab Basics

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.

Page 310: Matlab Basics

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 continuous u(t) goes through H(s) to produce the

continuous output y(t).

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.

7.3 Conversion using c2dm

There is a Matlab function called c2dm that converts a given continuous system (either in

transfer function or state-space form) to discrete system using the zero-order hold operation

explained above. The basic command for this c2dm is one of the following.

[numDz,denDz] = c2dm (num,den,Ts,'zoh')

[F,G,H,J] = c2dm (A,B,C,D,Ts,'zoh')

Page 311: Matlab Basics

The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the

closed-loop bandwidth frequency.

1. Transfer function

Suppose you have the following continuous transfer function

M = 1 kg

b = 10 N.s/m

k = 20 N/m

F(s) = 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;

num=[1];

den=[M b k];

Ts=1/100;

[numDz,denDz]=c2dm(num,den,Ts,'zoh')

Running this m-file in the command window should give you the following numDz and denDz

matrices.

numDz =

1.0e-04 *

0 0.4837 0.4678

Page 312: Matlab Basics

denDz =

1.0000 -1.9029 0.9048

From these matrices, the discrete transfer function can be written as

Note: The numerator and denominator matrices will be represented by the descending powers of

z. For more information on Matlab representation, please refer to Matlab representation.

Now you have the transfer function in discrete form.

2. State-Space

Suppose you have the following continuous state-space model

All constants are same as before

The following m-file converts the above continuous state-space to discrete state-space.

M=1;

b=10;

k=20;

A=[0 1;

-k/M -b/M];

Page 313: Matlab Basics

B=[ 0;

1/M];

C=[1 0];

D=[0];

Ts=1/100;

[F,G,H,J] = c2dm (A,B,C,D,Ts,'zoh')

Create an new m-file and copy the above commands. Running this m-file in the Matlab

command window should give you the following matrices.

F =

0.9990 0.0095

-0.1903 0.9039

G =

0.0000

0.0095

H =

1 0

J =

0

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

Page 314: Matlab Basics

Now you have the discrete time state-space model.

Note: For more information on the discrete state-space, please refer to Discrete State-Space.

7.4 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

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.

Page 315: Matlab Basics

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.

where

zeta = Damping ratio

Page 316: Matlab Basics

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

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];

pzmap(numDz,denDz)

axis([-1 1 -1 1])

zgrid

Page 317: Matlab Basics

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 the 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, the settling time of 0.65 sec and

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

[x] = dstep (numDz,denDz,51);

t = 0:0.05:2.5;

stairs (t,x)

Page 318: Matlab Basics

As you can see from the plot, all of the rise time, the settling time and the overshoot came out to

be what we expected. We proved you here that we can use the locations of poles and the above

three equations to analyze the transient response of the system.

For more analysis on the pole locations and transient response, see Transient Response.

7.5 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

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 the right gain (K). For the discrete root-locus analysis, we use the

function zgrid that has the same characteristics as the sgrid. The command zgrid(zeta, Wn) draws

lines of constant damping ratio (zeta) and natural frequency (Wn).

Page 319: Matlab Basics

Suppose we have the following discrete transfer function

and the requirements of having damping ratio greater than 0.6 and the 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 draws 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];

rlocus (numDz,denDz)

axis ([-1 1 -1 1])

zeta=0.4;

Wn=0.3;

zgrid (zeta,Wn)

Page 320: Matlab Basics

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.

Digital Control Example: Designing Cruise Control using Root-Locus method

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. If you refer to the Cruise Control: Modeling page,

the open-loop transfer function was derived as

where

m = 1000

b = 50

U(s) = 10

Y(s) = velocity output

The design requirements are

Rise time: Less than 5 seconds

Overshoot: Less than 10%

Steady-state error: Less than 2%

Discrete transfer function

The first step in performing a discrete analysis of a system is to find the discrete equivalent

transfer function of the continuous portion. We will convert the above transfer function

(Y(s)/U(s)) to the discrete transfer function using the Matlab function called c2dm. To use this

c2dm, you need to specify four arguments: Numerator matrix (num), denominator matrix (den),

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

Page 321: Matlab Basics

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 equals 1/50 sec assuming that the bandwidth frequency is 1 rad/sec. Now

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

num=[1];

den=[1000 50];

Ts=1/50;

[numDz,denDz] = c2dm (num,den,Ts,'zoh')

The following matrices should be returned to the command window.

numDz =

1.0e-04*

0 0.1999

denDz =

1.0000 -0.9990

From these matrices, the discrete transfer function can be written as

Root-Locus in z-plane

Recall from the Digital Control Tutorial, the Matlab function called zgrid should be used to find

an acceptable region of the discrete root-locus that gives the desired gain (K). The zgrid requires

two arguments: Natural frequency (Wn) and the damping ratio (zeta). These two arguments can

be found from the rise time and the overshoot requirements and the following two equations.

Page 322: Matlab Basics

where

Wn=Natural frequency (rad/sec)

zeta=Damping ratio

Tr=Rise time

Mp=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 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 the unit of rad/sample, so let the 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 (numDz,denDz)

zgrid (zeta, Wn)

axis ([-1 1 -1 1])

Page 323: Matlab Basics

The dotted line on the right, which is very small and can not be seen in this case, 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.

In the above plot, you see that the root-locus is drawn in the desired region. Let's find a gain (K)

using the Matlab function rlocfind and obtain the corresponding step response. Add the

following commands to the above m-file and rerun it in the Matlab command window.

[K,poles]=rlocfind (numDz,denDz)

[numcDz,dencDz] = cloop (K*numDz,denDz);

U=10;

[x] = dstep (U*numcDz,dencDz,201);

figure

t=0:0.05:10;

stairs (t,x)

In the command window, you should see the prompt asking you to select a point on the root-

locus. Click on the root-locus around +0.9. The gain (K) and the pole location should be returned

to the command window. Also, you should see the closed-loop stairstep response shown below.

Page 324: Matlab Basics

As you noticed, this response satisfies all of the design requirements. But the gain associated

with this response is approximately 4500. The system having this large gain (too much control

effort) might not be available in a real physical system , even though it is possible in the Matlab

simulation. To obtain a desired response with a reasonable control effort, we will modify the

discrete controller.

Compensation using a digital controller

Recall from the continuous Cruise Control: Root-Locus page, the lag controller 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 a function of the form

There is a guideline to design digital lead and lag compensators. However, design method

described there generally applies for improving the system response. In this this particular

Page 325: Matlab Basics

problem, we are not going to use the method described in that page and use our own educated

analysis to design the compensator.

First, we need to reduce the gain (K) while keeping the reasonable response. Recall from your

control textbook, the gain (K) equals 0 at poles and infinity at zeros. Thus, if we place the pole

inside the desired region and pick a locus near that pole, we should have a reasonable response

with smaller gain. Moreover, for the system to be stable, all poles must be placed inside the unit

circle.

Consider these two things, we will place the compensator pole somewhere outside the natural

frequency requirement and inside the damping ratio requirement, say at +0.6, and the zero at the

left of that pole, say at -0.6. The location of this zero can be changed later, if necessary.

Now we got the discrete compensator transfer function. Let's generate the root-locus and obtain

the step response. Create an new m-file and enter the following commands.

num=[1];

den=[1000 50];

Ts=1/50;

[numDz,denDz] = c2dm (num,den,Ts,'zoh');

numleadDz=[1 0.6];

denleadDz=[1 -0.6];

numDnew=conv (numDz,numleadDz);

denDnew=conv (denDz,denleadDz);

Wn=0.0072;

zeta=0.6;

rlocus (numDnew,denDnew)

zgrid (zeta, Wn)

axis ([-1 1 -1 1])

[K,poles] = rlocfind (numDnew,denDnew)

[numcDnew,dencDnew] = cloop (K*numDnew,denDnew);

Page 326: Matlab Basics

U=10;

[x] = dstep (U*numcDnew,dencDnew,201);

figure

t=0:0.05:10;

stairs (t,x)

Running this m-file in the command window give you the following root-locus.

In the command window, you should be asked to pick a point on the root-locus. Click on the

locus near +0.9. You should now have the step response similar to the one shown below.

Page 327: Matlab Basics

This response is about the same as what we obtained without the additional controller. However,

if you check the command window, the gain has decreased to around 1000. This system satisfies

all design requirements with the reasonable 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.

Example: Digital DC Motor Speed Control with PID Control

In this page, we will consider the digital control version of DC motor speed problem. A digital

DC motor model can be obtained from conversion of the analog model, as we will describe. The

controller for this example will be designed by a PID method.

From the Modeling: a DC Motor, the open-loop transfer function for DC motor's speed was

derived as:

Where:

*electrical resistance (R) = 1 ohm

*electrical inductance (L) = 0.5 H

Page 328: Matlab Basics

*electromotive force constant (Ke=Kt) = 0.01 Nm/Amp

*moment of inertia of the rotor (J) = 0.01 kg*m^2/s^2

*damping ratio of the mechanical system (b) = 0.1 Nms

*input (V): Source Voltage

*output (theta dot): Rotating speed

*The rotor and shaft are assumed to be rigid

The design requirements for 1 rad/sec step input are

Settling time: Less than 2 seconds

Overshoot: Less than 5%

Steady-state error: Less than 1%

Continuous to Discrete Conversion

The first step in designing a discrete control system is to convert the continuous transfer function

to a discrete transfer function. Matlab command c2dm will do this for you. The c2dm command

requires the following four arguments: the numerator polynomial (num), the denominator

polynomial (den), the sampling time (Ts) and the type of hold circuit. In this example, the hold

we will use is the zero-order hold ('zoh').

From the design requirement, let the sampling time, Ts equal to 0.12 seconds, which is 1/10 the

time constant of a system with a settling time of 2 seconds. Let's create a new m-file and enter

the following commands:

R=1;

L=0.5;

Kt=0.01;

J=0.01;

b=0.1;

num = Kt;

den = [(J*L) (J*R)+(L*b) (R*b)+(Kt^2)];

Ts = 0.12;

[numz,denz] = c2dm(num,den,Ts,'zoh')

Page 329: Matlab Basics

Running this m-file should return the following:

numz =

0 0.0092 0.0057

denz =

1.0000 -1.0877 0.2369

From these matrices, the discrete transfer function can be written as:

First, we would like to see what the closed-loop response of the system looks like without any

control. If you see the numz matrices shown above, it has one extra zero in the front, we have to

get rid of it before closing the loop with the Matlab cloop command. Add the following code into

the end of your m-file:

numz = [numz(2) numz(3)];

[numz_cl,denz_cl] = cloop(numz,denz);

After you have done this, let's see how the closed-loop step response looks like. The dstep

command will generate the vector of discrete output signals and stairs command will connect

these signals. Click here for more information. Add the following Matlab code at the end of

previous m-file and rerun it.

[x1] = dstep(numz_cl,denz_cl,101);

t=0:0.12:12;

stairs(t,x1)

xlabel('Time (seconds)')

ylabel('Velocity (rad/s)')

title('Stairstep Response:Original')

Page 330: Matlab Basics

You should see the following plot:

PID Controller

Recall that the continuous-time transfer function for a PID controller is:

There are several ways for mapping from the s-plane to z-plane. The most accurate one is

. We cannot obtain PID transfer function in this way because the discrete-time transfer function

would have more zeroes than poles, which is not realizable. Instead we are going to use the

bilinear transformation shown as follows:

Thus we can derive the discrete PID controller with bilinear transformation mapping. For more

detail derivation of discrete PID controller, see Discrete PID Controller. Equivalently, the c2dm

Page 331: Matlab Basics

command in Matlab will help you to convert the continuous-time PID compensator to discrete-

time PID compensator by using the "tustin" method in this case. The "tustin" method will use

bilinear approximation to convert to discrete time of the derivative. According to the PID Design

Method for the DC Motor page, Kp = 100, Ki = 200 and Kd = 10 are satisfied the design

requirement. We will use all of these gains in this example. Now add the following Matlab

commands to your previous m-file and rerun it in Matlab window.

% Discrete PID controller with bilinear approximation

Kp = 100;

Ki = 200;

Kd = 10;

[dencz,numcz]=c2dm([1 0],[Kd Kp Ki],Ts,'tustin');

Note that the numerator and denominator in c2dm were reversed above. The reason is that the

PID transfer function is not proper. Matlab will not allow this. By switching the numerator and

denominator the c2dm command can be fooled into giving the right answer. Let's see if the

performance of the closed-loop response with the PID compensator satisfies the design

requirements. Now add the following code to the end of your m-file and rerun it. You should get

the following close-loop stairstep response.

numaz = conv(numz,numcz);

denaz = conv(denz,dencz);

[numaz_cl,denaz_cl] = cloop(numaz,denaz);

[x2] = dstep(numaz_cl,denaz_cl,101);

t=0:0.12:12;

stairs(t,x2)

xlabel('Time (seconds)')

ylabel('Velocity (rad/s)')

title('Stairstep Response:with PID controller')

Page 332: Matlab Basics

As you can see from the above plot, the closed-loop response of the system is unstable.

Therefore there must be something wrong with compensated system. So we should take a look at

root locus of the compensated system. Let's add the following Matlab command into the end of

your m-file and rerun it.

rlocus(numaz,denaz)

title('Root Locus of Compensated System')

Page 333: Matlab Basics

From this root-locus plot, we see that the denominator of the PID controller has a pole at -1 in

the z-plane. We know that if a pole of a system is outside the unit circle, the system will be

unstable. This compensated system will always be unstable for any positive gain because there

are an even number of poles and zeroes to the right of the pole at -1. Therefore that pole will

always move to the left and outside the unit circle. The pole at -1 comes from the compensator,

and we can change its location by changing the compensator design. We choose it to cancel the

zero at -0.62. This will make the system stable for at least some gains. Furthermore we can

choose an appropriate gain from the root locus plot to satisfy the design requirements using

rlocfind.Enter the following Matlab code to your m-file.

dencz = conv([1 -1],[1.6 1])

numaz = conv(numz,numcz);

denaz = conv(denz,dencz);

rlocus(numaz,denaz)

title('Root Locus of Compensated System');

[K,poles] = rlocfind(numaz,denaz)

[numaz_cl,denaz_cl] = cloop(K*numaz,denaz);

[x3] = dstep(numaz_cl,denaz_cl,101);

t=0:0.12:12;

stairs(t,x3)

xlabel('Time (seconds)')

ylabel('Velocity (rad/s)')

title('Stairstep Response:with PID controller')

The new dencz will have a pole at -0.625 instead of -1, which almost cancels the zero of

uncompensated system. In the Matlab window, you should see the command asking you to select

the point on the root-locus plot. You should click on the plot as the following:

Page 334: Matlab Basics

Then Matlab will return the appropriate gain and the corresponding compensated poles, and it

will plot the closed-loop compensated response as follows.

Page 335: Matlab Basics

The plot shows that the settling time is less than 2 seconds and the percent overshoot is around

3%. In addition, the steady state error is zero. Also, the gain, K, from root locus is 0.2425 which

is reasonable. Therefore this response satisfies all of the design requirements.

Example: Root Locus Design for Digital DC Motor Position Control

In this digital DC motor control version of a DC motor, the controller will be designed by a Root

Locus method. A digital DC motor model can obtain from conversion of analog DC motor

model, as we will describe. According to the Modeling a DC Motor, the open-loop transfer

function for DC motor's position was derived by Laplace Transform as shown.

where:

*electric resistance (R) = 4 ohm

*electric inductance (L) = 2.75E-6 H

*electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp

*moment of inertia of the rotor (J) = 3.2284E-6 kg*m^2/s^2

*damping ratio of the mechanical system (b) = 3.5077E-6 Nms

*input (V): Source Voltage

*output (sigma dot): Rotating speed

*The rotor and shaft are assumed to be rigid

The design requirements are:

Settling time: Less than 0.04 seconds

Overshoot: Less than 16%

Steady-state error: 0 with a step disturbance input

Continuous to Discrete Conversion

The first step in the design of a discrete-time system is to convert a continuous transfer function

to a discrete transfer function. Matlab can be used to convert the above transfer function

to discrete transfer function by using the c2dm command. The c2dm command

requires four arguments: the numerator polynomial (num), the denominator polynomial (den), a

Page 336: Matlab Basics

sampling time (T) and a type of hold circuit. In this example we will use the zero-order hold

(zoh). Refer to the Digital Control Tutorials page for more information.

From the design requirement, let the sampling time, T equal to 0.001 seconds, which is 1/100 of

the required time constant or 1/40 of the required settling time. Let's create a new m-file and add

the following Matlab code:

R=4;

L=2.75E-6;

K=0.0274;

J=3.2284E-6;

b=3.5077E-6;

num = K;

den = [(J*L) (J*R)+(L*b) (R*b)+(K^2) 0];

T = 0.001;

[numd,dend] = c2dm(num,den,T,'zoh')

Matlab should return the following:

num =

0 0.0010 0.0010 0.0000

den =

1.0000 -1.9425 0.9425 0.0000

As noticed in above results, both numerator and denominator of discrete transfer function have

one extra root at z = 0. Also, we have to get rid of the leading zero coefficient in the numerator.

To do this add the following code to cancel out these extra pole and zero to avoid numerical

problem in Matlab. Otherwise it will consider both numerator and denominator to be fourth-

order polynomial.

Page 337: Matlab Basics

numd = numd(2:3);

dend = dend(1:3);

Therefore, the discrete-time transfer function from the motor position output to the voltage input

is:

We would like to see what the closed-loop response of the system looks like when no controller

is added. First, we have to close the loop of the transfer function by using the cloop command.

After closing the loop, let's see how the closed-loop stairstep response performs by using the

dstep and the stairs commands. The dstep command will provide the vector of discrete step

signals and stairs command will connect these discrete signals (click here for more information).

Add the following Matlab code at the end of previous m-file and rerun it.

[numd_cl,dend_cl] = cloop(numd,dend);

[x1] = dstep(numd_cl,dend_cl,501);

t=0:0.001:0.5;

stairs(t,x1)

xlabel('Time (seconds)')

ylabel('Position (rad)')

title('Stairstep Response:Original')

You should see the following plot:

Page 338: Matlab Basics

Root Locus Design

The main idea of root locus design is to obtain the closed-loop response from the open-loop root

locus plot. By adding zeroes and poles to the original system, the root locus will be modified that

leads to a new closed-loop response. First let's see the root-locus for the system itself imposed

with an unit circle. In your m-file, add the following commands and rerun it. You should get the

root-locus plot as shown below.

rlocus(numd,dend)

title('Root Locus of Original System')

zgrid(0,0)

Page 339: Matlab Basics

To get the zero steady-state error from the closed-loop response, we have to add an integral

control. Recall that the integral control in the continuous- time is 1/s. If we use the backward

difference approximation for mapping from the s-plane to the z-plane as described by s = 1/(z-1),

one pole will be added at 1 on the root locus plot. After adding extra pole at 1, root locus will

have three poles near 1, therefore the root locus will move out in the right half. The closed-loop

response will be more unstable. Then we must add one zero near 1 and inside the unit circle to

cancel out with one pole to pull the root locus in. We will add a pole at z = 0.95. In general, we

must at least add as many poles as zeroes for the controller to be causal. Now add the following

Matlab commands in to your m-file.

numi = [1 -0.95];

deni = [1 -1];

numad = conv(numd,numi);

denad = conv(dend,deni);

Recall from the Digital Control Tutorial page, the zgrid command should be used to find the

desired region, which satisfies the design requirement, on the discrete root locus plot. The zgrid

command requires two arguments: the natural frequency (Wn) and the damping ratio (zeta).

From the design requirement, the settling time is less than 0.04 seconds and the percent

overshoot is less than 16%. We know the formulas for finding the damping ratio and natural

frequency as shown:

where:

OS: the percent overshoot

Ts: the settling time

Page 340: Matlab Basics

The required damping ratio is 0.5 and the natural frequency is 200 rad/sec, but the zgrid

command requires a non-dimensional natural frequency. Therefore Wn = 200*T = 0.2

rad/sample. Add the following Matlab code into the end of your m-file and rerun it.

rlocus(numad,denad);

zgrid(0.5,0.2)

title('Root Locus of system with integral control')

You should get the following plot:

From the above root locus plot, we can see that system is unstable at all gains because the root

locus is outside the unit circle. Moreover, the root locus should be in the region where the

damping ratio line and natural frequency cross each other to satisfy the design requirements.

Thus we have to pull the root locus in more by first canceling the zero at approximately -0.98,

since this zero will add overshoot to the step response. Then we have to add one more pole and

two zeroes near the desired poles. After going through some trial and error to yield the root locus

in the desired region, one more pole is added at 0.61 and two zeroes are added at 0.76. Add the

following command in your m-file and rerun it in Matlab window.

numc = conv([1 -0.76],[1 -0.76]);

Page 341: Matlab Basics

denc = conv([1 0.98],[1 -0.61]);

numoc = conv(numad,numc);

denoc = conv(denad,denc);

rlocus(numoc,denoc);

zgrid(0.5,0.2)

title('Root Locus of Compensated System')

The denoc will have a pole at 0.61 instead of -0.98. You should get the following root locus plot:

From the above root locus plot, the root locus is drawn in the desired region. Let's find a gain, K,

on the root locus plot by using the rlocfind command and obtain the stairstep response with the

selected gain. Enter the following commands at the end of your m-file and rerun it.

K = rlocfind(numoc,denoc)

[numd_cl,dend_cl] = cloop(K*numoc,denoc);

[x2] = dstep(numd_cl,dend_cl,251);

t=0:0.001:0.25;

stairs(t,x2)

xlabel('Time (seconds)')

Page 342: Matlab Basics

ylabel('Position (rad)')

title('Stairstep Response of Compensated System')

In the Matlab window, you should see the command asking you to select the point on the root-

locus plot. You should click on the plot as the following:

The selected gain should be around 330, and it will plot the closed-loop compensated response as

follows.

Page 343: Matlab Basics

From the above closed-loop response, the settling time is about 0.05 seconds which is satisfy the

requirement, but the percent overshoot is 22% which is too large due to the zeroes. If we select

the gain to be larger, the requirements will be satisfied. On the other hand, the problem will be

unrealistic and a huge actuator is needed, you can try this yourself by picking a larger gain on the

previous root locus plot which will yield an unrealistically sudden step response. So we have to

move both one pole and two zeroes a little further to the right to pull root locus in a little bit

more, new pole will be put at 0.7 and two zeroes should be at 0.85. Go back to your m-file and

change only numc and denc as shown below and rerun it in Matlab window.

numc = conv([1 -0.85],[1 -0.85]);

denc = conv([1 0.98],[1 -0.7]);

Then you should see the following root locus plot.

On the new root locus, you should click on the plot to select a new gain as the following:

Page 344: Matlab Basics

The selected gain should be around 450, and then it will plot the closed-loop compensated

response as follows:

Page 345: Matlab Basics

Now we see that the settling time and percent overshoot meet the design require ments of the

system. The settling time is 0.04 seconds and the percent overshoot is about 10%.

Then let's take a look at a disturbance response of the closed-loop system by canceling the

selected gain, the integral transfer function and controller's transfer function from the closed-loop

transfer function. So add the following code into your m-file and rerun it.

numcld = conv(numd_cl,conv(denc,deni));

dencld = conv(dend_cl,conv(K*numc,numi));

[x4] = dstep(numcld,dencld,251);

t=0:0.001:.25;

stairs(t,x4)

xlabel('Time (seconds)')

ylabel('Position (rad)')

title('Stairstep Response of Compensated System')

Matlab should return the following plot:

Page 346: Matlab Basics

We can see that a response to the disturbance is small (3.3% of the disturbance ) and settles

within 2% of the disturbance after 0.04 seconds and eventually reaches zero.

Example: State Space Design for Digital Bus Suspension Control

In this example, we will design a digital state space controller for the bus suspension control

example. First we will convert the continuous time model to a discrete time model, and then use

the pole placement method to design the controller. From the bus suspension state space

modeling page, the state space model of the system is:

Where:

* body mass (m1) = 2500 kg,

* suspension mass (m2) = 320 kg,

* spring constant of suspension system(k1) = 80,000 N/m,

* spring constant of wheel and tire(k2) = 500,000 N/m,

* damping constant of suspension system(b1) = 350 Ns/m.

* damping constant of wheel and tire(b2) = 15,020 Ns/m.

* control force (u) = force from the controller we are going to design.

The design requirements are:

Overshoot: Output (X1-X2) less than 5% of disturbance (W)

Settling time: Less than 5 seconds

Page 347: Matlab Basics

Sampling Time Selection

The first step in the design of a discrete-time controller is to convert the continuous plant to its

discrete time equivalent. First, we need to pick an appropriate sampling time, T. In this example,

selection of sampling time is very important since a step in the road surface very quickly affects

the output. Physically, what happens is the road surface suddenly lifts the wheel, compressing

the spring, K2, and the damper, b2. Since the suspension mass is relatively low, and the spring

fairly stiff, the suspension mass rises quickly, increasing X2 almost immediately. Since the

controller can only see the effect of the disturbance after a complete sampling period, we have to

pick a sampling time, T, short enough so that the output (X1-X2) does not exceed the 5%

requirement in one sampling period. To pick the sampling period, we need to closely examine

the beginning of the step response. If you remember from the modeling page, the output quickly

goes negative in response to a step disturbance, and then begins to oscillate. We will simulate

just the beginning of this response by setting the time vector input to the step function to range

from 0 to .005. The response to a .1m step input is simulated by multiplying the B matrix by .1.

Create a new m-file and enter the following code:

m1=2500;

m2=320;

k1 = 80000;

k2 = 500000;

b1 = 350;

b2 = 15020;

A=[0 1 0

0

-(b1*b2)/(m1*m2) 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1)

-(b1/m1)

b2/m2 0 -((b1/m1)+(b1/m2)+(b2/m2))

1

k2/m2 0 -((k1/m1)+(k1/m2)+(k2/m2))

0];

B=[0 0

1/m1 (b1*b2)/(m1*m2)

0 -(b2/m2)

Page 348: Matlab Basics

(1/m1)+(1/m2) -(k2/m2)];

C=[0 0 1 0];

D=[0 0];

step(A,.1*B,C,D,2,0:0.0001:.005);

This plot shows that the spring, K1 compresses very quickly, and exceeds our requirement of

5mm in response to a .1m step after only a little more than 0.001s. Therefore, we will set

T=.0005s in order to give the controller a chance to respond.

Continuous to Discrete Conversion

Now that we have selected a sampling time, we can convert the plant to discrete time. Matlab

can be used to convert the above state space model, A,B,C, and D, to a discrete state space

model, Ad,Bd,Cd, and Dd, by using c2dm command. The c2dm command can take six

arguments: the four state matrices, the sampling time, T, and the type of hold circuit. In this

example we will use zero-order hold ('zoh'). Refer to the Digital Control Tutorials page for more

information.

Add the following code to your m-file:

Page 349: Matlab Basics

T=.0005;

[Ad Bd Cd Dd]=c2dm(A,B,C,D,T,'zoh')

Matlab should return the following:

Ad =

1.0000 0.0005 0.0000 0.0000

-0.0035 1.0000 -0.0124 -0.0001

0.0234 0.0000 0.9760 0.0005

0.7705 0.0002 -0.9112 0.9998

Bd =

0.0000 0.0000

0.0000 0.0035

0.0000 -0.0234

0.0000 -0.7705

Cd =

0 0 1 0

Dd =

0 0

which represent the new discrete-time state space model.

Adding an Integrator

In this example, we will need to add an integrator to the system in order to drive the steady-state

response to zero. We will add this integrator in series with the plant. This will have the effect of

Page 350: Matlab Basics

adding another state to the plant. We will add the integrator by representing it in state space and

the using the series command. This command takes the A,B,C, and D matrices of the two

systems to be connected in series as arguments and returns a new set of A,B,C, and D matrices.

An integrator in discrete time state space can be represented as a trapezoidal approximation of

integration over each sample period as follows:

To add this, add the following commands in your m-file:

Ai=1;

Bi=1;

Ci=T;

Di=T/2;

[Ada,Bda,Cda,Dda]=series(Ad,Bd,Cd,Dd,Ai,Bi,Ci,Di)

Matlab will return a new set of integrator-augmented state matrices, with dimension 5 rather than

dimension 4. Unfortunately, the output of these equations is now the new integrated state. We

must change the output Cda matrix to output the original output state. Add the following line:

Cda=[Cd 0]

Since the augmented state is the last state, this outputs the same state as the unaugmented

equations.

Designing the Controller

The structure of the controller is similar to the structure of the continuous-time state space

controller. We will now use the place command to compute the gain matrix, K, which will, in

feedback, give us sny desired closed-loop poles.

We first need to decide where to place the closed-loop poles. Since we get to place all five of the

closed-loop poles, we can be very selective about where to place them. In particular, we can

Page 351: Matlab Basics

place them to cancel all of the plant zeros, as well as give us the desired response. First, we will

find the plant zeros by converting the plant's digital state equations to a transfer function, and

then finding the roots of the numerator. We will use the ss2tf command which takes the state

matrices and the selected input as arguments and outputs a transfer function numerator and

denominator.

Add the following code to your m-file:

[num,den]=ss2tf(Ad,Bd,Cd,Dd,1);

zeros=roots(num)

Matlab will return the following:

zeros =

0.9986 + 0.0065i

0.9986 - 0.0065i

-0.9929

We will select these three zeros as three of our desired closed-loop poles. One of the other two

will be selected at .9992 since a pole there settles in approximately 10000 samples (or 5

seconds). The last pole will be selected at z=.2 since this is sufficiently fast to be insignificant.

Add the following code to your m-file:

p1=.97+.13i;

p2=.97-.13i;

p3=-.87;

p1=zeros(1);

p2=zeros(2);

p3=zeros(3);

p4=.9992;

p5=.5;

K=place(Ada,Bda*[1;0],[p1 p2 p3 p4 p5])

Matlab will return the following:

Page 352: Matlab Basics

place: ndigits= 19

K =

1.0e+09 *

0.0548 0.0000 1.0897 0.0011 0.0009

Simulating the Closed-Loop Response

We can use the dstep command to simulate the closed-loop response. Since multiplying the state

vector by K in our controller only returns a single signal, u, we need to add a row of zeros to K

by multiplying it by [1 0]T. This is identical to what was done in the continuous design to

compensate for the fact that there are two inputs to the plant, but only one is a control input. We

will simulate with a negative .1m step disturbance in the road to give us a positive deflection of

the bus for aesthetic reasons. Enter the following code into your m-file:

yout=dstep(Ada-Bda*[1 0]'*K,-.1*Bda,Cda,-.1*Dda,2,10001);

t=0:.0005:5;

stairs(t,yout);

You should see the following plot.

Page 353: Matlab Basics

We can see in this plot, that the overshoot is less than 5mm, and the response settles well within

5 seconds.

Digital Control Example: Inverted Pendulum using State-Space method

In this digital control version of the inverted pendulum problem, we are going to use the state-

space method to design the digital controller. If you refer to the Inverted Pendulum Modeling

page, the state-space equations were derived as

\

where

M mass of the cart 0.5 kg

m mass of the pendulum 0.5 kg

b friction of the cart 0.1 N/m/sec

l length to pendulum center of mass 0.3 m

I inertia of the pendulum 0.006 kg*m^2

u step force applied to the cart

x cart position coordinate

phi pendulum angle from vertical

Output are the cart displacement (x in meters) and the pendulum deflection angle (phi in

radians).

Page 354: Matlab Basics

The design requirements are

Settling time for x and phi less than 5 seconds

Rise time for x of less than 1 second

Overshoot of phi less than 0.35 rad (20 deg)

Steady-state error of x and phi less than 2%

Discrete state-space

The first thing to do here is to convert the above continuous state-space equations to discrete

state-space. To do this, we are going to use the Matlab function called c2dm. To use this c2dm,

we need to specify six arguments: four state-space matrices (A, B, C, and D), sampling time (Ts

in sec/sample), and the 'method'. You should already be familiar with how to enter A, B, C, and

D matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is the closed-

loop bandwidth frequency. The method we will use is the zero-order hold ('zoh').

Assuming that the closed-loop bandwidth frequencies are around 1 rad/sec for both the cart and

the pendulum, let the sampling time be 1/100 sec/sample. Now we are ready to use c2dm. Enter

the following commands to an m-file.

M = .5;

m = 0.2;

b = 0.1;

i = 0.006;

g = 9.8;

l = 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies

A = [0 1 0 0;

0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;

0 0 0 1;

0 -(m*l*b)/p m*g*l*(M+m)/p 0];

B = [ 0;

(i+m*l^2)/p;

Page 355: Matlab Basics

0;

m*l/p];

C = [1 0 0 0;

0 0 1 0];

D = [0;

0];

Ts=1/100;

[F,G,H,J]=c2dm (A,B,C,D,Ts,'zoh')

Running this m-file in the Matlab command window gives you the following four matrices.

F =

1.0000 0.0100 0.0001 0.0000

0 0.9982 0.0267 0.0001

0 0.0000 1.0016 0.0100

0 -0.0045 0.3119 1.0016

G =

0.0001

0.0182

0.0002

0.0454

H =

1 0 0 0

0 0 1 0

J =

0

0

Page 356: Matlab Basics

Now we have obtained the discrete state-space model of the form

Controllability and Observability

The next step is to check the controllability and the observability of the system. For the system to

be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns).

In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n.

Page 357: Matlab Basics

Since our controllability matrix and observability matrix are '4x4', the rank of both matrices must

be 4. The function rank can give you the rank of each matrix. In an new m-file, enter the

following commands and run it in the command window.

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

G = [0.0001;

0.0182;

0.0002;

0.0454];

H = [1 0 0 0;

0 0 1 0];

J = [0;

0];

co = ctrb (F,G);

ob = obsv (F,H);

Controllability = rank (co)

Observability = rank (ob)

In the command window, you should see

Controllability =

4

Observability =

4

Page 358: Matlab Basics

This proves that our discrete system is both completely state controllable and completely state

observable.

Control design via pole placement

The schematic of a full-state feedback system is shown below.

The next step is to assume that all four state are measurable, and find the control matrix (K). If

you refer to the continuous Inverted Pendulum: State-Space page, the Linear Quadratic

Regulator (LQR) method was used to find the control matrix (K). In this digital version, we will

use the same LQR method. This method allows you to find the optimal control matrix that results

in some balance between system errors and control effort. Please consult your control textbook

for details. To use this LQR method, we need to find three parameters: Performance index matrix

(R), state-cost matrix (Q), and weighting factors. For simplicity, we will choose the performance

index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to H' x H. The weighting

factors will be chosen by trial and errors. The state-cost matrix (Q) has the following structure

Q =

1 0 0 0

0 0 0 0

0 0 1 0

0 0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the

3,3 position will be used to weight the pendulum's angle. The weighting factors for the cart's

position and the pendulum's angle will be chosen individually.

Now we are ready to find the control matrix (K) and see the response of the system. Enter the

following commands to an new m-file and run it in the Matlab command window. You should

see the following step response.

Page 359: Matlab Basics

T=0:0.01:5;

U=0.2*ones(size(T));

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

G = [0.0001;

0.0182;

0.0002;

0.0454];

H = [1 0 0 0;

0 0 1 0];

J = [0;

0];

x=1; %weighting factor for the cart position

y=1; %weighting factor for the pendulum angle

Q=[x 0 0 0;

0 0 0 0;

0 0 y 0;

0 0 0 0];

R = 1;

K = dlqr(F,G,Q,R)

[Y,X]=dlsim(F-G*K,G,H,J,U);

stairs(T,Y)

legend('Cart (x)','Pendulum (phi)')

Page 360: Matlab Basics

Note: The function dlsim is the discrete version of the lsim and has very similar characteristics

as dstep.

The curve in green represents the pendulum's angle, in radians, and the curve in blue represents

the cart's position in meters. The pendulum's and cart's overshoot appear fine, but their settling

times need improvement and the cart's rise time needs to be decreased. Also the cart has, in fact,

moved in the opposite direction. For now, we will concentrate on improving the settling times

and the rise times, and fix the steady-state error later.

Let's increase the weighting factors (x and y) and see if both the settling and rise times decrease.

Go back to your m-file and change the x and y to x=5000 and y=100. Running this m-file in the

command window gives you the following new step response.

Page 361: Matlab Basics

From this plot, we see that all design requirements are satisfied except the steady-state error of

the cart position (x). We can easily correct this by introducing a feedforwarding scaling factor

(Nbar).

Reference input

Unlike other design methods, the full-state feedback system does not compare the output to the

reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference

(see the schematic shown above). Thus, we should not expect to see the output equals to the

input. To obtain the desired output, we need to scale the reference input so that the output equals

to the reference. This can be easily done by introducing a feedforwarding scaling factor called

Nbar. The basic schematic with the Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can

find it from trial and errors. After several trials, the Nbar equals to -61.55 provided the

satisfactory response. Try the following m-file and obtain the step response shown below.

T=0:0.01:5;

U=0.2*ones(size(T));

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

G = [0.0001;

0.0182;

0.0002;

Page 362: Matlab Basics

0.0454];

H = [1 0 0 0;

0 0 1 0];

J = [0;

0];

x=5000; %weighting factor for the cart position

y=100; %weighting factor for the pendulum angle

Q=[x 0 0 0;

0 0 0 0;

0 0 y 0;

0 0 0 0];

R = 1;

K = dlqr(F,G,Q,R)

Nbar=-61.55;

[Y,X]=dlsim(F-G*K,G*Nbar,H,J,U);

stairs(T,Y)

legend('Cart (x)','Pendulum (phi)')

Page 363: Matlab Basics

Notice that the steady-state error of the cart's position have been eliminated. Now we have

designed the system that satisfies all design requirements.

Observer design

The above response satisfies all design requirements; however, it was found assuming all states

are measurable. This assumption may not be valid for all systems. In this section, we develop a

technique for estimating the states of a plant from the information that is available concerning

the plant. The system that estimates the states of another system is called an observer. Thus, in

this section we will design a full-order state observer to estimate those states that are not

measurable. For further explanation on how an observer works, please consult your control

textbooks.

A basic schematic of the plant-observer system is shown below.

Page 364: Matlab Basics

To design the observer, first, we need to find the L matrix. To find the L matrix, we need to find

the poles of the system without the observer (the poles of F-G*K). Copy the following

commands to an new m-file and run it in the Matlab command window.

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

G = [0.0001;

0.0182;

0.0002;

0.0454];

H = [1 0 0 0;

0 0 1 0];

J = [0;

0];

x=5000; %weighting factor for the cart position

y=100; %weighting factor for the pendulum angle

Q=[x 0 0 0;

0 0 0 0;

0 0 y 0;

0 0 0 0];

R = 1;

K = dlqr(F,G,Q,R);

poles = eig (F-G*K)

In the command window, you should see

Page 365: Matlab Basics

poles =

0.9156+0.0729i

0.9156-0.0729i

0.9535+0.0079i

0.9535-0.0079i

We want to place observer poles so that the observer works a lot faster than the system without

the observer. Let's place the observer poles far left of above poles, say, at [-0.3 -0.31 -0.32 -

0.33]. These poles can be changed later, if necessary. We will use the Matlab function place to

find the L matrix. Enter the following commands to an new m-file and run it.

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

H = [1 0 0 0;

0 0 1 0];

P = [-0.3 -0.31 -0.32 -0.33];

L = place (F',H',P)'

You should see the following L matrix in the command window.

L =

2.6310 -0.0105

172.8146 -1.3468

-0.0129 2.6304

-2.2954 173.2787

Now we will obtain the overall system response including the observer. Once again, create an

new m-file and copy the following code.

Page 366: Matlab Basics

T=0:0.01:5;

U=0.2*ones(size(T));

F = [1.0000 0.0100 0.0001 0.0000;

0 0.9982 0.0267 0.0001;

0 0.0000 1.0016 0.0100;

0 -0.0045 0.3119 1.0016];

G = [0.0001;

0.0182;

0.0002;

0.0454];

H = [1 0 0 0;

0 0 1 0];

J = [0;

0];

x=5000; %weighting factor for the cart position

y=100; %weighting factor for the pendulum angle

Q=[x 0 0 0;

0 0 0 0;

0 0 y 0;

0 0 0 0];

R = 1;

K = dlqr(F,G,Q,R)

Nbar = -61.55;

L = [2.6310 -0.0105;

172.8146 -1.3468;

-0.0129 2.6304;

-2.2954 173.2787;

Page 367: Matlab Basics

Fce = [F-G*K G*K;

zeros(size(F)) (F-L*H)];

Gce = [G*Nbar;

zeros(size(G))];

Hce = [H zeros(size(H))];

Jce = [0;0];

[Y,X] = dlsim (Fce,Gce,Hce,Jce,U);

stairs (T,Y)

legend ('cart (x)','pendulum (phi)')

After running this m-file, you should get the following step response.

As you noticed, this response is about the same as before, and all of the design requirements

have been satisfied.

Page 368: Matlab Basics

Digital Control Example: Designing Pitch Controller using State-Space method

In this digital control version of the pitch controller problem, we are going to use the state-space

method to design the digital controller. If you refer to the Pitch Controller: Modeling page, the

state-space model was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the

pitch angle (theta).

The design requirements are

Overshoot: Less than 10%

Rise time: Less than 2 seconds

Settling time: Less than 10 seconds

Steady-state error: Less than 2%

Discrete state-space

The first thing to do here is to convert above continuous state-space model to discrete state-

space. To do this, we are going to use the Matlab function called c2dm. To use this c2dm, we

need to specify six arguments: Four state-space matrices (A, B, C, and D), sampling time (Ts),

and the 'method'. You should already be familiar with how to enter A, B, C, and D matrices. The

sampling time should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth

frequency. The method we will use is the zero-order hold.

From the closed-loop Bode plot, the bandwidth frequency was determined to be approximately 2

rad/sec (see this yourself) . Thus, to be sure we have small enough sampling time, we are going

Page 369: Matlab Basics

to use the sampling time of 1/100 sec/sample. Now we are ready to use the function c2dm. Enter

the following commands to an m-file.

A = [ -0.313 56.7 0;

-0.0139 -0.426 0;

0 56.7 0];

B = [ 0.232;

0.0203;

0];

C=[0 0 1];

D=[0];

Ts=1/100;

[F,G,H,J] = c2dm (A,B,C,D,Ts,'zoh')

Running this m-file in the Matlab command window gives you the following four matrices.

F =

0.9968 0.05649 0

-0.0001 0.9957 0

0 0.5658 1

G =

0.0024

0.0002

0.0001

H =

Page 370: Matlab Basics

0 0 1

J =

0

Now we have obtained the discrete state-space model of the form

Controllability and observability

The next step is to check the controllability and the observability of the system. For the system to

be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or

columns). In the same token, for the system to be completely state observable, the observability

matrix

Page 371: Matlab Basics

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3,

the rank of both matrices must be 3. The Matlab function rank can give you the rank of each

matrices. In an new m-file, enter the following commands and run it.

F = [0.9968 0.05649 0

-0.0001 0.9957 0

0 0.5658 1];

G = [0.0024;

0.0002;

0.0001];

H = [0 0 1];

J = [0];

co = ctrb (F,G);

ob = obsv (F,H);

Controllability = rank (co)

Observability = rank (ob)

In the command window, you should see

Controllability =

3

Observability =

3

This proves that our discrete system is both completely state controllable and completely state

observable.

Page 372: Matlab Basics

Control design via pole placement

The schematic of a full-state feedback system is shown below.

where

K=Control matrix

x=State matrix (alpha, q, theta)

de=-Kx=input

R=Reference

In the continuous Pitch Controller: State-Space page, the Linear Quadratic Regulator (LQR)

method was used to find the control matrix (K). In this digital version, we will use the same LQR

method. This method allows you to find the optimal control matrix that results in some balance

between system errors and control effort. Please consult your control textbook for details. To use

this LQR method, we need to find three parameters: Performance index matrix (R), state-cost

matrix (Q), and weighting factor (p). For simplicity, we will choose the performance index

matrix equals 1 (R=1), and the state-cost matrix (Q) equals to H' x H. The weighting factor (p)

will be chosen by trial and errors. The state-cost matrix (Q) has the following structure

Q =

0 0 0

0 0 0

0 0 1

Now we are ready to find the control matrix (K) and see the response of the system. First, let the

weighting factor (p) equals 50. Enter the following commands to a new m-file and run it in the

Matlab command window.

t=0:0.01:10;

de=0.2*ones(size(t));

Page 373: Matlab Basics

F = [0.9968 0.05649 0

-0.0001 0.9957 0

0 0.5658 1];

G = [0.0024;

0.0002;

0.0001];

H = [0 0 1];

J = [0];

p=50;

Q = [0 0 0

0 0 0

0 0 p];

[K] = dlqr (F,G,Q,1)

[x] = dlsim (F-G*K,G,H,J,de);

stairs (t,x)

After you run this m-file, you should see the control matrix (K) in the command window and the

step response similar to the one shown below.

Page 374: Matlab Basics

The rise time, the overshoot, and the settling time look satisfactory. However, there is a large

steady-state error. This can be easily corrected by introducing the feedforwarding scaling factor

(Nbar).

Reference input

Unlike other design methods, the full-state feedback system does not compare the output to the

reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference

(see the schematic shown above). Thus, we should not expect to see the output equals to the

input. To obtain the desired output, we need to scale the reference input so that the output equals

to the reference. This can be easily done by introducing a feedforwarding scaling factor called

Nbar. The basic schematic with the Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can

find it from trial and errors. After several trials, the Nbar equals to 6.95 provided the satisfactory

response. Try the following m-file and obtain the stairstep response shown below.

t=0:0.01:10;

de=0.2*ones(size(t));

F = [0.9968 0.05649 0

-0.0001 0.9957 0

0 0.5658 1];

G = [0.0024;

0.0002;

0.0001];

Page 375: Matlab Basics

H = [0 0 1];

J = [0];

p=50;

Q = [0 0 0

0 0 0

0 0 p];

[K,S,E] = dlqr (F,G,Q,1)

Nbar = 6.95;

[x] = dlsim (F-G*K,G*Nbar,H,J,de);

stairs (t,x)

From this plot, we see that the Nbar eliminated the steady-state error. Now all design

requirements are satisfied.

Note: Assuming all states are measurable, an observer design will not be explained in this page.

Page 376: Matlab Basics

Digital Control Example: Ball and Beam problem using PID Control

In this digital control version of the ball and beam experiment, we are going to use the PID

control method to design the digital controller. If you refer to the Ball and Beam Modeling page,

the open-loop transfer function was derived as

m mass of the ball 0.11 kg

g gravitational acceleration 9.8 m/s^2

d lever arm offset 0.03 m

L length of the beam 1.0 m

R radius of the ball 0.015 m

J ball's moment of inertia 9.99e-6 kgm^2

R(s) ball position coordinate (m)

theta(s) servo gear angle 0.25 rad

The design criteria for this problem are:

Settling time less than 3 seconds

Overshoot less than 5%

Digital PID controller

If you refer to any of the PID control problem for continuous systems, the PID transfer function

was expressed as

Page 377: Matlab Basics

As you noticed the above transfer function was written in terms of s. For the digital PID control,

we use the following transfer function in terms of z.

Discrete transfer function

The first thing to do here is to convert the above continuous system transfer function to discrete

transfer function. To do this, we are going to use the Matlab function called c2dm. To use this

c2dm, we need to specify four arguments: numerator and denominator matrices, sampling time

(Ts), and the 'method'. You should already be familiar with how to enter numerator and

denominator matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is

the closed-loop bandwidth frequency. The method we will use is the zero-order hold ('zoh').

Assuming that the closed-loop bandwidth frequency is around 1 rad/sec, let the sampling time be

1/50 sec/sample. Now we are ready to use c2dm. Enter the following commands to an m-file.

m = 0.111;

R = 0.015;

g = -9.8;

L = 1.0;

d = 0.03;

J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];

den = [1 0 0];

Ts = 1/50;

[numDz,denDz]= c2dm (num,den,Ts,'zoh')

Running this m-file in the Matlab command window gives you the following matrices.

numDz =

1.0e-0.4 *

Page 378: Matlab Basics

0 0.4200 0.4200

denDz =

1 -2 1

From these matrices, the discrete transfer function can be written as

Open-loop response

Now we will observe the ball's response to a step input of 0.25 m. To do this, enter the following

commands to an new m-file and run it in the command window. You should see the following

response.

numDz = 0.0001*[0.42 0.42];

denDz = [1 -2 1];

[x] = dstep (0.25*numDz,denDz,251);

t=0:0.02:5;

stairs(t,x)

Page 379: Matlab Basics

From this plot, it is clear that the open-loop system is unstable causing the ball to roll off from

the end of the beam.

Proportional control

Now we will add the proportional control (Kp) to the system and obtain the closed-loop system

response. For now let Kp equal to 100 and see what happens to the response. Enter the following

commands to an new m-file and run it in the command window.

numDz = 0.0001*[0.42 0.42];

denDz = [1 -2 1];

Kp=100;

[numDzC,denDzC]=cloop (Kp*numDz,denDz);

[x] = dstep (0.25*numDzC,denDzC,251);

t=0:0.02:5;

stairs(t,x)

As you can see, the addition of proportional control does not make the system stable. You may

try to increase the proportional gain (Kp) and confirm that the system remains unstable.

Page 380: Matlab Basics

Proportional-Derivative control

Now we will add a derivative term to the controller. Keep the proportional gain (Kp) equal to

100, and let the derivative gain (Kd) equal to 10. Copy the following code to an new m-file and

run it to view the system response.

numDz = 0.0001*[0.42 0.42];

denDz = [1 -2 1];

Kp=100;

Kd=10;

numpd = [Kp+Kd -(Kp+2*Kd) Kd];

denpd = [1 1 0];

numDnew = conv(numDz,numpd);

denDnew = conv(denDz,denpd);

[numDnewC,denDnewC] = cloop(numDnew,denDnew);

[x] = dstep (0.25*numDnewC,denDnewC,251);

t=0:0.02:5;

stairs(t,x)

Page 381: Matlab Basics

Now the system is stable, but the rise time is too long. From the PID Tutorial page, we see that

the increasing the proportional gain (Kp) will decrease the rise time. Let's increase the

proportional gain (Kp) to 1000 and see what happens. Change Kp in the above m-file from 100

to 1000 and rerun it in the command window. You should see the following step response.

As you can see, all of the design requirements are satisfied. For this particular problem, no

implementation of an integral control was needed. But remember there is more than one solution

for a control problem. For practice, you may try different P, I and D combinations to obtain a

satisfactory response.