Matlab Tutorial Ctm

434
A completely revised version of these tutorials has recently been published as a CD-ROM by Addison-Wesley (now Prentice Hall). The new version has been updated for Matlab 5 and expanded to include Simulink tutorials. Please contact Prentice Hall for more information; request ISBN 0-201-47700-9. A site license of the new version is available for users at the University of Michigan. CTM: Control Tutorials for Matlab http://www.engin.umich.edu/group/ctm/ (1 de 3) [07/04/2003 07:15:00 p.m.]

Transcript of Matlab Tutorial Ctm

Page 1: Matlab Tutorial Ctm

A completely revised version of these tutorials hasrecently been published as a CD-ROM byAddison-Wesley (now Prentice Hall). The newversion has been updated for Matlab 5 andexpanded to include Simulink tutorials. Pleasecontact Prentice Hall for more information; requestISBN 0-201-47700-9.A site license of the new version is available for users at the University of Michigan.

CTM: Control Tutorials for Matlab

http://www.engin.umich.edu/group/ctm/ (1 de 3) [07/04/2003 07:15:00 p.m.]

Page 2: Matlab Tutorial Ctm

Welcome to the Control Tutorials for Matlab. We invite you to read more about the tutorials. They aredesigned to help you learn how to use Matlab for the analysis and design of automatic control systems.They cover the basics of Matlab, the most common classical control design techniques (PID, root locus,and frequency response), as well as some modern (state-space) control design. The flow of the tutorials isgiven by the image map above: each tutorial is a white box. There are also four examples which arefollowed through the tutorials (each example page is indicated in the image map by a blue dot).Throughout the tutorials, you will find links at the bottom of each page to all of the tutorials as well aslinks to similar examples. Links are also given to come back to this page (Home), to the complete index,and to the list of Matlab commands.

CTM: Control Tutorials for Matlab

http://www.engin.umich.edu/group/ctm/ (2 de 3) [07/04/2003 07:15:00 p.m.]

Page 3: Matlab Tutorial Ctm

We envision that you will follow along with these tutorials by running Matlab in one window and thetutorials in another. You should be able to run most of the Matlab programs by copying and pastingbetween windows. You may also find the tutorials helpful as an on-line reference while doing homeworkassignments or for reviewing concepts before exams. There are anonymous feedback forms at the bottomof each tutorial and example; we are interested to hear how you use the tutorials and your suggestions forimprovements.

Assuming you have no prior experience with Matlab, the first tutorial, Matlab Basics, is recommended.

Copyright (C) 1996 by the Regents of the University of Michigan.

8/18/97 CJC

CTM: Control Tutorials for Matlab

http://www.engin.umich.edu/group/ctm/ (3 de 3) [07/04/2003 07:15:00 p.m.]

Page 4: Matlab Tutorial Ctm

Matlab Basics TutorialVectorsFunctionsPlottingPolynomialsMatricesPrintingUsing M-files in MatlabGetting help in Matlab

Key Matlab Commands used in this tutorial are: plot polyval roots conv deconv polyadd inv eig polyNote: Non-standard Matlab commands used in this tutorials are highlighted in green.

Matlab is an interactive program for numerical computation and data visualization; it is used extensivelyby control engineers for analysis and design. There are many different toolboxes available which extendthe basic functions of Matlab into different application areas; in these tutorials, we will make extensiveuse of the Control Systems Toolbox. Matlab is supported on Unix, Macintosh, and Windowsenvironments; a student version of Matlab is available for personal computers. For more information onMatlab, contact the Mathworks.

The idea behind these tutorials is that you can view them in one window whilerunning Matlab in another window. You should be able to re-do all of the plotsand calculations in the tutorials by cutting and pasting text from the tutorials intoMatlab or an m-file.

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (1 de 10) [07/04/2003 07:15:17 p.m.]

Page 5: Matlab Tutorial Ctm

VectorsLet's start off by creating something simple, like a vector. Enter each element of the vector (separated bya space) between brackets, and set it equal to a variable. For example, to create the vector a, enter into theMatlab 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 ofthe 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 iseasy. Simply add the two as shown below:

c = a + b

c = 4 6 8 10 12 14 20 18 16

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

FunctionsTo make life easier, Matlab includes many standard functions. Each function is a block of code thataccomplishes 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 alsoincorporated into Matlab.

sin(pi/4)

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (2 de 10) [07/04/2003 07:15:17 p.m.]

Page 6: Matlab Tutorial Ctm

ans =

0.7071

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

Matlab even allows you to write your own functions with the function command; follow the link tolearn how to write your own functions and see a listing of the functions we created for this tutorial.

PlottingIt 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 thevalues) and then compute the sin value at each time.

t=0:0.25:7;y = sin(t);plot(t,y)

The plot contains approximately one period of a sine wave. Basic plotting is very easy in Matlab, and theplot command has extensive add-on capabilities. I would recommend you visit the plotting page tolearn more about it.

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (3 de 10) [07/04/2003 07:15:17 p.m.]

Page 7: Matlab Tutorial Ctm

PolynomialsIn Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply enter eachcoefficient of the polynomial into the vector in descending order. For instance, let's say you have thefollowing 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 ismissing any coefficients, you must enter zeros in the appropriate place in the vector. For example,

would be represented in Matlab as:

y = [1 0 0 0 1]

You can find the value of a polynomial using the polyval function. For example, to find the value ofthe 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 polynomialsuch 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

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (4 de 10) [07/04/2003 07:15:17 p.m.]

Page 8: Matlab Tutorial Ctm

Let's say you want to multiply two polynomials together. The product of two polynomials is found bytaking 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)

z = 1 6 16 16

Dividing two polynomials is just as easy. The deconv function will return the remainder as well as theresult. 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, theremainder 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 (thevectors x and y must have the same length). In the general case, the user-defined function, polyadd canbe used. To use polyadd, copy the function into an m-file, and then use it just as you would any otherfunction in the Matlab toolbox. Assuming you had the polyadd function stored as a m-file, and youwanted to add the two uneven polynomials, x and y, you could accomplish this by entering thecommand:

z = polyadd(x,y)

x = 1 2

y = 1 4 8

z = 1 5 10

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (5 de 10) [07/04/2003 07:15:17 p.m.]

Page 9: Matlab Tutorial Ctm

MatricesEntering matrices into Matlab is the same as entering a vector, except each row of elements is separatedby 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 matrixusing 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 complexconjugate transpose. To get the transpose, use .' (the two commands are the same if the matix is notcomplex).

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

D = B * C

D = 30 70 110 70 174 278 110 278 446

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (6 de 10) [07/04/2003 07:15:17 p.m.]

Page 10: Matlab Tutorial Ctm

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 twomatrices 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 12 20

If you have a square matrix, like E, you can also multiply it by itself as many times as you like by raisingit 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)

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (7 de 10) [07/04/2003 07:15:17 p.m.]

Page 11: Matlab Tutorial Ctm

X = -2.0000 1.0000 1.5000 -0.5000

or its eigenvalues:

eig(E)

ans = -0.3723 5.3723

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

PrintingPrinting in Matlab is pretty easy. Just follow the steps illustrated below:

Macintosh

To print a plot or a m-file from a Macintosh, just click on the plot or m-file, select Print under theFile menu, and hit return.

Windows

To print a plot or a m-file from a computer running Windows, just selct Print from the File menuin the window of the plot or m-file, and hit return.

Unix

To print a plot on a Unix workstation enter the command:

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (8 de 10) [07/04/2003 07:15:17 p.m.]

Page 12: Matlab Tutorial Ctm

print -P<printername>

If you want to save the plot and print it later, enter the command:

print plot.ps

Sometime later, you could print the plot using the command "lpr -P plot.ps" If you are using a HPworkstation to print, you would instead use the command "lpr -d plot.ps"

To print a m-file, just print it the way you would any other file, using the command "lpr -P <nameof m-file>.m" If you are using a HP workstation to print, you would instead use the command "lpr-d plot.ps<name of m-file>.m"

Using M-files in MatlabThere 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 useany other editor you like (but be sure to save the files in text format and load them when you startMatlab).

Windows

Running Matlab from Windows is very similar to running it on a Macintosh. However, you needto know that your m-file will be saved in the clipboard. Therefore, you must make sure that it issaved as filename.m

Unix

You will need to run an editor separately from Matlab. The best strategy is to make a directory forall your m-files, then cd to that directory before running both Matlab and the editor. To startMatlab from your Xterm window, simply type: matlab.

You can either type commands directly into matlab, or put all of the commands that you will needtogether in an m-file, and just run the file. If you put all of your m-files in the same directory that you runmatlab from, then matlab will always find them.

Getting help in MatlabMatlab 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 youare looking for; a list of the all the ones used in these tutorials is given in the command listing; a link tothis 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.

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (9 de 10) [07/04/2003 07:15:17 p.m.]

Page 13: Matlab Tutorial Ctm

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 asemicolon 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".

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital | Examples

8/11/97 dmt

CTM: Matlab Basics Tutorial

http://www.engin.umich.edu/group/ctm/basic/basic.html (10 de 10) [07/04/2003 07:15:17 p.m.]

Page 14: Matlab Tutorial Ctm

Modeling TutorialTrain systemFree body diagram and Newton's lawState-variable and output equationsMatlab representation

Matlab can be used to represent a physical system or a model. To begin with, let's start with a review of howto represent a physical system as a set of differential equations.

Train systemIn this example, we will consider a toy train consisting of an engine and a car. Assuming that the train onlytravels in one direction, we want to apply control to the train so that it has a smooth start-up and stop, alongwith a constant-speed ride.

The mass of the engine and the car will be represented by M1 and M2, respectively. The two are held togetherby a spring, which has the stiffness coefficient of k. F represents the force applied by the engine, and theGreek letter, mu (which will also be represented by the letter u), represents the coefficient of rolling friction.

Photo courtesy: Dr. Howard Blackburn

Free body diagram and Newton's lawThe system can be represented by following Free Body Diagrams.

CTM: Modeling Tutorial

http://www.engin.umich.edu/group/ctm/model/model.html (1 de 5) [07/04/2003 07:17:44 p.m.]

Page 15: Matlab Tutorial Ctm

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 forcesacting on M2 are the spring and the friction. In the vertical direction, the gravitational force is canceled by thenormal force applied by the ground, so that there will be no acceleration in the vertical direction. Theequations of motion in the horizontal direction are the followings:

State-variable and output equationsThis set of system equations can now be manipulated into state-variable form. Knowing state-variables are X1and 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 outputequations.

CTM: Modeling Tutorial

http://www.engin.umich.edu/group/ctm/model/model.html (2 de 5) [07/04/2003 07:17:44 p.m.]

Page 16: Matlab Tutorial Ctm

Using these equations, derive the transfer function Y(s)/F(s) in terms of constants. When finding the transferfunction, zero initial conditions must be assumed. The transfer function should look like the one shownbelow.

2. State-space

Another method to solve the problem is to use the state-space form. Four matrices A, B, C, and D characterizethe system behavior, and will be used to solve the problem. The state-space form that were manipulated fromthe state-variable and the output equations is shown below.

Matlab representationNow we will show you how to enter the equations derived above into an m-file for Matlab. Since Matlab cannot manipulate symbolic variables, let's assign numerical values to each of the variables. Let

M1 = 1 kg●

M2 = 0.5 kg●

CTM: Modeling Tutorial

http://www.engin.umich.edu/group/ctm/model/model.html (3 de 5) [07/04/2003 07:17:44 p.m.]

Page 17: Matlab Tutorial Ctm

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;

Now you have one of two choices: 1) Use the transfer function, or 2) Use the state-space form to solve theproblem. If you choose to use the transfer function, add the following commands onto the end of the m-filewhich 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 numand 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.

Continue solving the problemNow, you are ready to obtain the system output (with an addition of few more commands). It should be notedthat many operations can be done using either the transfer function or the state-space model. Furthermore, it issimple to transfer between the two if the other form of representation is required. If you need to learn how toconvert from one representation to the other, click Conversion.

This tutorial contain seven examples which allows you to learn more about modeling. You can link to themfrom below.

CTM: Modeling Tutorial

http://www.engin.umich.edu/group/ctm/model/model.html (4 de 5) [07/04/2003 07:17:44 p.m.]

Page 18: Matlab Tutorial Ctm

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have for improvement,errors that you found, or any other comments that you have. This feedback is anonymous; include your emailaddress if you want a reply.

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller |

Ball and Beam

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Examples

8/26/97 DK

CTM: Modeling Tutorial

http://www.engin.umich.edu/group/ctm/model/model.html (5 de 5) [07/04/2003 07:17:44 p.m.]

Page 19: Matlab Tutorial Ctm

Example: Modeling a Cruise Control System

Physical setup and system equationsDesign requirementsMatlab representationOpen-loop responseClosed-loop transfer function

Physical setup and system equationsThe model of the cruise control system is relatively simple. If the inertia of the wheels is neglected, and itis assumed that friction (which is proportional to the car's speed) is what is opposing the motion of thecar, then the problem is reduced to the simple mass and damper system shown below.

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

(1)

CTM Example: Cruise Control Modeling

http://www.engin.umich.edu/group/ctm/examples/cruise/cc.html (1 de 5) [21/11/2003 05:41:27 p.m.]

Page 20: Matlab Tutorial Ctm

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

m = 1000kgb = 50Nsec/m

u = 500N

Design requirementsThe next step in modeling this system is to come up with some design criteria. When the engine gives a500 Newton force, the car will reach a maximum velocity of 10 m/s (22 mph). An automobile should beable 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 thesame reason.

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

Rise time < 5 secOvershoot < 10%Steady state error < 2%

Matlab representation

1. Transfer Function

To find the transfer function of the above system, we need to take the Laplace transform of the modelingequations (1). When finding the transfer function, zero initial conditions must be assumed. Laplacetransforms 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:

CTM Example: Cruise Control Modeling

http://www.engin.umich.edu/group/ctm/examples/cruise/cc.html (2 de 5) [21/11/2003 05:41:27 p.m.]

Page 21: Matlab Tutorial Ctm

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. Butbefore 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];B = [1/m];C = [1];D = 0;

Note: It is possible to convert from the state-space representation to the transfer function or vise versausing Matlab. To learn more about the conversion, click Conversion

Open-loop responseNow let's see how the open-loop system responds to a step input. Add the following command onto theend of the m-file written for the tranfer function (the m-file with num and den matrices) and run it in theMatlab command window:

step (u*num,den)

You should get the following plot:

CTM Example: Cruise Control Modeling

http://www.engin.umich.edu/group/ctm/examples/cruise/cc.html (3 de 5) [21/11/2003 05:41:27 p.m.]

Page 22: Matlab Tutorial Ctm

To use the m-file written for the state-space (the m-file with A, B, C, D matrices), add the followingcommand 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 10m/s. This does not satisfy our rise time criterion of less than 5 seconds.

Closed-loop transfer functionTo solve this problem, a unity feedback controller will be added to improve the system performance. Thefigure 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}. Thecontroller will to be designed to satisfy all design criteria. Four different methods to design the controllerare listed at the bottom of this page. You may choose on PID, Root-locus, Frequency response, orState-space.

CTM Example: Cruise Control Modeling

http://www.engin.umich.edu/group/ctm/examples/cruise/cc.html (4 de 5) [21/11/2003 05:41:27 p.m.]

Page 23: Matlab Tutorial Ctm

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Cruise Control Modeling

http://www.engin.umich.edu/group/ctm/examples/cruise/cc.html (5 de 5) [21/11/2003 05:41:27 p.m.]

Page 24: Matlab Tutorial Ctm

Example: Solution to the Cruise ControlProblem Using PID control

Proportional controlPI controlPID 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.

The design criteria for this problem are:

Rise time < 5 secOvershoot < 10%

Steady state error < 2%

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (1 de 7) [21/11/2003 05:42:07 p.m.]

Page 25: Matlab Tutorial Ctm

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 controlThe 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 proportionalcontroller becomes:

Recall from the PID tutorial page, a proportional controller (Kp) decreases the rise time. This is what weneed, 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 thefollowing commands.

kp=100;m=1000;b=50;u=10;num=[kp];den=[m b+kp];t=0:0.1:20;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.

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (2 de 7) [21/11/2003 05:42:07 p.m.]

Page 26: Matlab Tutorial Ctm

Note: You can use the Matlab command cloop to find the closed-loop response directly from theopen-loop transfer function. If you choose to do so, change the m-file to the following and run it in thecommand 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 designcriteria. You can increase the proportional gain (Kp) to improve the system output. Change the existingm-file so that Kp equal 10000 and rerun it in the Matlab command window. You should see thefollowing plot.

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (3 de 7) [21/11/2003 05:42:07 p.m.]

Page 27: Matlab Tutorial Ctm

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 thespeed 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 controlThe 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 thesteady-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;num=[kp ki];den=[m b+kp ki];t=0:0.1:20;step(u*num,den,t)

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (4 de 7) [21/11/2003 05:42:07 p.m.]

Page 28: Matlab Tutorial Ctm

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:

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) mostlikely unstabilize the response.

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

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (5 de 7) [21/11/2003 05:42:07 p.m.]

Page 29: Matlab Tutorial Ctm

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

PID controlFor this particular example, no implementation of a derivative controller was needed to obtain a requiredoutput. However, you might want to see how to work with a PID control for the future reference. Theclosed-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;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 ofKp, Kd, and Ki until you obtain satisfactory results. We will leave this as an exercise for you to work on.

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (6 de 7) [21/11/2003 05:42:07 p.m.]

Page 30: Matlab Tutorial Ctm

Suggestion: Usually choosing appropriate gains require trial and error processes. The best way to attackthis tedious process is to adjust one variable (Kp, Kd, or Ki) at a time and observe how changing onevariable influences the system output. The characteristics of Kp, Kd, and Ki are summarized in the PIDTutorial page.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: PID control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccPID.html (7 de 7) [21/11/2003 05:42:07 p.m.]

Page 31: Matlab Tutorial Ctm

Example: DC Motor Speed ModelingPhysical setup and system equationsDesign requirementsMatlab representation and open-loop response

Physical setup and system equations

Photo courtesy: Pope Electric Motors Pty Limited

A common actuator in control systems is the DC motor. It directly providesrotary motion and, coupled with wheels or drums and cables, can providetransitional motion. The electric circuit of the armature and the free bodydiagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values werederived 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

CTM Example: DC Motor Speed Modeling

http://www.engin.umich.edu/group/ctm/examples/motor/motor.html (1 de 5) [21/11/2003 05:42:23 p.m.]

Page 32: Matlab Tutorial Ctm

* 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, isrelated 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 withKirchhoff's law:

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 isthe 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 andelectric current as the state variables and the voltage as an input. The output is chosen to be the rotationalspeed.

CTM Example: DC Motor Speed Modeling

http://www.engin.umich.edu/group/ctm/examples/motor/motor.html (2 de 5) [21/11/2003 05:42:23 p.m.]

Page 33: Matlab Tutorial Ctm

Design requirementsFirst, our uncompensated motor can only rotate at 0.1 rad/sec with an input voltage of 1 Volt (this will bedemonstrated later when the open-loop response is simulated). Since the most basic requirement of amotor is that it should rotate at the desired speed, the steady-state error of the motor speed should be lessthan 1%. The other performance requirement is that the motor must accelerate to its steady-state speed assoon as it turns on. In this case, we want it to have a settling time of 2 seconds. Since a speed faster thanthe 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 denominatormatrices 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)];

CTM Example: DC Motor Speed Modeling

http://www.engin.umich.edu/group/ctm/examples/motor/motor.html (3 de 5) [21/11/2003 05:42:23 p.m.]

Page 34: Matlab Tutorial Ctm

Now let's see how the original open-loop system performs. Add the following commands onto the end ofthe m-file and run it in the Matlab command window:

step(num,den,0:0.1:3)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 maximumspeed of 0.1 rad/sec, ten times smaller than our desired speed. Also, it takes the motor 3 seconds to reachits 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 newm-file.

J=0.01;b=0.1;K=0.01;R=1;L=0.5;A=[-b/J K/J -K/L -R/L];

CTM Example: DC Motor Speed Modeling

http://www.engin.umich.edu/group/ctm/examples/motor/motor.html (4 de 5) [21/11/2003 05:42:23 p.m.]

Page 35: Matlab Tutorial Ctm

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

User feedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Speed ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/7/97 BRN8/24/97 WM

CTM Example: DC Motor Speed Modeling

http://www.engin.umich.edu/group/ctm/examples/motor/motor.html (5 de 5) [21/11/2003 05:42:23 p.m.]

Page 36: Matlab Tutorial Ctm

Example: Modeling DC Motor PositionPhysical SetupSystem EquationsDesign RequirementsMatlab Representation and Open-Loop Response

Physical SetupA common actuator in control systems is the DC motor. It directlyprovides rotary motion and, coupled with wheels or drums and cables,can provide transitional motion. The electric circuit of the armatureand the free body diagram of the rotor are shown in the followingfigure:

For this example, we will assume the following values for the physical parameters. These values were derived byexperiment 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* 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

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (1 de 6) [21/11/2003 05:42:30 p.m.]

Page 37: Matlab Tutorial Ctm

* The rotor and shaft are assumed to be rigid

System EquationsThe motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to therotational 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 voltageis an input.

However during this example we will be looking at the position, as being the output. We can obtain the position byintegrating 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 armaturecurrent as our state variables, we can write the equations as follows:

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (2 de 6) [21/11/2003 05:42:30 p.m.]

Page 38: Matlab Tutorial Ctm

Design requirementsWe will want to be able to position the motor very precisely, thus the steady-state error of the motor position shouldbe zero. We will also want the steady-state error due to a disturbance, to be zero as well. The other performancerequirement is that the motor reaches its final position very quickly. In this case, we want it to have a settling time of40ms. 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●

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-fileand run it in the Matlab command window:

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

You should get the following plot:

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (3 de 6) [21/11/2003 05:42:30 p.m.]

Page 39: Matlab Tutorial Ctm

From the plot we see that when 1 volt is applied to the system, the motor position changes by 6 radians, six timesgreater than our desired position. For a 1 volt step input the motor should spin through 1 radian. Also, the motordoesn'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.

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (4 de 6) [21/11/2003 05:42:30 p.m.]

Page 40: Matlab Tutorial Ctm

Error in ==> /usr/local/lib/matlab/toolbox/control/step.mOn line 84 ==> dt = t(2)-t(1);

There are numerical scaling problems with this representation of the dynamic equations. To fix the problem, wescale time by tscale = 1000. Now the output time will be in milliseconds rather than in seconds. The equations aregiven 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 bytscale.

[y,x,t]=step(A,B,C,D);plot(t/tscale,y)ylabel('Amplitude')xlabel('Time (sec)')

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errorsthat you found, or any other comments that you have. This feedback is anonymous.

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Balland Beam

Motor Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control:RL

Tutorials

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (5 de 6) [21/11/2003 05:42:30 p.m.]

Page 41: Matlab Tutorial Ctm

Basics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Examples

8/24/97 WM

CTM Example: Motor Position Control Modeling

http://www.engin.umich.edu/group/ctm/examples/motor2/motor.html (6 de 6) [21/11/2003 05:42:30 p.m.]

Page 42: Matlab Tutorial Ctm

Example: Modeling a Bus Suspension System usingTransfer Function

Physical setupDesign requirementsEquations of motionTransfer function equationEntering equations into MatlabOpen-loop responseControl Block Diagram

Physical setup

Photo courtesy: Eisenhower Center

Designing an automatic suspension system for a bus turns out to be an interestingcontrol problem. When the suspension system is designed, a 1/4 bus model (one of thefour wheels) is used to simplify the problem to a one dimensional spring-dampersystem. A diagram of this system is shown below:

Where:

* body mass (m1) = 2500 kg,* suspension mass (m2) = 320 kg,

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (1 de 7) [21/11/2003 05:42:43 p.m.]

Page 43: Matlab Tutorial Ctm

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

Design requirements:

A good bus suspension system should have satisfactory road holding ability, while still providing comfort when riding overbumps and holes in the road. When the bus is experiencing any road disturbance (i.e. pot holes, cracks, and unevenpavement),the bus body should not have large oscillations, and the oscillations should dissipate quickly. Since the distanceX1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2instead 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 ofa pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settlingtime shorter than 5 seconds. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within arange 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:

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 abump. The dynamic equations above can be expressed in a form of transfer functions by taking Laplace Transform of theabove equations. The derivation from above equations of the Transfer Functions G1(s) and G2(s) of output,X1-X2, and twoinputs,U and W, are as follows.

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (2 de 7) [21/11/2003 05:42:43 p.m.]

Page 44: Matlab Tutorial Ctm

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

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 firsttwo 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 TransferFunctions in the form, nump/denp for actuated force input and num1/den1 for disturbance input, of the standard transferfunction 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];denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1)k1*k2];

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (3 de 7) [21/11/2003 05:42:43 p.m.]

Page 45: Matlab Tutorial Ctm

'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 thefollowing commands into the m-file and run it in the Matlab command window to see the response of unit step actuatedforce 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. Peoplesitting in the bus will feel very small amount of oscillation and the steady-state error is about 0.013 mm. Moreover, the bustakes very unacceptably long time for it to reach the steady state or the settling time is very large. The solution to thisproblem is to add a feedback controller into the system's block diagram.

step(0.1*num1,den1)

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (4 de 7) [21/11/2003 05:42:43 p.m.]

Page 46: Matlab Tutorial Ctm

To see some details, you can change the axis:

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

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 bumpon the road, the bus body will oscillate for an unacceptably long time(100 seconds) with larger amplitude, 13 cm, than theinitial impact. People sitting in the bus will not be comfortable with such an oscillation. The big overshoot (from the impactitself) and the slow settling time will cause damage to the suspension system. The solution to this problem is to add afeedback controller into the system to improve the performance. The schematic of the closed-loop system is the following:

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (5 de 7) [21/11/2003 05:42:43 p.m.]

Page 47: Matlab Tutorial Ctm

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

F=num1/(den1*Plant)

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that youfound, or any other comments that you have. This feedback is anonymous.

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (6 de 7) [21/11/2003 05:42:43 p.m.]

Page 48: Matlab Tutorial Ctm

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball andBeam

Bus Suspension ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

6/3/97 PP8/24/97 WM

CTM Example: Bus Suspension Modeling

http://www.engin.umich.edu/group/ctm/examples/susp/susp.html (7 de 7) [21/11/2003 05:42:43 p.m.]

Page 49: Matlab Tutorial Ctm

Example: Modeling an Inverted PendulumProblem setup and design requirementsForce analysis and system equationsMatlab representation and the open-loop response

Problem setup and design requirementsThe cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F. Determine thedynamic equations of motion for the system, and linearize about the pendulum's angle, theta = Pi (inother 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 kgm mass of the pendulum 0.5 kgb friction of the cart 0.1 N/m/sec

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (1 de 10) [21/11/2003 05:42:50 p.m.]

Page 50: Matlab Tutorial Ctm

l length to pendulum center of mass 0.3 mI inertia of the pendulum 0.006 kg*m^2F force applied to the cartx cart position coordinatetheta pendulum angle from vertical

For the PID, root locus, and frequency response sections of this problem we will be only interested in thecontrol of the pendulums position. This is because the techniques used in these tutorials can only beapplied for a single-input-single-output (SISO) system. Therefore, none of the design criteria deal withthe cart's position. For these sections we will assume that the system starts at equilibrium, andexperiences 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 thependulum's angle and the cart's position. To make the design more challenging we will be applying a stepinput to the cart. The cart should achieve it's desired position within 5 seconds and have a rise time under0.5 seconds. We will also limit the pendulum's overshoot to 20 degrees (0.35 radians), and it should alsosettle 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 equationsBelow are the two Free Body Diagrams of the system.

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (2 de 10) [21/11/2003 05:42:50 p.m.]

Page 51: Matlab Tutorial Ctm

Summing the forces in the Free Body Diagram of the cart in the horizontal direction, you get thefollowing equation of motion:

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

Summing the forces in the Free Body Diagram of the pendulum in the horizontal direction, you can getan 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 systemalong 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 thependulum 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,

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (3 de 10) [21/11/2003 05:42:50 p.m.]

Page 52: Matlab Tutorial Ctm

cos(theta) = -1, sin(theta) = -ø, and (d(theta)/dt)^2 = 0. After linearization the two equations of motionbecome (where u represents the input):

1. Transfer Function

To obtain the transfer function of the linearized system equations analytically, we must first take theLaplace transform of the system equations. The Laplace transforms are:

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:

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (4 de 10) [21/11/2003 05:42:50 p.m.]

Page 53: Matlab Tutorial Ctm

where,

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

2. State-Space

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

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (5 de 10) [21/11/2003 05:42:50 p.m.]

Page 54: Matlab Tutorial Ctm

The C matrix is 2 by 4, because both the cart's position and the pendulum's position are part of theoutput. For the state-space design problem we will be controlling a multi-output system so we will beobserving 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 thenumerator and denominator as vectors. Create an m-file and copy the following text to model the transferfunction:

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

To observe the system's velocity response to an impulse force applied to the cart add the following linesat 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:

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (6 de 10) [21/11/2003 05:42:50 p.m.]

Page 55: Matlab Tutorial Ctm

As you can see from the plot, the response is entirely unsatisfactory. It is not stable in open loop. Youcan change the axis to see more of the response if you need to convince yourself that the system isunstable.

1. State-Space

Below, we show how the problem would be set up using Matlab for the state-space model. If you copythe 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 ofthe 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 matriciesA = [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;

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (7 de 10) [21/11/2003 05:42:50 p.m.]

Page 56: Matlab Tutorial Ctm

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

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (8 de 10) [21/11/2003 05:42:50 p.m.]

Page 57: Matlab Tutorial Ctm

The blue line represents the cart's position and the green line represents the pendulum's angle. It isobvious from this plot and the one above that some sort of control will have to be designed to improvethe 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 aworkable controller for the inverted pendulum problem. As stated previously, when we put this probleminto the single-input, single-output framework, we ignored the x position of the cart. The pendulum canbe 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 positionwhen our controller is implemented on the system. We emphasize that the purpose of these examples isto demonstrate design and analysis techniques using Matlab; not to actually control an invertedpendulum.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (9 de 10) [21/11/2003 05:42:50 p.m.]

Page 58: Matlab Tutorial Ctm

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/11/97 CJC

CTM Example: Inverted Pendulum Modeling

http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html (10 de 10) [21/11/2003 05:42:50 p.m.]

Page 59: Matlab Tutorial Ctm

Example: Modeling a Pitch Controller

Photo courtesy: Boeing

Physical setup and system equationsDesign requirementsTransfer function and state-spaceMatlab representation and open-loop responseClosed-loop transfer function

Physical setup and system equationsThe equations governing the motion of an aircraft are a very complicated set of six non-linear coupleddifferential equations. However, under certain assumptions, they can be decoupled and linearized into thelongitudinal and lateral equations. Pitch control is a longitudinal problem, and in this example, we willdesign 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:

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (1 de 6) [21/11/2003 05:42:59 p.m.]

Page 60: Matlab Tutorial Ctm

Assume that the aircraft is in steady-cruise at constant altitude and velocity; thus, the thrust and dragcancel out and the lift and weight balance out each other. Also, assume that change in pitch angle doesnot 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:

(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 requirementsThe next step is to set some design criteria. We want to design a feedback controller so that the outputhas 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 pitchangle will not exceed 0.22 rad, reaches 0.2 rad within 2 seconds, settles 2% of the steady-state within 10seconds, 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%●

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (2 de 6) [21/11/2003 05:42:59 p.m.]

Page 61: Matlab Tutorial Ctm

Transfer function and the state-spaceBefore finding transfer function and the state-space model, let's plug in some numerical values tosimplify 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 abovemodeling equations (2). Recall from your control textbook, when finding a transfer function, zeroinitial 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 rewritethem into the state-space model.

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

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (3 de 6) [21/11/2003 05:42:59 p.m.]

Page 62: Matlab Tutorial Ctm

Matlab representation and open-loop responseNow, we are ready to observe the system characteristics using Matlab. First, let's obtain an open-loopsystem to a step input and determine which system characteristics need improvement. Let the input (deltae) 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)

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 theopen-loop response is unstable.

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (4 de 6) [21/11/2003 05:42:59 p.m.]

Page 63: Matlab Tutorial Ctm

If you noticed, the above m-file uses the numerical values from the transfer function. To use thestate-space model, enter the following commands into a new m-file (instead of the one shown above) andrun 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. Tolearn more about conversions, see Conversions

Closed-loop transfer functionTo solve this problem, a feedback controller will be added to improve the system performance. Thefigure 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. Fourdifferent methods to design a controller are listed at the bottom of this page. You may choose: PID,Root-locus, Frequency response, or State-space.

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (5 de 6) [21/11/2003 05:42:59 p.m.]

Page 64: Matlab Tutorial Ctm

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Pitch Controller ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Modeling Pitch Controller

http://www.engin.umich.edu/group/ctm/examples/pitch/Mpitch.html (6 de 6) [21/11/2003 05:42:59 p.m.]

Page 65: Matlab Tutorial Ctm

Example: Modeling the Ball and BeamExperiment

Problem SetupSystem EquationsMatlab Representation and Open-Loop Response

Problem SetupA ball is placed on a beam, see figure below, where it is allowed to roll with 1 degree of freedom alongthe length of the beam. A lever arm is attached to the beam at one end and a servo gear at the other. Asthe servo gear turns by an angle theta, the lever changes the angle of the beam by alpha. When the angleis changed from the vertical position, gravity causes the ball to roll along the beam. A controller will bedesigned for this system so that the ball's position can be manipulated.

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (1 de 8) [21/11/2003 05:43:05 p.m.]

Page 66: Matlab Tutorial Ctm

For this problem, we will assume that the ball rolls without slipping and friction between the beam andball is negligible. The constants and variables for this example are defined as follows:

M mass of the ball 0.11 kgR radius of the ball 0.015 md lever arm offset 0.03 mg gravitational acceleration 9.8 m/s^2L length of the beam 1.0 mJ ball's moment of inertia 9.99e-6 kgm^2r ball position coordinatealpha beam angle coordinatetheta servo gear angle

The design criteria for this problem are:

Settling time less than 3 seconds●

Overshoot less than 5%●

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

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (2 de 8) [21/11/2003 05:43:05 p.m.]

Page 67: Matlab Tutorial Ctm

Linearization of this equation about the beam angle, alpha = 0, gives us the following linearapproximation of the system:

The equation which relates the beam angle to the angle of the gear can be approximated as linear by theequation below:

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 tobe zero.

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (3 de 8) [21/11/2003 05:43:05 p.m.]

Page 68: Matlab Tutorial Ctm

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 marginallystable 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 byselecting the ball's position (r) and velocity (rdot) as the state variables and the gear angle (theta) as theinput. The state-space representation is shown below:

However, for our state-space example we will be using a slightly different model. The same equation forthe ball still applies but instead of controlling the position through the gear angle, theta, we will controlalpha-doubledot. This is essentially controlling the torque of the beam. Below is the representation of thissystem:

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (4 de 8) [21/11/2003 05:43:05 p.m.]

Page 69: Matlab Tutorial Ctm

Note: For this system the gear and lever arm would not be used, instead a motor at the center of the beamwill 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 thenumerator and denominator as vectors. To do this we must create an m-file and copy the following textinto it:

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 toadd the following line to your m-file:

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (5 de 8) [21/11/2003 05:43:05 p.m.]

Page 70: Matlab Tutorial Ctm

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:

From this plot it is clear that the system is unstable in open-loop causing the ball to roll right off the endof the beam. Therefore, some method of controlling the ball's position in this system is required. Threeexamples of controller design are listed below for the transfer function problem. You may select fromPID, Root Locus, and Frequency Response.

2. State-Space

The state-space equations can be represented in Matlab with the following commands (these equationsare 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

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (6 de 8) [21/11/2003 05:43:05 p.m.]

Page 71: Matlab Tutorial Ctm

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:

Like the plot for the transfer function this plot shows that the system is unstable and the ball will rollright off the end of the beam. Therefore, we will require some method of controlling the ball's position inthis system. The State-Space example below shows how to implement a controller for this type ofsystem.

If you are interested in knowing how to convert state-space representations to transfer functionrepresentations, and vice versa, please refer to the Conversions page.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (7 de 8) [21/11/2003 05:43:05 p.m.]

Page 72: Matlab Tutorial Ctm

Modeling ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Ball & Beam ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: Ball & Beam Modeling

http://www.engin.umich.edu/group/ctm/examples/ball/ball.html (8 de 8) [21/11/2003 05:43:05 p.m.]

Page 73: Matlab Tutorial Ctm

Example: PID Design Method for DC MotorSpeed Control

Proportional controlPID controlTuning the gains

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

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling aDC Motor page.

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

Settling time less than 2 seconds●

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (1 de 7) [21/11/2003 05:43:13 p.m.]

Page 74: Matlab Tutorial Ctm

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 thefollowing 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 controlLet's first try using a proportional controller with a gain of 100. Add the following code to the end ofyour 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 toyour m-file:

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

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

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

t=0:0.01:5;step(numac,denac,t)title('Step response with Proportion Control')

You should get the following plot:

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (2 de 7) [21/11/2003 05:43:13 p.m.]

Page 75: Matlab Tutorial Ctm

PID controlFrom the plot above we see that both the steady-state error and the overshoot are too large. Recall fromthe PID tutorial page that adding an integral term will eliminate the steady-state error and a derivativeterm will reduce the overshoot. Let's try a PID controller with small Ki and Kd. Change your m-file so itlooks like the following. Running this new m-file gives you the following plot.

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)];

Kp=100;Ki=1;Kd=1;numc=[Kd, Kp, Ki];denc=[1 0];numa=conv(num,numc);dena=conv(den,denc);

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (3 de 7) [21/11/2003 05:43:13 p.m.]

Page 76: Matlab Tutorial Ctm

[numac,denac]=cloop(numa,dena);step(numac,denac)title('PID Control with small Ki and Kd')

Tuning the gainsNow the settling time is too long. Let's increase Ki to reduce the settling time. Go back to your m-file andchange Ki to 200. Rerun the file and you should get the plot like this:

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (4 de 7) [21/11/2003 05:43:13 p.m.]

Page 77: Matlab Tutorial Ctm

Now we see that the response is much faster than before, but the large Ki has worsened the transientresponse (big overshoot). Let's increase Kd to reduce the overshoot. Go back to the m-file and change Kdto 10. Rerun it and you should get this plot:

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (5 de 7) [21/11/2003 05:43:13 p.m.]

Page 78: Matlab Tutorial Ctm

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

Kp=100,Ki=200,Kd=10,

all of our design requirements will be satisfied.

User feedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (6 de 7) [21/11/2003 05:43:13 p.m.]

Page 79: Matlab Tutorial Ctm

Motor Speed ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID

TutorialsBasics | PID | Modeling | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/7/97 BRN8/24/97 WM

CTM Example: PID Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/PID2.html (7 de 7) [21/11/2003 05:43:13 p.m.]

Page 80: Matlab Tutorial Ctm

Example: PID Design Method for the DC MotorPosition

Proportional controlPID controlTuning the gains

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 aDC 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●

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (1 de 10) [21/11/2003 05:43:19 p.m.]

Page 81: Matlab Tutorial Ctm

Now let's design a PID controller and add it into the system. First create a new m-file and type in thefollowing 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];

Recall that the transfer function for a PID controller is:

Proportional controlLet's first try using a proportional controller with a gain of 1.7. Add the following code to the end of yourm-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 toyour m-file:

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

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

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

t=0:0.001:0.2;step(numc,denc,t)

You should get the following plot:

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (2 de 10) [21/11/2003 05:43:19 p.m.]

Page 82: Matlab Tutorial Ctm

Now lets take a look at the step disturbance response. Add the following to the end of your m-file, andrun it in the command window:

numdcl=conv(numc,1);dendcl=conv(denc,Kp);step(numdcl,dendcl,t);

You should get the following plot:

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (3 de 10) [21/11/2003 05:43:19 p.m.]

Page 83: Matlab Tutorial Ctm

PID controlFrom 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 PIDtutorial page that adding an integral term will eliminate the steady-state error and a derivative term willreduce the overshoot. Let's first try a PI controller to get rid of the disturbance steady state error. Changeyour 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)

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (4 de 10) [21/11/2003 05:43:19 p.m.]

Page 84: Matlab Tutorial Ctm

You should get the following step response:

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

figurenumdcl=conv(numc,dencf);dendcl=conv(denc,numcf);step(numdcl,dendcl,t);

You should get the following plot:

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (5 de 10) [21/11/2003 05:43:19 p.m.]

Page 85: Matlab Tutorial Ctm

Tuning the gainsThe settling time is still too long. Let's increase the gains in order to speed up the response. Go back toyour m-file and change Ki to 200 and Kp to 17. Rerun the file and you should get plots like these:

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (6 de 10) [21/11/2003 05:43:19 p.m.]

Page 86: Matlab Tutorial Ctm

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

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (7 de 10) [21/11/2003 05:43:19 p.m.]

Page 87: Matlab Tutorial Ctm

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 timeis roughly 40ms, and there is no steady-state error. However the step disturbance response is now reallyslow. Lets increase Ki to speed up the disturbance response. Change Ki to 600 in your m-file and rerunthe file. You should get the following plots:

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (8 de 10) [21/11/2003 05:43:19 p.m.]

Page 88: Matlab Tutorial Ctm

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,

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (9 de 10) [21/11/2003 05:43:19 p.m.]

Page 89: Matlab Tutorial Ctm

all of our design requirements will be satisfied.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control:RL

TutorialsBasics | PID | Modeling | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/24/97 WM

CTM Example: DC Motor Position PID Control

http://www.engin.umich.edu/group/ctm/examples/motor2/PID2.html (10 de 10) [21/11/2003 05:43:19 p.m.]

Page 90: Matlab Tutorial Ctm

Example:PID Design Method for the Bus SuspensionSystem

Adding a PID controllerPlotting the closed-loop responseChoosing the gains for the PID controller

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

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

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (1 de 7) [21/11/2003 05:43:24 p.m.]

Page 91: Matlab Tutorial Ctm

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 tomain 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;

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 allthree of these gains in our controller. To begin, we might start with guessing a gain for each: KP=208025, KI=832100 andKD=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];

Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the road. From the schematicabove we can find the transfer function from the road disturbance W to the output(X1-X2):

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (2 de 7) [21/11/2003 05:43:24 p.m.]

Page 92: Matlab Tutorial Ctm

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. Clickhere 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 thistransfer 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 thecontroller. Let's see what the closed-loop step response for this system looks like before we begin the control process. Keepin 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 numaby 0.1. Add the following code into your m-file:

t=0:0.05:5;step(0.1*numa,dena,t)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:

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (3 de 7) [21/11/2003 05:43:24 p.m.]

Page 93: Matlab Tutorial Ctm

From the graph, the percent overshoot = 9%, which is 4% larger than the requirement, but the settling time is satisfied, lessthan 5 seconds. To choose the proper gain that yields reasonable output from the beginning, we start with choosing a poleand 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 thepole 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'sposition to get the system to fulfill the requirement. Add the following command in the m-file, so you can adjust thesecond-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);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 onthe graph by yourself:

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (4 de 7) [21/11/2003 05:43:24 p.m.]

Page 94: Matlab Tutorial Ctm

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,andKI variables. From the figure above, we can see that the system has larger damping than required, but the settling time isvery short. This response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can be rectified byadjusting the KD, KP and KI variables to find better response. Let's increase KP,KI,KD by 2 times to see what willhappen. Go back to your m-file and multiply KP,KI,KD by 2 and then rerun the program, you should get the following plot:

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (5 de 7) [21/11/2003 05:43:24 p.m.]

Page 95: Matlab Tutorial Ctm

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

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

Now we see that the percent overshoot and settling time meet the requirements of the system. The percent overshoot is about5% of the input's amplitude and settling time is 2 seconds less than 5 seconds from requirement.

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (6 de 7) [21/11/2003 05:43:25 p.m.]

Page 96: Matlab Tutorial Ctm

For this problem, it turns out that the PID design method adequately controls the system. This can been seen by looking atthe root locus plot. Such a task can be achieved by simply changing only the gains of a PID controller. Feel free to playaround with all three of the parameters,KD,KP and KI, as we suggested, but you will most likely get the response to haveeither large percent overshoot or very long settling time. But if you do find a good PID design, please email us with yourresults! We are always interested in different ways to solve our examples; we may include your solution in a future versionof these tutorials.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that youfound, or any other comments that you have. This feedback is anonymous.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball andBeam

Bus Suspension ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

6/20/97 PP8/24/97 WM

CTM Example: PID Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/PID1.html (7 de 7) [21/11/2003 05:43:25 p.m.]

Page 97: Matlab Tutorial Ctm

Example: Solution to the Inverted PendulumProblem Using PID Control

Open-loop RepresentationClosed-loop transfer functionAdding the PID controllerWhat happens to the cart's position?

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.

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (1 de 10) [21/11/2003 05:43:31 p.m.]

Page 98: Matlab Tutorial Ctm

Open-loop RepresentationThe first thing to do when using PID control in Matlab is to find the transfer function of the system andto check to see if it makes sense. The transfer function found from the Laplace transforms for the outputPhi (the pendulum's angle) can be set up using Matlab by inputting the numerator and denominator asvectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copy the followingtext 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 functionThe 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 initialdisturbance, the reference signal we are tracking should be zero. The force applied to the cart can beadded 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

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (2 de 10) [21/11/2003 05:43:31 p.m.]

Page 99: Matlab Tutorial Ctm

the schematic as follows:

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

Adding the PID controllerThis closed-loop transfer function can be modeled in Matlab by copying the following code to the end ofyour m-file (whether your using the transfer function from the Laplace transforms or from the state-spacerepresentation):

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 along withproportional control. This does not have to be the case. If you wanted to start with PI control, just removethe kd term from numPID. If you wanted to start with PD control, just remove the ki term fromnumPID and change denPID to equal [1]. Assuming you do not change the PID control, you shouldget the following closed-loop numerator and denominator in the Matlab command window:

numc = 4.5455 0 0

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (3 de 10) [21/11/2003 05:43:31 p.m.]

Page 100: Matlab Tutorial Ctm

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

This response is still not stable. Let's start by increasing the proportional control to the system. Increasethe 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:

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (4 de 10) [21/11/2003 05:43:31 p.m.]

Page 101: Matlab Tutorial Ctm

The settling time is acceptable at about 2 seconds. Since the steady-state error has already been reducedto zero, no more integral control is needed. You can remove the integral gain constant to see for yourselfthat the small integral control is needed. The overshoot is too high, so that must be fixed. To alleviate thisproblem, increase the kd variable. With kd=20, you should get a satisfactory result. You should nowsee the following velocity response plot:

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (5 de 10) [21/11/2003 05:43:31 p.m.]

Page 102: Matlab Tutorial Ctm

As you can see, the overshoot has been reduced so that the pendulum does not move more than 0.05radians 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 wasnot entirely complete. The block representing the the position was left out because that variable was notbeing controlled. It is interesting though, to see what is happening to the cart's position when thecontroller for the pendulum's angle is in place. To see this we need to consider the actual system blockdiagram:

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

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (6 de 10) [21/11/2003 05:43:31 p.m.]

Page 103: Matlab Tutorial Ctm

The feedback loop represents the controller we have designed for the pendulum's. The transfer functionfrom the cart's position to the impulse force, with the PID feedback controller which we designed, isgiven as follows:

Recall that den1=den2 if the pole/zero at the origin that was cancelled is added back in. So the transferfunction 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 weneed the transfer function for the cart's position. To get this we need to go back to the laplace transformsof the system equations and find the transfer function from X(s) to U(s). Below is this transfer function:

where,

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (7 de 10) [21/11/2003 05:43:31 p.m.]

Page 104: Matlab Tutorial Ctm

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 thatnow den1 = den2, making calculations easier. Now, create a new m-file and run it in the commandwindow:

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

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (8 de 10) [21/11/2003 05:43:31 p.m.]

Page 105: Matlab Tutorial Ctm

As you can see, the cart moves in the negative direction with a constant velocity. So although the PIDcontroller stabilizes the angle of the pendulum, this design would not be feasible to implement on anactual physical system.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (9 de 10) [21/11/2003 05:43:31 p.m.]

Page 106: Matlab Tutorial Ctm

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: PID control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invPID.html (10 de 10) [21/11/2003 05:43:31 p.m.]

Page 107: Matlab Tutorial Ctm

Example: PID Design method for the PitchController

Proportional controlProportional-Derivative controlProportional-Integral-Derivative control

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 pitchangle (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 inan unity feedback system shown below to study the system output.

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (1 de 6) [21/11/2003 05:43:38 p.m.]

Page 108: Matlab Tutorial Ctm

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

Proportional controlThe first thing in solving this problem using PID control is to find a closed-loop transfer function with aproportional control (Kp). A closed-loop transfer function can be obtained either by hand or using theMatlab 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 followingcommands to an m-file and run it in the Matlab command window. You should obtain the numericalcoefficients of numerator and denominator of the closed-loop transfer function.

Kp=[1]; %Enter any numerical value for the proportional gainnum=[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 theclosed-loop transfer function derived by hand. Enter the following commands into a new m-file and runit 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)

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (2 de 6) [21/11/2003 05:43:38 p.m.]

Page 109: Matlab Tutorial Ctm

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

PD controlRecall from the PID Tutorial page, the derivative controller will reduce both the overshoot and thesettling time. Let's try a PD controller. The closed-loop transfer function of the system with a PDcontroller is:

Using the commands shown below and with several trial-and-error runs, a proportional gain (Kp) of 9and a derivative gain (Kd) of 4 provided the reasonable response. To comfirm this, change your m-file tothe following and run it in the Matlab command window. You should obtain the step response similar tothe 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)

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (3 de 6) [21/11/2003 05:43:38 p.m.]

Page 110: Matlab Tutorial Ctm

This step response shows the rise time of less than 2 seconds, the overshoot of less than 10%, the settlingtime of less than 10 seconds, and the steady-state error of less than 2%. All design requirements aresatisfied.

PID ControlEven 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 providedsmoother step response that still satisfies all design requirements. To cofirm this, enter the followingcommands to an m-file and run it in the command window. You should obtain the step response shownbelow:

Note: This time we are going to use the function cloop to find the closed-loop transfer function, andthen 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];denpid=[1 0];

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (4 de 6) [21/11/2003 05:43:38 p.m.]

Page 111: Matlab Tutorial Ctm

num1=conv(numo,numpid);den1=conv(deno,denpid);

[numc,denc] = cloop(num1,den1);t=0:0.01:10; step (de*numc,denc,t)

CommentsTo find appropriate gains (Kp, Kd, and Ki), you can use the table shown in PID Tutorial page;however, please keep in your mind that changing one gain might change the effect of the othertwo. As a result, you may need to change other two gains as you change one gain.

1.

Our system with a PI controller do not provide the desired response; thus, a PI Control sectionwas omitted from this page. You may confirm this by using the m-file shown in PID Controlsection, and set Kd equals to zero.

2.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (5 de 6) [21/11/2003 05:43:38 p.m.]

Page 112: Matlab Tutorial Ctm

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Control | Ball and Beam

Pitch Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Pitch Controller -- PID design method

http://www.engin.umich.edu/group/ctm/examples/pitch/PIDpitch.html (6 de 6) [21/11/2003 05:43:38 p.m.]

Page 113: Matlab Tutorial Ctm

Example: Solution to the Ball & Beam ProblemUsing PID Control

Closed-loop RepresentationProportional ControlProportional-Derivative 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.

Closed-loop RepresentationThe block diagram for this example with a controller and unity feedback of the ball's position is shownbelow:

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (1 de 7) [21/11/2003 05:43:43 p.m.]

Page 114: Matlab Tutorial Ctm

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 ControlThe closed-loop transfer function for proportional control with a proportional gain (kp) equal to 100, canbe modeled by copying the following lines of Matlab code into an m-file (or a '.m' file located in thesame 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.

You numerator and denominator should be:

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (2 de 7) [21/11/2003 05:43:43 p.m.]

Page 115: Matlab Tutorial Ctm

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 toyour 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 thevalue of kp and note that the system remains unstable.

Proportional-Derivative ControlNow, we will add a derivative term to the controller. Copy the following lines of code to an m-file andrun it to view the system's response to this control method.

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (3 de 7) [21/11/2003 05:43:43 p.m.]

Page 116: Matlab Tutorial Ctm

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, we see that by

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (4 de 7) [21/11/2003 05:43:43 p.m.]

Page 117: Matlab Tutorial Ctm

increasing kd we can lower overshoot and decrease the settling time slightly. Therefore, make kd = 20in 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 settlingtime we may try increasing the kp slightly to increase the rise time. The derivative gain (kd) can also beincreased to take off some of the overshoot that increasing kp will cause. After playing with the gains abit, the following step response plot can be achieved with kp = 15 and kd = 40:

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (5 de 7) [21/11/2003 05:43:43 p.m.]

Page 118: Matlab Tutorial Ctm

As you can see from the above plot all the control objectives have been met without the use of an integralcontroller (settling time for this example is considered achieved when the response is less than 2% of it'sfinal 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, andstate-space links below.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Ball & Beam Examples

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (6 de 7) [21/11/2003 05:43:43 p.m.]

Page 119: Matlab Tutorial Ctm

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: PID Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbPID.html (7 de 7) [21/11/2003 05:43:43 p.m.]

Page 120: Matlab Tutorial Ctm

Example: Solution to the Cruise ControlProblem Using Root Locus Method

Proportional ControllerLag controller

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 secOvershoot < 10%

Steady state error < 2%

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

Proportional controllerRecall from the Root-Locus Tutorial page, the root-locus plot shows the locations of all possibleclosed-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:

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (1 de 7) [21/11/2003 05:43:52 p.m.]

Page 121: Matlab Tutorial Ctm

Also, from the Root-Locus Tutorial, we know that the Matlab command called sgrid should be used tofind an acceptable region of the root-locus plot. To use the sgrid, both the damping ratio (zeta) and thenatural frequency (Wn) need to be determined first. The following two equations will be used to find thedamping ratio and the natural frequency:

where

Wn=Natural frequencyzeta=Damping ratioTr=Rise timeMp=Maximum overshoot

One of our design criteria is to have a rise time of less than 5 seconds. From the first equation, we seethat the natural frequency must be greater than 0.36. Also using the second equation, we see that thedamping 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 theroot-locus. Create an new m-file and enter the following commands.

hold off;m = 1000;b = 50;u = 10;numo=[1];deno=[m b];

figurehold;axis([-0.6 0 -0.6 0.6]);rlocus (numo,deno) sgrid(0.6, 0.36)[Kp, poles]=rlocfind(numo,deno)

figurehold;

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (2 de 7) [21/11/2003 05:43:52 p.m.]

Page 122: Matlab Tutorial Ctm

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.

The two dotted lines in an angle indicate the locations of constant damping ratio (zeta=0.6); the dampingratio is greater than 0.6 in between these lines and less than 0.6 outside the lines. The semi-ellipseindicates the locations of constant natural frequency (Wn=0.36); the natural frequency is greater than0.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 theroot-locus plot. Since you want to pick a point in between dotted lines (zeta>0.6) and outside thesemi-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 shouldsee the closed-loop step response similar to the one shown below.

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (3 de 7) [21/11/2003 05:43:52 p.m.]

Page 123: Matlab Tutorial Ctm

With the specified gain Kp (the one you just picked), the rise time and the overshoot criteria have beenmet; however, the steady-state error of more than 10% remained.

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

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

Finally, the closed-loop transfer function becomes:

If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in Lead and LagCompensator page, the pole and the zero of a lag controller need to be placed close together. Also, itstates that the steady-state error will be reduce by a factor of Zo/Po. For these reasons, let Zo equals -0.3

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (4 de 7) [21/11/2003 05:43:52 p.m.]

Page 124: Matlab Tutorial Ctm

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];

figurehold;axis ([-0.6 0 -0.4 0.4])rlocus(numo,deno)sgrid(0.6,0.36)[Kp, poles]=rlocfind(numo,deno)

figuret=0:0.1:20;numc=[Kp Kp*Zo];denc=[m b+m*Po+Kp b*Po+Kp*Zo];axis ([0 20 0 12])step (u*numc,denc,t)

Running this m-file should give you the root-locus plot similar to the following:

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (5 de 7) [21/11/2003 05:43:52 p.m.]

Page 125: Matlab Tutorial Ctm

In the Matlab command window, you should see the prompt asking you to select a point on theroot-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 thezero added in the lag controller.

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (6 de 7) [21/11/2003 05:43:52 p.m.]

Page 126: Matlab Tutorial Ctm

Now all of the design criteria have been met and no further iterations will be needed.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Root Locus control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccRL.html (7 de 7) [21/11/2003 05:43:52 p.m.]

Page 127: Matlab Tutorial Ctm

Example: Root Locus Design Method for DCMotor Speed Control

Drawing the open-loop root locusFinding the gain using the rlocfind commandAdding a lag controllerPlotting the closed-loop response

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

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling aDC Motor page.

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (1 de 8) [21/11/2003 05:43:57 p.m.]

Page 128: Matlab Tutorial Ctm

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 gettingthose 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)];

Drawing the open-loop root locusThe 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'sfirst 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 directlybefore 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 overshootof 5%), and the natural frequency (Wn) term (= 0 corresponds to no rise time criterion) respectively. Thesingle argument in the sigrid command is the sigma term (4.6/2 seconds = 2.3). After you have savedsigma.m file to your directly, run the above m-file in the command window. You should get the rootlocus plot shown below:

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (2 de 8) [21/11/2003 05:43:57 p.m.]

Page 129: Matlab Tutorial Ctm

Finding the gain using the rlocfind commandIf you recall, we need the settling time and the overshoot to be as small as possible. Large dampingcorresponds to points on the root locus near the real axis. A fast response corresponds to points on theroot 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 atonce. 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 dampingrequirement, say at -6+2.5i. Matlab should return the output similar to the following.

selected_point = -5.9596 + 2.0513i

k = 10.0934

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (3 de 8) [21/11/2003 05:43:57 p.m.]

Page 130: Matlab Tutorial Ctm

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 shouldat least have the same order of magnitude. You should also get the following plot:

As you can see, the system is overdamped and the settling time is about one second, so the overshoot andsettling time requirements are satisfied. The only problem we can see from this plot is the steady- stateerror of about 50%. If we increase the gain to reduce the steady-state error, the overshoot becomes toolarge (Try this yourself). We need to add a lag controller to reduce the steady-state error.

Adding a lag controllerFrom the plot we see that this is a very simple root locus. The damping and settling time criteria weremet with the proportional controller. The steady-state error is the only criterion not met with theproportional controller. A lag compensator can reduce the steady-state error. By doing this, we mighthowever increase our settling time. Try the following lag controller first:

This can be done by changing your m-file to look like the following:

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (4 de 8) [21/11/2003 05:43:57 p.m.]

Page 131: Matlab Tutorial Ctm

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)];

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 thenumerator and denominator of the overall open-loop transfer function.

You should get the following root locus, which looks very similar to the original one:

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (5 de 8) [21/11/2003 05:43:57 p.m.]

Page 132: Matlab Tutorial Ctm

Plotting the closed-loop responseNow let's close the loop and see the closed-loop step response Enter the following code at the end of yourm-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 nearthe 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 notethat even though the gain was selected to correlate with a position close to the damping criterion, theovershoot is not even close to five percent. This is due to the effect of the lag controller kicking in at alater time than the plant. (its pole is slower). What this means is that we can go beyond the dotted linesthat represent the limit, and get the higher gains without worrying about the overshoot . Rerun yourm-file, place the gain just above the white, dotted line. Keep trying until you get a satisfactory response.It should look similar to the following (we used a gain of around 50):

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (6 de 8) [21/11/2003 05:43:57 p.m.]

Page 133: Matlab Tutorial Ctm

The steady-state error is smaller than 1%, and the settling time and overshoot requirements have beenmet. As you can see, the design process for root locus is very much a trial and error process. That is whyit is nice to plot the root locus, pick the gain, and plot the response all in one step. If we had not been ableto get a satisfactory response by choosing the gains, we could have tried a different lag controller, oreven added a lead controller.

User feedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (7 de 8) [21/11/2003 05:43:57 p.m.]

Page 134: Matlab Tutorial Ctm

Motor Speed ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/29/96 YS8/24/97 WM

CTM Example: Root Locus Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/rlocus2.html (8 de 8) [21/11/2003 05:43:57 p.m.]

Page 135: Matlab Tutorial Ctm

Example: Root Locus Design Method for DCMotor Position Control

Drawing the open-loop root locusIntegral ControlProportional plus Integral ControlProportional plus Integral plus Derivative ControlFinding the gain using the rlocfind command and plotting the closed-loopresponse

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

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (1 de 11) [21/11/2003 05:44:04 p.m.]

Page 136: Matlab Tutorial Ctm

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 gettingthose 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 locusThe 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'sfirst 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)

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 thewn term (no rise time criteria) respectively. The variable in the sigrid command is the sigma term (4/0.04seconds = 100). Run the above m-file and you should get the root locus plot below:

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (2 de 11) [21/11/2003 05:44:04 p.m.]

Page 137: Matlab Tutorial Ctm

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 systembecomes unstable. We will ignore it by changing the axes to zoom in to just the lower-gain portion of theroot 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 forsmall gains.

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (3 de 11) [21/11/2003 05:44:04 p.m.]

Page 138: Matlab Tutorial Ctm

We can see from this plot that the closed-loop poles are never fast enough to meet the settling timerequirement (that is, they never move to the left of the sigma=100 vertical line). Also, recall that we needan integrator in the controller (not just in the system) to remove steady-state error due to a disturbance.

Integral ControlNow, let's try using integral control to remove steady-state error to a disturbance. Modify your m-file soit 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)sgrid(.5,0)sigrid(100)axis([-400 100 -200 200])

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (4 de 11) [21/11/2003 05:44:04 p.m.]

Page 139: Matlab Tutorial Ctm

Note that this adds a 1/s term to the forward loop. In this m-file, we are ignoring the fast pole, and justzooming in to the low-gain portion of the root locus. Run this m-file and you will obtain the followingplot.

From this root locus we can see that the closed-loop system under integral control is never stable, andanother controller must be used.

Proportional plus Integral ControlNow, let's modify the integral controller to a PI controller. Using PI instead of I control adds a zero to theopen-loop system. We'll place this zero at s=-20. The zero must lie between the open-loop poles of thesystem in this case so that the closed-loop will be stable. Change the lines defining the controller (numcfand dencf) in your m-file to the following.

numcf=[1 20];dencf=[1 0];

Re-run your m-file and obtain the following plot.

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (5 de 11) [21/11/2003 05:44:04 p.m.]

Page 140: Matlab Tutorial Ctm

Now, we have managed to stabilize the system with zero steady-state error to a disturbance, but thesystem will still not be fast enough.

Proportional plus Integral plus Derivative ControlIn order to pull the root locus further to the left, to make it faster, we need to place a second open-loopzero, resulting in a PID controller. After some experimentation, we can place the two PID zeros at s=-60and 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.

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (6 de 11) [21/11/2003 05:44:04 p.m.]

Page 141: Matlab Tutorial Ctm

Now, we can see that two of the closed-loop poles loop around well within both the settling time andpercent overshoot requirements. The third closed loop pole moves from the open-loop pole at s=-59.2 tothe open loop zero at s=-60. This closed-loop pole nearly cancels with the zero (which remains in theclosed loop transfer function) because it is so close. Therefore, we can ignore it's effect. However, theother open-loop zero also remains in the closed-loop, and will affect the response, slowing it down, andadding overshoot. Therefore, we have to be conservative in picking where on the root locus we want theclosed-loop poles to lie.

Finding the gain using the rlocfind command

If you recall, we need the settling time and the overshoot to be as small as possible, particularly becauseof the effect of the extra zero. Large damping corresponds to points on the root locus near the real axis. Afast response corresponds to points on the root locus far to the left of the imaginary axis. To find the gaincorresponding to a point on the root locus, we can use the rlocfind command. We can find the gain andplot the step response using this gain all at once. To do this, enter the following commands at the end ofyour 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 shownbelow with the small black + marks. This will ensure that the response will be as fast as possible with aslittle overshoot as possible. These pole locations would indicate that the response would have almost noovershoot, but you must remember that the zero will add some overshoot.

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (7 de 11) [21/11/2003 05:44:04 p.m.]

Page 142: Matlab Tutorial Ctm

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 shouldat least have the same order of magnitude. You should also get the following step response plot:

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (8 de 11) [21/11/2003 05:44:04 p.m.]

Page 143: Matlab Tutorial Ctm

As you can see, the system has an overshoot of approximately 15%, a settling time of approximately 0.04seconds, and no steady-state error.

Let's now look at the disturbance response by computing the closed-loop disturbance transfer functionand 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.

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (9 de 11) [21/11/2003 05:44:04 p.m.]

Page 144: Matlab Tutorial Ctm

You can see that the disturbance reaches a steady-state value of zero, and in fact, stays within 0.02 (or2%) 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-anderror is necessary to place the zeros, it is helpful to understand how the root locus is drawn, and Matlab isused to verify and refine the placement.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (10 de 11) [21/11/2003 05:44:04 p.m.]

Page 145: Matlab Tutorial Ctm

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/7/97 JL 8/24/97 WM

CTM Example: Root Locus Design for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/rlocus2.html (11 de 11) [21/11/2003 05:44:04 p.m.]

Page 146: Matlab Tutorial Ctm

Example: Root Locus Design Method for the BusSuspension System

Plotting the root locusAdding a notch filterFinding the gain from the root locusPlotting closed-loop response

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

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (1 de 8) [21/11/2003 05:44:09 p.m.]

Page 147: Matlab Tutorial Ctm

If you are interested in running an animation of this example based on the control techniques used in the root locus tutorialplease go to the bus suspension animation page after completing this tutorial.

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 cmhigh 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 tomain 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

Therefore the dominant poles are the roots -0.1098+/-5.2504i, which are close to the complex axis with a small dampingratio.

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 addingzeros and/or poles to the original system (adding a compensator), the root locus and thus the closed-loop response will bemodified. Let's first view the root locus for the plant. In your m-file, add the following command and then run the file, youshould get the root locus plot below:

rlocus(nump,denp)z=-log(0.05)/sqrt(pi^2+(log(0.05)^2))sgrid(z,0)

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (2 de 8) [21/11/2003 05:44:09 p.m.]

Page 148: Matlab Tutorial Ctm

Note from the specification, we required the overshoot, %OS, to be less than 5% and damping ratio, zeta, can be find fromapproximation damping ratio equation, z = -log(%OS/100)/sqrt(pi^2+[log(%OS/100)^2]). The commandsgrid is used tooverlay 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 polesand zeros are almost on the imaginary axis, they might make the bus system marginally stable, which might cause aproblem. We have to make all of the poles and zeros move into the left-half plane as far as possible to avoid an unstablesystem. We have to put two zeros very close to the two poles on the imaginary axis of uncompensated system forpole-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 thecompensator zeros instead of to the plant zeros on the imaginary axis. We'll also need two poles placed far to the left to pullthe 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 30and 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 them-file, you should get a new root locus plot looking like this:

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (3 de 8) [21/11/2003 05:44:09 p.m.]

Page 149: Matlab Tutorial Ctm

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)

:

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (4 de 8) [21/11/2003 05:44:09 p.m.]

Page 150: Matlab Tutorial Ctm

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 designrequirements. Recall that we want the settling time and the overshoot to be as small as possible. Generally, to get a smallovershoot and a fast response, we need to select a gain corresponding to a point on the root locus near the real axis and farfrom the complex axis or the point that the root locus crosses the desired damping ratio line. But in this case, we need thecancellation of poles and zeros near the imaginary axis, so we need to select a gain corresponding to a point on the rootlocus near zeros and percent overshoot line. There is a method to do this with the rlocfind command in matlab. Enter thefollowing command into the Matlab command window:

[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

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (5 de 8) [21/11/2003 05:44:09 p.m.]

Page 151: Matlab Tutorial Ctm

-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 thesame 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)));

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. Clickhere for more information.

Plotting the 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 mhigh step as the disturbance. To simulate this, simply multiply numa by 0.1. Add the following commands into the m-fileand put % marks in front of all rlocus and rlocfind commands.

step(0.1*numa,dena)

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (6 de 8) [21/11/2003 05:44:09 p.m.]

Page 152: Matlab Tutorial Ctm

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 fromthe 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 want to go back to the original open-loop root locus andtry 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 thistutorial please go to the Bus Suspension Animation Page.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that youfound, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball andBeam

Bus Suspension Examples

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (7 de 8) [21/11/2003 05:44:09 p.m.]

Page 153: Matlab Tutorial Ctm

Modeling | PID | Root Locus | Frequency Response | State Space | Digital | Animation

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

6/25/97 PP8/24/97 WM

CTM Example: Root Locus Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/rlocus1.html (8 de 8) [21/11/2003 05:44:09 p.m.]

Page 154: Matlab Tutorial Ctm

Example: Solution to the inverted pendulumproblem using Root Locus method

Transfer functionsRoot locus designLead-lag controllerWhat happens to the cart's position?

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.

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (1 de 13) [21/11/2003 05:44:15 p.m.]

Page 155: Matlab Tutorial Ctm

Transfer functionsThe rlocus command in Matlab can find the root locus for a system described by state-space equationsor by a transfer function. For this problem it will be easier in the long run to use a transfer function (thereason for this will become clear later). The transfer function found from the Laplace transforms for theoutput Phi (the pendulum's angle) can be set up using Matlab by inputting the numerator anddenominator as vectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copythe 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

3. 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 initialdisturbance, the reference signal we are tracking should be zero. The force applied to the cart can beadded as an impulse disturbance. The schematic for this problem should look like the following.

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (2 de 13) [21/11/2003 05:44:15 p.m.]

Page 156: Matlab Tutorial Ctm

It will be easier to determine the appropriate transfer function to enter into Matlab if we first rearrangethe schematic as follows:

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

Root locus designThis closed-loop transfer function can be modeled in Matlab. The first thing to do is to look at the rootlocus 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 morethan 4.6/5=0.92. We can put this criteria right on the root locus using the sigrid function, which has to becopied to a m-file. To do this, copy the following code to the end of your m-file (whether your using thetransfer 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:

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (3 de 13) [21/11/2003 05:44:15 p.m.]

Page 157: Matlab Tutorial Ctm

As you can see, one of the roots of the closed-loop transfer function is in the right-half-plane. This meansthat the system will be unstable. Look back at the root locus to see why. Part of the root locus liesbetween the origin and the pole in the right-half-plane. No matter what gain you chose, you will alwayshave a closed-loop pole in this region, making your impulse response unstable. To solve this problem, weneed to add another pole at the origin so all the zeros and poles at the origin will cancel each other outand multiple roots will be created in this right-half-plane region. The multiple roots can then be drawninto 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:

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (4 de 13) [21/11/2003 05:44:15 p.m.]

Page 158: Matlab Tutorial Ctm

Now we can begin trying to draw the branches of the root locus into the left half plane. Enter thefollowing 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 Matlabcommand 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: onealong the real axis in the negative direction, and the other two at 120 degree angles from this. For thestate-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 numberof asymptotes from three to two by adding one more zero than pole the controller. If just 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.

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (5 de 13) [21/11/2003 05:44:15 p.m.]

Page 159: Matlab Tutorial Ctm

This means the two asymptotes will leave the real axis at roughly -0.1-(1/2)z2. Making z2 as small aspossible (assume near the origin) will not pull the multiple roots far enough into the left-half-plane tomeet the design requirements (asymptotes will leave at about -0.1).

Lead-lag controllerThe solution to this problem is to add another pole far to the left of the other poles and zeros. To keep theright number of asymptotes, another zero should be added as well. The placement of the added pole andzeros is not important except that the pole should be relatively large and the zeros should be relativelysmall.

Try the m-file below to see what effect the poles and zeros have on the root locus. The polyaddfunction 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];

num3 = conv(conv(num, numlead), numlag);den3 = conv(conv(den, denlead), denlag);

rlocus(num3,den3)sigrid(0.92)axis([-50 50 -50 50])figurerlocus(num3,den3)sigrid(0.92)axis([-10 10 -10 10])

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (6 de 13) [21/11/2003 05:44:15 p.m.]

Page 160: Matlab Tutorial Ctm

[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 hadto be at the origin, the other had to be far to the left, and the two zeros had to be small. Furthermore, Ifound that if the two zeros were close together and to the right of the farthest left plant pole, the responsewas better. You should see first the following root locus plot with the zeros and poles listed above:

The second plot should be of the same root locus magnified a little so that the root locus around theorigin can be seen.

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (7 de 13) [21/11/2003 05:44:15 p.m.]

Page 161: Matlab Tutorial Ctm

When prompted to pick a location on the root locus, chose a spot on the multiple roots just before theyreturn to the real axis. Your velocity response to the impulse disturbance should look similar to thefollowing:

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (8 de 13) [21/11/2003 05:44:15 p.m.]

Page 162: Matlab Tutorial Ctm

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 wasnot entirely complete. The block representing the the position was left out because that part was notbeing controlled. It would be interesting though, to see what is happening to the cart's position when thecontroller for the pendulum's angle is in place. To see this, we need to consider the actual system blockdiagram:

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

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (9 de 13) [21/11/2003 05:44:15 p.m.]

Page 163: Matlab Tutorial Ctm

The feedback loop represents the controller we have designed for the pendulum. The transfer functionfrom the cart's position to the impulse force, with the feedback controller which we designed, is given asfollows:

Recall that den1=den2 if the pole/zero at the origin that was canceled is added back in. So the transferfunction from X to F can be simplified to:

Transfer FunctionNow that we have the transfer function for the entire system, let's take a look at the response. First weneed the transfer function for the cart's position. To get this we need to go back to the laplace transformsof the system equations and find the transfer function from X(s) to U(s). Below is this transfer function:

where,

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (10 de 13) [21/11/2003 05:44:15 p.m.]

Page 164: Matlab Tutorial Ctm

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 nowden1 = den2, making calculations easier. Now, create a new m-file and run it in the commandwindow:

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

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

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (11 de 13) [21/11/2003 05:44:15 p.m.]

Page 165: Matlab Tutorial Ctm

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

If you select the point on the root locus you selected before (near the real axis), you should see thefollowing plot:

The top curve represents the pendulum's angle, and the bottom curve represents the cart's position. As

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (12 de 13) [21/11/2003 05:44:15 p.m.]

Page 166: Matlab Tutorial Ctm

you can see, the cart moves, is stabilized at near zero for almost five seconds, and then goes unstable. Itis possible that friction (which was neglected in the modeling of this problem) will actually cause thecart's position to be stabilized. Keep in mind that if this is in fact true, it is due more to luck than toanything else, since the cart's position was not included in the control design.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: Root Locus control of the inverted pendulum model

http://www.engin.umich.edu/group/ctm/examples/pend/invRL.html (13 de 13) [21/11/2003 05:44:15 p.m.]

Page 167: Matlab Tutorial Ctm

Example: Root-Locus Design method for thePitch Controller

Original root-locus plotLead compensatorQuick summary

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 pitchangle (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 plotRecall from the Root-Locus Tutorial page, a root-locus plot shows all possible closed-loop pole locationsfor a pure proportional controller. Since not all poles are acceptable, the Matlab function calledsgrid should be used to find an acceptable region of the locus. This sgrid function requires twoarguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments can be determinedfrom the rise time, the settling time, and the overshoot requirements and three equations shown below.

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (1 de 6) [21/11/2003 05:44:21 p.m.]

Page 168: Matlab Tutorial Ctm

where,

Wn=Natural frequencyzeta=Damping ratioTs=Settling timeTr=Rise timeMp=Maximum overshoot

From these three equations, we can determine that the natural frequency (Wn) must be greater than 0.9and the damping ratio (zeta) must be greater than 0.52.

Let's generate a root-locus plot and use the sgrid to find the acceptable region of the locus. Create anew 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 oneshown below:

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (2 de 6) [21/11/2003 05:44:21 p.m.]

Page 169: Matlab Tutorial Ctm

The two dotted lines in an angle indicate the locations of constant damping ratio, and the damping ratio isgreater than 0.52 in between these lines. The dotted semi-ellipse indicates the locations of constantnatural frequency, and the natural frequency is greater than 0.9 outside the semi-ellipse. As you maynoticed, there is no root-locus plotted in our desired region. We need to bring the root-locus in betweentwo dotted lines and outside the semi-ellipse by modifying the controller.

Lead compensatorWe need to shift the root-locus more toward the left to get it inside our desired region. If you refer to theDesigning Lead and Lag Compensators page, you will notice that the lead compensator can move theroot 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 isplaced at a distance 3 to 20 times the value of the zero location. Let's place the zero (Zo) at 0.9 and thepole (Zo) at 20.

This time, let Matlab functions conv and cloop determine the closed-loop transfer function with thelead compensator. Enter the following commands to an new m-file and run it in the Matlab commandwindow. You should obtain the following root-locus plot:

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (3 de 6) [21/11/2003 05:44:21 p.m.]

Page 170: Matlab Tutorial Ctm

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)

The root-locus has been generated in our desired region. Now, we are ready to pick a gain (K) andgenerate the step response corresponding to that gain. Add the following commands to the m-file shownabove and run it in the command window. You should see a prompt asking you to pick a point on theroot-locus plot. Pick a point close to the zero on the natural frequency criterion, say around -1 on realaxis. This point should give you the gain around 200. You should see a step response similar to the oneshown below.

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

de=0.2;[numc,denc]=cloop (K*num,den,-1); step (de*numc,denc)

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (4 de 6) [21/11/2003 05:44:21 p.m.]

Page 171: Matlab Tutorial Ctm

This response satisfies all the design requirements.

How to solve a problem using Root-Locus method:Quick Summary

Obtain a root-locus plot with the sgrid using the original plant transfer function.1.

Add a lead (or lag) compensator to bring the root-locus to the desired region, if necessary.2.

Pick a point on the root-locus and obtain the corresponding gain (K).3.

Generate the step response with the chosen gain (K).4.

Determine what needs to be changed from the step response.5.

Add or modify the lead (or lag or lead-lag) compensator.6.

Obtain new root-locus plot with the sgrid command active.7.

Repeat steps 3 to 7 until you obtain a satisfactory result.8.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (5 de 6) [21/11/2003 05:44:21 p.m.]

Page 172: Matlab Tutorial Ctm

Root-Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Control | Ball and Beam

Pitch Controller ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Pitch Controller -- Root-Locus method

http://www.engin.umich.edu/group/ctm/examples/pitch/RLpitch.html (6 de 6) [21/11/2003 05:44:21 p.m.]

Page 173: Matlab Tutorial Ctm

Example: Solution to the Ball & Beam ProblemUsing Root Locus Method

Open-loop Root LocusLead ControllerSelecting a GainPlotting the Closed-loop Response

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. Aschematic of the closed loop system with a controller is given below:

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (1 de 8) [21/11/2003 05:44:26 p.m.]

Page 174: Matlab Tutorial Ctm

Open-loop Root LocusThe main idea of the root locus design is to estimate the closed-loop response from the open-loop rootlocus plot. By adding zeroes and/or poles to the original system (adding a compensator), the root locusand thus the closed-loop response will be modified. Let us first view the root locus for the plant in openloop. 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:

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (2 de 8) [21/11/2003 05:44:26 p.m.]

Page 175: Matlab Tutorial Ctm

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 commandgenerates a grid of constant damping ratio and natural frequency. The damping ratio and naturalfrequency 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% ofit's final value. From these equation damping ratio and natural frequency were found to be 0.7 and 1.9respectively.

sgrid(0.70, 1.9)axis([-5 5 -2 2])

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (3 de 8) [21/11/2003 05:44:26 p.m.]

Page 176: Matlab Tutorial Ctm

The area between the two dotted diagnol lines represents locations where the percent overshoot is lessthan 5%. The area outside the curved line represents locations where the setttling time is less than 3seconds. Note that no region of the plot falls within the design criteria shown be these lines. To remedythis and bring the root locus into the left-hand plane for stability we will try adding a lead-compensator tothe system.

Lead ControllerA first order lead compensator tends to shift the root locus into the left-hand plane. For a more detaileddescription of lead compensators refer to the Lead & Lag Compensator Design page. A lead compensatorhas 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 theorigin to cancel out one of the poles. The pole of our compensator will be placed to the left of the originto pull the root locus further into the left-hand plane. Add the following lines of Matlab code to yourm-file.

zo = 0.01;po = 5;

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (4 de 8) [21/11/2003 05:44:26 p.m.]

Page 177: Matlab Tutorial Ctm

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:

Now, the branches of the root locus are within our design criteria.

Selecting a GainNow that we have moved the root locus into the left-hand plane, we may select a gain that will satisfyour design requirements. We can use the rlocfind command to help us do this. Add the followingonto 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:

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (5 de 8) [21/11/2003 05:44:26 p.m.]

Page 178: Matlab Tutorial Ctm

You should see in the Matlab command window something similar to the following (your numbers willbe 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.

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (6 de 8) [21/11/2003 05:44:26 p.m.]

Page 179: Matlab Tutorial Ctm

Plotting the Closed-loop ResponseThis value of kc can be put into the system and the closed-loop response to a step input of 0.25 m can beobtained. 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;figurestep(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 stepresponse should look like the following:

From this plot we see that when a 0.25m step input is given to the system both the settling time andpercent 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 adifferent point each time and study the effect this has on the step response. For practice you may alsowant to go back to the original open-loop root locus and try to find other ways to add zeros and poles toget a better response.

User Feedback

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (7 de 8) [21/11/2003 05:44:26 p.m.]

Page 180: Matlab Tutorial Ctm

We would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Ball & Beam ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: Root Locus Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbRL.html (8 de 8) [21/11/2003 05:44:26 p.m.]

Page 181: Matlab Tutorial Ctm

Example: Solution to the Cruise ControlProblem Using Frequency Response

Bode plot and open-loop responseProportional controllerLag controller

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 secOvershoot < 10%

Steady state error < 2%

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

Bode plot and open-loop responseThe first step in solving this problem using frequency response is to determine what open-loop transferfunction to use. Just like for the Root-Locus design method, we will only use a proportional controller tosolve the problem. The block diagram and the open-loop transfer function are shown below.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (1 de 11) [21/11/2003 05:44:52 p.m.]

Page 182: Matlab Tutorial Ctm

In order to use a Bode plot, the open-loop response must be stable. Let Kp equals 1 for now and see howthe 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)

Running this new m-file should give you the following Bode plot.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (2 de 11) [21/11/2003 05:44:52 p.m.]

Page 183: Matlab Tutorial Ctm

Proportional controllerLet's see what system characteristics we can determine from the above Bode plot. Recall from theRoot-Locus Tutorial, the bandwidth frequency (BW) (the frequency at the gain M(dB)=-6~-7.5dB) isroughly 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 donot reach -6~-7.5dB. Moreover, we see from the Root-Locus Tutorial that the damping ratio is roughlyequals 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 knowthat the system is overdamped (since the damping ratio is greater than 1). Finally, the steady-state errorcan be found from the following equation:

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (3 de 11) [21/11/2003 05:44:52 p.m.]

Page 184: Matlab Tutorial Ctm

For our system, since the low frequency gain M(dB) is approximately -35dB, the steady-state errorshould 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 thelow frequency gain increase, then both the rise time and the steady-state error will improve. We can dothat by increasing the proportional gain (Kp). Let's increase the proportional gain (Kp) to ,say, 100 andsee 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.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (4 de 11) [21/11/2003 05:44:52 p.m.]

Page 185: Matlab Tutorial Ctm

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'stake 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 theproportional gain even higher and see what happens. Change the m-file to the following and rerun it. Youshould get the following Bode plot.

m = 1000;

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (5 de 11) [21/11/2003 05:44:52 p.m.]

Page 186: Matlab Tutorial Ctm

b = 50;u = 10;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 of9%, and the bandwidth frequency is around 0.6 that predicts the rise time of 3 sec (within the desiredvalue). Thus, both the steady-state error and the rise time should have been improved. Again, let'sconfirm these by generating the closed-loop step response.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (6 de 11) [21/11/2003 05:44:52 p.m.]

Page 187: Matlab Tutorial Ctm

If you noticed, the steady-state error will eventually reach the desired value by increasing theproportional gain even higher. However, by the time the steady-state error reaches the desired value, therise time becomes too fast (unrealistic for the real physical system). Thus, let's leave the Kp as it is andimplement a lag controller to handle the steady-state error problem.

Lag controllerIf you take a look at the "Lag or Phase-Lag Compensator using Frequency Response"section of the Leadand Lag Compensator page, the lag controller adds gain at the low freqencies while keeping thebandwidth frequency at the same place. This is actually what we need: Larger low frequency gain toreduce 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 usingFrequency Response" page, the steady-state error will decrease by a factor of a. The value T should bechosen so that two corner frequencies will not be placed close together because transient response getsworse. So let a equals 0.05 and T equals 700 and see what happens. Copy the following m-file and run itin the Matlab command window. You should see the following Bode plot.

m = 1000;b = 50;u = 10;

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (7 de 11) [21/11/2003 05:44:52 p.m.]

Page 188: Matlab Tutorial Ctm

Kp=600;

numo=[Kp/b];deno=[m/b 1];

a = 0.05;T=700;numlag = [a*T 1];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, thesteady-state error should be reduced and the rise time should stay the same. Let's confirm this bygenerating a closed-loop step response.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (8 de 11) [21/11/2003 05:44:52 p.m.]

Page 189: Matlab Tutorial Ctm

It may be hard to see, but there should be a green, dotted line across just below 10. This line shows thesteady-state value of the step, and we can see that the steady-state error has been met. However, thesettling time is too long. To fix this, raise the proportional gain to Kp=1500. This gain was chosen fromtrial-and-error that will not be described here in the interest of length. With this change made, thefollowing Bode and step response plots can be generated.

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (9 de 11) [21/11/2003 05:44:52 p.m.]

Page 190: Matlab Tutorial Ctm

As you can see, the overshoot is in fact zero, the steady state error is close to zero, the rise time is about 2seconds, and the settling time is less than 3.5 seconds. The system has now met all of the designrequirements. No more iteration is needed.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (10 de 11) [21/11/2003 05:44:52 p.m.]

Page 191: Matlab Tutorial Ctm

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Frequency Response control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccFR.html (11 de 11) [21/11/2003 05:44:52 p.m.]

Page 192: Matlab Tutorial Ctm

Example: Frequency Design Method for DCMotor Speed Control

Drawing the original Bode plotAdding proportional gainPlotting the closed-loop responseAdding a lag controller

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

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling aDC Motor page.

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (1 de 8) [21/11/2003 05:44:59 p.m.]

Page 193: Matlab Tutorial Ctm

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 ofgetting 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)];

Drawing the original Bode plotThe main idea of frequency-based design is to use the Bode plot of the open-loop transfer function toestimate 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-looptransfer function. Add the following code to the end of your m-file, and then run it in the Matlabcommand window.

bode(num,den)

You should get the following Bode plot:

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (2 de 8) [21/11/2003 05:44:59 p.m.]

Page 194: Matlab Tutorial Ctm

Adding proportional gainFrom the bode plot above, we see that the phase margin can be greater than about 60 degrees if w is lessthan 10 rad/sec. Let's add gain to the system so the bandwidth frequency is 10 rad/sec, which will give usa 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 withleft-hand arguments, can also be used to give you the exact magnitude:

[mag,phase,w] = bode(num,den,10)

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:

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (3 de 8) [21/11/2003 05:44:59 p.m.]

Page 195: Matlab Tutorial Ctm

Plotting the closed-loop responseFrom the plot above we see that the phase margin is now quite large. Let's see what the closed-loopresponse look like. Add a % in front of the bode commands and add the following code to the end ofyour m-file:

[numc,denc]=cloop(num, den, -1);t=0:0.01:10;step(numc,denc,t)

You will see the following plot:

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (4 de 8) [21/11/2003 05:44:59 p.m.]

Page 196: Matlab Tutorial Ctm

The settling time is fast enough, but the overshoot and the steady-state error are too high. The overshootcan be reduced by reducing the gain a bit to get a higher phase margin, but this would cause thesteady-state error to increase. A lag controller is probably needed.

Adding a lag controllerWe can add a lag controller to reduce the steady-state error. At the same time, we should try to reduce theovershoot 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 settlingtime). 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;p=0.1;numa=[1 z];dena=[1 p];

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (5 de 8) [21/11/2003 05:44:59 p.m.]

Page 197: Matlab Tutorial Ctm

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

[numc,denc]=cloop(numb, denb, -1);t=0:0.01:10;step(numc,denc,t)

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (6 de 8) [21/11/2003 05:44:59 p.m.]

Page 198: Matlab Tutorial Ctm

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.

User feedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Speed ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (7 de 8) [21/11/2003 05:44:59 p.m.]

Page 199: Matlab Tutorial Ctm

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/28/1996 YS 8/24/97 WM

CTM Example: Frequency Design Method for DC Motor Speed Control

http://www.engin.umich.edu/group/ctm/examples/motor/freq2.html (8 de 8) [21/11/2003 05:44:59 p.m.]

Page 200: Matlab Tutorial Ctm

Example: Frequency Design Method for DCMotor Position Control

Drawing the original Bode plotAdding an integratorGain and phase margin specification and controller design

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 aDC Motor page.

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

Settling time less than 40 milliseconds●

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (1 de 15) [21/11/2003 05:45:07 p.m.]

Page 201: Matlab Tutorial Ctm

Overshoot less than 16%●

No steady-state error●

No steady-state error due to a step disturbance●

Drawing the original Bode plotCreate a new m-file and type in the following commands (refer to the main problem for the details ofgetting 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 toestimate 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-looptransfer 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (2 de 15) [21/11/2003 05:45:07 p.m.]

Page 202: Matlab Tutorial Ctm

Adding an integratorNow lets add an integrator for zero steady-state error in response to a step disturbance. Add the followinglines 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (3 de 15) [21/11/2003 05:45:07 p.m.]

Page 203: Matlab Tutorial Ctm

Gain and Phase Margin Specifications andController DesignWe want less than a 16% overshoot, so lets compute the damping ratio based on a 16% overshoot. Alsothe corresponding phase margin is 100 times the damping ratio. From the settling time requirement, weare 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.5dB at some frequency after 250 rad/sec. From the Bode plot we see that we must add about 80 degrees ofphase and 60 dB of gain at a frequency of 250 rad/sec. The gain plot will then lie between -6 and -7.5 dBand after 244 rad/sec. From the Bode phase plot we can see that there is a pole near 60 rad/sec. We willuse a PI controller to put a zero at s=60 to flatten out the phase curve. Add the following lines to yourm-file:

numpi=[1 60];denpi=[1 0];numpiol=conv(numpi,num);

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (4 de 15) [21/11/2003 05:45:07 p.m.]

Page 204: Matlab Tutorial Ctm

denpiol=conv(denpi,den);bode(numpiol,denpiol,w)

You should see the following plot:

From the bode plot we can see that we need 50 more degrees of phase at a frequency of 250 rad/sec. Letsnow 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)

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (5 de 15) [21/11/2003 05:45:07 p.m.]

Page 205: Matlab Tutorial Ctm

This new Bode plot now shows that the phase margin is about right at 250 rad/sec, but the gain is toosmall by about 20 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up by 20 dB wewill 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (6 de 15) [21/11/2003 05:45:07 p.m.]

Page 206: Matlab Tutorial Ctm

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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (7 de 15) [21/11/2003 05:45:07 p.m.]

Page 207: Matlab Tutorial Ctm

The overshoot is now too large, however the settling time is better than expected. So let's try anotherdesign 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (8 de 15) [21/11/2003 05:45:07 p.m.]

Page 208: Matlab Tutorial Ctm

This new bode plot shows that the phase margin is good at around 250 rad/sec, but the gain is too smallby about 14 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up we will multiply by again of 5. Add the following lines to your m-file:

kpid = 5;bode(kpid*numpilol,denpilol,w)

You should get the following plot:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (9 de 15) [21/11/2003 05:45:07 p.m.]

Page 209: Matlab Tutorial Ctm

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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (10 de 15) [21/11/2003 05:45:07 p.m.]

Page 210: Matlab Tutorial Ctm

From the step response we now see that the overshoot is fine, but the settling time is too long. Let's try aslightly 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (11 de 15) [21/11/2003 05:45:07 p.m.]

Page 211: Matlab Tutorial Ctm

This new bode plot shows that the phase margin is about right at a frequency of 250 rad/sec, but the gainis too small by about 18 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up we willmultiply 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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (12 de 15) [21/11/2003 05:45:07 p.m.]

Page 212: Matlab Tutorial Ctm

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:

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (13 de 15) [21/11/2003 05:45:07 p.m.]

Page 213: Matlab Tutorial Ctm

Now everything looks good. We have less than 16% overshoot and a settling time of about 40milliseconds.

Note: As you noticed, the frequency response method for this particular problem requires substantialamount of trial and error runs. The m-file below is the simplified version of what was done above. Afteryou 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));

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (14 de 15) [21/11/2003 05:45:07 p.m.]

Page 214: Matlab Tutorial Ctm

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)

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 BRN

CTM Example: Frequency Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/freq2.html (15 de 15) [21/11/2003 05:45:07 p.m.]

Page 215: Matlab Tutorial Ctm

Example: Frequency Design Method for the BusSuspension System

Plotting the frequency response using the bode commandAdding a two-lead controllerPlotting the closed-loop response

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

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (1 de 8) [21/11/2003 05:45:13 p.m.]

Page 216: Matlab Tutorial Ctm

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;

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 theclosed-loop response. Adding a controller to the system changes the open-loop Bode plot so that the closed-loop responsewill also change. Let's first draw the Bode plot for the original open-loop transfer function. Add the following line of code toyour m-file and rerun:

w = logspace(-1,2);bode(nump,denp,w)

You should get the following bode plot:

For convenience in representing systems with different natural frequencies of the system, we normalize and scale our

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (2 de 8) [21/11/2003 05:45:13 p.m.]

Page 217: Matlab Tutorial Ctm

finding before plotting the Bode plot, so that the low-frequency asymptote of each term is at 0 dB. This normalization byadjusting the gain, K, makes it easier to add the components of the Bode plot. The effect of K is move the magnitude curveup (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.1rad/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 phasearound this region, so that the phase will remain above the -180 degree line. Since a large phase margin leads to a smallovershoot, we will want to add at least 140 degrees of positive phase at the area near 5 rad/sec. Since one lead controller canadd no more than +90 degrees, we will use a two-lead controller:

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 polefor the desired maximum phase added.

4: Determine T and aT from the following equations, these determine the corner frequencies so that the maximum phasewill be added at the desired frequency.

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (3 de 8) [21/11/2003 05:45:13 p.m.]

Page 218: Matlab Tutorial Ctm

Now let's put our 2-Lead controller into the system and see what the Bode plot looks like. Add the following code to yourm-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:

Since the Bode plot has a limited phase range (-360-0), the above plot is a little deceiving. The plot is equivalent to thefollowing:

w=logspace(-4,4);[mag,phase,w] = bode(conv(nump,numc),conv(denp,denc),w);subplot(2,1,1);semilogx(w,20*log10(mag));gridtitle('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])gridxlabel('Frequency (rad/s)')ylabel('Phase (degrees)')

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (4 de 8) [21/11/2003 05:45:13 p.m.]

Page 219: Matlab Tutorial Ctm

From this plot we see that the concave portion of the phase plot is above -180 degrees now, and the phase margin is largeenough for the design criteria. Let's see how the output (the distance X1-X2) responds to a bump on the road (W). Recallthat the schematic of the system is:

and the closed-loop transfer function can be derived as follows:

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (5 de 8) [21/11/2003 05:45:13 p.m.]

Page 220: Matlab Tutorial Ctm

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. Clickhere for more information.

Refer to the bus modeling page, nump = denf as we can see in the matlab command above. Thus we can simplified thistransfer 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. Tosimulate 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:

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (6 de 8) [21/11/2003 05:45:13 p.m.]

Page 221: Matlab Tutorial Ctm

The amplitude of response is a lot smaller than the percent overshoot requirement and the settling time also is less than 5seconds. Since we can see that an amplitude of the output's response less than 0.0001 m or 1% of input magnitude after 4seconds. Therefore we can say that the settling time is 4 seconds from the above plot. From the Bode plot above, we see thatincreasing the gain will increase the crossover frequency and thus make the response faster. We will increase the gain andsee if we can get a better response. Go back to your m-file and change numc to numc=4*conv([3.14831],[3.1483 1]). Rerun the m-file and you should get the following plot:

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (7 de 8) [21/11/2003 05:45:13 p.m.]

Page 222: Matlab Tutorial Ctm

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 alsoless than 5 seconds. This response is now satisfactory and no more design iteration is needed.

User FeedbackWe would liketo hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that youfound, or any other comments that you have. This feedback is anonymous.

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball andBeam

Bus Suspension ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital

TutorialsBasics | Modeling | Starting | Root Locus | Frequency Response | State Space | Examples

6/25/97 PP8/24/97 WM

CTM Example: Frequency Response Design Method For Bus Suspension System

http://www.engin.umich.edu/group/ctm/examples/susp/freq1.html (8 de 8) [21/11/2003 05:45:13 p.m.]

Page 223: Matlab Tutorial Ctm

Example: Solution to the Inverted PendulumProblem Using Frequency Response Method

Open-loop RepresentationClosed-loop response with no compensationClosed-loop response with compensationWhat happens to the cart's position?

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 wereremoved from the transfer function. However, in this example they will be left in for reasons that willbecome clear later.

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

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (1 de 17) [21/11/2003 05:45:21 p.m.]

Page 224: Matlab Tutorial Ctm

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 problemto solve with the frequency response method. As you will soon find out, this problem has a pole in theright-half-plane, making it unstable. The frequency response method works best when the system isstable in the open loop. For this reason I would not suggest trying to follow this example if you are tryingto learn how to use frequency response. This problem is for people who want to learn how to solvefrequency response problems that are more complicated.

Open-loop RepresentationThe frequency response method uses the bode command in Matlab to find the frequency response for asystem 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 beset 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

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (2 de 17) [21/11/2003 05:45:21 p.m.]

Page 225: Matlab Tutorial Ctm

Closed-loop response with no compensationWe will now design an inverted pendulum controller for an impulse force using Nyquist diagrams (wecannot use Bode plots because the system is unstable in open loop). Let's begin by looking at the blockdiagram for this system:

If you try to model this system in Matlab, you will have all sorts of problems. The best way to useMatlab to solve this problem is to first change the schematic to something that can be modeled muchmore 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 inthe right-half plane. This means that we will need one anti-clockwise encirclement of -1 in order to havea stable closed-loop system (Z = P + N; P = 1, N = -1). The following m-file will be very useful fordesigning our controller. Please note that you will need the function polyadd.m to run this m-file. Copyand paste the function from your browser to a m-file in your directory (make sure the function command

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (3 de 17) [21/11/2003 05:45:21 p.m.]

Page 226: Matlab Tutorial Ctm

starts in the first column of the m-file).

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

function[ ] = pend()

clffigure(1)clf

%define TFnum = [4.5455 0 0];den =[1.0000 0.1818 -31.1818 -4.4545 0];figure(1)

%ask user for controllernumc = input('numc?.........');denc = input('denc?.........');k = input('K?............');

%view compensated system bodebode(k*conv(numc,num), conv(denc,den))

%view compensated system nyquistfigure(2)subplot (2,1,1)nyquist(k*conv(numc,num), conv(denc,den))

%view compensated CL system impulse responsesubplot(2,1,2)clnum = conv(num,denc);temp1 = k*conv(numc,num);temp2 = conv(denc, den);clden = polyadd(temp1,temp2);impulse (clnum,clden)

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 controllernumerator, denominator and gain equal to one. Enter pend at the command prompt, and enter 1 fornumc, denc, and K. You should see the following plots in your screen:

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (4 de 17) [21/11/2003 05:45:21 p.m.]

Page 227: Matlab Tutorial Ctm

numc?.........1 denc?.........1 K?............1

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (5 de 17) [21/11/2003 05:45:21 p.m.]

Page 228: Matlab Tutorial Ctm

Closed-loop response with compensationThe system is unstable in closed loop (no encirclements of -1). Our first step will be to add an integratorto cancel the extra zero at the origin (we will then have two poles and two zeros at the origin). Use thepend command again.

numc?.........1 denc?.........[1 0] K?............1

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (6 de 17) [21/11/2003 05:45:21 p.m.]

Page 229: Matlab Tutorial Ctm

Notice that the nyquist diagram encircles the -1 point in a clockwise fashion. Now we have two poles inthe right-half plane (Z= P + N = 1 + 1). We need to add phase in order to get an anti-clockwise

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (7 de 17) [21/11/2003 05:45:21 p.m.]

Page 230: Matlab Tutorial Ctm

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

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (8 de 17) [21/11/2003 05:45:21 p.m.]

Page 231: Matlab Tutorial Ctm

as you can see, this wasn't enough phase. The encirclement around -1 is still clockwise. We are going toneed to add a second zero.

numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............1

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (9 de 17) [21/11/2003 05:45:22 p.m.]

Page 232: Matlab Tutorial Ctm

We still have one clockwise encirclement of the -1 point. However, if we add some gain, we can makethe system stable by shifting the nyquist plot to the left, moving the anti-clockwise circle around -1, so

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (10 de 17) [21/11/2003 05:45:22 p.m.]

Page 233: Matlab Tutorial Ctm

that N = -1. help impulse

numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............10

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (11 de 17) [21/11/2003 05:45:22 p.m.]

Page 234: Matlab Tutorial Ctm

As you can see, the system is now stable. We can now concentrate on improving the response. We canmodify the poles of the controller in order to do this. We have to keep in mind that small poles (close tothe origin) will affect the response at small frequencies, while larger poles (farther from the origin) willaffect the response at high frequencies. When designing via frequency response, we are interested inobtaining 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 thecommand axis([0 5 -0.05 0.1]) after the pend command.)

numc?.........conv([1 1.1],[1 5]) denc?.........[1 0] K?............10

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (12 de 17) [21/11/2003 05:45:22 p.m.]

Page 235: Matlab Tutorial Ctm

Our response has met our design goals. Feel free to vary the parameters and observe what happens.

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (13 de 17) [21/11/2003 05:45:22 p.m.]

Page 236: Matlab Tutorial Ctm

What happens to the cart's position?At the beginning on this solution page, the block diagram for this problem was given. The diagram wasnot entirely complete. The block representing the the position was left out because that variable was notbeing controlled. It is interesting though, to see what is happening to the cart's position when thecontroller for the pendulum's angle is in place. To see this we need to consider the actual system blockdiagram:

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

The feedback loop represents the controller we have designed for the pendulum. The transfer functionfrom the cart's position to the impulse force, with the frequency response feedback controller which wedesigned, is given as follows:

Recall that den1=den2 (the two transfer functions G1 and G2 differ in numerator alone), so the transferfunction from X to F can be simplified to:

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (14 de 17) [21/11/2003 05:45:22 p.m.]

Page 237: Matlab Tutorial Ctm

Transfer FunctionNow that we have the transfer function for the entire system, let's take a look at the response. First weneed the transfer function for the cart's position. To get this we need to go back to the laplace transformsof 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 nowden1 = den2, making calculations easier. Now, create a new m-file and run it in the commandwindow:

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;

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (15 de 17) [21/11/2003 05:45:22 p.m.]

Page 238: Matlab Tutorial Ctm

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)

As you can see, the cart moves in the negative direction and stabilizes at about -0.18 meters. This designmight 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, andthe fact that we have is a fortunate side effect.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (16 de 17) [21/11/2003 05:45:22 p.m.]

Page 239: Matlab Tutorial Ctm

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: Frequency Response Control of the Inverted Pendulum Model

http://www.engin.umich.edu/group/ctm/examples/pend/invFR.html (17 de 17) [21/11/2003 05:45:22 p.m.]

Page 240: Matlab Tutorial Ctm

Example: Frequency Response Design methodfor the Pitch Controller

Open-loop responseLead compensatorLag compensator

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

Open-loop responseRecall from your control textbook that the frequency response design method is most effective forsystems with stable open-loop. To check the open-loop stability of our system, create a new m-file, andenter the following commands. Running this m-file in the Matlab command window should give you thestep response shown below:

de=0.2;

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (1 de 11) [21/11/2003 05:45:28 p.m.]

Page 241: Matlab Tutorial Ctm

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 viafrequency response method (even though this might not be the easiest way). First, let's generate theopen-loop Bode plot and see what it looks like. Change the m-file to the following and re-run it in theMatlab command window. You should see a Bode plot similar to the one shown below:

num=[1.151 0.1774];den=[1 0.739 0.921 0];bode (num,den)

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (2 de 11) [21/11/2003 05:45:28 p.m.]

Page 242: Matlab Tutorial Ctm

From our design requirements, we can determine that the natural frequency (Wn) must be greater than0.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 bandwidthfrequency and the phase margin must be greater than 0.9 and 52 degrees, respectively.

● Tr = Rise time

● Wn = Natural frequency

● BW = Bandwidth frequency

● zeta = Damping ratio

● PM = Phase margin

Currently, we have the bandwidth frequency of 1 rad/sec and the phase margin of 80 degrees. Thesevalues are within our desired region. Let's plot the closed-loop step response and see what it looks like.Delete the bode command from the above m-file and add the following commands. Running this newm-file should give you the following closed-loop step response:

[numc,denc]=cloop(num,den,-1);de=0.2;t=0:0.01:10;

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (3 de 11) [21/11/2003 05:45:28 p.m.]

Page 243: Matlab Tutorial Ctm

step (de*numc,denc,t)

As you can see, the transient response is worse that results in long settling time. We will implement alead compensator to improve the system response.

Lead CompensatorReferring to the "Lead or phase-lead compensator using frequency response" section of Lead and LagCompensator page, a lead compensator will add a positive phase to the system. An additional positivephase increases the phase margin; thus, increase damping. The settling time should decrease as a result ofthis increased damping.

The transfer function of a typical first-order lead compensator is

We need to find alead, Tlead and Klead. First, the phase margin requirement and the following equationcan be used to find alead

Since we are required to have the phase margin of greater than 52 degrees, the alead must be greater than8.43. Using this alead, the bandwidth frequency requirement of greater than 0.9 and the followingequation leads us to have the Tlead of smaller than 0.382.

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (4 de 11) [21/11/2003 05:45:28 p.m.]

Page 244: Matlab Tutorial Ctm

Let the Klead equal 0.1, alead equal 10, and Tlead equal 0.3 for now and enter the following commandsto 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;figurestep (de*numc,denc,t)

Running this m-file in the Matlab command window gives you the following Bode and step responseplots.

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (5 de 11) [21/11/2003 05:45:28 p.m.]

Page 245: Matlab Tutorial Ctm

Although both bandwidth frequency and phase margin increased, the response still does not satisfy thedesign requirements. Let's increase alead and decrease Tlead. After several trial and error runs, an aleadof 200, Tlead of 0.0025, and Klead of 0.05 , were found which gave the following lead compensator,

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (6 de 11) [21/11/2003 05:45:28 p.m.]

Page 246: Matlab Tutorial Ctm

provided the desired transient response. To see the step response and the corresponding Bode plot, enterthe following commands to an m-file and run it in the command window. You should see both the Bodeplot 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;figurestep (de*numc,denc,t)

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (7 de 11) [21/11/2003 05:45:28 p.m.]

Page 247: Matlab Tutorial Ctm

If you compare the above Bode plot to the original Bode plot, you see both the phase margin and thebandwidth frequency have increased. Increasing both of them improves the rise time, the overshoot, andthe settling time, as seen in the above step response plot. To improve the steady-state error, we will add alag compensator to the system.

Lag compensatorReferring to the "Lag or phase-lag Compensator using frequency response" section of Lead and LagCompensator page, a lag compensator reduces the steady-state error. The typical first-order transferfunction of a lead compensator is

The steady-state error will be reduced by a factor of alag. From the above step response, we see that thesteady-state error is roughly 10%. Thus, alag needs to be approximately 0.1. The Tlag should be greaterthan 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 thefollowing lag compensator,

provided the desired response. To see the step response and the corresponding Bode plot, enter the

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (8 de 11) [21/11/2003 05:45:28 p.m.]

Page 248: Matlab Tutorial Ctm

following commands to an new m-file. Running this m-file in the command window should give you thetwo 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];

num2=conv(num1,numlag);den2=conv(den1,denlag);

bode (num2,den2)

[numc2,denc2]=cloop(num2,den2,-1);figurestep (0.2*numc2,denc2,t)

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (9 de 11) [21/11/2003 05:45:28 p.m.]

Page 249: Matlab Tutorial Ctm

If you see the Bode plot, the low frequency gain has increased while keeping the bandwidth frequencythe same. This tells us that steady-state error has reduced while keeping the same rise time. The abovestep response shows that the steady-state error got eliminated. Now all design requirements are satisfied.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (10 de 11) [21/11/2003 05:45:28 p.m.]

Page 250: Matlab Tutorial Ctm

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Control | Ball and Beam

Pitch Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Pitch Controller -- Frequency Response design method

http://www.engin.umich.edu/group/ctm/examples/pitch/FRpitch.html (11 de 11) [21/11/2003 05:45:28 p.m.]

Page 251: Matlab Tutorial Ctm

Example: Solution to the Ball & Beam ProblemUsing Frequency Response Method

Open-loop Bode PlotPhase-Lead ControllerAdding More Phase

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. Aschematic of the closed loop system with a controller is given below:

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (1 de 11) [21/11/2003 05:45:35 p.m.]

Page 252: Matlab Tutorial Ctm

Open-loop Bode PlotThe main idea of frequency based design is to use the Bode plot of the open-loop transfer function toestimate 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-looptransfer function. Create an m-file with the following code and then run it in the Matlab commandwindow:

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];

bode(num,den)

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

You should get the following Bode plot:

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (2 de 11) [21/11/2003 05:45:35 p.m.]

Page 253: Matlab Tutorial Ctm

From this plot we see that the phase margin is zero. Since the phase margin is defined as the change inopen-loop phase shift necessary to make a closed-loop system stable this means that our zero phasemargin indicates our system is unstable. We want to increase the phase margin and we can use a leadcompensator controller to do this. For more information on Phase and Gain margins please refer to theFrequency Response Tutorial.

Phase-Lead ControllerA 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 and1/T, which are called the corner frequencies. The maximum added phase for one lead compensator is 90degrees. 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 aflat line. However, we have a relation between bandwidth frequency (wbw) and settling time (referto the Bandwidth Frequency page for this equation) which tells us that wbw is approximately 1.92rad/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 betweenthe zero and the pole for the maximum phase added.

where phi refers to the desired phase margin. For 70 degrees, a = 0.0311.

4. Determine "T" and "aT" from the following equations:

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (3 de 11) [21/11/2003 05:45:35 p.m.]

Page 254: Matlab Tutorial Ctm

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

You can see that our phase margin is now 70 degrees. Let's check the closed-loop response to a step inputof 0.25m. Add the following to your m-file:

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (4 de 11) [21/11/2003 05:45:35 p.m.]

Page 255: Matlab Tutorial Ctm

[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 notsatisfactory. Increasing the gain will increase the crossover frequency and make the response faster.Make k = 5, your response should look like:

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (5 de 11) [21/11/2003 05:45:35 p.m.]

Page 256: Matlab Tutorial Ctm

The response is faster, however, the overshoot is much too high. Increasing the gain further will justmake the overshoot worse.

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (6 de 11) [21/11/2003 05:45:35 p.m.]

Page 257: Matlab Tutorial Ctm

Adding More PhaseWe can increase our phase-lead compensator to decrease the overshoot. In order to make the iterativeprocess easier use the following program. Create an m-file and copy the function from your web-browserinto it (make sure the function command starts in the first column of the m-file).

function[ ] = phaseball()

%define TFm = 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];

%ask user for controller informationpm = input('Phase Margin?.......');w = input('Center Frequency?...');k = input('Gain?...............');

%view compensated system bode plotpmr = 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);figurebode(numl,denl)

%view step response[numcl,dencl]=cloop(numl,denl);t=0:0.01:5;figurestep(0.25*numcl,dencl,t)

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (7 de 11) [21/11/2003 05:45:35 p.m.]

Page 258: Matlab Tutorial Ctm

With this m-file you can choose the phase margin, center frequency, and gain. Run your m-file with thefollowing values and you should see the plots below on your screen.

Phase Margin?.......80Center Frequency?...1Gain?...............1

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (8 de 11) [21/11/2003 05:45:35 p.m.]

Page 259: Matlab Tutorial Ctm

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?.......85Center Frequency?...1.9Gain?...............2

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (9 de 11) [21/11/2003 05:45:35 p.m.]

Page 260: Matlab Tutorial Ctm

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 addedphase, gain, or center frequency.

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (10 de 11) [21/11/2003 05:45:35 p.m.]

Page 261: Matlab Tutorial Ctm

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Frequency Response ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Ball & Beam ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: Frequency Response Control of the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbFR.html (11 de 11) [21/11/2003 05:45:35 p.m.]

Page 262: Matlab Tutorial Ctm

Example: Solution to the Cruise ControlProblem Using State Space

Control design using pole placementReference input

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 secOvershoot < 10%

Steady state error < 2%

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

Control design using pole placementThe schematic of a full-state feedback system is shown below.

CTM Example: State Space control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccSS.html (1 de 5) [21/11/2003 05:45:46 p.m.]

Page 263: Matlab Tutorial Ctm

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 obtainthe desired output. Poles of a closed-loop system can be found from the characteristic equation: thedeterminate of [sI-(A-B*K)] matrix. If desired poles can be placed into the system by designing rightcontrol 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 andenter 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

CTM Example: State Space control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccSS.html (2 de 5) [21/11/2003 05:45:46 p.m.]

Page 264: Matlab Tutorial Ctm

following step response.

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

Reference inputOnce 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 commandrscale 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 thefollowing commands to an m-file and run it in the Matlab command window. You should get the stepresponse shown below.

m=1000;b=50;t=0:0.1:10;u=500*ones(size(t));

CTM Example: State Space control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccSS.html (3 de 5) [21/11/2003 05:45:46 p.m.]

Page 265: Matlab Tutorial Ctm

A=[-b/m];B=[1/m];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 theovershoot is, in fact, zero. All the design requirements are satisfied.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

CTM Example: State Space control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccSS.html (4 de 5) [21/11/2003 05:45:46 p.m.]

Page 266: Matlab Tutorial Ctm

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: State Space control of the cruise control model

http://www.engin.umich.edu/group/ctm/examples/cruise/ccSS.html (5 de 5) [21/11/2003 05:45:46 p.m.]

Page 267: Matlab Tutorial Ctm

Example: A State-Space Controller for DCMotor Speed

Designing the full-state feedback controllerAdding a reference input

From the main problem, the dynamic equations in state-space form are the following:

For the original problem setup and the derivation of the above equations, please refer to the Modeling aDC 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 ofgetting these commands).

J=0.01;b=0.1;K=0.01;R=1;L=0.5;A=[-b/J K/J

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (1 de 6) [21/11/2003 05:45:53 p.m.]

Page 268: Matlab Tutorial Ctm

-K/L -R/L];B=[0 1/L];C=[1 0];D=0;

Designing the full-state feedback controllerSince both of the state variables in our problem are very easy to measure (simply add an ammeter forcurrent and a tachometer for the speed), we can design a full-state feedback controller for the systemwithout worrying about having to add an observer. The schematic for a full-state feedback system is:

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 2poles for the system. By designing a full-state feedback controller, we can move these two polesanywhere we want them. We shall first try to place them at -5 + i and -5-i (note that this corresponds to azeta = 0.98 which gives 0.1% overshoot and a sigma = 5 which leads to a 1 sec settling time). Once wecome up with the poles we want, Matlab will find the controller matrix,K, for us. Simply add thefollowing 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, thestate-space equations become:

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (2 de 6) [21/11/2003 05:45:53 p.m.]

Page 269: Matlab Tutorial Ctm

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:

Adding a reference inputFrom 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 feedingback both states. We need to compute what the steady-state value of the states should be, multiply that bythe chosen gain K, and use this new value as our reference for computing the input. This can be done inone step by adding a constant gain Nbar after the reference:

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (3 de 6) [21/11/2003 05:45:53 p.m.]

Page 270: Matlab Tutorial Ctm

We can find this Nbar factor by using the Matlab command rscale:

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 youuse it. Click here for more information. Now we can plot the step response by adding the following lineof 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')

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (4 de 6) [21/11/2003 05:45:53 p.m.]

Page 271: Matlab Tutorial Ctm

This time, the steady-state error is much less than 1%, and all the other design criteria have been met aswell.

User feedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Speed ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (5 de 6) [21/11/2003 05:45:53 p.m.]

Page 272: Matlab Tutorial Ctm

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/28/1996 YS8/24/97 WM

CTM Example: State Space Design Method for DC Motor Speed

http://www.engin.umich.edu/group/ctm/examples/motor/ss2.html (6 de 6) [21/11/2003 05:45:53 p.m.]

Page 273: Matlab Tutorial Ctm

Example: A State-Space Controller for DCMotor Position Control

Designing the full-state feedback controllerDisturbance ResponseAdding Integral Action

From the main problem, the dynamic equations in state-space form are the following:

For the original problem setup and the derivation of the above equations, please refer to the ModelingDC 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 ofgetting those commands).

J=3.2284E-6; b=3.5077E-6;

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (1 de 8) [21/11/2003 05:45:59 p.m.]

Page 274: Matlab Tutorial Ctm

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 controllerSince all of the state variables in our problem are very easy to measure (simply add an ammeter forcurrent, a tachometer for speed, and a potentiometer for position), we can design a full-state feedbackcontroller for the system without worrying about having to add an observer. The schematic for afull-state feedback system is:

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 3poles for the system. By designing a full-state feedback controller, we can move these three polesanywhere we want them. We shall first try to place them at -100 + 100i and -100-100i (note that thiscorresponds to a zeta = 0.5 which gives 0.16% overshoot and a sigma = 100 which leads to a .04 secsettling time). Once we come up with the poles we want, Matlab will find the controller matrix,Kc, forus. 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

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (2 de 8) [21/11/2003 05:45:59 p.m.]

Page 275: Matlab Tutorial Ctm

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.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 ResponseIn order to get the disturbance response, we must provide the proper input to the system. Physically, adisturbance is a torque which acts on the inertia of the motor. A torque acts as an additive term in thesecond state equation (which gets divided by J, as do all the other terms in this equation). We cansimulate this simply by modifying our closed loop input matrix, B, to have a 1/J in the second row. Addthe following line to your m-file and re-run.

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (3 de 8) [21/11/2003 05:45:59 p.m.]

Page 276: Matlab Tutorial Ctm

step(A-B*Kc,[0;1/J;0],C,D,1,t)

This is not a zero steady-state error to a disturbance, and we will have to compensate for this.

Adding Integral ActionWe know that if we put an extra integrator in series with the plant it can remove steady-state error to aninput. If the integrator comes before the injection of the disturbance, it will cancel the disturbance insteady state. This changes our control structure so it now resembles the following:

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (4 de 8) [21/11/2003 05:45:59 p.m.]

Page 277: Matlab Tutorial Ctm

We can model the integrator by augmenting our state equations with an extra state which is the integralof 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 the system before theintegrator, 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 thematrices in this equation as Aa, Ba, Ca, and Da. We will refer to the state vector of the augmented systemas xa. Note that the reference, r, does not affect the states (except the integrator state) or the output of theplant - this is expected, since there is no path from the reference to the plant input, u, withoutimplementing 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 thiscase, 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 zeroadded as a first row. Since u=Kc*xa is the input to the plant for the closed loop, but r is the input to theclosed loop system, the closed loop equations will depend on both Bau and Ba. The closed loop equationswill become:

Now, the integral of the output will be fed back, and will be used by the controller to remove steady stateerror to a disturbance. We can now redesign our controller. Since we need to place one closed-loop polefor each pole in the plant, we will place another pole at -300, which will be faster than the rest of thepoles. Since the closed-loop system matrix depends on Bau, we will use Bau in the place command ratherthat Ba. Add the following to your m-file:

Aa=[0 1 0 0 0 0 1 0 0 0 -b/J K/J

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (5 de 8) [21/11/2003 05:45:59 p.m.]

Page 278: Matlab Tutorial Ctm

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 simulatingthe disturbance response.

step(Aa-Bau*Kc,[0 ; 0 ; 1/J ; 0] ,Ca,Da,1,t)

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (6 de 8) [21/11/2003 05:45:59 p.m.]

Page 279: Matlab Tutorial Ctm

We can see that all of the design specifications have been met by this controller.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

Motor Position Examples

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (7 de 8) [21/11/2003 05:45:59 p.m.]

Page 280: Matlab Tutorial Ctm

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control:RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/22/1997 JL8/24/1997 WM

CTM Example: State Space Design Method for DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/ss2.html (8 de 8) [21/11/2003 05:45:59 p.m.]

Page 281: Matlab Tutorial Ctm

Example: A State-space Controller for a Bus SuspensionSystem

Designing the full-state feedback controllerPlotting the closed-loop response

From the main problem, the dynamic equations in state-space form are the following:

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, thebus 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 mainproblem for the details of getting those commands). We need to define the A, B, C, D matrices by entering the following into them-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];

CTM Example: State Space Design for Bus Suspension

http://www.engin.umich.edu/group/ctm/examples/susp/ss1.html (1 de 4) [21/11/2003 05:46:05 p.m.]

Page 282: Matlab Tutorial Ctm

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];

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 (thisassumption 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 2matrix, 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 statesbecome 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 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.

CTM Example: State Space Design for Bus Suspension

http://www.engin.umich.edu/group/ctm/examples/susp/ss1.html (2 de 4) [21/11/2003 05:46:05 p.m.]

Page 283: Matlab Tutorial Ctm

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 isobtained from the new state. The proportional control is obtained from a gain on Y1 or X1-X2. The direct derivative control for theoutput isn't possible, since derivative of Y1 or X1-X2 isn't a state. Instead we use the derivative of X1 , which is available forfeedback. (While X1 maybe hard to measure, could be obtained by integrating the output of an accelerometer mounted on thebus.) It is similar to adding more damping to velocity of oscillation of the bus. Add the following matlab code for controller K inthe 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 wepreviously 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:

We can now obtain the closed-loop response by simply adding the following code into your m-file. Note that we need to multiply Bmatrix 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:

CTM Example: State Space Design for Bus Suspension

http://www.engin.umich.edu/group/ctm/examples/susp/ss1.html (3 de 4) [21/11/2003 05:46:05 p.m.]

Page 284: Matlab Tutorial Ctm

From the plot we see that the percent overshoot and settling time requirements are satisfied. Moreover the steady-state errorapproaches zero as well. Therefore, we will determine that the response is satisfactory. Feel free to play around with the gain formatrix K. But you will most likely get the response to have either large percent overshoot or very long settling time. But if you dofind a better response, please email us with your results! We are always interested in different ways to solve our examples; we mayinclude your solution in a future version of these tutorials.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that you found, orany other comments that you have. This feedback is anonymous.

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

6/25/97 PP 8/24/97 WM

CTM Example: State Space Design for Bus Suspension

http://www.engin.umich.edu/group/ctm/examples/susp/ss1.html (4 de 4) [21/11/2003 05:46:05 p.m.]

Page 285: Matlab Tutorial Ctm

Example: State-space design for the invertedpendulum

Open-loop polesLQR designAdding the reference InputObserver design

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 designcriteria for this example are different. In the other other examples we were dealing with an impulse andnot a step input. Also, we were only concerned with the pendulums angle and disregarded the cart'sposition in the design of the controller. However, for an inverted pendulum it is unrealistic to considerjust the single output system. Using state-space methods it is relatively simple to work with a

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (1 de 11) [21/11/2003 05:46:12 p.m.]

Page 286: Matlab Tutorial Ctm

multi-output system, so in this example we will design a controller with both the pendulum angle and thecart 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 isshown below:

If you are interested in running an animation of this example based on the control techniques used in thestate-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 andvelocity of the cart and the angle and angular velocity of the pendulum. The output y contains both theposition of the cart and the angle of the pendulum. We want to design a controller so that when an stepinput is given to the system, the pendulum should be displaced, but eventually return to zero (i.e. thevertical) and the cart should move to it's new commanded position. To view the system's open-loopresponse 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. Enterthe 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; 0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (2 de 11) [21/11/2003 05:46:12 p.m.]

Page 287: Matlab Tutorial Ctm

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 thesystem 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 canmeasure all four states), and find the vector K which determines the feedback control law. This can bedone in a number of ways. If you know the desired closed-loop poles, you can use the place or ackercommand. Another option is to use the lqr function; this will give you the optimal controller (undercertain assumptions; consult your textbook for more details). The lqr function allows you to choose twoparameters, R and Q, which will balance the relative importance of the input and state in the costfunction that you are trying to optimize. The simplest case is to assume R=1, and Q=C'*C. You maynotice that we are using both outputs (the pendulum's angle and the cart's position). Essentially, the lqrmethod allows for the control of both outputs. In this case, it is pretty easy to do. The controller can betuned by changing the nonzero elements in the Q matrix to get a desirable response.

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

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (3 de 11) [21/11/2003 05:46:12 p.m.]

Page 288: Matlab Tutorial Ctm

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,3position will be used to weight the pendulum's angle. The input weighting R will remain at 1. Now thatwe know what the Q matrix should look like we can experiment to find the K matrix that will give us agood controller. We will go ahead and find the K matrix and plot the response all in one step so thatchanges can be made in the control and be seen automatically in the response. Enter the following textinto 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;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

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (4 de 11) [21/11/2003 05:46:12 p.m.]

Page 289: Matlab Tutorial Ctm

The curve in green represents the pendulum's angle, in radians and the curve in blue represents the cart'sposition in meters. As you can see, this plot is not satisfactory. The pendulum and cart's overshoot appearfine, but their settling times need improvement and the cart's rise time needs to go down. As I'm sure youhave noticed the cart is not near the desired location but has in fact moved in the other direction. Thiserror will be dealt with in the next section and right now we will focus on the settling and rise times. Goback to your m-file and change the x and y variables to see if you can get a better response. You will findthat 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:

K =

-70.7107 -37.8345 105.5298 20.9238

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (5 de 11) [21/11/2003 05:46:12 p.m.]

Page 290: Matlab Tutorial Ctm

You may have noted that if you increased x and y even higher, you could improve the response evenmore. The reason this plot was chosen was because it satisfied the design requirements while keeping xand y as small as possible. In this problem, x and y have been used to describe the relative weight of thetracking error in the cart's position and pendulum's angle versus the control effort. The higher x and yare, the more control effort is used, but the smaller the tracking error. The system response has a settlingtime 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 wefeedback the output and compare it to the reference input to compute an error, with a full-state feedbackcontroller we are feeding back all the states. We need to compute what the steady-state value of the statesshould be, multiply that by the chosen gain K, and use a new value as our reference for computing theinput. This can be done by adding a constant gain Nbar after the reference. The schematic below showsthis relationship:

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (6 de 11) [21/11/2003 05:46:13 p.m.]

Page 291: Matlab Tutorial Ctm

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

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (7 de 11) [21/11/2003 05:46:13 p.m.]

Page 292: Matlab Tutorial Ctm

Now, the steady-state error is within our limits, the rise and settling times are met and the pendulum'sovershoot 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 validassumption. To compensate for this, we will next design a full-order estimator to estimate those statesthat 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 yourm-file:

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (8 de 11) [21/11/2003 05:46:13 p.m.]

Page 293: Matlab Tutorial Ctm

p = eig(Ac)

If you changed the weighting factors x and y above to x=5000 and y=100, you should see the followingpoles 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 willuse the place command in Matlab to find the L vector (note that acker would also work). Rememberthat the place command cannot have all the desired poles at the same location. Delete from the lsimcommand 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 inMatlab by computing rank(obsv(A,C(2,:))). This should make sense to you: if you can onlymeasure 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 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. Theresponse should be similar to the one from the control-law design. To set up the compensator copy thefollowing 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;

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (9 de 11) [21/11/2003 05:46:13 p.m.]

Page 294: Matlab Tutorial Ctm

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:

This response is about the same as before. All of the design requirements have been met with theminimum 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 spacemethod than with any other of the methods.

If you are interested in running an animation of the inverted pendulum example based on the controltechniques used in this tutorial please go to the Inverted Pendulum Animation Page.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (10 de 11) [21/11/2003 05:46:13 p.m.]

Page 295: Matlab Tutorial Ctm

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Animation

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: State-space design for the inverted pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/invSS.html (11 de 11) [21/11/2003 05:46:13 p.m.]

Page 296: Matlab Tutorial Ctm

Example: State-space method for the PitchController

Controllability and ObservabilityControl design via pole placementReference input

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 pitchangle (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%●

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (1 de 7) [21/11/2003 05:46:19 p.m.]

Page 297: Matlab Tutorial Ctm

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 thestate-space tutorial please go to the Pitch Controller Animation page after completing this tutorial.

Controllability and ObservabilityThe first thing to do in designing a system via state-space method is to check the controllability andobservability 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 thesame 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 rankfor both matrices must be 3. The Matlab command rank can give you the ranks of both matrices. Createa 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];

co=ctrb (A,B);ob=obsv (A,C);

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (2 de 7) [21/11/2003 05:46:19 p.m.]

Page 298: Matlab Tutorial Ctm

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 placementThe 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●

Recall from the State-Space Tutorial page, the "pole placement" technique should be used to find thecontrol matrix (K). Since the determinate of [sI-(A-BK)] matrix is a third-order polynomial, there arethree poles we can place.

In the State-Space Tutorial, the dominant second-order pole placement method was introduced. Howeverfor this example, we will use another method called Linear Quadratic Regulator (LQR) method. Thismethod allows you to find the optimal control matrix that results in some balance between system errorsand control effort. Please consult your control textbook for details. To use this LQR method, we need tofind three parameters: performance index matrix (R), state-cost matrix (Q), and weighting factor (p). Forsimplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q)

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (3 de 7) [21/11/2003 05:46:19 p.m.]

Page 299: Matlab Tutorial Ctm

equals to C' x C. The weighting factor (p) will be varied as we see the step response. To see the structureof 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

Now we are ready to find the control matrix (K) and see the response of the system. First, let theweighting factor (p) equal 50. Enter the following commands to a new m-file and run it in the Matlabcommand 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:

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (4 de 7) [21/11/2003 05:46:19 p.m.]

Page 300: Matlab Tutorial Ctm

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 inputUnlike 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 schematicshown above). Thus, we should not expect to see the output equal to the input. To obtain the desiredoutput, we need to scale the reference input so that the output equals the reference. This can be easilydone by introducing a feed-forwarding scaling factor called Nbar. The basic schematic with the scalingfactor (Nbar) is shown below:

We can easily find Nbar from the Matlab function rscale. Since this rscale is a user-definedfunction, you need to copy and save the rscale m-file to your directory. For further assistance in usinguser-defined functions, refer to Function. After you have saved the rscale m-file to your directory,enter the following commands to a new m-file and run it in the Matlab command window. You shouldsee the response shown below:

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (5 de 7) [21/11/2003 05:46:19 p.m.]

Page 301: Matlab Tutorial Ctm

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)

Now the steady-state error has been eliminated and all design requirements are satisfied.

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (6 de 7) [21/11/2003 05:46:19 p.m.]

Page 302: Matlab Tutorial Ctm

If you are interested in running an animation of the pitch controller example based on the controltechniques used in this tutorial please go to the Pitch Controller Animation page.

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

State-Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Control | Ball and Beam

Pitch Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS | Animation

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM Example: Pitch Controller -- State-Space method

http://www.engin.umich.edu/group/ctm/examples/pitch/SSpitch.html (7 de 7) [21/11/2003 05:46:19 p.m.]

Page 303: Matlab Tutorial Ctm

Example: Solution to the Ball & Beam ProblemUsing the State-space Design Method

Full-State Feedback ControllerReference Input

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 andball, here we are controlling alpha-doubledot. By doing this we are essentially controlling a torqueapplied at the center of the beam by a motor. Therefore, we do not need a gear and lever system.

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

If you are interested in running an animation of this example based on the control techniques used in thestate-space tutorial please go to the Ball & Beam Animation Page after completing this tutorial.

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (1 de 7) [21/11/2003 05:46:26 p.m.]

Page 304: Matlab Tutorial Ctm

Full-State Feedback ControllerWe will design a controller for this physical system that utilizes full-state feedback control. A schematicof 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, thereshould be four poles for our system. In designing our full-state feedback controller we can move thesepoles anywhere we want.

For our design we desire an overshoot of less than 5% which corresponds to a zeta of 0.7 (please refer toyour textbook for the relationship between overshoot and damping ratio). On a root locus this criterion isrepresented as a 45 degree line emanating from the origin and extending out into the left-half plane. Wewant 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 rootlocus. Anything beyond this line in the left-half plane is a suitable place for our poles. Therefore we willplace our poles at -2+2i and -2-2i. We will place the other poles far to the left for now, so that they willnot affect the response too much. To start with place them at -20 and -80. Now that we have our poles wecan use Matlab to find the controller (K matrix) by using the place command. Copy the following codeto 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);

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (2 de 7) [21/11/2003 05:46:26 p.m.]

Page 305: Matlab Tutorial Ctm

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 * 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. Addthe 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:

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (3 de 7) [21/11/2003 05:46:26 p.m.]

Page 306: Matlab Tutorial Ctm

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 toreduce the overshoot further than we would make the imaginary part of the pole smaller than the realpart. Also, if we wanted a faster settling time we would move the poles further in the left-half plane. Feelfree to experiment with the pole positions to see these trends.

Reference InputNow we want to get rid of the steady-state error. In contrast to the other design methods, where wefeedback the output and compare it to the reference input to compute an error, with a full-state feedbackcontroller we are feeding back both states. We need to compute what the steady-state value of the statesshould be, multiply that by the chosen gain K, and use a new value as our reference for computing theinput. This can be done by adding a constant gain Nbar after the reference. The schematic below showsthis relationship:

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (4 de 7) [21/11/2003 05:46:26 p.m.]

Page 307: Matlab Tutorial Ctm

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)

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

Your output should be:

place: ndigits= 15 Nbar = 1.8286e+03

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (5 de 7) [21/11/2003 05:46:26 p.m.]

Page 308: Matlab Tutorial Ctm

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 thepole positions to see how the system responds.

If you are interested in running an animation of the ball & beam example based on the control techniquesused in this tutorial please go to the Ball & Beam Animation Page.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

State-space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (6 de 7) [21/11/2003 05:46:26 p.m.]

Page 309: Matlab Tutorial Ctm

Ball & Beam ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Animation

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM Example: State-space design for the Ball & Beam

http://www.engin.umich.edu/group/ctm/examples/ball/bbSS.html (7 de 7) [21/11/2003 05:46:26 p.m.]

Page 310: Matlab Tutorial Ctm

Digital Control Example: Designing CruiseControl using Root-Locus method

Discrete transfer functionRoot-Locus in z-planeCompensation using a digital controller

In this digital control version of the cruise control problem, we are going to use the root-locus designmethod to design the digital controller. If you refer to the Cruise Control: Modeling page, the open-looptransfer 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 functionThe first step in performing a discrete analysis of a system is to find the discrete equivalent transferfunction of the continuous portion. We will convert the above transfer function (Y(s)/U(s)) to the discretetransfer function using the Matlab function called c2dm. To use this c2dm, you need to specify four

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (1 de 8) [21/11/2003 05:46:34 p.m.]

Page 311: Matlab Tutorial Ctm

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 theunit of sec/sample, should be smaller than 1/(30*BW), where BW is the closed-loop bandwidthfrequency. 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 thefollowing 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-planeRecall from the Digital Control Tutorial, the Matlab function called zgrid should be used to find anacceptable region of the discrete root-locus that gives the desired gain (K). The zgrid requires twoarguments: Natural frequency (Wn) and the damping ratio (zeta). These two arguments can be foundfrom the rise time and the overshoot requirements and the following two equations.

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (2 de 8) [21/11/2003 05:46:34 p.m.]

Page 312: Matlab Tutorial Ctm

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 determinethat the natural frequency (Wn) must be greater than 0.36 rad/sec and the damping ratio (zeta) must begreater than 0.6.

Let's generate the root-locus and use the zgrid to find the acceptable region of the root-locus. Butbefore doing that, if you refer to the Digital Control Tutorial, the natural frequency argument for zgridneeds to be in the unit of rad/sample, so let the Wn = 0.36*Ts = 0.0072 rad/sample. Now add thefollowing 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])

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (3 de 8) [21/11/2003 05:46:34 p.m.]

Page 313: Matlab Tutorial Ctm

The dotted line on the right, which is very small and can not be seen in this case, indicates the locationsof constant natural frequency (Wn), and the natural frequency is greater than 0.0072 outside the line. Theother dotted line indicates the locations of constant damping ratio (zeta), and the damping ratio is greaterthan 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) usingthe Matlab function rlocfind and obtain the corresponding step response. Add the following commands tothe 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. Clickon the root-locus around +0.9. The gain (K) and the pole location should be returned to the commandwindow. Also, you should see the closed-loop stairstep response shown below.

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (4 de 8) [21/11/2003 05:46:34 p.m.]

Page 314: Matlab Tutorial Ctm

As you noticed, this response satisfies all of the design requirements. But the gain associated with thisresponse is approximately 4500. The system having this large gain (too much control effort) might not beavailable in a real physical system , even though it is possible in the Matlab simulation. To obtain adesired response with a reasonable control effort, we will modify the discrete controller.

Compensation using a digital controllerRecall from the continuous Cruise Control: Root-Locus page, the lag controller was added to the systemto obtain the desired response. In this digital control version of the cruise control problem, we willmodify 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 describedthere generally applies for improving the system response. In this this particular problem, we are notgoing to use the method described in that page and use our own educated analysis to design thecompensator.

First, we need to reduce the gain (K) while keeping the reasonable response. Recall from your controltextbook, the gain (K) equals 0 at poles and infinity at zeros. Thus, if we place the pole inside the desiredregion 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.

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (5 de 8) [21/11/2003 05:46:34 p.m.]

Page 315: Matlab Tutorial Ctm

Consider these two things, we will place the compensator pole somewhere outside the natural frequencyrequirement 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 stepresponse. 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);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.

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (6 de 8) [21/11/2003 05:46:34 p.m.]

Page 316: Matlab Tutorial Ctm

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.

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (7 de 8) [21/11/2003 05:46:34 p.m.]

Page 317: Matlab Tutorial Ctm

This response is about the same as what we obtained without the additional controller. However, if youcheck the command window, the gain has decreased to around 1000. This system satisfies all designrequirements with the reasonable control effort.

Note: A design problem does not necessarily have a unique answer. For practice, you may try othercompensators to obtain a better response than the one shown above.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Digital Control ExamplesCruise Control: RL | Motor Speed: PID | Motor Position: RL | Bus Suspension: SS | Inverted

Pendulum:SS | Pitch Controller: SS | Ball and Beam:PID

Cruise Control ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM: Digital Control Example: Cruise Control: Root-Locus

http://www.engin.umich.edu/group/ctm/examples/cruise/digCCRL.html (8 de 8) [21/11/2003 05:46:34 p.m.]

Page 318: Matlab Tutorial Ctm

Example: Root Locus Design for Digital DCMotor Position Control

Continuous to Discrete ConversionRoot Locus Design

In this digital DC motor control version of a DC motor, the controller will be designed by a Root Locusmethod. A digital DC motor model can obtain from conversion of analog DC motor model, as we willdescribe. According to the Modeling a DC Motor, the open-loop transfer function for DC motor'sposition 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●

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (1 de 12) [21/11/2003 05:46:42 p.m.]

Page 319: Matlab Tutorial Ctm

Continuous to Discrete ConversionThe first step in the design of a discrete-time system is to convert a continuous transfer function to adiscrete transfer function. Matlab can be used to convert the above transfer function todiscrete transfer function by using the c2dm command. The c2dm command requires four arguments: thenumerator polynomial (num), the denominator polynomial (den), a sampling time (T) and a type of holdcircuit. In this example we will use the zero-order hold (zoh). Refer to the Digital Control Tutorials pagefor more information.

From the design requirement, let the sampling time, T equal to 0.001 seconds, which is 1/100 of therequired time constant or 1/40 of the required settling time. Let's create a new m-file and add thefollowing 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 extraroot at z = 0. Also, we have to get rid of the leading zero coefficient in the numerator. To do this add thefollowing code to cancel out these extra pole and zero to avoid numerical problem in Matlab. Otherwiseit will consider both numerator and denominator to be fourth-order polynomial.

numd = numd(2:3);dend = dend(1:3);

Therefore, the discrete-time transfer function from the motor position output to the voltage input is:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (2 de 12) [21/11/2003 05:46:42 p.m.]

Page 320: Matlab Tutorial Ctm

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 theloop, let's see how the closed-loop stairstep response performs by using the dstep and the stairscommands. The dstep command will provide the vector of discrete step signals and stairs command willconnect these discrete signals (click here for more information). Add the following Matlab code at theend 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:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (3 de 12) [21/11/2003 05:46:42 p.m.]

Page 321: Matlab Tutorial Ctm

Root Locus DesignThe main idea of root locus design is to obtain the closed-loop response from the open-loop root locusplot. By adding zeroes and poles to the original system, the root locus will be modified that leads to anew 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 shownbelow.

rlocus(numd,dend)title('Root Locus of Original System')zgrid(0,0)

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 differenceapproximation for mapping from the s-plane to the z-plane as described by s = 1/(z-1), one pole will beadded 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 rootlocus in. We will add a pole at z = 0.95. In general, we must at least add as many poles as zeroes for thecontroller to be causal. Now add the following Matlab commands in to your m-file.

numi = [1 -0.95];

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (4 de 12) [21/11/2003 05:46:42 p.m.]

Page 322: Matlab Tutorial Ctm

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 desiredregion, which satisfies the design requirement, on the discrete root locus plot. The zgrid commandrequires two arguments: the natural frequency (Wn) and the damping ratio (zeta). From the designrequirement, the settling time is less than 0.04 seconds and the percent overshoot is less than 16%. Weknow the formulas for finding the damping ratio and natural frequency as shown:

where:

OS: the percent overshoot

Ts: the settling time

The required damping ratio is 0.5 and the natural frequency is 200 rad/sec, but the zgrid commandrequires a non-dimensional natural frequency. Therefore Wn = 200*T = 0.2 rad/sample. Add thefollowing 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:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (5 de 12) [21/11/2003 05:46:42 p.m.]

Page 323: Matlab Tutorial Ctm

From the above root locus plot, we can see that system is unstable at all gains because the root locus isoutside the unit circle. Moreover, the root locus should be in the region where the damping ratio line andnatural frequency cross each other to satisfy the design requirements. Thus we have to pull the root locusin more by first canceling the zero at approximately -0.98, since this zero will add overshoot to the stepresponse. Then we have to add one more pole and two zeroes near the desired poles. After going throughsome trial and error to yield the root locus in the desired region, one more pole is added at 0.61 and twozeroes 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]);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:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (6 de 12) [21/11/2003 05:46:42 p.m.]

Page 324: Matlab Tutorial Ctm

From the above root locus plot, the root locus is drawn in the desired region. Let's find a gain, K, on theroot 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)')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:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (7 de 12) [21/11/2003 05:46:42 p.m.]

Page 325: Matlab Tutorial Ctm

The selected gain should be around 330, and it will plot the closed-loop compensated response asfollows.

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (8 de 12) [21/11/2003 05:46:42 p.m.]

Page 326: Matlab Tutorial Ctm

From the above closed-loop response, the settling time is about 0.05 seconds which is satisfy therequirement, but the percent overshoot is 22% which is too large due to the zeroes. If we select the gainto be larger, the requirements will be satisfied. On the other hand, the problem will be unrealistic and ahuge actuator is needed, you can try this yourself by picking a larger gain on the previous root locus plotwhich will yield an unrealistically sudden step response. So we have to move both one pole and twozeroes a little further to the right to pull root locus in a little bit more, new pole will be put at 0.7 and twozeroes should be at 0.85. Go back to your m-file and change only numc and denc as shown below andrerun 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:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (9 de 12) [21/11/2003 05:46:42 p.m.]

Page 327: Matlab Tutorial Ctm

The selected gain should be around 450, and then it will plot the closed-loop compensated response asfollows:

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (10 de 12) [21/11/2003 05:46:42 p.m.]

Page 328: Matlab Tutorial Ctm

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. Soadd 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:

We can see that a response to the disturbance is small (3.3% of the disturbance ) and settles within 2% ofthe disturbance after 0.04 seconds and eventually reaches zero.

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (11 de 12) [21/11/2003 05:46:42 p.m.]

Page 329: Matlab Tutorial Ctm

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Digital Control ExamplesCruise Control:RL | Motor Speed: PID | Motor Position: RL | Bus Suspension: SS | Inverted

Pendulum: SS | Pitch Controller: SS | Ball and Beam: PID

Motor Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control: RL

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/21/97 PP 8/24/97 WM

CTM Example: Root Locus Design for Digital DC Motor Position Control

http://www.engin.umich.edu/group/ctm/examples/motor2/digital.html (12 de 12) [21/11/2003 05:46:42 p.m.]

Page 330: Matlab Tutorial Ctm

Example: State Space Design for Digital Bus SuspensionControl

Sampling Time SelectionContinuous to Discrete ConversionDesigning the ControllerSimulating the Closed-Loop Response

In this example, we will design a digital state space controller for the bus suspension control example. First we will convert thecontinuous time model to a discrete time model, and then use the pole placement method to design the controller. From the bussuspension 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

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (1 de 6) [21/11/2003 05:46:49 p.m.]

Page 331: Matlab Tutorial Ctm

Sampling Time SelectionThe first step in the design of a discrete-time controller is to convert the continuous plant to its discrete time equivalent. First, weneed to pick an appropriate sampling time, T. In this example, selection of sampling time is very important since a step in theroad surface very quickly affects the output. Physically, what happens is the road surface suddenly lifts the wheel, compressingthe spring, K2, and the damper, b2. Since the suspension mass is relatively low, and the spring fairly stiff, the suspension massrises quickly, increasing X2 almost immediately. Since the controller can only see the effect of the disturbance after a completesampling 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 tooscillate. We will simulate just the beginning of this response by setting the time vector input to the step function to range from 0to .005. The response to a .1m step input is simulated by multiplying the B matrix by .1. Create a new m-file and enter thefollowing 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) (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 stepafter only a little more than 0.001s. Therefore, we will set T=.0005s in order to give the controller a chance to respond.

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (2 de 6) [21/11/2003 05:46:49 p.m.]

Page 332: Matlab Tutorial Ctm

Continuous to Discrete ConversionNow that we have selected a sampling time, we can convert the plant to discrete time. Matlab can be used to convert the abovestate space model, A,B,C, and D, to a discrete state space model, Ad,Bd,Cd, and Dd, by using c2dm command. The c2dmcommand can take six arguments: the four state matrices, the sampling time, T, and the type of hold circuit. In this example wewill use zero-order hold ('zoh'). Refer to the Digital Control Tutorials page for more information.

Add the following code to your m-file:

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 =

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (3 de 6) [21/11/2003 05:46:49 p.m.]

Page 333: Matlab Tutorial Ctm

0 0 1 0

Dd =

0 0

which represent the new discrete-time state space model.

Adding an IntegratorIn this example, we will need to add an integrator to the system in order to drive the steady-state response to zero. We will addthis integrator in series with the plant. This will have the effect of adding another state to the plant. We will add the integrator byrepresenting it in state space and the using the series command. This command takes the A,B,C, and D matrices of the twosystems to be connected in series as arguments and returns a new set of A,B,C, and D matrices. An integrator in discrete timestate 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 outputstate. 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 ControllerThe structure of the controller is similar to the structure of the continuous-time state space controller. We will now use the placecommand 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 bevery selective about where to place them. In particular, we can place them to cancel all of the plant zeros, as well as give us thedesired response. First, we will find the plant zeros by converting the plant's digital state equations to a transfer function, andthen finding the roots of the numerator. We will use the ss2tf command which takes the state matrices and the selected input asarguments 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:

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (4 de 6) [21/11/2003 05:46:49 p.m.]

Page 334: Matlab Tutorial Ctm

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 apole there settles in approximately 10000 samples (or 5 seconds). The last pole will be selected at z=.2 since this is sufficientlyfast 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:

place: ndigits= 19

K =

1.0e+09 *

0.0548 0.0000 1.0897 0.0011 0.0009

Simulating the Closed-Loop ResponseWe can use the dstep command to simulate the closed-loop response. Since multiplying the state vector by K in our controlleronly 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 donein the continuous design to compensate for the fact that there are two inputs to the plant, but only one is a control input. We willsimulate with a negative .1m step disturbance in the road to give us a positive deflection of the bus for aesthetic reasons. Enterthe 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.

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (5 de 6) [21/11/2003 05:46:49 p.m.]

Page 335: Matlab Tutorial Ctm

We can see in this plot, that the overshoot is less than 5mm, and the response settles well within 5 seconds.

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have for improvement, errors that you found,or any other comments that you have. This feedback is anonymous; include your email address if you want a reply.

Digital Control ExamplesCruise Control: RL | Motor Speed: PID | Motor Position: RL | Bus Suspension: SS | Inverted Pendulum: SS | Pitch

Controller: SS | Ball and Beam: PID

Bus Suspension Position ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Examples

8/24/97 JL8/24/97 WM

Example: State Space Design for Digital Bus Suspension Control

http://www.engin.umich.edu/group/ctm/examples/susp/digital.html (6 de 6) [21/11/2003 05:46:49 p.m.]

Page 336: Matlab Tutorial Ctm

Digital Control Example: Inverted Pendulumusing State-Space method

Discrete state-spaceControllability and ObservabilityControl design via pole placementReference inputObserver design

In this digital control version of the inverted pendulum problem, we are going to use the state-spacemethod to design the digital controller. If you refer to the Inverted Pendulum Modeling page, thestate-space equations were derived as

where

M mass of the cart 0.5 kgm mass of the pendulum 0.5 kgb friction of the cart 0.1 N/m/secl length to pendulum center of mass 0.3 mI inertia of the pendulum 0.006 kg*m^2

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (1 de 15) [21/11/2003 05:47:00 p.m.]

Page 337: Matlab Tutorial Ctm

u step force applied to the cartx cart position coordinatephi pendulum angle from vertical

Output are the cart displacement (x in meters) and the pendulum deflection angle (phi in radians).

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-spaceThe 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 specifysix 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 timeshould be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The methodwe 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 thependulum, let the sampling time be 1/100 sec/sample. Now we are ready to use c2dm. Enter thefollowing 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; 0; m*l/p];

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (2 de 15) [21/11/2003 05:47:00 p.m.]

Page 338: Matlab Tutorial Ctm

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

Now we have obtained the discrete state-space model of the form

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (3 de 15) [21/11/2003 05:47:00 p.m.]

Page 339: Matlab Tutorial Ctm

Controllability and ObservabilityThe next step is to check the controllability and the observability of the system. For the system to becompletely 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 thesame 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 '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 followingcommands 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;

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (4 de 15) [21/11/2003 05:47:00 p.m.]

Page 340: Matlab Tutorial Ctm

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

This proves that our discrete system is both completely state controllable and completely stateobservable.

Control design via pole placementThe 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

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (5 de 15) [21/11/2003 05:47:00 p.m.]

Page 341: Matlab Tutorial Ctm

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 LQRmethod. This method allows you to find the optimal control matrix that results in some balance betweensystem errors and control effort. Please consult your control textbook for details. To use this LQRmethod, we need to find three parameters: Performance index matrix (R), state-cost matrix (Q), andweighting factors. For simplicity, we will choose the performance index matrix equals 1 (R=1), and thestate-cost matrix (Q) equals to H' x H. The weighting factors will be chosen by trial and errors. Thestate-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,3position will be used to weight the pendulum's angle. The weighting factors for the cart's position and thependulum'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 followingcommands to an new m-file and run it in the Matlab command window. You should see the followingstep response.

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 positiony=1; %weighting factor for the pendulum angle

Q=[x 0 0 0;

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (6 de 15) [21/11/2003 05:47:00 p.m.]

Page 342: Matlab Tutorial Ctm

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

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'sposition in meters. The pendulum's and cart's overshoot appear fine, but their settling times needimprovement and the cart's rise time needs to be decreased. Also the cart has, in fact, moved in theopposite direction. For now, we will concentrate on improving the settling times and the rise times, andfix 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 backto your m-file and change the x and y to x=5000 and y=100. Running this m-file in the commandwindow gives you the following new step response.

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (7 de 15) [21/11/2003 05:47:00 p.m.]

Page 343: Matlab Tutorial Ctm

From this plot, we see that all design requirements are satisfied except the steady-state error of the cartposition (x). We can easily correct this by introducing a feedforwarding scaling factor (Nbar).

Reference inputUnlike 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 schematicshown above). Thus, we should not expect to see the output equals to the input. To obtain the desiredoutput, we need to scale the reference input so that the output equals to the reference. This can be easilydone by introducing a feedforwarding scaling factor called Nbar. The basic schematic with the Nbar isshown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can find itfrom 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.

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (8 de 15) [21/11/2003 05:47:00 p.m.]

Page 344: Matlab Tutorial Ctm

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 positiony=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)')

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (9 de 15) [21/11/2003 05:47:00 p.m.]

Page 345: Matlab Tutorial Ctm

Notice that the steady-state error of the cart's position have been eliminated. Now we have designed thesystem that satisfies all design requirements.

Observer designThe above response satisfies all design requirements; however, it was found assuming all states aremeasurable. This assumption may not be valid for all systems. In this section, we develop a technique forestimating the states of a plant from the information that is available concerning the plant. The systemthat estimates the states of another system is called an observer. Thus, in this section we will design afull-order state observer to estimate those states that are not measurable. For further explanation on howan observer works, please consult your control textbooks.

A basic schematic of the plant-observer system is shown below.

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (10 de 15) [21/11/2003 05:47:00 p.m.]

Page 346: Matlab Tutorial Ctm

To design the observer, first, we need to find the L matrix. To find the L matrix, we need to find thepoles of the system without the observer (the poles of F-G*K). Copy the following commands to an newm-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];

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (11 de 15) [21/11/2003 05:47:00 p.m.]

Page 347: Matlab Tutorial Ctm

x=5000; %weighting factor for the cart positiony=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

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 theobserver. Let's place the observer poles far left of above poles, say, at [-0.3 -0.31 -0.32 -0.33]. Thesepoles 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 =

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (12 de 15) [21/11/2003 05:47:00 p.m.]

Page 348: Matlab Tutorial Ctm

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-fileand copy the following code.

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 positiony=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;

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (13 de 15) [21/11/2003 05:47:00 p.m.]

Page 349: Matlab Tutorial Ctm

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

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (14 de 15) [21/11/2003 05:47:00 p.m.]

Page 350: Matlab Tutorial Ctm

Digital Control ExamplesCruise Control:RL | Motor Speed:PID | Motor Position:RL | Bus Suspension:SS | InvertedPendulum:SS | Pitch Controller:SS | Ball & Beam:PID

Inverted Pendulum ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/21/97 DK

CTM: Digital Control Example: Inverted Pendulum

http://www.engin.umich.edu/group/ctm/examples/pend/digINVSS.html (15 de 15) [21/11/2003 05:47:00 p.m.]

Page 351: Matlab Tutorial Ctm

Digital Control Example: Designing PitchController using State-Space method

Discrete state-spaceControllability and observability Control design via pole placementReference input

In this digital control version of the pitch controller problem, we are going to use the state-space methodto design the digital controller. If you refer to the Pitch Controller: Modeling page, the state-space modelwas derived as

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

The design requirements are

Overshoot: Less than 10%●

Rise time: Less than 2 seconds●

Settling time: Less than 10 seconds●

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (1 de 9) [21/11/2003 05:47:12 p.m.]

Page 352: Matlab Tutorial Ctm

Steady-state error: Less than 2%●

Discrete state-spaceThe first thing to do here is to convert above continuous state-space model to discrete state-space. To dothis, we are going to use the Matlab function called c2dm. To use this c2dm, we need to specify sixarguments: Four state-space matrices (A, B, C, and D), sampling time (Ts), and the 'method'. You shouldalready be familiar with how to enter A, B, C, and D matrices. The sampling time should be smaller than1/(30*BW), where BW is the closed-loop bandwidth frequency. The method we will use is thezero-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 to use thesampling time of 1/100 sec/sample. Now we are ready to use the function c2dm. Enter the followingcommands 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

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (2 de 9) [21/11/2003 05:47:12 p.m.]

Page 353: Matlab Tutorial Ctm

H =

0 0 1 J =

0

Now we have obtained the discrete state-space model of the form

Controllability and observabilityThe next step is to check the controllability and the observability of the system. For the system to becompletely 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 thesame 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

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (3 de 9) [21/11/2003 05:47:12 p.m.]

Page 354: Matlab Tutorial Ctm

of both matrices must be 3. The Matlab function rank can give you the rank of each matrices. In an newm-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 stateobservable.

Control design via pole placementThe schematic of a full-state feedback system is shown below.

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (4 de 9) [21/11/2003 05:47:12 p.m.]

Page 355: Matlab Tutorial Ctm

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) methodwas used to find the control matrix (K). In this digital version, we will use the same LQR method. Thismethod allows you to find the optimal control matrix that results in some balance between system errorsand control effort. Please consult your control textbook for details. To use this LQR method, we need tofind three parameters: Performance index matrix (R), state-cost matrix (Q), and weighting factor (p). Forsimplicity, 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 theweighting factor (p) equals 50. Enter the following commands to a new m-file and run it in the Matlabcommand window.

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];

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (5 de 9) [21/11/2003 05:47:12 p.m.]

Page 356: Matlab Tutorial Ctm

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 stepresponse similar to the one shown below.

The rise time, the overshoot, and the settling time look satisfactory. However, there is a large steady-stateerror. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference inputUnlike 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 schematicshown above). Thus, we should not expect to see the output equals to the input. To obtain the desired

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (6 de 9) [21/11/2003 05:47:12 p.m.]

Page 357: Matlab Tutorial Ctm

output, we need to scale the reference input so that the output equals to the reference. This can be easilydone by introducing a feedforwarding scaling factor called Nbar. The basic schematic with the Nbar isshown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can findit 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]; 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)

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (7 de 9) [21/11/2003 05:47:12 p.m.]

Page 358: Matlab Tutorial Ctm

From this plot, we see that the Nbar eliminated the steady-state error. Now all design requirements aresatisfied.

Note: Assuming all states are measurable, an observer design willnot be explained in this page.

User feedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Digital Control ExamplesCruise Control: RL | Motor Speed: PID | Motor Position: RL | Bus Suspension: SS | Inverted

Pendulum:SS | Pitch Controller: SS | Ball and Beam: PID

Pitch Control Examples

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (8 de 9) [21/11/2003 05:47:12 p.m.]

Page 359: Matlab Tutorial Ctm

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control: SS

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM: Digital Control Tutorial: Pitch Controller: State-Space

http://www.engin.umich.edu/group/ctm/examples/pitch/digPCSS.html (9 de 9) [21/11/2003 05:47:12 p.m.]

Page 360: Matlab Tutorial Ctm

Digital Control Example: Ball and Beamproblem using PID Control

Digital PID controllerDiscrete transfer functionOpen-loop responseProportional controlProportional-Derivative control

In this digital control version of the ball and beam experiment, we are going to use the PID controlmethod to design the digital controller. If you refer to the Ball and Beam Modeling page, the open-looptransfer function was derived as

m mass of the ball 0.11 kgg gravitational acceleration 9.8 m/s^2d lever arm offset 0.03 mL length of the beam 1.0 mR radius of the ball 0.015 mJ ball's moment of inertia 9.99e-6 kgm^2R(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●

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (1 de 7) [21/11/2003 05:47:18 p.m.]

Page 361: Matlab Tutorial Ctm

Overshoot less than 5%●

Digital PID controllerIf you refer to any of the PID control problem for continuous systems, the PID transfer function wasexpressed as

As you noticed the above transfer function was written in terms of s. For the digital PID control, we usethe following transfer function in terms of z.

Discrete transfer functionThe first thing to do here is to convert the above continuous system transfer function to discrete transferfunction. To do this, we are going to use the Matlab function called c2dm. To use this c2dm, we need tospecify four arguments: numerator and denominator matrices, sampling time (Ts), and the 'method'. Youshould already be familiar with how to enter numerator and denominator matrices. The sampling timeshould be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The methodwe will use is the zero-order hold ('zoh'). Assuming that the closed-loop bandwidth frequency is around 1rad/sec, let the sampling time be 1/50 sec/sample. Now we are ready to use c2dm. Enter the followingcommands 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')

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (2 de 7) [21/11/2003 05:47:18 p.m.]

Page 362: Matlab Tutorial Ctm

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

numDz = 1.0e-0.4 * 0 0.4200 0.4200 denDz = 1 -2 1

From these matrices, the discrete transfer function can be written as

Open-loop responseNow we will observe the ball's response to a step input of 0.25 m. To do this, enter the followingcommands 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)

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (3 de 7) [21/11/2003 05:47:18 p.m.]

Page 363: Matlab Tutorial Ctm

From this plot, it is clear that the open-loop system is unstable causing the ball to roll off from the end ofthe beam.

Proportianal ControlNow 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 annew 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)

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (4 de 7) [21/11/2003 05:47:18 p.m.]

Page 364: Matlab Tutorial Ctm

As you can see, the addition of proportional control does not make the system stable. You may try toincrease the proportional gain (Kp) and confirm that the system remains unstable.

Proportional-Derivative controlNow we will add a derivative term to the controller. Keep the proportional gain (Kp) equal to 100, andlet the derivative gain (Kd) equal to 10. Copy the following code to an new m-file and run it to view thesystem 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)

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (5 de 7) [21/11/2003 05:47:18 p.m.]

Page 365: Matlab Tutorial Ctm

Now the system is stable, but the rise time is too long. From the PID Tutorial page, we see that theincreasing 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 thecommand window. You should see the following step response.

As you can see, all of the design requirements are satisfied. For this particular problem, no

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (6 de 7) [21/11/2003 05:47:18 p.m.]

Page 366: Matlab Tutorial Ctm

implementation of an integral control was needed. But remember there is more than one solution for acontrol problem. For practice, you may try different P, I and D combinations to obtain a satisfactoryresponse.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Digital Control ExamplesCruise Control: RL | Motor Speed:PID | Motor Position:RL | Bus Suspension: SS | Inverted

Pendulum:SS | Pitch Controller: SS | Ball and Beam:PID

Ball & Beam ExamplesModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/25/97 DK

CTM: Digital Control Example: Ball and Beam

http://www.engin.umich.edu/group/ctm/examples/ball/digBBPID.html (7 de 7) [21/11/2003 05:47:18 p.m.]

Page 367: Matlab Tutorial Ctm

PID TutorialIntroductionThe three-term controllerThe characteristics of P, I, and D controllersExample Problem

Open-loop step responseProportional controlProportional-Derivative controlProportional-Integral controlProportional-Integral-Derivative control

General tips for designing a PID controller

Key Matlab Commands used in this tutorial are: step cloopNote: Matlab commands from the control system toolbox are highlighted in red.

IntroductionThis tutorial will show you the characteristics of the each of proportional (P), the integral (I), and thederivative (D) controls, and how to use them to obtain a desired response. In this tutorial, we willconsider the following unity feedback system:

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (1 de 11) [21/11/2003 05:49:07 p.m.]

Page 368: Matlab Tutorial Ctm

Plant: A system to be controlledController: Provides the excitation for the plant; Designed to control the overall system behavior

The three-term controllerThe 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 schematicshown above. The variable (e) represents the tracking error, the difference between the desired inputvalue (R) and the actual output (Y). This error signal (e) will be sent to the PID controller, and thecontroller computes both the derivative and the integral of this error signal. The signal (u) just past thecontroller is now equal to the proportional gain (Kp) times the magnitude of the error plus the integralgain (Ki) times the integral of the error plus the derivative gain (Kd) times the derivative of the error.

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 errorsignal and computes its derivative and its integral again. This process goes on and on.

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (2 de 11) [21/11/2003 05:49:07 p.m.]

Page 369: Matlab Tutorial Ctm

The characteristics of P, I, and D controllersA proportional controller (Kp) will have the effect of reducing the rise time and will reduce ,but nevereliminate, the steady-state error. An integral control (Ki) will have the effect of eliminating thesteady-state error, but it may make the transient response worse. A derivative control (Kd) will have theeffect of increasing the stability of the system, reducing the overshoot, and improving the transientresponse. Effects of each of controllers Kp, Kd, and Ki on a closed-loop system are summarized in thetable 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 eachother. In fact, changing one of these variables can change the effect of the other two. For this reason, thetable should only be used as a reference when you are determining the values for Ki, Kp and Kd.

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

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

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (3 de 11) [21/11/2003 05:49:07 p.m.]

Page 370: Matlab Tutorial Ctm

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●

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

num=1;den=[1 10 20];step(num,den)

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

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (4 de 11) [21/11/2003 05:49:07 p.m.]

Page 371: Matlab Tutorial Ctm

The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output to an unit stepinput. This corresponds to the steady-state error of 0.95, quite large indeed. Furthermore, the rise time isabout one second, and the settling time is about 1.5 seconds. Let's design a controller that will reduce therise time, reduce the settling time, and eliminates the steady-state error.

Proportional controlFrom the table shown above, we see that the proportional controller (Kp) reduces the rise time, increasesthe overshoot, and reduces the steady-state error. The closed-loop transfer function of the above systemwith a proportional controller is:

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

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.

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (5 de 11) [21/11/2003 05:49:07 p.m.]

Page 372: Matlab Tutorial Ctm

Note: The Matlab function called cloop can be used to obtain a closed-loop transfer function directlyfrom the open-loop transfer function (instead of obtaining closed-loop transfer function by hand). Thefollowing m-file uses the cloop command that should give you the identical plotas 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 therise time and the steady-state error, increased the overshoot, anddecreased the settling time by small amount.

Proportional-Derivative controlNow, let's take a look at a PD control. From the table shown above, wesee that the derivative controller (Kd) reduces both the overshoot andthe settling time. The closed-loop transfer function of the givensystem with a PD controller is:

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (6 de 11) [21/11/2003 05:49:07 p.m.]

Page 373: Matlab Tutorial Ctm

Let Kp equals to 300 as before and let Kd equals 10. Enter thefollowing commands into an m-file and run it in the Matlab commandwindow.

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 theovershoot and the settling time, and had small effect on the rise timeand the steady-state error.

Proportional-Integral controlBefore 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 therise time, increases both the overshoot and the settling time, andeliminates the steady-state error. For the given system, theclosed-loop transfer function with a PI control is:

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (7 de 11) [21/11/2003 05:49:07 p.m.]

Page 374: Matlab Tutorial Ctm

Let's reduce the Kp to 30, and let Ki equals to 70. Create an newm-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 thefollowing plot.

We have reduced the proportional gain (Kp) because the integralcontroller also reduces the rise time and increases the overshoot asthe proportional controller does (double effect). The above responseshows that the integral controller eliminated the steady-state error.

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (8 de 11) [21/11/2003 05:49:07 p.m.]

Page 375: Matlab Tutorial Ctm

Proportional-Integral-Derivative controlNow, let's take a look at a PID controller. The closed-loop transferfunction of the given system with a PID controller is:

After several trial and error runs, the gains Kp=350, Ki=300, andKd=50 provided the desired response. To confirm, enter the followingcommands to an m-file and run it in the command window. You should getthe 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.

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (9 de 11) [21/11/2003 05:49:07 p.m.]

Page 376: Matlab Tutorial Ctm

General tips for designing a PID controllerWhen you are designing a PID controller for a given system, follow thesteps shown below to obtain a desired response.

Obtain an open-loop response and determine what needs to beimproved

1.

Add a proportional control to improve the rise time2.

Add a derivative control to improve the overshoot3.

Add an integral control to eliminate the steady-state error4.

Adjust each of Kp, Ki, and Kd until you obtain a desired overallresponse. You can always refer to the table shown in this "PIDTutorial" page to find out which controller controls whatcharacteristics.

5.

Lastly, please keep in mind that you do not need to implement allthree controllers (proportional, derivative, and integral) into asingle system, if not necessary. For example, if a PI controller givesa good enough response (like the above example), then you don't needto implement derivative controller to the system. Keep the controlleras simple as possible.

User FeedbackWe would like to hear about difficulties you had with the tutorials,suggestions you have for improvement, errors that you found, or anyother comments that you have. This feedback is anonymous; include youremail address if you want a reply.

PID ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension |

Inverted Pendulum | Pitch Controller | Ball and Beam

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (10 de 11) [21/11/2003 05:49:07 p.m.]

Page 377: Matlab Tutorial Ctm

Space | Digital Control | Examples

8/26/97 DK

CTM: PID Tutorial

http://www.engin.umich.edu/group/ctm/PID/PID.html (11 de 11) [21/11/2003 05:49:07 p.m.]

Page 378: Matlab Tutorial Ctm

Root Locus TutorialClosed-loop polesPlotting the root locus of a transfer functionChoosing a value of K from root locusClosed-loop response

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

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

Closed-Loop PolesThe root locus of an (open-loop) transfer function H(s) is a plot of the locations (locus) of all possibleclosed 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:

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (1 de 7) [21/11/2003 05:49:18 p.m.]

Page 379: Matlab Tutorial Ctm

Let n = order of a(s) and m = order of b(s) [the order of a polynomial is the highest power of s thatappears in it].

We will consider all positive values of k. In the limit as k -> 0, the poles of the closed-loop system area(s) = 0 or the poles of H(s). In the limit as k -> infinity, the poles of the closed-loop system are b(s) = 0or the zeros of H(s).

No matter what we pick k to be, the closed-loop system must always have n poles, where n is thenumber of poles of H(s). The root locus must have n branches, each branch starts at a pole of H(s) andgoes 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 infinityis n-m, the number of poles minus the number of zeros, and is the number of branches of the root locusthat go to infinity (asymptotes).

Since the root locus is actually the locations of all possible closed loop poles, from the root locus we canselect a gain such that our closed-loop system will perform the way we want. If any of the selected polesare on the right half plane, the closed-loop system will be unstable. The poles that are closest to theimaginary axis have the greatest influence on the closed-loop response, so even though the system hasthree or four poles, it may still act like a second or even first order system depending on the location(s) ofthe dominant pole(s).

Plotting the root locus of a transfer functionConsider 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 designcriteria are 5% overshoot and 1 second rise time. Make a Matlab file called rl.m. Enter the transferfunction, 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])

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (2 de 7) [21/11/2003 05:49:18 p.m.]

Page 380: Matlab Tutorial Ctm

Choosing a value of K from the root locusThe 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 thelocus is acceptable, we can use the command sgrid(Zeta,Wn) to plot lines of constant damping ratioand natural frequency. Its two arguments are the damping ratio (Zeta) and natural frequency (Wn) [thesemay be vectors if you want to look at a range of acceptable values]. In our problem, we need anovershoot less than 5% (which means a damping ratio Zeta of greater than 0.7) and a rise time of 1second (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)

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (3 de 7) [21/11/2003 05:49:18 p.m.]

Page 381: Matlab Tutorial Ctm

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. Thesemicircle indicates pole locations with a natural frequency Wn = 1.8; inside the circle, Wn < 1.8 andoutside 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 twowhite dotted lines, and to make the rise time shorter than 1 second, the poles have to be outside of thewhite dotted semicircle. So now we know only the part of the locus outside of the semicircle and inbetween the two lines are acceptable. All the poles in this location are in the left-half plane, so theclosed-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 weneed only a proportional controller to move the poles to the desired region. You can use rlocfindcommand 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 pointsindicated in the plot below to satisfy the design criteria.

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (4 de 7) [21/11/2003 05:49:18 p.m.]

Page 382: Matlab Tutorial Ctm

Note that since the root locus may has more than one branch, when you select a pole, you may want tofind out where the other pole (poles) are. Remember they will affect the response too. From the plotabove we see that all the poles selected (all the white "+") are at reasonable positions. We can go aheadand use the chosen kd as our proportional controller.

Closed-loop responseIn order to find the step response, you need to know the closed-loop transfer function. You couldcompute 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 functionfeedback, 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)

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (5 de 7) [21/11/2003 05:49:18 p.m.]

Page 383: Matlab Tutorial Ctm

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

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Root Locus ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball & Beam

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (6 de 7) [21/11/2003 05:49:18 p.m.]

Page 384: Matlab Tutorial Ctm

8/12/97 CJC

CTM: Root Locus Tutorial

http://www.engin.umich.edu/group/ctm/rlocus/rlocus.html (7 de 7) [21/11/2003 05:49:18 p.m.]

Page 385: Matlab Tutorial Ctm

Frequency Response Analysis and DesignTutorial

I. Bode plots [ Gain and phase margin | Bandwidth frequency | Closed loop response ]

II. The Nyquist diagram [ Closed loop stability | Gain margin | Phase margin ]

Key matlab commands used in these tutorial are bode, nyquist, nyquist1, lnyquist1, margin, lsim, step,and cloop

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 functionsfrom physical data.

The frequency response of a system can be viewed two different ways: via the Bode plot or via theNyquist diagram. Both methods display the same information; the difference lies in the way theinformation 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 varyingfrequencies. The output of a linear system to a sinusoidal input is a sinusoid of the same frequency butwith a different magnitude and phase. The frequency response is defined as the magnitude and phasedifferences between the input and output sinusoids. In this tutorial, we will see how we can use theopen-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" andinfinity) and compute the value of the plant transfer function at those frequencies. If G(s) is the open looptransfer function of a system and w is the frequency vector, we then plot G(j*w) vs. w. Since G(j*w) is acomplex number, we can plot both its magnitude and phase (the Bode plot) or its position in the complexplane (the Nyquist plot). More information is available on plotting the frequency response.

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (1 de 15) [21/11/2003 05:49:27 p.m.]

Page 386: Matlab Tutorial Ctm

Bode PlotsAs noted above, a Bode plot is the representation of the magnitude and phase of G(j*w) (where thefrequency vector w contains only positive frequencies). To see the Bode plot of a transfer function, youcan 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

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

Click here to see a few simple Bode plots.

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (2 de 15) [21/11/2003 05:49:27 p.m.]

Page 387: Matlab Tutorial Ctm

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 isdefined as the change in open loop gain required to make the system unstable. Systems with greater gainmargins 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 loopsystem unstable.

The phase margin also measures the system's tolerance to time delay. If there is a time delaygreater than 180/Wpc in the loop (where Wpc is the frequency where the phase shift is 180deg), the system will become unstable in closed loop. The time delay can be thought of as anextra block in the forward path of the block diagram that adds phase to the system but hasno effect the gain. That is, a time delay can be represented as a block with magnitude of 1and 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 gainand phase margins on a Bode plot:

The phase margin is the difference in phase between the phase curve and -180 deg at the pointcorresponding 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 pointcorresponding to the frequency that gives us a phase of -180 deg (the phase cross over frequency, Wpc).

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (3 de 15) [21/11/2003 05:49:27 p.m.]

Page 388: Matlab Tutorial Ctm

One nice thing about the phase margin is that you don't need to replot the Bode in order to find the newphase margin when changing the gains. If you recall, adding gain only shifts the magnitude plot up. Thisis the equivalent of changing the y-axis on the magnitude plot. Finding the phase margin is simply thematter of finding the new cross-over frequency and reading off the phase margin. For example, supposeyou 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

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (4 de 15) [21/11/2003 05:49:27 p.m.]

Page 389: Matlab Tutorial Ctm

entering the command bode(100*50,[1 9 30 40]). You should get the following plot (note Ichanged the axis so the scale would be the same as the plot above, your bode plot may not be exactly thesame 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 achieved if they-axis of the magnitude plot was shifted down 40dB. Try this, look at the first Bode plot, find where thecurve crosses the -40dB line, and read off the phase margin. It should be about -60 degrees, the same asthe second Bode plot.

We can find the gain and phase margins for a system directly, by using Matlab. Just enter the margincommand. 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])

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (5 de 15) [21/11/2003 05:49:27 p.m.]

Page 390: Matlab Tutorial Ctm

Bandwidth Frequency

The bandwidth frequency is defined as the frequency at which the closed-loop magnitude response isequal to -3 dB. However, when we design via frequency response, we are interested in predicting theclosed-loop behavior from the open-loop response. Therefore, we will use a second-order systemapproximation and say that the bandwidth frequency equals the frequency at which the open-loopmagnitude response is between -6 and - 7.5dB, assuming the open loop phase response is between -135deg and -225 deg. For a complete derivation of this approximation, consult your textbook.

If you would like to see how the bandwidth of a system can be found mathematically from theclosed-loop damping ratio and natural frequency, the relevant equations as well as some plots and Matlabcode are given on our Bandwidth Frequency page.

In order to illustrate the importance of the bandwidth frequency, we will show how the output changeswith different input frequencies. We will find that sinusoidal inputs with frequency less than Wbw (thebandwidth frequency) are tracked "reasonably well" by the system. Sinusoidal inputs with frequencygreater than Wbw are attenuated (in magnitude) by a factor of 0.707 or greater (and are also shifted inphase).

Let's say that we have the following closed-loop transfer function representing a system:

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (6 de 15) [21/11/2003 05:49:27 p.m.]

Page 391: Matlab Tutorial Ctm

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 frequencycorresponding to a gain of -3 dB. looking at the plot, we find that it is approximately 1.4 rad/s. We canalso read off the plot that for an input frequency of 0.3 radians, the output sinusoid should have amagnitude about one and the phase should be shifted by perhaps a few degrees (behind the input). For aninput 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 tosimulate 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 wewant to view the steady state response. Therefore, we will modify the axes in order to see the steady stateresponse 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)

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (7 de 15) [21/11/2003 05:49:27 p.m.]

Page 392: Matlab Tutorial Ctm

axis([50,100,-2,2])

Note that the output (blue) tracks the input (purple) fairly well; it is perhaps a few degrees behind theinput as expected.

However, if we set the frequency of the input higher than the bandwidth frequency for the system, weget 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); [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 exactlyout of phase (180 degrees behind) the input. Feel free to experiment and view the response for severaldifferent frequencies w, and see if they match the Bode plot.

Closed-loop performance

In order to predict closed-loop performance from open-loop frequency response, we need to have severalconcepts clear:

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (8 de 15) [21/11/2003 05:49:27 p.m.]

Page 393: Matlab Tutorial Ctm

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), thenthe closed-loop system will be stable.

For second-order systems, the closed-loop damping ratio is approximately equal to the phasemargin divided by 100 if the phase margin is between 0 and 60 deg. We can use this concept withcaution if the phase margin is greater than 60 deg.

For second-order systems, a relationship between damping ratio, bandwidth frequency and settlingtime 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 naturalfrequency.

Let's use these concepts to design a controller for the following system:

Where Gc(s) is the controller and G(s) is:

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. Createan m-file with the following code:

num = 10;den = [1.25,1]; bode(num, den)

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (9 de 15) [21/11/2003 05:49:27 p.m.]

Page 394: Matlab Tutorial Ctm

There are several several characteristics of the system that can be read directly from this Bode plot. Firstof all, we can see that the bandwidth frequency is around 10 rad/sec. Since the bandwidth frequency isroughly the same as the natural frequency (for a second order system of this type), the rise time is1.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 ofPM/100=95/100=0.95. Plugging in this value into the equation relating overshoot and the damping ratio(or consulting a plot of this relation), we find that the damping ratio corresponding to this overshoot isapproximately 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 theBode plot as well. The constant (Kp, Kv, or Ka) are located at the intersection of the low frequencyasymptote with the w=1 line. Just extend the low frequency line to the w=1 line. The magnitude at thispoint 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 thetable of system types and error functions. The steady-state error is 1/(1+Kp)=1/(1+10)=0.091. If oursystem was type one instead of type zero, the constant for the steady-state error would be found in amanner similar to the following

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (10 de 15) [21/11/2003 05:49:27 p.m.]

Page 395: Matlab Tutorial Ctm

Let's check our predictions by looking at a step response plot. This can be done by adding the followingtwo lines of code into the Matlab command window.

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

As you can see, our predictions were very good. The system has a rise time of about 2 seconds, isoverdamped, and has a steady-state error of about 9%. Now we need to choose a controller that willallow us to meet the design criteria. We choose a PI controller because it will yield zero steady state errorfor a step input. Also, the PI controller has a zero, which we can place. This gives us additional designflexibility to help us meet our criteria. Recall that a PI controller is given by:

K*(s+a)

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (11 de 15) [21/11/2003 05:49:27 p.m.]

Page 396: Matlab Tutorial Ctm

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 thisrelation), 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 ratioplot, we find that Ts*Wbw ~ 21. We must have a bandwidth frequency greater than or equal to 12 if wewant 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 bethe 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 thefollowing (this adds an integral term but no proportional term):

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'splace the zero at 1 for now and see what happens. Change your m-file to look like the following:

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (12 de 15) [21/11/2003 05:49:28 p.m.]

Page 397: Matlab Tutorial Ctm

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

It turns out that the zero at 1 with a unit gain gives us a satisfactory answer. Our phase margin is greaterthan 60 degrees (even less overshoot than expected) and our bandwidth frequency is approximately 11rad/s, which will give us a satisfactory response. Although satisfactory, the response is not quite as goodas we would like. Therefore, let's try to get a higher bandwidth frequency without changing the phasemargin too much. Let's try to increase the gain to 5 and see what happens .This will make the gain shiftand 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))

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (13 de 15) [21/11/2003 05:49:28 p.m.]

Page 398: Matlab Tutorial Ctm

That looks really good. Let's look at our step response and verify our results. Add the following two linesto 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 luckyand usually have to play around with the gain and the position of the poles and/or zeros in order toachieve our design requirements.

This tutorial is continued on the Nyquist page (the link is after the feedback form).

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with the

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (14 de 15) [21/11/2003 05:49:28 p.m.]

Page 399: Matlab Tutorial Ctm

tutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

Frequency Response II: The Nyquist Diagram

Frequency responseExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | PitchController | Ball and Beam

TutorialsBasics | Modeling | PID | Root Locus | Frequency response | State Space | Digital Control |Examples

8/27/96 LJO

CTM: Frequency Response Tutorial

http://www.engin.umich.edu/group/ctm/freq/freq.html (15 de 15) [21/11/2003 05:49:28 p.m.]

Page 400: Matlab Tutorial Ctm

State Space TutorialState-space equationsControl design using pole placementIntroducing the reference inputObserver design

Key Matlab commands used in this tutorial: acker, lsim, place, plot, rscale

Matlab commands from the control system toolbox are highlighted in red.Non-standard Matlab commands used in this tutorial are highlighted in green.

State-space equationsThere are several different ways to describe a system of linear differential equations. The state-spacerepresentation is given by the equations:

where x is an n by 1 vector representing the state (commonly position and velocity variables inmechanical systems), u is a scalar representing the input (commonly a force or torque in mechanicalsystems), 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 nfirst-order differential equations. State space representation can also be used for systems with multipleinputs and outputs (MIMO), but we will only use single-input, single-output (SISO) systems in thesetutorials.

To introduce the state space design method, we will use themagnetically suspended ball as an example. The current through

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (1 de 12) [21/11/2003 05:49:36 p.m.]

Page 401: Matlab Tutorial Ctm

the coils induces a magnetic force which can balance the force ofgravity and cause the ball (which is made of a magnetic material)to be suspended in midair. The modeling of this system has beenestablished in many control text books (including AutomaticControl Systems by B. C. Kuo, the seventh edition). Theequations for the system are given by:

where h is the vertical position of the ball, i is the current through the electromagnet, V is the appliedvoltage, M is the mass of the ball, g is gravity, L is the inductance, R is the resistance, and K is acoefficient that determines the magnetic force exerted on the ball. For simplicity, we will choose valuesM = 0.05 Kg, K = 0.0001, L = 0.01 H, R = 1 Ohm, g = 9.81 m/sec^2 . The system is at equilibrium (theball is suspended in midair) whenever h = K i^2/Mg (at which point dh/dt = 0). We linearize theequations about the point h = 0.01 m (where the nominal current is about 7 amp) and get the state spaceequations:

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 arethe 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 =

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (2 de 12) [21/11/2003 05:49:36 p.m.]

Page 402: Matlab Tutorial Ctm

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 thefollowing 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 interestplot(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 ballhits the table or the floor first (and also probably goes out of the range where our linearization is valid).

Control design using pole placementLet's build a controller for this system. The schematic of a full-state feedback system is the following:

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (3 de 12) [21/11/2003 05:49:36 p.m.]

Page 403: Matlab Tutorial Ctm

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 usingfull-state feedback we can place the poles anywhere we want. We could use the Matlab function placeto 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. Supposethe criteria for the controller were settling time < 0.5 sec and overshoot < 5%, then we might try to placethe two dominant poles at -10 +/- 10i (at zeta = 0.7 or 45 degrees with sigma = 10 > 4.6*2). The thirdpole we might place at -50 to start, and we can change it later depending on what the closed-loopbehavior is. Remove the lsim command from your m-file and everything after it, then add the followinglines 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);

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (4 de 12) [21/11/2003 05:49:36 p.m.]

Page 404: Matlab Tutorial Ctm

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 ifthe 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);

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (5 de 12) [21/11/2003 05:49:36 p.m.]

Page 405: Matlab Tutorial Ctm

This time the overshoot is smaller. Consult your textbook for further suggestions on choosing the desiredclosed-loop poles.

Compare the control effort required (K) in both cases. In general, the farther you move the poles, themore 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 afunction called acker which works similarly to place:

K = acker(A,B,[p1 p2 p3])

Introducing the reference inputNow, we will take the control system as defined above and apply a step input (we choose a small valuefor the step, so we remain in the region where our linearization is valid). Replace t,u and lsim in yourm-file with the following,

t = 0:0.01:2; u = 0.001*ones(size(t));lsim(A-B*K,B,C,0,u,t)

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (6 de 12) [21/11/2003 05:49:36 p.m.]

Page 406: Matlab Tutorial Ctm

The system does not track the step well at all; not only is the magnitude not one, but it is negative insteadof positive!

Recall the schematic above, we don't compare the output to the reference; instead we measure all thestates, multiply by the gain vector K, and then subtract this result from the reference. There is no reasonto expect that K*x will be equal to the desired output. To eliminate this problem, we can scale thereference input to make it equal to K*x_steadystate. This scale factor is often called Nbar; it isintroduced 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)

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (7 de 12) [21/11/2003 05:49:36 p.m.]

Page 407: Matlab Tutorial Ctm

Note that this function is not standard in Matlab. You will need to copy it to a new m-file to use it. Clickhere for more information on using functions in Matlab. Now, if we want to find the response of thesystem under state feedback with this introduction of the reference, we simply note the fact that the inputis multiplied by this new factor, Nbar:

lsim(A-B*K,B*Nbar,C,0,u,t)

and now a step can be tracked reasonably well.

Observer designWhen we can't measure all the states x (as is commonly the case), we can build an observer to estimatethem, 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:

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (8 de 12) [21/11/2003 05:49:36 p.m.]

Page 408: Matlab Tutorial Ctm

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 theobserver 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 muchfaster than the system itself, we need to place the poles at least five times farther to the left than thedominant poles of the system. If we want to use place, we need to put the three observer poles atdifferent locations.

op1 = -100;op2 = -101;op3 = -102;

Because of the duality between controllability and observability, we can use the same technique used tofind the control matrix, but replacing the matrix B by the matrix C and taking the transposes of eachmatrix (consult your text book for the derivation):

L = place(A',C',[op1 op2 op3])';

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), wearrive 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.

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (9 de 12) [21/11/2003 05:49:36 p.m.]

Page 409: Matlab Tutorial Ctm

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 tocompute x-e.

Zoom in to see some detail:

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (10 de 12) [21/11/2003 05:49:36 p.m.]

Page 410: Matlab Tutorial Ctm

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 thesteady-state.

The plot above can be obtained by using the plot command.

User FeedbackWe would like to hear about suggestions you have for improvement, difficulties you had with thetutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

State Space ExamplesCruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum PitchController | Ball & Beam

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (11 de 12) [21/11/2003 05:49:36 p.m.]

Page 411: Matlab Tutorial Ctm

8/12/97 CJC

CTM: State Space Tutorial

http://www.engin.umich.edu/group/ctm/state/state.html (12 de 12) [21/11/2003 05:49:36 p.m.]

Page 412: Matlab Tutorial Ctm

Digital Control TutorialIntroductionZero-order hold equivalenceConversion using c2dmStability and transient responseDiscrete Root-Locus

Key Matlab Commands used in this tutorial are: c2dm pzmap zgrid dstep stairs rlocusNote: Matlab commands from the control system toolbox are highlighted in red.

IntroductionThe figure below shows the typical continuous feedback system that we have been considering so far inthis 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, shownbelow, that performs same control task as the continuous controller. The basic difference between thesecontrollers is that the digital system operates on discrete signals (or samples of the sensed signal) rather

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (1 de 13) [21/11/2003 05:49:45 p.m.]

Page 413: Matlab Tutorial Ctm

than on continuous signals.

Different types of signals in the above digital schematic can be represented by the following plots.

The purpose of this Digital Control Tutorial is to show you how to work with discrete functions either intransfer function or state-space form to design digital control systems.

Zero-order hold equivalenceIn the above schematic of the digital control system, we see that the digital control system contains bothdiscrete and the continuous portions. When designing a digital control system, we need to find thediscrete equivalent of the continuous portion so that we only need to deal with discrete functions.

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (2 de 13) [21/11/2003 05:49:45 p.m.]

Page 414: Matlab Tutorial Ctm

For this technique, we will consider the following portion of the digital control system and rearrange asfollows.

The clock connected to the D/A and A/D converters supplies a pulse every T seconds and each D/A andA/D sends a signal only when the pulse arrives. The purpose of having this pulse is to require thatHzoh(z) have only samples u(k) to work on and produce only samples of output y(k); thus, Hzoh(z) canbe 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 apiecewise constant input to the continuous system H(s), the sampled output of the continuous systemequals the discrete output. Suppose the signal u(k) represents a sample of the input signal. There aretechniques for taking this sample u(k) and holding it to produce a continuous signal uhat(t). The sketchbelow shows that the uhat(t) is held constant at u(k) over the interval kT to (k+1)T. This operation ofholding uhat(t) constant over the sampling time is called zero-order hold.

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (3 de 13) [21/11/2003 05:49:45 p.m.]

Page 415: Matlab Tutorial Ctm

The zero-order held signal uhat(t) goes through H2(s) and A/D to produce the output y(k) that will be thepiecewise 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.

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (4 de 13) [21/11/2003 05:49:45 p.m.]

Page 416: Matlab Tutorial Ctm

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 toa hold circuit implemented in digital control systems. For information, see Lagging effect associated withthe hold.

Conversion using c2dmThere is a Matlab function called c2dm that converts a given continuous system (either in transferfunction or state-space form) to discrete system using the zero-order hold operation explained above. Thebasic 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')

The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the closed-loopbandwidth 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 samplingtime (Ts) equal to 1/100 sec. Now, create an new m-file and enter the following commands.

M=1;

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (5 de 13) [21/11/2003 05:49:45 p.m.]

Page 417: Matlab Tutorial Ctm

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

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (6 de 13) [21/11/2003 05:49:45 p.m.]

Page 418: Matlab Tutorial Ctm

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];

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

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (7 de 13) [21/11/2003 05:49:45 p.m.]

Page 419: Matlab Tutorial Ctm

Now you have the discrete time state-space model.

Note: For more information on the discrete state-space, please refer to Discrete State-Space.

Stability and transient responseFor continuous systems, we know that certain behaviors results from different pole locations in thes-plane. For instance, a system is unstable when any pole is located to the right of the imaginary axis. Fordiscrete systems, we can analyze the system behaviors from different pole locations in the z-plane. Thecharacteristics 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.

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (8 de 13) [21/11/2003 05:49:45 p.m.]

Page 420: Matlab Tutorial Ctm

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 islocated outside.

For analyzing the transient response from pole locations in the z-plane, the following three equationsused in continuous system designs are still applicable.

where

zeta = Damping ratio●

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (9 de 13) [21/11/2003 05:49:45 p.m.]

Page 421: Matlab Tutorial Ctm

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 usethe 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 windowgives 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

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

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (10 de 13) [21/11/2003 05:49:45 p.m.]

Page 422: Matlab Tutorial Ctm

= 28.2 rad/sec) and using three equations shown above, we can determine that this system should havethe rise time of 0.06 sec, the settling time of 0.65 sec and the maximum overshoot of 45% (0.45 morethan the steady-state value). Let's obtain the step response and see if these are correct. Add the followingcommands to the above m-file and rerun it in the command window. You should get the following stepresponse.

[x] = dstep (numDz,denDz,51);t = 0:0.05:2.5;stairs (t,x)

As you can see from the plot, all of the rise time, the settling time and the overshoot came out to be whatwe expected. We proved you here that we can use the locations of poles and the above three equations toanalyze the transient response of the system.

For more analysis on the pole locations and transient response, see Transient Response.

Discrete Root-LocusThe root-locus is the locus of points where roots of characteristic equation can be found as a single gainis 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 transferfunction in z.

The mechanics of drawing the root-loci are exactly the same in the z-plane as in the s-plane. Recall from

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (11 de 13) [21/11/2003 05:49:45 p.m.]

Page 423: Matlab Tutorial Ctm

the continuous Root-Locus Tutorial, we used the Matlab function called sgrid to find the root-locusregion that gives the right gain (K). For the discrete root-locus analysis, we use the function zgrid thathas the same characteristics as the sgrid. The command zgrid(zeta, Wn) draws lines of constantdamping ratio (zeta) and natural frequency (Wn).

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.4rad/sample (these can be found from design requirements, sampling time (sec/sample) and threeequations shown in the previous section). The following commands draws the root-locus with the lines ofconstant 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)

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (12 de 13) [21/11/2003 05:49:45 p.m.]

Page 424: Matlab Tutorial Ctm

From this plot, you should realize that the system is stable because all poles are located inside the unitcircle. Also, you see two dotted lines of constant damping ratio and natural frequency. The naturalfrequency is greater than 0.3 outside the constant-Wn line, and the damping ratio is greater than 0.4inside 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 thatsatisfies design requirements.

User FeedbackWe would like to hear about difficulties you had with the tutorials, suggestions you have forimprovement, errors that you found, or any other comments that you have. This feedback is anonymous;include your email address if you want a reply.

Digital Control ExamplesCruise Control: RL | Motor Speed: PID | Motor Position: RL | Bus Suspension: SS | Inverted

Pendulum: SS | Pitch Controller: SS | Ball and Beam: PID

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |

Examples

8/26/97 DK

CTM: Digital control Tutorial

http://www.engin.umich.edu/group/ctm/digital/digital.html (13 de 13) [21/11/2003 05:49:45 p.m.]

Page 425: Matlab Tutorial Ctm

Matlab Tutorials IndexHome

About the tutorialsConventions used in the tutorials

About the authors

More about automatic control

Copyright

Matlab BasicsVectors

Functions

Plotting

Polynomials

Matrices

Using M-files in Matlab

Getting help in Matlab

ModelingTrain system

Free body diagram and Newton's law

State-variable and output equations

Matlab representation

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

PID

CTM: Index

http://www.engin.umich.edu/group/ctm/home.text.html (1 de 4) [21/11/2003 05:51:44 p.m.]

Page 426: Matlab Tutorial Ctm

Introduction

The three-term controller

Characteristics of P, I, and D controllers

Open-loop step response

Proportional control

PD control

PI control

PID control

General tips for designing a PID controller

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

Root LocusClosed-loop poles

Plotting the root locus of a transfer function

Choosing a value of K

Closed-loop response

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

Frequency ResponseBode plot

Gain and phase margin

Bandwidth frequency

Closed-loop performance

Nyquist diagram

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

State SpaceState-space equations

Control design using pole placement

Introducing the reference input

Observer design

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

CTM: Index

http://www.engin.umich.edu/group/ctm/home.text.html (2 de 4) [21/11/2003 05:51:44 p.m.]

Page 427: Matlab Tutorial Ctm

Digital ControlIntroduction

Zero-order hold equivalence

Conversion using c2dm

Stability and transient response

Discrete root-locus

Examples: Cruise Control, Motor Speed, Motor Position, Bus Suspension, Inverted Pendulum,Pitch Controller, Ball & Beam

ExamplesCruise Control

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Motor SpeedModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Motor PositionModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Bus Suspension SystemModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Inverted PendulumModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Pitch ControllerModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

Ball & BeamModeling | PID | Root Locus | Frequency Response | State Space | Digital Control

AnimationBus Suspension

Inverted Pendulum

Pitch Controller

Ball & Beam

CommandsExtrasConversions

dstep and stairs

CTM: Index

http://www.engin.umich.edu/group/ctm/home.text.html (3 de 4) [21/11/2003 05:51:44 p.m.]

Page 428: Matlab Tutorial Ctm

Difference equations and system representations

Digital lead and lag

Digital steady-state error

Discrete pole locations and transient response

Functions

jgrid | lnyquist1 | nyquist1 | polyadd | rscale | sigrid | wbw

Lagging effect associated with the hold

Lead/lag

lsim

mfile

Notch filter

plot

PID Bilinear Approximation

Pole/zero cancelation

step

Steady-state error

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control |Examples

8/12/97 CJC

CTM: Index

http://www.engin.umich.edu/group/ctm/home.text.html (4 de 4) [21/11/2003 05:51:44 p.m.]

Page 429: Matlab Tutorial Ctm

Matlab Commands ListThe following list of commands can be very useful for future reference. Use "help" in Matlab for moreinformation on how to use the commands.

In these tutorials, we use commands both from Matlab and from the Control Systems Toolbox, as well assome commands/functions which we wrote ourselves. For those commands/functions which are notstandard in Matlab, we give links to their descriptions. For more information on writing Matlabfunctions, see the function page.

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

Non-standard Matlab commands are highlighted in green.

Command Description

abs Absolute value

acker Compute the K matrix to place the poles of A-BK, see also place

axis Set the scale of the current plot, see also plot, figure

bode Draw the Bode plot, see also logspace, margin, nyquist1

c2dm Continuous system to discrete system

clf Clear figure (use clg in Matlab 3.5)

conv Convolution (useful for multiplying polynomials), see also deconv

ctrb The controllability matrix, see also obsv

deconv Deconvolution and polynomial division, see also conv

det Find the determinant of a matrix

dimpulse Impulse response of discrete-time linear systems, see also dstep

dlqr Linear-quadratic requlator design for discrete-time systems, see also lqr

dlsim Simulation of discrete-time linear systems, see also lsim

dstep Step response of discrete-time linear systems, see also stairs

eig Compute the eigenvalues of a matrix

eps Matlab's numerical tolerance

Matlab Commands List

http://www.engin.umich.edu/group/ctm/extras/commands.html (1 de 4) [21/11/2003 05:51:56 p.m.]

Page 430: Matlab Tutorial Ctm

feedback Feedback connection of two systems.

figure Create a new figure or redefine the current figure, see also subplot, axis

for For, next loop

format Number format (significant digits, exponents)

function Creates function m-files

grid Draw the grid lines on the current plot

gtext Add a piece of text to the current plot, see also text

help HELP!

hold Hold the current graph, see also figure

if Conditionally execute statements

imag Returns the imaginary part of a complex number, see also real

impulse Impulse response of continuous-time linear systems, see also step, lsim, dlsim

input Prompt for user input

inv Find the inverse of a matrix

jgrid Generate grid lines of constant damping ratio (zeta) and settling time (sigma), see alsosgrid, sigrid, zgrid

legend Graph legend

length Length of a vector, see also size

linspace Returns a linearly spaced vector

lnyquist1 Produce a Nyquist plot on a logarithmic scale, see also nyquist1

log natural logarithm, also log10: common logarithm

loglog Plot using log-log scale, also semilogx/semilogy

logspace Returns a logarithmically spaced vector

lqr Linear quadratic regulator design for continuous systems, see also dlqr

lsim Simulate a linear system, see also step, impulse, dlsim.

margin Returns the gain margin, phase margin, and crossover frequencies, see also bode

norm Norm of a vector

nyquist1 Draw the Nyquist plot, see also lnyquist1. Note this command was written to replace theMatlab standard command nyquist to get more accurate Nyquist plots.

obsv The observability matrix, see also ctrb

ones Returns a vector or matrix of ones, see also zeros

place Compute the K matrix to place the poles of A-BK, see also acker

plot Draw a plot, see also figure, axis, subplot.

poly Returns the characteristic polynomial

polyadd Add two different polynomials

Matlab Commands List

http://www.engin.umich.edu/group/ctm/extras/commands.html (2 de 4) [21/11/2003 05:51:56 p.m.]

Page 431: Matlab Tutorial Ctm

polyval Polynomial evaluation

print Print the current plot (to a printer or postscript file)

pzmap Pole-zero map of linear systems

rank Find the number of linearly independent rows or columns of a matrix

real Returns the real part of a complex number, see also imag

rlocfind Find the value of k and the poles at the selected point

rlocus Draw the root locus

roots Find the roots of a polynomial

rscale Find the scale factor for a full-state feedback system

setSet(gca,'Xtick',xticks,'Ytick',yticks) to control the number and spacing of tick marks onthe axes

series Series interconnection of Linear time-independent systems

sgridGenerate grid lines of constant damping ratio (zeta) and natural frequency (Wn), see alsojgrid, sigrid, zgrid

sigrid Generate grid lines of constant settling time (sigma), see also jgrid, sgrid, zgrid

size Gives the dimension of a vector or matrix, see also length

sqrt Square root

ss Create state-space models or convert LTI model to state space, see also tf

ss2tf State-space to transfer function representation, see also tf2ss

ss2zp State-space to pole-zero representation, see also zp2ss

stairs Stairstep plot for discreste response, see also dstep

step Plot the step response, see also impulse, lsim, dlsim.

subplot Divide the plot window up into pieces, see also plot, figure

text Add a piece of text to the current plot, see also title, xlabel, ylabel, gtext

tf Creation of transfer functions or conversion to transfer function, see also ss

tf2ss Transfer function to state-space representation, see also ss2tf

tf2zp Transfer function to Pole-zero representation, see also zp2tf

title Add a title to the current plot

wbw Returns the bandwidth frequency given the damping ratio and the rise or settling time.

xlabel/ylabel Add a label to the horizontal/vertical axis of the current plot, see also title, text, gtext

zeros Returns a vector or matrix of zeros

zgridGenerates grid lines of constant damping ratio (zeta) and natural frequency (Wn), see alsosgrid, jgrid, sigrid

zp2ss Pole-zero to state-space representation, see also ss2zp

zp2tf Pole-zero to transfer function representation, see also tf2zp

Matlab Commands List

http://www.engin.umich.edu/group/ctm/extras/commands.html (3 de 4) [21/11/2003 05:51:56 p.m.]

Page 432: Matlab Tutorial Ctm

10/22/96 YS, 8/18/97 DK

Matlab Commands List

http://www.engin.umich.edu/group/ctm/extras/commands.html (4 de 4) [21/11/2003 05:51:56 p.m.]

Page 433: Matlab Tutorial Ctm

About the TutorialsConventions used in the tutorialsAbout the authorsMore about automatic controlCopyright

Matlab is an interactive program for numerical computation and data visualization; it is used extensively bycontrol engineers for analysis and design. There are many different toolboxes available which extend the basicfunctions of Matlab into different application areas; in these tutorials, we will make extensive use of the ControlSystems Toolbox. Matlab is supported on Unix, Macintosh, and Windows environments; a student version ofMatlab is available for personal computers. For more information on Matlab, contact the Mathworks.

Conventions used in the tutorials

Throughout the tutorials, we will use the following conventions for Matlab input and output.

Matlab input commands will be displayed like this so they can easily be copied and and pasted into the Matlab window. Actual Matlab commands will be shown in red.

Matlab's output will be displayed directly beneath like this.

If you find that the font is too hard to read, you can change the default font in your browser (under thePreferences menu in Netscape).

About the authors

These tutorials were developed by Professor Bill Messner of the Department of Mechanical Engineering atCarnegie Mellon University and Professor Dawn Tilbury of the Department of Mechanical Engineering andApplied Mechanics at the University of Michigan. Funding was provided by the National Science Foundationunder grant number DUE 9554819. Most of the development work was done by undergraduate students LuisOms (CMU), Joshua Pagel (UM), Yanjie Sun (UM), and Munish Suri (CMU) over the summer of 1996 andChristopher Caruana (UM), Dai Kawano (UM), Brian Nakai (CMU) and Pradya Prempraneerach (CMU) overthe summer of 1997. Graduate student Jonathon Luntz (CMU) also contributed. A prototype set of tutorials,developed by Prof. Tilbury, won an Undergraduate Computational Science Award from the UCES Project,

CTM: About the Tutorials

http://www.engin.umich.edu/group/ctm/about.html (1 de 2) [21/11/2003 05:52:59 p.m.]

Page 434: Matlab Tutorial Ctm

sponsored by the U.S. Department of Energy through the Ames Laboratory.

More about automatic control

If you are interested in learning more about the topic of automatic control, there are a multitude of resourcesboth on the WWW and in the library. Professor Dennis Bernstein at the University of Michigan has written AStudent's Guide to Classical Control. There is a Virtual Library on control engineering, and a newsgroupsci.engr.control devoted to control theory and practice.

There are many textbooks which treat the material covered in these tutorials, including:

Richard C. Dorf and Robert M. Bishop, Modern Control Systems, Seventh Edition, Addison-Wesley,Reading, Massachusetts, 1995.

1.

Gene F. Franklin, J. David Powell, and Abbas Emani-Naeini, Feedback Control of Dynamic Systems,Third Edition, Addison-Wesley, Reading, Massachusetts, 1994.

2.

Benjamin C. Kuo, Automatic Control Systems, Seventh Edition, Prentice Hall, Englewood Cliffs, NewJersey, 1995.

3.

Norman S. Nise, Control Systems Engineering, Second Edition, Benjamin-Cummings, Redwood City,California, 1995.

4.

Copyright

Copyright (C) 1997 by the Regents of the University of Michigan. User agrees to reproduce said copyrightnotice on all copies of the software made by the recipient.

All Rights Reserved. Permission is hereby granted for the recipient to make copies and use this software for itsown internal purposes only, under the conditions that this copyright message is retained intact and that nomodifications are made to the software. Recipients of this software may not re-distribute this software outside oftheir own institution. Permission to market this software commercially, to include this product as part of acommercial product, or to make a derivative work for commercial purposes, is explicitly prohibited. All otheruses are also prohibited unless authorized in writing by the Regents of the University of Michigan.

This software is offered without warranty. The Regents of the University of Michigan disclaim all warranties,express or implied, including but not limited to the implied warranties of merchantability and fitness for anyparticular purpose. In no event shall the Regents of the University of Michigan be liable for loss or damage ofany kind, including but not limited to incidental, indirect, consequential, or special damages.

TutorialsBasics | Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Examples

8/14/97 DT

CTM: About the Tutorials

http://www.engin.umich.edu/group/ctm/about.html (2 de 2) [21/11/2003 05:52:59 p.m.]