You are on page 1of 252

Example: Modeling a Cruise Control System

Physical setup and system equations Design requirements Matlab representation Open-loop response Closed-loop transfer function

Physical setup and system equations


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

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

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

m = 1000kg b = 50Nsec/m u = 500N

Design requirements
The next step in modeling this system is to come up with some design criteria. When the engine gives a 500 Newton force, the car will reach a maximum velocity of 10 m/s (22 mph). An automobile should be able to accelerate up to that speed in less than 5 seconds. Since this is only a cruise control system, a 10% overshoot on the velocity will not do much damage. A 2% steady-state error is also acceptable for the same reason.

Keeping the above in mind, we have proposed the following design criteria for this problem: Rise time < 5 sec Overshoot < 10% Steady state error < 2%

Matlab representation
1. Transfer Function
To find the transfer function of the above system, we need to take the Laplace transform of the modeling equations (1). When finding the transfer function, zero initial conditions must be assumed. Laplace transforms of the two equations are shown below.

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

The transfer function of the system becomes

To solve this problem using Matlab, copy the following commands into an new m-file:
m=1000; b=50; u=500; num=[1]; den=[m b];

These commands will later be used to find the open-loop response of the system to a step input. But before getting into that, let's take a look at another representation, the state-space.

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

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

u A B C D

= = = = =

500; [-b/m]; [1/m]; [1]; 0;

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

Open-loop response
Now let's see how the open-loop system responds to a step input. Add the following command onto the end of the m-file written for the tranfer function (the m-file with num and den matrices) and run it in the Matlab command window: step (u*num,den) You should get the following plot:

To use the m-file written for the state-space (the m-file with A, B, C, D matrices), add the following command at the end of the m-file and run it in the Matlab command window: step (A,u*B,C,D) You should get the same plot as the one shown above. From the plot, we see that the vehicle takes more than 100 seconds to reach the steady-state speed of 10 m/s. This does not satisfy our rise time criterion of less than 5 seconds.

Closed-loop transfer function


To solve this problem, a unity feedback controller will be added to improve the system performance. The figure shown below is the block diagram of a typical unity feedback system.

The transfer function in the plant is the transfer function derived above {Y(s)/U(s)=1/ms+b}. The controller will to be designed to satisfy all design criteria. Four different methods to design the controller are listed at the bottom of this page. You may choose on PID, Rootlocus, Frequency response, or State-space.

Example: Solution to the Cruise Control Problem Using PID control


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

m = 1000 b = 50 U(s) = 10 Y(s) = velocity output

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

The design criteria for this problem are: Rise time < 5 sec Overshoot < 10% Steady state error < 2%

To see the original problem setup, see Cruise Control Modeling page. Recall from the PID tutorial page, the transfer function of a PID controller is

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

Proportional control
The first thing to do in this problem is to find a closed-loop transfer function with a proportional control (Kp) added. By reducing the block diagram, the closed-loop transfer function with a proportional controller becomes:

Recall from the PID tutorial page, a proportional controller (Kp) decreases the rise time. This is what we need, if you refer to the Cruise Control Modeling page. For now, let Kp equals 100 and see what happens to the response. Create an new m-file and enter the following commands.
kp=100; m=1000; b=50; u=10; num=[kp]; den=[m b+kp]; t=0:0.1:20; step(u*num,den,t) axis([0 20 0 10])

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

Note: You can use the Matlab command cloop to find the closed-loop response directly from the open-loop transfer function. If you choose to do so, change the m-file to the following and run it in the command window. You should get the same plot as the one shown above.
kp=100; m=1000; b=50; u=10; num=[1]; den=[m b]; [numc,denc]=cloop(kp*num,den,-1); t = 0:0.1:20; step (u*numc,denc,t) axis([0 20 0 10])

As you can see from the plot, both the steady-state error and the rise time do not satisfy our design criteria. You can increase the proportional gain (Kp) to improve the system output. Change the existing m-file so that Kp equal 10000 and rerun it in the Matlab command window. You should see the following plot.

The steady-state error has dropped to near zero and the rise time has decreased to less than 0.5 second. However, this response is unrealistic because a real cruise control system generally can not change the speed of the vehicle from 0 to 10 m/s in less than 0.5 second. The solution to this problem is to choose a proportional gain (Kp) that will give a reasonable rise time, and add an integral controller to eliminate the steady-state error.

PI control
The closed-loop transfer function of this cruise control system with a PI controller is:

Recall from the PID tutrial page, an addition of an integral controller to the system eliminates the steady-state error. For now, let Kp equals 600 and Ki equals 1 and see what happens to the response. Change your m-file to the following.
kp = 600; ki = 1; m=1000; b=50; u=10; num=[kp ki]; den=[m b+kp ki]; t=0:0.1:20; step(u*num,den,t) axis([0 20 0 10])

Note: If you choose to obtain the closed-loop response directly from the open-loop transfer function, enter the following commands instead of the ones shown above:
kp=600; ki=1; m=1000; b=50; u=10; num=[1]; den=[m b]; num1=[kp ki]; den1=[1 0]; num2=conv(num,num1); den2=conv(den,den1); [numc,denc]=cloop(num2,den2,-1); t=0:0.1:20; step(u*numc,denc,t) axis([0 20 0 10])

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

Now adjust both the proportional gain (Kp) and the integral gain (Ki) to obtain the desired response. When you adjust the integral gain (Ki), we suggest you to start with a small value since large (Ki) most likely unstabilize the response.

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

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

PID control
For this particular example, no implementation of a derivative controller was needed to obtain a required output. However, you might want to see how to work with a PID control for the future reference. The closed-loop transfer function for this cruise control system with a PID controller is.

Let Kp equals 1, Ki equals 1, and Kd equals 1 and enter the following commands into an new m-file.
kp=1; ki=1; kd=1; m=1000; b=50; u=10; num=[kd kp ki]; den=[m+kd b+kp ki]; t=0:0.1:20; step(u*num,den,t) axis([0 20 0 10])

Running this m-file should give you the step response of the system with PID controller. Adjust all of Kp, Kd, and Ki until you obtain satisfactory results. We will leave this as an exercise for you to work on. Suggestion: Usually choosing appropriate gains require trial and error processes. The best way to attack this tedious process is to adjust one variable (Kp, Kd, or Ki) at a time and

observe how changing one variable influences the system output. The characteristics of Kp, Kd, and Ki are summarized in the PID Tutorial page.

Example: Solution to the Cruise Control Problem Using Root Locus Method
Proportional Controller Lag 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 sec Overshoot < 10% Steady state error < 2% To see the original problem setup, refer to Cruise Control Modeling page.

Proportional controller
Recall from the Root-Locus Tutorial page, the root-locus plot shows the locations of all possible closed-loop poles when a single gain is varied from zero to infinity. Thus, only a proportional controller (Kp) will be considered to solve this problem. Then, the closed-loop transfer function becomes:

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

where Wn=Natural frequency zeta=Damping ratio Tr=Rise time Mp=Maximum overshoot One of our design criteria is to have a rise time of less than 5 seconds. From the first equation, we see that the natural frequency must be greater than 0.36. Also using the second equation, we see that the damping ratio must be greater than 0.6, since the maximum overshoot must be less than 10%. Now, we are ready to generate a root-locus plot and use the sgrid to find an acceptable region on the root-locus. Create an new m-file and enter the following commands.
hold off; m = 1000; b = 50; u = 10; numo=[1]; deno=[m b]; figure hold; axis([-0.6 0 -0.6 0.6]); rlocus (numo,deno) sgrid(0.6, 0.36) [Kp, poles]=rlocfind(numo,deno) figure hold; numc=[Kp]; denc=[m (b+Kp)]; t=0:0.1:20; step (u*numc,denc,t) axis ([0 20 0 10])

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

The two dotted lines in an angle indicate the locations of constant damping ratio (zeta=0.6); the damping ratio is greater than 0.6 in between these lines and less than 0.6 outside the lines. The semi-ellipse indicates the locations of constant natural frequency (Wn=0.36); the natural frequency is greater than 0.36 outside the semi-ellipse, and smaller than 0.36 inside. If you look at the Matlab command window, you should see a prompt asking you to pick a point on the root-locus plot. Since you want to pick a point in between dotted lines (zeta>0.6) and outside the semi-ellipse (Wn>0.36), click on the real axis just outside the semi-ellipse (around -0.4). You should see the gain value (Kp) and pole locations in the Matlab command window. Also you should see the closed-loop step response similar to the one shown below.

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

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

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

Finally, the closed-loop transfer function becomes:

If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in Lead and Lag Compensator page, the pole and the zero of a lag controller need to be placed close together. Also, it states that the steady-state error will be reduce by a factor of Zo/Po. For these reasons, let Zo equals -0.3 and Po equals -0.03. Create an new m-file, and enter the following commands.
hold off; m = 1000; b = 50; u = 10; Zo=0.3; Po=0.03; numo=[1 Zo]; deno=[m b+m*Po b*Po]; figure hold; axis ([-0.6 0 -0.4 0.4]) rlocus(numo,deno) sgrid(0.6,0.36) [Kp, poles]=rlocfind(numo,deno) figure t=0:0.1:20; numc=[Kp Kp*Zo]; denc=[m b+m*Po+Kp b*Po+Kp*Zo]; axis ([0 20 0 12]) step (u*numc,denc,t)

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

In the Matlab command window, you should see the prompt asking you to select a point on the root-locus plot. Once again, click on the real axis around -0.4. You should have the following response.

As you can see, the steady-state error has been reduced to near zero. Slight overshoot is a result of the zero added in the lag controller. Now all of the design criteria have been met and no further iterations will be needed.

Example: Solution to the Cruise Control Problem Using Frequency Response


Bode plot and open-loop response Proportional controller Lag 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 sec Overshoot < 10% Steady state error < 2% To see the original problem set, see the Cruise Control Modeling page.

Bode plot and open-loop response


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

In order to use a Bode plot, the open-loop response must be stable. Let Kp equals 1 for now and see how the open-loop response looks like. Create an new m-file and enter the following commands.
m = 1000; b = 50; u = 500;

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

step (u*numo,deno) Running this m-file in the Matlab command window should give you the following plot.

As you can see, the open-loop system is stable; thus, we can go ahead and generate the Bode plot. Change the above m-file by deleting step command and add in the following command. bode(numo,deno) Running this new m-file should give you the following Bode plot.

Proportional controller

Let's see what system characteristics we can determine from the above Bode plot. Recall from the Root-Locus Tutorial, the bandwidth frequency (BW) (the frequency at the gain M(dB)=-6~-7.5dB) is roughly equals to the natural frequency (Wn). Using the equation,

the rise time (Tr) for our system can be determined to be extremely long since the gain shown above do not reach -6~-7.5dB. Moreover, we see from the Root-Locus Tutorial that the damping ratio is roughly equals to the phase margin (in degrees) divided by 100.

Since our phase margin is approximately 155 degrees, the damping ratio will be 1.55. Thus, we know that the system is overdamped (since the damping ratio is greater than 1). Finally, the steady-state error can be found from the following equation:

For our system, since the low frequency gain M(dB) is approximately -35dB, the steady-state error should be 98%. We can confirm these by generating a closed-loop step response.

In terms of the Bode plot, if we can shift the gain upward so that both the bandwidth frequency and the low frequency gain increase, then both the rise time and the steady-state error will improve. We can do that by increasing the proportional gain (Kp). Let's increase the proportional gain (Kp) to ,say, 100 and see what happens. Change the m-file to the following.
m = 1000; b = 50; u = 10;

Kp=100; numo=[Kp]; deno=[m b];

bode(numo,deno) Running this m-file in the Matlab command window should give you the following Bode plot.

Now, the low frequency gain is about 6dB (magnitude 2) which predicts the steady-state error of 33%. The bandwidth frequency is about 0.1 rad/sec so that the rise time should be around 18 seconds. Let's take a look at the closed-loop step response and confirm these.

As we predicted, both the steady-state error and the rise time have improved. Let's increase the proportional gain even higher and see what happens. Change the m-file to the following and rerun it. You should get the following Bode plot.

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

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

Lag controller
If you take a look at the "Lag or Phase-Lag Compensator using Frequency Response"section of the Lead and Lag Compensator page, the lag controller adds gain at the low freqencies while keeping the bandwidth frequency at the same place. This is actually what we need: Larger low frequency gain to reduce the steady-state error and keep the same bandwidth frequency to maintain the desired rise time. The transfer function of the lag controller is shown below.

Now, we need to choose a value for a and T. Recall from the "Lag or Phase-Lag Compensator using Frequency Response" page, the steady-state error will decrease by a factor of a. The value T should be chosen so that two corner frequencies will not be placed close together because transient response gets worse. So let a equals 0.05 and T equals 700 and see what happens. Copy the following m-file and run it in the Matlab command window. You should see the following Bode plot.

m = 1000; b = 50; u = 10; Kp=600; numo=[Kp/b]; deno=[m/b 1]; a = 0.05; T=700; numlag = [a*T 1]; denlag = a*[T 1]; newnum = conv(numo,numlag); newden = conv(deno,denlag);

bode(newnum,newden)
figure [numc,denc]=cloop(newnum,newden); step (u*numc,denc)

Since the low frequency gain has increased while the bandwidth frequency stayed the same, the steady-state error should be reduced and the rise time should stay the same. Let's confirm this by generating a closed-loop step response.

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

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

Example: Solution to the Cruise Control Problem Using State Space


Control design using pole placement Reference 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 sec Overshoot < 10% Steady state error < 2% To see the original problem setup , see Cruise Control Modeling page.

Control design using pole placement


The schematic of a full-state feedback system is shown below.

where

K=Control matrix U=-Kv=input R=Reference

Recall from the State-Space Tutorial page, we should use the technique called "pole placement" to obtain the desired output. Poles of a closed-loop system can be found from the characteristic equation: the determinate of [sI-(A-B*K)] matrix. If desired poles can be placed into the system by designing right control matrix (K), then the desired output can be obtained. In this tutorial, poles will be chosen first, then use Matlab to find the corresponding control matrix (K). Now, we need to determine where to place poles for our system. Since our [sI-(A-B*K)] matrix is 1x1, we have only one pole to place. Let the pole to be at -1.5 (arbitrary). Just as in the State-Space Tutorial, the Matlab function called place will be used to find the control matrix K . Create an new m-file and enter the following commands.

m=1000; b=50; t=0:0.1:10; u=500*ones(size(t)); A=[-b/m]; B=[1/m]; C=[1]; D=[0]; x0=[0]; p1=-1.5; K=place(A,B,[p1]) A1=A-B*K; lsim(A1,B,C,D,u,t,x0);

Running this m-file in the Matlab command window should give you the control matrix and the following step response.

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

Reference input
Once again from the State-Space Tutorial, scaling factor called Nbar (the schematic is shown below) should be used to eliminate the steady-state error. Unlike the example in the Tutorial, the command rscale is not applicable for our system. Nbar needs to be determined manually.

After several trial-and-error runs, the Nbar equals 30 provided the desired step response. Copy the following commands to an m-file and run it in the Matlab command window. You should get the step response shown below.
m=1000; b=50; t=0:0.1:10; u=500*ones(size(t)); A=[-b/m]; B=[1/m]; C=[1]; D=[0]; x0=[0]; p1=-1.5 K=place(A,B,[p1]); Nbar=30; A1=A-B*K;

lsim(A1,B*Nbar,C,D,u,t,x0);

As you can see, the steady-state error has been eliminated. The rise time is less than 5 seconds and the overshoot is, in fact, zero. All the design requirements are satisfied.

Digital Control Example: Designing Cruise Control using Root-Locus method


Discrete transfer function Root-Locus in z-plane Compensation using a digital controller
In this digital control version of the cruise control problem, we are going to use the root-locus design method to design the digital controller. If you refer to the Cruise Control: Modeling page, the open-loop transfer function was derived as

where

m = 1000 b = 50 U(s) = 10 Y(s) = velocity output

The design requirements are


Rise time: Less than 5 seconds Overshoot: Less than 10% Steady-state error: Less than 2%

Discrete transfer function


The first step in performing a discrete analysis of a system is to find the discrete equivalent transfer function of the continuous portion. We will convert the above transfer function (Y(s)/U(s)) to the discrete transfer function using the Matlab function called c2dm. To use this c2dm, you need to specify four arguments: Numerator matrix (num), denominator matrix (den), sampling time (Ts), and the 'method'. You should already be familiar with how to enter num and den matrices. The sampling time (Ts), in the unit of sec/sample, should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. For the method, we will use the zero-order hold ('zoh'). Let the sampling time equals 1/50 sec assuming that the bandwidth frequency is 1 rad/sec. Now enter the following commands to an m-file and run it in the command window.
num=[1]; den=[1000 50]; Ts=1/50; [numDz,denDz] = c2dm (num,den,Ts,'zoh')

The following matrices should be returned to the command window.


numDz = 1.0e-04* 0 denDz = 1.0000 -0.9990 0.1999

From these matrices, the discrete transfer function can be written as

Root-Locus in z-plane
Recall from the Digital Control Tutorial, the Matlab function called zgrid should be used to find an acceptable region of the discrete root-locus that gives the desired gain (K). The zgrid requires two arguments: Natural frequency (Wn) and the damping ratio (zeta). These two arguments can be found from the rise time and the overshoot requirements and the following two equations.

where

Wn=Natural frequency (rad/sec) zeta=Damping ratio Tr=Rise time Mp=Maximum overshoot

Since our rise time and overshoot requirements are 5 seconds and 10%, respectively, we can determine that the natural frequency (Wn) must be greater than 0.36 rad/sec and the damping ratio (zeta) must be greater than 0.6. Let's generate the root-locus and use the zgrid to find the acceptable region of the root-locus. But before doing that, if you refer to the Digital Control Tutorial, the natural frequency argument for zgrid needs to be in the unit of rad/sample, so let the Wn = 0.36*Ts = 0.0072 rad/sample. Now add the following commands to the above m-file and rerun it. You should get the following plot.

Wn=0.0072; zeta=0.6;

rlocus (numDz,denDz) zgrid (zeta, Wn)


axis ([-1 1 -1 1])

The dotted line on the right, which is very small and can not be seen in this case, indicates the locations of constant natural frequency (Wn), and the natural frequency is greater than 0.0072 outside the line. The other dotted line indicates the locations of constant damping ratio (zeta), and the damping ratio is greater than 0.6 inside the line. In the above plot, you see that the root-locus is drawn in the desired region. Let's find a gain (K) using the Matlab function rlocfind and obtain the corresponding step response. Add the following commands to the above m-file and rerun it in the Matlab command window.
[K,poles]=rlocfind (numDz,denDz) [numcDz,dencDz] = cloop (K*numDz,denDz); U=10; [x] = dstep (U*numcDz,dencDz,201); figure t=0:0.05:10; stairs (t,x)

In the command window, you should see the prompt asking you to select a point on the rootlocus. Click on the root-locus around +0.9. The gain (K) and the pole location should be returned to the command window. Also, you should see the closed-loop stairstep response shown below.

As you noticed, this response satisfies all of the design requirements. But the gain associated with this response is approximately 4500. The system having this large gain (too much control effort) might not be available in a real physical system , even though it is possible in the Matlab simulation. To obtain a desired response with a reasonable control effort, we will modify the discrete controller.

Compensation using a digital controller


Recall from the continuous Cruise Control: Root-Locus page, the lag controller was added to the system to obtain the desired response. In this digital control version of the cruise control problem, we will modify the existing digital controller by adding a function of the form

There is a guideline to design digital lead and lag compensators. However, design method described there generally applies for improving the system response. In this this particular problem, we are not going to use the method described in that page and use our own educated analysis to design the compensator. First, we need to reduce the gain (K) while keeping the reasonable response. Recall from your control textbook, the gain (K) equals 0 at poles and infinity at zeros. Thus, if we place the pole inside the desired region and pick a locus near that pole, we should have a reasonable response with smaller gain. Moreover, for the system to be stable, all poles must be placed inside the unit circle. Consider these two things, we will place the compensator pole somewhere outside the natural frequency requirement and inside the damping ratio requirement, say at +0.6, and the zero at the left of that pole, say at -0.6. The location of this zero can be changed later, if necessary.

Now we got the discrete compensator transfer function. Let's generate the root-locus and obtain the step response. Create an new m-file and enter the following commands.
num=[1]; den=[1000 50]; Ts=1/50; [numDz,denDz] = c2dm (num,den,Ts,'zoh'); numleadDz=[1 denleadDz=[1 numDnew=conv denDnew=conv Wn=0.0072; zeta=0.6; 0.6]; -0.6]; (numDz,numleadDz); (denDz,denleadDz);

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.

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.

This response is about the same as what we obtained without the additional controller. However, if you check the command window, the gain has decreased to around 1000. This system satisfies all design requirements with the reasonable control effort. Note: A design problem does not necessarily have a unique answer. For practice, you may try other compensators to obtain a better response than the one shown above.

Example: DC Motor Speed Modeling


Physical setup and system equations Design requirements Matlab 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 provides rotary motion and, coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab. * moment of inertia of the rotor (J) = 0.01 kg.m^2/s^2 * damping ratio of the mechanical system (b) = 0.1 Nms * electromotive force constant (K=Ke=Kt) = 0.01 Nm/Amp * electric resistance (R) = 1 ohm * electric inductance (L) = 0.5 H * input (V): Source Voltage * output (theta): position of shaft * The rotor and shaft are assumed to be rigid The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to the rotational velocity by the following equations:

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

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

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

By eliminating I(s) we can get the following open-loop transfer function, where the rotational speed is the output and the voltage is the input.

2. State-Space
In the state-space form, the equations above can be expressed by choosing the rotational speed and electric current as the state variables and the voltage as an input. The output is chosen to be the rotational speed.

Design requirements
First, our uncompensated motor can only rotate at 0.1 rad/sec with an input voltage of 1 Volt (this will be demonstrated later when the open-loop response is simulated). Since the most basic requirement of a motor is that it should rotate at the desired speed, the steady-state error of the motor speed should be less than 1%. The other performance requirement is that the motor must accelerate to its steady-state speed as soon as it turns on. In this case, we want it to have a settling time of 2 seconds. Since a speed faster than the reference may damage the equipment, we want to have an overshoot of less than 5%. If we simulate the reference input (r) by an unit step input, then the motor speed output should have:

Settling time less than 2 seconds Overshoot less than 5%

Steady-state error less than 1%

Matlab representation and open-loop response


1. Transfer Function
We can represent the above transfer function into Matlab by defining the numerator and denominator matrices as follows:

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


J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Now let's see how the original open-loop system performs. Add the following commands onto the end of the m-file and run it in the Matlab command window:
step(num,den,0:0.1:3) title('Step Response for the Open Loop System')

You should get the following plot:

From the plot we see that when 1 volt is applied to the system, the motor can only achieve a maximum speed of 0.1 rad/sec, ten times smaller than our desired speed. Also, it takes the motor 3 seconds to reach its steady-state speed; this does not satisfy our 2 seconds settling time criterion.

2. State-Space
We can also represent the system using the state-space equations. Try the following commands in a new m-file.
J=0.01; b=0.1; K=0.01; R=1; L=0.5; A=[-b/J K/J -K/L -R/L]; B=[0 1/L]; C=[1 0]; D=0; step(A, B, C, D)

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

Example: PID Design Method for DC Motor Speed Control


Proportional control PID control Tuning 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 a DC Motor page. With a 1 rad/sec step input, the design criteria are:

Settling time less than 2 seconds Overshoot less than 5% Steady-stage error less than 1%

Now let's design a PID controller and add it into the system. First create a new m-file and type in the following commands (refer to the Modeling page for the details of getting these commands).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Recall that the transfer function for a PID controller is:

Proportional control
Let's first try using a proportional controller with a gain of 100. Add the following code to the end of your m-file:
Kp=100; numa=Kp*num; dena=den;

To determine the closed-loop transfer function, we use the cloop command. Add the following line to your m-file:
[numac,denac]=cloop(numa,dena); Note that numac and denac are the numerator and the denominator of the overall closed-loop

transfer function. Now let's see how the step response looks, add the following to the end of your m-file, and run it in the command window:
t=0:0.01:5;

step(numac,denac,t) title('Step response with Proportion Control')

You should get the following plot:

PID control
From the plot above we see that both the steady-state error and the overshoot are too large. Recall from the PID tutorial page that adding an integral term will eliminate the steady-state error and a derivative term will reduce the overshoot. Let's try a PID controller with small Ki and Kd. Change your m-file so it looks like the following. Running this new m-file gives you the following plot.
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; Kp=100; Ki=1; Kd=1; numc=[Kd, Kp, Ki]; denc=[1 0]; numa=conv(num,numc); dena=conv(den,denc); [numac,denac]=cloop(numa,dena); step(numac,denac) title('PID Control with small Ki and Kd')

Tuning the gains


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

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

So now we know that if we use a PID controller with Kp=100, Ki=200, Kd=10, all of our design requirements will be satisfied.

Example: Root Locus Design Method for DC Motor Speed Control


Drawing the open-loop root locus Finding the gain using the rlocfind command Adding a lag controller Plotting the closed-loop response
From the main problem, the dynamic equations and the open-loop transfer function of DC Motor Speed are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling a DC Motor page. With a 1 rad/sec step reference, the design criteria are:

Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1%

Now let's design a controller using the root locus method. Create a new m-file and type in the following commands (refer to main problem for the details of getting those commands).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Drawing the open-loop root locus


The main idea of root locus design is to find the closed-loop response from the open-loop root locus plot. Then by adding zeros and/or poles to the original plant, the closed-loop response can be modified. Let's first view the root locus for the plant. Add the following commands at the end of your m-file.
rlocus(num,den) sgrid(.8,0) sigrid(2.3) title('Root Locus without a controller')

The command sigrid is the user-defined function. You need to copy the sigrid.m file to your directly before using it. For more information on how to use functions, refer to functions. Two arguments in the sgrid command are the damping ratio (zeta) term (0.8 corresponds to a overshoot of 5%), and the natural frequency (Wn) term (= 0 corresponds to no rise time criterion) respectively. The single argument in the sigrid command is the sigma term (4.6/2

seconds = 2.3). After you have saved sigma.m file to your directly, run the above m-file in the command window. You should get the root locus plot shown below:

Finding the gain using the rlocfind command


If you recall, we need the settling time and the overshoot to be as small as possible. Large damping corresponds to points on the root locus near the real axis. A fast response corresponds to points on the root locus far to the left of the imaginary axis. To find the gain corresponding to a point on the root locus, we can use the rlocfind command. We can find the gain and plot the step response using this gain all at once. To do this, enter the following commands at the end of your m-file and rerun it.
[k,poles] = rlocfind(num,den) [numc,denc]=cloop(k*num,den,-1); t=0:0.01:3; step(numc,denc,t) title('Step response with gain')

Go to the plot and select a point on the root locus half-way between the real axis and the damping requirement, say at -6+2.5i. Matlab should return the output similar to the following.
selected_point = -5.9596 + 2.0513i k = 10.0934 poles = -6.0000 + 2.0511i -6.0000 - 2.0511i

Note that the values returned in your Matlab command window may not be exactly the same, but should at least have the same order of magnitude. You should also get the following plot:

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

Adding a lag controller


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

This can be done by changing your m-file to look like the following:
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; z1=1; p1=0.01; numa = [1 z1]; dena = [1 p1]; numb=conv(num,numa);

denb=conv(den,dena); rlocus(numb,denb) sgrid(.8,0) sigrid(2.3) title('Root Locus with a lag controller')

numa and dena are the numerator and denominator of the controller, and numb and denb are the numerator and denominator of the overall open-loop transfer function. You should get the following root locus, which looks very similar to the original one:

Plotting the closed-loop response


Now let's close the loop and see the closed-loop step response Enter the following code at the end of your m-file:
[k,poles]=rlocfind(numb,denb) [numc,denc]=cloop(k*numb,denb,-1); t=0:0.01:3; step(numc,denc,t) title('Step response with a lag controller')

Rerun this m-file in the Matlab command window. When prompted to select a point, pick one that is near the damping requirement (diagonal dotted line). You should get the a plot similar to the following:

Your gain should be about 20. As you can see the response is not quite satisfactory. You may also note that even though the gain was selected to correlate with a position close to the damping criterion, the overshoot is not even close to five percent. This is due to the effect of the lag controller kicking in at a later time than the plant. (its pole is slower). What this means is that we can go beyond the dotted lines that represent the limit, and get the higher gains without worrying about the overshoot . Rerun your m-file, place the gain just above the white, dotted line. Keep trying until you get a satisfactory response. It should look similar to the following (we used a gain of around 50):

The steady-state error is smaller than 1%, and the settling time and overshoot requirements have been met. As you can see, the design process for root locus is very much a trial and

error process. That is why it is nice to plot the root locus, pick the gain, and plot the response all in one step. If we had not been able to get a satisfactory response by choosing the gains, we could have tried a different lag controller, or even added a lead controller.

Example: Frequency Design Method for DC Motor Speed Control


Drawing the original Bode plot Adding proportional gain Plotting the closed-loop response Adding a lag controller
From the main problem, the dynamic equations and the open-loop transfer function of DC Motor Speed are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling a DC Motor page. With the 1 rad/sec step input, the design criteria are:

Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1%

Create a new m-file and type in the following commands (refer to the main problem for the details of getting those commands).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Drawing the original Bode plot


The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the Bode plot for the original open-loop transfer function. Add the following code to the end of your m-file, and then run it in the Matlab command window.
bode(num,den)

You should get the following Bode plot:

Adding proportional gain


From the bode plot above, we see that the phase margin can be greater than about 60 degrees if w is less than 10 rad/sec. Let's add gain to the system so the bandwidth frequency is 10 rad/sec, which will give us a phase margin of about 60 degrees. To find the gain at 10 rad/sec, you can try to read it off the Bode plot (it looks to be slightly more than -40 dB, or 0.01 in magnitude). The bode command, invoked with left-hand arguments, can also be used to give you the exact magnitude:

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

To have a gain of 1 at 10 rad/sec, multiply the numerator by 1/0.0139 or approximately 72.


num = 70*num

and rerun your m-file. You should have the following Bode plot:

Plotting the closed-loop response


From the plot above we see that the phase margin is now quite large. Let's see what the closed-loop response look like. Add a % in front of the bode commands and add the following code to the end of your m-file:
[numc,denc]=cloop(num, den, -1); t=0:0.01:10; step(numc,denc,t)

You will see the following plot:

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

Adding a lag controller


We can add a lag controller to reduce the steady-state error. At the same time, we should try to reduce the overshoot by reducing the gain. Let's reduce the gain to 50, and try a lag controller of

which should reduce the steady-state error by a factor of 1/0.01 = 100 (but could increase the settling time). Go back and change your m-file so it looks like the following:
num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; num=50*K; z=1; p=0.1; numa=[1 z]; dena=[1 p]; numb=conv(num,numa); denb=conv(den,dena); bode(numb,denb)

Rerun the file and you will get this plot:

The phase margin looks good. The steady-state error is predicted to be about 1/40dB or 1%, as desired. Close the loop and look at the step response. Add the following lines of code to the end of you m-file and rerun.
[numc,denc]=cloop(numb, denb, -1); t=0:0.01:10; step(numc,denc,t)

Now you have a step response that meets the design requirements. The steady-state error is less than 1%, the overshoot is about 5%, and the settling time is about 2 seconds.

Example: A State-Space Controller for DC Motor Speed


Designing the full-state feedback controller Adding 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 a DC Motor page. With a 1 rad/sec reference added to the system, the design criteria are:

Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1%

Create a new m-file and type in the following commands (refer to the main problem for the details of getting these commands).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; A=[-b/J K/J -K/L -R/L]; B=[0 1/L]; C=[1 0]; D=0;

Designing the full-state feedback controller


Since both of the state variables in our problem are very easy to measure (simply add an ammeter for current and a tachometer for the speed), we can design a full-state feedback controller for the system without worrying about having to add an observer. The schematic for a full-state feedback system is:

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI(A-BK)) where s is the Laplace variable. Since the matrices A and B*K are both 2x2 matrices, there should be 2 poles for the system. By designing a full-state feedback controller, we can move these two poles anywhere we want them. We shall first try to place them at -5 + i and -5-i (note that this corresponds to a zeta = 0.98 which gives 0.1% overshoot and a sigma = 5 which leads to a 1 sec settling time). Once we come up with the poles we want, Matlab will find the controller matrix,K, for us. Simply add the following code to the end of your m-file :
p1 = -5 + i; p2 = -5 - i; K = place(A,B,[p1 p2]);

Now look at the schematic above again. We see that after adding the K matrix into the system, the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-file:
t=0:0.01:3; step(A-B*K,B,C,D,1,t)

Run your m-file in the command window, You should see the following plot:

Adding a reference input


From this plot we see that the steady-state error is too large. In contrast to the other design methods, where we feed back the output and compare it to the reference to compute an error, here we are feeding back both states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain K, and use this new value as our reference for computing the input. This can be done in one step by adding a constant gain Nbar after the reference:

We can find this Nbar factor by using the Matlab command rscale:
Nbar=rscale(A,B,C,D,K) Note that the function rscale is not a standard function in Matlab. You will have to copy it

before you use it. Click here for more information. Now we can plot the step response by adding the following line of code to your m-file:
t=0:0.01:10; step(A-B*K,B*Nbar,C,D,1,t) title('Step Response with K Controller and Nbar')

This time, the steady-state error is much less than 1%, and all the other design criteria have been met as well.

Example: Digital DC Motor Speed Control with PID Control


Continuous to Discrete Conversion PID Controller
In this page, we will consider the digital control version of DC motor speed problem. A digital DC motor model can be obtained from conversion of the analog model, as we will describe. The controller for this example will be designed by a PID method. From the Modeling: a DC Motor, the open-loop transfer function for DC motor's speed was derived as:

Where: *electrical resistance (R) = 1 ohm *electrical inductance (L) = 0.5 H *electromotive force constant (Ke=Kt) = 0.01 Nm/Amp *moment of inertia of the rotor (J) = 0.01 kg*m^2/s^2 *damping ratio of the mechanical system (b) = 0.1 Nms

*input (V): Source Voltage *output (theta dot): Rotating speed *The rotor and shaft are assumed to be rigid The design requirements for 1 rad/sec step input are

Settling time: Less than 2 seconds Overshoot: Less than 5% Steady-state error: Less than 1%

Continuous to Discrete Conversion


The first step in designing a discrete control system is to convert the continuous transfer function to a discrete transfer function. Matlab command c2dm will do this for you. The c2dm command requires the following four arguments: the numerator polynomial (num), the denominator polynomial (den), the sampling time (Ts) and the type of hold circuit. In this example, the hold we will use is the zero-order hold ('zoh'). From the design requirement, let the sampling time, Ts equal to 0.12 seconds, which is 1/10 the time constant of a system with a settling time of 2 seconds. Let's create a new m-file and enter the following commands:
R=1; L=0.5; Kt=0.01; J=0.01; b=0.1; num = Kt; den = [(J*L) (J*R)+(L*b) (R*b)+(Kt^2)]; Ts = 0.12; [numz,denz] = c2dm(num,den,Ts,'zoh')

Running this m-file should return the following:


numz = 0 denz = 1.0000 -1.0877 0.2369 0.0092 0.0057

From these matrices, the discrete transfer function can be written as:

First, we would like to see what the closed-loop response of the system looks like without any control. If you see the numz matrices shown above, it has one extra zero in the front, we have to get rid of it before closing the loop with the Matlab cloop command. Add the following code into the end of your m-file:

numz = [numz(2) numz(3)]; [numz_cl,denz_cl] = cloop(numz,denz);

After you have done this, let's see how the closed-loop step response looks like. The dstep command will generate the vector of discrete output signals and stairs command will connect these signals. Click here for more information. Add the following Matlab code at the end of previous m-file and rerun it.
[x1] = dstep(numz_cl,denz_cl,101); t=0:0.12:12; stairs(t,x1) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response:Original')

You should see the following plot:

PID Controller
Recall that the continuous-time transfer function for a PID controller is:

There are several ways for mapping from the s-plane to z-plane. The most accurate one is . We cannot obtain PID transfer function in this way because the discrete-time transfer function would have more zeroes than poles, which is not realizable. Instead we are going to use the bilinear transformation shown as follows:

Thus we can derive the discrete PID controller with bilinear transformation mapping. For more detail derivation of discrete PID controller, see Discrete PID Controller. Equivalently, the c2dm command in Matlab will help you to convert the continuous-time PID compensator to discrete-time PID compensator by using the "tustin" method in this case. The "tustin"

method will use bilinear approximation to convert to discrete time of the derivative. According to the PID Design Method for the DC Motor page, Kp = 100, Ki = 200 and Kd = 10 are satisfied the design requirement. We will use all of these gains in this example. Now add the following Matlab commands to your previous m-file and rerun it in Matlab window.
% Discrete PID controller with bilinear approximation Kp = 100; Ki = 200; Kd = 10; [dencz,numcz]=c2dm([1 0],[Kd Kp Ki],Ts,'tustin');

Note that the numerator and denominator in c2dm were reversed above. The reason is that the PID transfer function is not proper. Matlab will not allow this. By switching the numerator and denominator the c2dm command can be fooled into giving the right answer. Let's see if the performance of the closed-loop response with the PID compensator satisfies the design requirements. Now add the following code to the end of your m-file and rerun it. You should get the following close-loop stairstep response.
numaz = conv(numz,numcz); denaz = conv(denz,dencz); [numaz_cl,denaz_cl] = cloop(numaz,denaz); [x2] = dstep(numaz_cl,denaz_cl,101); t=0:0.12:12; stairs(t,x2) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response:with PID controller')

As you can see from the above plot, the closed-loop response of the system is unstable. Therefore there must be something wrong with compensated system. So we should take a look at root locus of the compensated system. Let's add the following Matlab command into the end of your m-file and rerun it.
rlocus(numaz,denaz) title('Root Locus of Compensated System')

From this root-locus plot, we see that the denominator of the PID controller has a pole at -1 in the z-plane. We know that if a pole of a system is outside the unit circle, the system will be unstable. This compensated system will always be unstable for any positive gain because there are an even number of poles and zeroes to the right of the pole at -1. Therefore that pole will always move to the left and outside the unit circle. The pole at -1 comes from the compensator, and we can change its location by changing the compensator design. We choose it to cancel the zero at -0.62. This will make the system stable for at least some gains. Furthermore we can choose an appropriate gain from the root locus plot to satisfy the design requirements using rlocfind.Enter the following Matlab code to your m-file.
dencz = conv([1 -1],[1.6 1]) numaz = conv(numz,numcz); denaz = conv(denz,dencz); rlocus(numaz,denaz) title('Root Locus of Compensated System'); [K,poles] = rlocfind(numaz,denaz) [numaz_cl,denaz_cl] = cloop(K*numaz,denaz); [x3] = dstep(numaz_cl,denaz_cl,101); t=0:0.12:12; stairs(t,x3) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response:with PID controller')

The new dencz will have a pole at -0.625 instead of -1, which almost cancels the zero of uncompensated system. In the Matlab window, you should see the command asking you to select the point on the root-locus plot. You should click on the plot as the following:

Then Matlab will return the appropriate gain and the corresponding compensated poles, and it will plot the closed-loop compensated response as follows.

The plot shows that the settling time is less than 2 seconds and the percent overshoot is around 3%. In addition, the steady state error is zero. Also, the gain, K, from root locus is 0.2425 which is reasonable. Therefore this response satisfies all of the design requirements.

Example: Modeling DC Motor Position


Physical Setup System Equations Design Requirements Matlab Representation and Open-Loop Response

Physical Setup
A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab. * moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2 * damping ratio of the mechanical system (b) = 3.5077E-6 Nms * electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp * electric resistance (R) = 4 ohm * electric inductance (L) = 2.75E-6 H * input (V): Source Voltage * output (theta): position of shaft * The rotor and shaft are assumed to be rigid

System Equations
The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to the rotational velocity by the following equations:

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

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

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

By eliminating I(s) we can get the following transfer function, where the rotating speed is the output and the voltage is an input.

However during this example we will be looking at the position, as being the output. We can obtain the position by integrating Theta Dot, therefore we just need to divide the transfer function by s.

2. State Space
These equations can also be represented in state-space form. If we choose motor position, motor speed, and armature current as our state variables, we can write the equations as follows:

Design requirements
We will want to be able to position the motor very precisely, thus the steady-state error of the motor position should be zero. We will also want the steady-state error due to a disturbance, to be zero as well. The other performance requirement is that the motor reaches its final

position very quickly. In this case, we want it to have a settling time of 40ms. We also want to have an overshoot smaller than 16%. If we simulate the reference input (R) by a unit step input, then the motor speed output should have:

Settling time less than 40 milliseconds Overshoot less than 16% No steady-state error No steady-state error due to a disturbance

Matlab representation and open-loop response


1. Transfer Function
We can put the transfer function into Matlab by defining the numerator and denominator as vectors: Create a new m-file and enter the following commands:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Now let's see how the original open-loop system performs. Add the following command onto the end of the m-file and run it in the Matlab command window:
step(num,den,0:0.001:0.2)

You should get the following plot:

From the plot we see that when 1 volt is applied to the system, the motor position changes by 6 radians, six times greater than our desired position. For a 1 volt step input the motor should

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

2. State Space
We can put the state space equations into Matlab by defining the system's matrices as follows:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; A=[0 1 0 0 -b/J K/J 0 -K/L -R/L]; B=[0 ; 0 ; 1/L]; C=[1 0 0]; D=[0];

The step response is obtained using the command


step(A,B,C,D)

Unfortunately, Matlab responds with


Warning: Divide by zero ??? Index exceeds matrix dimensions. Error in ==> /usr/local/lib/matlab/toolbox/control/step.m On line 84 ==> dt = t(2)-t(1);

There are numerical scaling problems with this representation of the dynamic equations. To fix the problem, we scale time by tscale = 1000. Now the output time will be in milliseconds rather than in seconds. The equations are given by
tscale = 1000; J=3.2284E-6*tscale^2; b=3.5077E-6*tscale; K=0.0274*tscale; R=4*tscale; L=2.75E-6*tscale^2; A=[0 1 0 0 -b/J K/J 0 -K/L -R/L]; B=[0 ; 0 ; 1/L]; C=[1 0 0]; D=[0];

The output appears the same as when obtained through the transfer function, but the time vector must be divided by tscale.
[y,x,t]=step(A,B,C,D); plot(t/tscale,y) ylabel('Amplitude') xlabel('Time (sec)')

Example: PID Design Method for the DC Motor Position


Proportional control PID control Tuning 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 a DC Motor page. With a 1 rad/sec step reference, the design criteria are:

Settling time less than 0.04 seconds Overshoot less than 16% No steady-state error No steady-state error due to a disturbance

Now let's design a PID controller and add it into the system. First create a new m-file and type in the following commands(refer to main problem for the details of getting those commands).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Recall that the transfer function for a PID controller is:

Proportional control
Let's first try using a proportional controller with a gain of 1.7. Add the following code to the end of your m-file:
Kp=1.7; numcf=[Kp]; dencf=[1]; numf=conv(numcf,num); denf=conv(dencf,den);

To determine the closed-loop transfer function, we use the cloop command. Add the following line to your m-file:
[numc,denc]=cloop(numf,denf);

Note that numc and denc are the numerator and the denominator of the overall closed-loop transfer function. Now let's see how the step response looks. Add the following to the end of your m-file, and run it in the command window:
t=0:0.001:0.2; step(numc,denc,t)

You should get the following plot:

Now lets take a look at the step disturbance response. Add the following to the end of your m-file, and run it in the command window:
numdcl=conv(numc,1); dendcl=conv(denc,Kp); step(numdcl,dendcl,t);

You should get the following plot:

PID control
From the plots above we see that although the steady-state error looks good the settling time is too large, as is the overshoot. We also see that the steady-state error to a disturbance is large. Recall from PID tutorial page that adding an integral term will eliminate the steadystate error and a derivative term will reduce the overshoot. Let's first try a PI controller to get rid of the disturbance steady state error. Change your m-file so it looks like:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; Kp=1.7; Ki=20; numcf=[Kp Ki]; dencf=[1 0]; numf=conv(numcf,num); denf=conv(dencf,den); [numc,denc]=cloop(numf,denf,-1); t=0:0.001:0.4; step(numc,denc,t)

You should get the following step response:

Lets see what happened to the step disturbance response, add the following to your m-file:
figure numdcl=conv(numc,dencf); dendcl=conv(denc,numcf); step(numdcl,dendcl,t);

You should get the following plot:

Tuning the gains


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

Now we see that the response is faster than before, but the large Ki has worsened the transient response (big overshoot). Let's now try a PID controller to reduce the overshoot. Go back to the m-file and make the following changes to look at the step response.
Kp=17; Ki=200; Kd=0.15; numcf=[Kd Kp Ki]; dencf=[1 0]; numf=conv(numcf,num); denf=conv(dencf,den); [numc,denc]=cloop(numf,denf,-1); t=0:0.001:0.1; step(numc,denc,t)

Rerun it and you should get this plot:

Your step disturbance plot should look like this:

We now see that our step response looks really good, it has less than 16% overshoot and the settling time is roughly 40ms, and there is no steady-state error. However the step disturbance response is now really slow. Lets increase Ki to speed up the disturbance response. Change Ki to 600 in your m-file and rerun the file. You should get the following plots:

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, all of our design requirements will be satisfied.

Example: Root Locus Design Method for DC Motor Position Control

Drawing the open-loop root locus Integral Control Proportional plus Integral Control Proportional plus Integral plus Derivative Control Finding the gain using the rlocfind command and plotting the 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, please refer to the Modeling a DC Motor page. With a 1 rad/sec step reference, the design criteria are:

Settling time less than 0.04 seconds Overshoot less than 16% No steady-state error No steady-state error due to a disturbance

Now let's design a controller using the root locus method. Create a new m-file and type in the following commands (refer to main problem for the details of getting those commands).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Drawing the open-loop root locus

The main idea of root locus design is to find the closed-loop response from the open-loop root locus plot. Then by adding zeros and/or poles to the original plant, the closed-loop response will be modified. Let's first view the root locus for the plant. Add the following commands at the end of your m-file.
rlocus(num,den) sgrid(.5,0) sigrid(100)

The commands sgrid and sigrid are functions. Sgrid is a function in the Matlab tool box, but sigrid is not. You need to copy the sigrid.m file to your directly. Click here to see how to copy sigrid.m into an m-file. The variables in the sgrid command are the zeta term (0.5 corresponds to a overshoot of 16%), and the wn term (no rise time criteria) respectively. The variable in the sigrid command is the sigma term (4/0.04 seconds = 100). Run the above mfile and you should get the root locus plot below:

If you look at the axis scales on this plot, one open-loop pole is very far to the left (further than -1x10^6). This pole does not affect the closed loop dynamics unless very large gains are used, where the system becomes unstable. We will ignore it by changing the axes to zoom in to just the lower-gain portion of the root locus (Click here for more info.) Add the following command to your m-file and re-run it.
axis([-400 100 -200 200])

You should obtain the following plot in which you can see that the closed-loop system will be stable for small gains.

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

Integral Control
Now, let's try using integral control to remove steady-state error to a disturbance. Modify your m-file so it looks like:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; numcf=[1]; dencf=[1 0]; numf=conv(numcf,num); denf=conv(dencf,den); rlocus(numf,denf) sgrid(.5,0) sigrid(100) axis([-400 100 -200 200])

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

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

Proportional plus Integral Control


Now, let's modify the integral controller to a PI controller. Using PI instead of I control adds a zero to the open-loop system. We'll place this zero at s=-20. The zero must lie between the open-loop poles of the system in this case so that the closed-loop will be stable. Change the lines defining the controller (numcf and dencf) in your m-file to the following.
numcf=[1 20]; dencf=[1 0];

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

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

Proportional plus Integral plus Derivative Control


In order to pull the root locus further to the left, to make it faster, we need to place a second open-loop zero, resulting in a PID controller. After some experimentation, we can place the two PID zeros at s=-60 and s=-70. Change the lines defining the controller (numcf and dencf) in your m-file to the following.
numcf=conv([1 60],[1 70]); dencf=[1 0];

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

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

Finding the gain using the rlocfind command


If you recall, we need the settling time and the overshoot to be as small as possible, particularly because of the effect of the extra zero. Large damping corresponds to points on the root locus near the real axis. A fast response corresponds to points on the root locus far to the left of the imaginary axis. To find the gain corresponding to a point on the root locus, we can use the rlocfind command. We can find the gain and plot the step response using this gain all at once. To do this, enter the following commands at the end of your m-file and rerun it.
[k,poles] = rlocfind(numf,denf) [numc,denc]=cloop(k*numf,denf,-1); t=0:0.001:.1; step(numc,denc,t)

Go to the plot and select a point on the root locus on left side of the loop, close to the real axis as shown below with the small black + marks. This will ensure that the response will be

as fast as possible with as little overshoot as possible. These pole locations would indicate that the response would have almost no overshoot, but you must remember that the zero will add some overshoot.

After doing this, you should see the following output in the Matlab command window.
selected_point = -1.3943e+02+ k = 0.1309 poles = 1.0e+06 * -1.4542 -0.0001 + 0.0000i -0.0001 - 0.0000i -0.0001 1.8502e+01i

Note that the values returned in your Matlab command window may not be exactly the same, but should at least have the same order of magnitude. You should also get the following step response plot:

As you can see, the system has an overshoot of approximately 15%, a settling time of approximately 0.04 seconds, and no steady-state error. Let's now look at the disturbance response by computing the closed-loop disturbance transfer function and plotting its step response. Add the following lines to your m-file:
numdcl=conv(numc,dencf); dendcl=conv(denc,numcf); step(numdcl,dendcl,t);

Re-run your m-file, selecting the same point on the root locus, and you will get the following plot.

You can see that the disturbance reaches a steady-state value of zero, and in fact, stays within 0.02 (or 2%) afte 0.04 seconds. Therefore, all the design requirements have been met.

In this example, we placed zeros on the root locus to shape it the way we wanted. While some trial-and error is necessary to place the zeros, it is helpful to understand how the root locus is drawn, and Matlab is used to verify and refine the placement.

Example: Frequency Design Method for DC Motor Position Control


Drawing the original Bode plot Adding an integrator Gain 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 a DC Motor page. With a 1 rad/sec step reference, the design criteria are:

Settling time less than 40 milliseconds Overshoot less than 16% No steady-state error No steady-state error due to a step disturbance

Drawing the original Bode plot

Create a new m-file and type in the following commands (refer to the main problem for the details of getting those commands).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the Bode plot for the original open-loop transfer function. Add the following code to the end of your m-file, and then run the m-file.
w=logspace(0,4,101); bode(num,den,w)

You should get the following Bode plot:

Adding an integrator
Now lets add an integrator for zero steady-state error in response to a step disturbance. Add the following lines to your m-file:
numi=1; deni=[1 0]; numiol=conv(num,numi); deniol=conv(den,deni); bode(numiol,deniol,w)

You should get the following plot:

Gain and Phase Margin Specifications and Controller Design


We want less than a 16% overshoot, so lets compute the damping ratio based on a 16% overshoot. Also the corresponding phase margin is 100 times the damping ratio. From the settling time requirement, we are able to compute the desired bandwidth frequency. Add the following lines to your m-file:
zeta=-log(.16)/sqrt(pi^2+(log(.16))^2); PM=100*zeta; wbw=(4/(0.04*zeta))*sqrt((1-2*zeta^2)+sqrt(4*zeta^4-4*zeta^2+2));

We want to have at least 50 degrees of phase margin, therefore the gain should fall between 6 and -7.5 dB at some frequency after 250 rad/sec. From the Bode plot we see that we must add about 80 degrees of phase and 60 dB of gain at a frequency of 250 rad/sec. The gain plot will then lie between -6 and -7.5 dB and after 244 rad/sec. From the Bode phase plot we can see that there is a pole near 60 rad/sec. We will use a PI controller to put a zero at s=60 to flatten out the phase curve. Add the following lines to your m-file:
numpi=[1 60]; denpi=[1 0]; numpiol=conv(numpi,num); denpiol=conv(denpi,den); bode(numpiol,denpiol,w)

You should see the following plot:

From the bode plot we can see that we need 50 more degrees of phase at a frequency of 250 rad/sec. Lets now try a lead compensator to add exactly 50 degrees of phase. Add the following lines to your m-file:
a=(1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T=1/(wbw*sqrt(a)); numpil = conv([1 60],[T 1]); denpil = conv([1 0],[a*T 1]); numpilol = conv(numpil,num); denpilol = conv(denpil,den); w = logspace(2,3,101); bode(numpilol,denpilol,w)

This new Bode plot now shows that the phase margin is about right at 250 rad/sec, but the gain is too small by about 20 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up by 20 dB we will multiply by a gain of 10. Add the following lines to your m-file:
kpid = 10; bode(kpid*numpilol,denpilol,w)

You should get the following plot:

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:

The overshoot is now too large, however the settling time is better than expected. So let's try another design where the phase margin is larger, say around 70 degrees. Add the following lines to your m-file:
PM=70; a=(1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T=1/(wbw*sqrt(a)); numpil=conv([1 60],[T 1]); denpil=conv([1 0],[a*T 1]); numpilol=conv(numpil,num); denpilol=conv(denpil,den); w=logspace(2,3,101); bode(numpilol,denpilol,w)

You should get the following plot:

This new bode plot shows that the phase margin is good at around 250 rad/sec, but the gain is too small by about 14 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up we will multiply by a gain of 5. Add the following lines to your m-file:
kpid = 5; bode(kpid*numpilol,denpilol,w)

You should get the following plot:

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:

From the step response we now see that the overshoot is fine, but the settling time is too long. Let's try a slightly higher bandwidth. Add the following lines to your m-file:
wbw=300; a=(1-sin(PM*pi/180))/(1+sin(PM*pi/180)); T=1/(wbw*sqrt(a)); numpil=conv([1 60],[T 1]); denpil=conv([1 0],[a*T 1]); numpilol=conv(numpil,num); denpilol=conv(denpil,den); w=logspace(2,3,101); bode(numpilol,denpilol,w)

You should get the following plot:

This new bode plot shows that the phase margin is about right at a frequency of 250 rad/sec, but the gain is too small by about 18 dB. The gain crossover must occur at 240 rad/sec. To bring the gain up we will multiply by a gain of 8. Add the following lines to your m-file:
kpid=8; bode(kpid*numpilol,denpilol,w);

You should get the following plot:

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:

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

Note: As you noticed, the frequency response method for this particular problem requires
substantial amount of trial and error runs. The m-file below is the simplified version of what was done above. After you run this m-file, you will get the last two plots shown above.
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; PM=70; wbw=300; a=(1-sin(PM*pi/180))/(1+sin(PM*pi/180)); T=1/(wbw*sqrt(a)); numpil=conv([1 60],[T 1]); denpil=conv([1 0],[a*T 1]); numpilol=conv(numpil,num); denpilol=conv(denpil,den); kpid=8; w=logspace(2,3,101); bode(kpid*numpilol,denpilol,w)

figure [numpilcl,denpilcl]=cloop(kpid*numpilol,denpilol,-1); t=0:0.001:0.1; step(numpilcl,denpilcl)

Example: A State-Space Controller for DC Motor Position Control


Designing the full-state feedback controller Disturbance Response Adding 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 Modeling DC Motor Position page. With a 1 rad reference added to the system, the design criteria are:

Settling time less than 0.04 seconds Overshoot less than 16% Zero steady-state error to a step input Zero steady-state error to a step disturbance

Create a new m-file and type in the following commands (refer to the main problem for the details of getting those commands).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; A=[0 1 0 0 -b/J K/J 0 -K/L -R/L]; B=[0 ; 0 ; 1/L]; C=[1 0 0]; D=[0];

Designing the full-state feedback controller


Since all of the state variables in our problem are very easy to measure (simply add an ammeter for current, a tachometer for speed, and a potentiometer for position), we can design a full-state feedback controller for the system without worrying about having to add an observer. The schematic for a full-state feedback system is:

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI(A-BKc)) where s is the Laplace variable. Since the matrices A and B*Kc are both 3x3 matrices, there should be 3 poles for the system. By designing a full-state feedback controller, we can move these three poles anywhere we want them. We shall first try to place them at 100 + 100i and -100-100i (note that this corresponds to a zeta = 0.5 which gives 0.16% overshoot and a sigma = 100 which leads to a .04 sec settling time). Once we come up with the poles we want, Matlab will find the controller matrix,Kc, for us. Simply add the following code to the end of your m-file :
p1=-100+100i; p2=-100-100i; p3=-200; Kc=place(A,B,[p1,p2,p3]);

Now look at the schematic above again. We see that after adding the K matrix into the system, the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-file:
t=0:0.001:.05; step(A-B*Kc,B,C,D,1,t)

Run your m-file in the command window, You should see the following plot:

Disturbance Response
In order to get the disturbance response, we must provide the proper input to the system. Physically, a disturbance is a torque which acts on the inertia of the motor. A torque acts as an additive term in the second state equation (which gets divided by J, as do all the other terms in this equation). We can simulate this simply by modifying our closed loop input matrix, B, to have a 1/J in the second row. Add the following line to your m-file and re-run.
step(A-B*Kc,[0;1/J;0],C,D,1,t)

This is not a zero steady-state error to a disturbance, and we will have to compensate for this.

Adding Integral Action


We know that if we put an extra integrator in series with the plant it can remove steady-state error to an input. If the integrator comes before the injection of the disturbance, it will cancel the disturbance in steady state. This changes our control structure so it now resembles the following:

We can model the integrator by augmenting our state equations with an extra state which is the integral of the output. This adds an extra equation which states that the derivative of the integral of theta is theta. This equation will be placed at the top of our matrices. The input, r, now enters the system before the integrator, so it appears in the newly added top equation. The output of the system remains the same.

These equations represent the dynamics of the system before the loop is closed. We will refer to the matrices in this equation as Aa, Ba, Ca, and Da. We will refer to the state vector of the augmented system as xa. Note that the reference, r, does not affect the states (except the integrator state) or the output of the plant - this is expected, since there is no path from the reference to the plant input, u, without implementing the feedback matrix, Kc. In order to find the closed loop equations, we have to look at how the input, u, affects the plant. In this case, it is exactly the same as in the unaugmented equations. Therefore, there is a vector, call it Bau, which replaces Ba when we are treating u as the input. This is just our old B vector with an extra zero added as a first row. Since u=Kc*xa is the input to the plant for the closed loop, but r is the input to the closed loop system, the closed loop equations will depend on both Bau and Ba. The closed loop equations will become:

Now, the integral of the output will be fed back, and will be used by the controller to remove

steady state error to a disturbance. We can now redesign our controller. Since we need to place one closed-loop pole for each pole in the plant, we will place another pole at -300, which will be faster than the rest of the poles. Since the closed-loop system matrix depends on Bau, we will use Bau in the place command rather that Ba. Add the following to your mfile:
Aa=[0 1 0 0 0 0 1 0 0 0 -b/J K/J 0 0 -K/L -R/L]; Ba=[ -1 ; 0 ; 0 ; 0]; Bau=[0 ; 0 ; 0 ; 1/L ]; Ca=[0 1 0 0]; Da=[0]; p4=-300; Kc=place(Aa,Bau,[p1,p2,p3,p4]); t=0:0.001:.05; step(Aa-Bau*Kc,Ba,Ca,Da,1,t)

Run your m-file (or just these new lines) and you will get the following output.

To look at the disturbance response, we apply a similar B matrix as we did previously when simulating the disturbance response.
step(Aa-Bau*Kc,[0 ; 0 ; 1/J ; 0] ,Ca,Da,1,t)

We can see that all of the design specifications have been met by this controller.

Example: Root Locus Design for Digital DC Motor Position Control


Continuous to Discrete Conversion Root Locus Design
In this digital DC motor control version of a DC motor, the controller will be designed by a Root Locus method. A digital DC motor model can obtain from conversion of analog DC motor model, as we will describe. According to the Modeling a DC Motor, the open-loop transfer function for DC motor's position was derived by Laplace Transform as shown.

where: *electric resistance (R) = 4 ohm *electric inductance (L) = 2.75E-6 H *electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp *moment of inertia of the rotor (J) = 3.2284E-6 kg*m^2/s^2 *damping ratio of the mechanical system (b) = 3.5077E-6 Nms *input (V): Source Voltage

*output (sigma dot): Rotating speed *The rotor and shaft are assumed to be rigid The design requirements are:

Settling time: Less than 0.04 seconds Overshoot: Less than 16% Steady-state error: 0 with a step disturbance input

Continuous to Discrete Conversion


The first step in the design of a discrete-time system is to convert a continuous transfer function to a discrete transfer function. Matlab can be used to convert the above transfer function to discrete transfer function by using the c2dm command. The c2dm command requires four arguments: the numerator polynomial (num), the denominator polynomial (den), a sampling time (T) and a type of hold circuit. In this example we will use the zero-order hold (zoh). Refer to the Digital Control Tutorials page for more information. From the design requirement, let the sampling time, T equal to 0.001 seconds, which is 1/100 of the required time constant or 1/40 of the required settling time. Let's create a new m-file and add the following Matlab code:
R=4; L=2.75E-6; K=0.0274; J=3.2284E-6; b=3.5077E-6; num = K; den = [(J*L) (J*R)+(L*b) (R*b)+(K^2) 0]; T = 0.001; [numd,dend] = c2dm(num,den,T,'zoh')

Matlab should return the following:


num = 0 den = 1.0000 -1.9425 0.9425 0.0000 0.0010 0.0010 0.0000

As noticed in above results, both numerator and denominator of discrete transfer function have one extra root at z = 0. Also, we have to get rid of the leading zero coefficient in the numerator. To do this add the following code to cancel out these extra pole and zero to avoid numerical problem in Matlab. Otherwise it will consider both numerator and denominator to be fourth-order polynomial.
numd = numd(2:3); dend = dend(1:3);

Therefore, the discrete-time transfer function from the motor position output to the voltage input is:

We would like to see what the closed-loop response of the system looks like when no controller is added. First, we have to close the loop of the transfer function by using the cloop command. After closing the loop, let's see how the closed-loop stairstep response performs by using the dstep and the stairs commands. The dstep command will provide the vector of discrete step signals and stairs command will connect these discrete signals (click here for more information). Add the following Matlab code at the end of previous m-file and rerun it.
[numd_cl,dend_cl] = cloop(numd,dend); [x1] = dstep(numd_cl,dend_cl,501); t=0:0.001:0.5; stairs(t,x1) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response:Original')

You should see the following plot:

Root Locus Design


The main idea of root locus design is to obtain the closed-loop response from the open-loop root locus plot. By adding zeroes and poles to the original system, the root locus will be modified that leads to a new closed-loop response. First let's see the root-locus for the system itself imposed with an unit circle. In your m-file, add the following commands and rerun it. You should get the root-locus plot as shown below.
rlocus(numd,dend) title('Root Locus of Original System') zgrid(0,0)

To get the zero steady-state error from the closed-loop response, we have to add an integral control. Recall that the integral control in the continuous- time is 1/s. If we use the backward difference approximation for mapping from the s-plane to the z-plane as described by s = 1/(z-1), one pole will be added at 1 on the root locus plot. After adding extra pole at 1, root locus will have three poles near 1, therefore the root locus will move out in the right half. The closed-loop response will be more unstable. Then we must add one zero near 1 and inside the unit circle to cancel out with one pole to pull the root locus in. We will add a pole at z = 0.95. In general, we must at least add as many poles as zeroes for the controller to be causal. Now add the following Matlab commands in to your m-file.
numi = [1 -0.95]; deni = [1 -1]; numad = conv(numd,numi); denad = conv(dend,deni);

Recall from the Digital Control Tutorial page, the zgrid command should be used to find the desired region, which satisfies the design requirement, on the discrete root locus plot. The zgrid command requires two arguments: the natural frequency (Wn) and the damping ratio (zeta). From the design requirement, the settling time is less than 0.04 seconds and the percent overshoot is less than 16%. We know the formulas for finding the damping ratio and natural frequency as shown:

where: OS: the percent overshoot Ts: the settling time The required damping ratio is 0.5 and the natural frequency is 200 rad/sec, but the zgrid command requires a non-dimensional natural frequency. Therefore Wn = 200*T

= 0.2 rad/sample. Add the following Matlab code into the end of your m-file and rerun it.
rlocus(numad,denad); zgrid(0.5,0.2) title('Root Locus of system with integral control')

You should get the following plot:

From the above root locus plot, we can see that system is unstable at all gains because the root locus is outside the unit circle. Moreover, the root locus should be in the region where the damping ratio line and natural frequency cross each other to satisfy the design requirements. Thus we have to pull the root locus in more by first canceling the zero at approximately -0.98, since this zero will add overshoot to the step response. Then we have to add one more pole and two zeroes near the desired poles. After going through some trial and error to yield the root locus in the desired region, one more pole is added at 0.61 and two zeroes are added at 0.76. Add the following command in your m-file and rerun it in Matlab window.
numc = conv([1 -0.76],[1 -0.76]); denc = conv([1 0.98],[1 -0.61]); numoc = conv(numad,numc); denoc = conv(denad,denc); rlocus(numoc,denoc); zgrid(0.5,0.2) title('Root Locus of Compensated System')

The denoc will have a pole at 0.61 instead of -0.98. You should get the following root locus plot:

From the above root locus plot, the root locus is drawn in the desired region. Let's find a gain, K, on the root locus plot by using the rlocfind command and obtain the stairstep response with the selected gain. Enter the following commands at the end of your m-file and rerun it.
K = rlocfind(numoc,denoc) [numd_cl,dend_cl] = cloop(K*numoc,denoc); [x2] = dstep(numd_cl,dend_cl,251); t=0:0.001:0.25; stairs(t,x2) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response of Compensated System')

In the Matlab window, you should see the command asking you to select the point on the root-locus plot. You should click on the plot as the following:

The selected gain should be around 330, and it will plot the closed-loop compensated response as follows.

From the above closed-loop response, the settling time is about 0.05 seconds which is satisfy the requirement, but the percent overshoot is 22% which is too large due to the zeroes. If we select the gain to be larger, the requirements will be satisfied. On the other hand, the problem will be unrealistic and a huge actuator is needed, you can try this yourself by picking a larger gain on the previous root locus plot which will yield an unrealistically sudden step response. So we have to move both one pole and two zeroes a little further to the right to pull root locus in a little bit more, new pole will be

put at 0.7 and two zeroes should be at 0.85. Go back to your m-file and change only numc and denc as shown below and rerun it in Matlab window.
numc = conv([1 -0.85],[1 -0.85]); denc = conv([1 0.98],[1 -0.7]);

Then you should see the following root locus plot.

On the new root locus, you should click on the plot to select a new gain as the following:

The selected gain should be around 450, and then it will plot the closed-loop compensated response as follows:

Now we see that the settling time and percent overshoot meet the design require ments of the system. The settling time is 0.04 seconds and the percent overshoot is about 10%. Then let's take a look at a disturbance response of the closed-loop system by canceling the selected gain, the integral transfer function and controller's transfer function from the closed-loop transfer function. So add the following code into your m-file and rerun it.
numcld = conv(numd_cl,conv(denc,deni)); dencld = conv(dend_cl,conv(K*numc,numi)); [x4] = dstep(numcld,dencld,251); t=0:0.001:.25; stairs(t,x4) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response of Compensated System')

Matlab should return the following plot:

We can see that a response to the disturbance is small (3.3% of the disturbance ) and settles within 2% of the disturbance after 0.04 seconds and eventually reaches zero.

Example: Modeling a Bus Suspension System using Transfer Function


Physical setup Design requirements Equations of motion Transfer function equation Entering equations into Matlab Open-loop response Control Block Diagram

Physical setup
Photo courtesy: Eisenhower Center

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

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.

Design requirements:
A good bus suspension system should have satisfactory road holding ability, while still providing comfort when riding over bumps and holes in the road. When the bus is experiencing any road disturbance (i.e. pot holes, cracks, and uneven pavement),the bus body should not have large oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an estimation. The road disturbance (W) in this problem will be simulated by a step input. This step could represent the bus coming out of a pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and return to a smooth ride within 5 seconds.

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

Transfer Function Equation:

Assume that all of the initial condition are zeroes, so these equations represent the situation when the bus's wheel go up a bump. The dynamic equations above can be expressed in a form of transfer functions by taking Laplace Transform of the above equations. The derivation from above equations of the Transfer Functions G1(s) and G2(s) of output,X1-X2, and two inputs,U and W, are as follows.

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

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

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

Also we can express and derive the above equations in state-space form. Even though this approach will express the first two equations above in the standard form of matrix, it will simplify the transfer function without going through any algebra, because we can use a function ss2tf to transform from state-space form to transfer function form for both inputs

Entering equations into Matlab


We can put the above Transfer Function equations into Matlab by defining the numerator and denominator of Transfer Functions in the form, nump/denp for actuated force input and num1/den1 for disturbance input, of the standard transfer function G1(s) and G2(s): G1(s) = nump/denp G2(s) = num1/den1 Now, let's create a new m-file and enter the following code:
m1=2500; m2=320; k1=80000; k2=500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; 'G(s)1' printsys(nump,denp) num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; 'G(s)2' printsys(0.1*num1,den1)

Open-loop response
We can use Matlab to display how the original open-loop system performs (without any feedback control). Add the following commands into the m-file and run it in the Matlab command window to see the response of unit step actuated force input and unit step disturbance input.Note that the step command will generate the unit step inputs for each input.
step(nump,denp)

From this graph of the open-loop response for a unit step actuated force, we can see that the system is under-damped. People sitting in the bus will feel very small amount of oscillation and the steady-state error is about 0.013 mm. Moreover, the bus takes very unacceptably long time for it to reach the steady state or the settling time is very large. The solution to this problem is to add a feedback controller into the system's block diagram.
step(0.1*num1,den1)

To see some details, you can change the axis:


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

From this graph of open-loop response for 0.1 m step disturbance, we can see that when the bus passes a 10 cm high bump on the road, the bus body will oscillate for an unacceptably long time(100 seconds) with larger amplitude, 13 cm, than the initial impact. People sitting in the bus will not be comfortable with such an oscillation. The big overshoot (from the impact itself) and the slow settling time will cause damage to the suspension system. The solution to this problem is to add a feedback controller into the system to improve the performance. The schematic of the closed-loop system is the following:

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

From the schematic above we see that: Plant = nump/denp F * Plant=num1/den1 so that F=num1/(den1*Plant)

Example:PID Design Method for the Bus Suspension System


Adding a PID controller Plotting the closed-loop response Choosing 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 modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in Matlab by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands).
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2] denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] num1=[-(m1*b2) -(m1*k2) 0 0] den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] numf=num1; denf=nump;

Adding a PID controller


Recall that the transfer function for a PID controller is:

where KP is the proportional gain, KI is the integral gain, and KD is the derivative gain. Let's assume that we will need all three of these gains in our controller. To begin, we might start

with guessing a gain for each: KP=208025, KI=832100 and KD=624075. This can be implemented into Matlab by adding the following code into your m-file:
KD=208025; KP=832100; KI=624075; numc=[KD,KP,KI]; denc=[1 0];

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

This transfer function can be modeled in Matlab by adding the following code into your mfile:
numa=conv(conv(numf,nump),denc); dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Note that the function "polyadd" is not a standard function in Matlab; you will need to copy it to a new m-file to use it. Click here for more information on defining new functions in Matlab. Refer to the bus modeling page, nump = denf as we can see in the matlab command above. Thus we can simplified this transfer function to be the following:
numa=conv(numf,denc); dena=polyadd(conv(denp,denc),conv(nump,numc));

Plotting the closed-loop response


Now we have created the closed-loop transfer function in Matlab that will represent the plant, the disturbance, as well as the controller. Let's see what the closed-loop step response for this system looks like before we begin the control process. Keep in mind that we are going to use a 0.1 m high step as our disturbance, to simulate this, all we need to do is to multiply numa by 0.1. Add the following code into your m-file:
t=0:0.05:5; step(0.1*numa,dena,t) title('closed-loop response to 0.1m high step w/ pid controller')

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

From the graph, the percent overshoot = 9%, which is 4% larger than the requirement, but the settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable output from the beginning, we start with choosing a pole and two zeros for PID controller. A pole of this controller must be at zero and one of the zeros has to be very close to the pole at the origin, at 1. The other zero, we will put further from the first zero, at 3, actually we can adjust the second-zero's position to get the system to fulfill the requirement. Add the following command in the m-file, so you can adjust the second-zero's location and choose the gain to have a rough idea what gain you should use for KD,KP, and KI.
z1=1; z2=3; p1=0; numc=conv([1 z1],[1 z2]) denc=[1 p1] num2=conv(nump,numc); den2=conv(denp,denc); rlocus(num2,den2) title('root locus with PID controller') [K,p]=rlocfind(num2,den2)

you should see the closed-loop poles and zeros on the s-plane like this and you can choose the gain and dominant poles on the graph by yourself:

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

Choosing the gains for the PID controller


Now that we have the closed-loop transfer function, controlling the system is simply a matter of changing the KD,KP,and KI variables. From the figure above, we can see that the system has larger damping than required, but the settling time is very short. This response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can be rectified by adjusting the KD, KP and KI variables to find better response. Let's increase KP,KI,KD by 2 times to see what will happen. Go back to your m-file and multiply KP,KI,KD by 2 and then rerun the program, you should get the following plot:

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 about 5% of the input's amplitude and settling time is 2 seconds less than 5 seconds from requirement. For this problem, it turns out that the PID design method adequately controls the system. This can been seen by looking at the root locus plot. Such a task can be achieved by simply

changing only the gains of a PID controller. Feel free to play around with all three of the parameters,KD,KP and KI, as we suggested, but you will most likely get the response to have either large percent overshoot or very long settling time. But if you do find a good PID design, please email us with your results! We are always interested in different ways to solve our examples; we may include your solution in a future version of these tutorials.

Example: Root Locus Design Method for the Bus Suspension System
Plotting the root locus Adding a notch filter Finding the gain from the root locus Plotting 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 modeling page. If you are interested in running an animation of this example based on the control techniques used in the root locus tutorial please go to the bus suspension animation page after completing this tutorial.

We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in Matlab by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands).
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2] denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] num1=[-(m1*b2) -(m1*k2) 0 0] den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] numf=num1; denf=nump;

We are now ready to design a controller using the root locus design method. First let's see what the open loop poles of the system are:
R=roots(denp)

Matlab should return:


R = -23.9758 +35.1869i -23.9758 -35.1869i -0.1098 + 5.2504i -0.1098 - 5.2504i

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

Plotting the root locus


The main idea of root locus design is to estimate the closed-loop response from the open-loop root locus plot. By adding zeros and/or poles to the original system (adding a compensator), the root locus and thus the closed-loop response will be modified. Let's first view the root locus for the plant. In your m-file, add the following command and then run the file, you should get the root locus plot below:
rlocus(nump,denp) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2)) sgrid(z,0)

Note from the specification, we required the overshoot, %OS, to be less than 5% and damping ratio, zeta, can be find from approximation damping ratio equation, z = log(%OS/100)/sqrt(pi^2+[log(%OS/100)^2]). The commandsgrid is used to overlay desired percent overshoot line on the close-up root locus, you can find more information from commands list. From the plot above, we see that there are two pair of the poles and zeros that are very close together. These pair of poles and zeros are almost on the imaginary axis, they might make the bus system marginally stable, which might cause a problem. We have to make all of the poles and zeros move into the left-half plane as far as possible to avoid an unstable system. We have to put two zeros very close to the two poles on the imaginary axis of uncompensated system for pole-and-zero cancellation. Moreover, we put another two poles further on the real axis to get fast response.

Adding a notch filter


We will probably need two zeros near the two poles on the complex axis to draw the root locus, leaving those poles to the compensator zeros instead of to the plant zeros on the imaginary axis. We'll also need two poles placed far to the left to pull the locus to the left. It seems that a notch filter (2-lead controller) will probably do the job. Let's try putting the zeros at 30 and 60 and the poles at 3+/-3.5i. In your m-file add the following lines of code:
z1=3+3.5i; z2=3-3.5i; p1=30; p2=60; numc=conv([1 z1],[1 z2]); denc=conv([1 p1],[1 p2]); rlocus(conv(nump,numc),conv(denp,denc))

Add % in front of the rlocus(nump,denp) command (Matlab treats any text after a % sign as a comment) and rerun the m-file, you should get a new root locus plot looking like this:

Now let's change axis to see the details of the root locus.
rlocus(conv(nump,numc),conv(denp,denc)) axis([-40 10 -30 30]) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2)) sgrid(z,0)

Finding the gain from the root locus


Now that we have moved the root locus across the 5% damping ratio line, we can choose a gain that will satisfy the design requirements. Recall that we want the settling time and the overshoot to be as small as possible. Generally, to get a small overshoot and a fast response,

we need to select a gain corresponding to a point on the root locus near the real axis and far from the complex axis or the point that the root locus crosses the desired damping ratio line. But in this case, we need the cancellation of poles and zeros near the imaginary axis, so we need to select a gain corresponding to a point on the root locus near zeros and percent overshoot line. There is a method to do this with the rlocfind command in matlab. Enter the following command into the Matlab command window:
[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 -0.6322 -0.0294 -0.0294 -0.0292 -0.0292 + + + 6.1536i 6.1536i 0.1306i 0.1306i 0.0367i 0.0367i

Note that the value returned from your Matlab command window may not be exactly the same, but should at least have the same order of magnitude. This returned value can be used as the gain for the compensator. Add this gain to the system:
numc=k*numc;

Recall that the schematic of the system is the following:

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

To obtain the closed-loop transfer function from W to X1-X2, add the following to your mfile:
numa=conv(conv(numf,nump),denc); dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Note that the function "polyadd" is not a Matlab standard function. You will need to copy it to a new m-file to use it. Click here for more information.

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 m high step as the disturbance. To simulate this, simply multiply numa by 0.1. Add the following commands into the m-file and put % marks in front of all rlocus and rlocfind commands.
step(0.1*numa,dena) title('closed-loop response to 0.1m high step w/ notch filter')

and you should see the following plot:

From this plot we see that when the bus encounters a 0.1 m step on the road, the maximum deviation of the bus body from the wheel (or the road) is about 3.75 mm, and the oscillations settle in 2 seconds. Thus this response is satisfactory. Note: A design problem does not necessarily have an unique answer. Using the root locus method (or any other method) may result in many different compensators. For practice, you may want to go back to the original open-loop root locus and try to find other good ways to add zeros and poles to get a better response. If you are interested in running an animation of the bus suspension example based on the control techniques used in this tutorial please go to the Bus Suspension Animation Page.

Example: Frequency Design Method for the Bus Suspension System


Plotting the frequency response using the bode command Adding a two-lead controller Plotting 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 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in Matlab by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands).
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2] denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] num1=[-(m1*b2) -(m1*k2) 0 0] den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2] numf=num1; denf=nump;

Plotting the frequency response using the bode command


The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the

open-loop Bode plot so that the closed-loop response will also change. Let's first draw the Bode plot for the original open-loop transfer function. Add the following line of code to your m-file and rerun:
w = logspace(-1,2); bode(nump,denp,w)

You should get the following bode plot:

For convenience in representing systems with different natural frequencies of the system, we normalize and scale our finding before plotting the Bode plot, so that the low-frequency asymptote of each term is at 0 dB. This normalization by adjusting the gain, K, makes it easier to add the components of the Bode plot. The effect of K is move the magnitude curve up (increasing K) or down (decreasing K) by an amount 20*logK, but the gain, K, has no effect on the phase curve. Therefore from the previous plot, K must be equal to 100 dB or 100,000 to move the magnitude curve up to 0 dB at 0.1 rad/s. Go back to your m-file and add the following line of code to your m-file before the bode command and rerun:
nump=100000*nump

You should get the following bode plot:

Adding a two-lead controller


From the Bode plot above, we see that the phase curve is concave at about 5 rad/sec. First, we will try to add positive phase around this region, so that the phase will remain above the -180 degree line. Since a large phase margin leads to a small overshoot, we will want to add at least 140 degrees of positive phase at the area near 5 rad/sec. Since one lead controller can add no more than +90 degrees, we will use a two-lead controller:

To obtain T and a, the following steps can be used: 1: Determine the positive phase needed : Since we want 140 degrees total, we will need 70 degrees from each controller. 2: Determine the frequency where the phase should be added: In our case this frequency should be 5.0 rad/sec. 3: Determine the constant a from the equation below, this determines the required space between the zero and the pole for the desired maximum phase added.

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

Now let's put our 2-Lead controller into the system and see what the Bode plot looks like. Add the following code to your m-file, and add a % in front of the previous bode command (if there is one):

numc=conv([1.13426 1], [1.13426 1]); denc=conv([0.035265 1], [0.035265 1]); margin(conv(nump,numc),conv(denp,denc))

You should get the following Bode plot:

Since the Bode plot has a limited phase range (-360-0), the above plot is a little deceiving. The plot is equivalent to the following:
w=logspace(-4,4); [mag,phase,w] = bode(conv(nump,numc),conv(denp,denc),w); subplot(2,1,1); semilogx(w,20*log10(mag)); grid title('Bode plot of system with notch filter') xlabel('Frequency (rad/s)') ylabel('20logM') subplot(2,1,2); semilogx(w,phase); axis([1e-4, 1e4, -180, 360]) grid xlabel('Frequency (rad/s)') ylabel('Phase (degrees)')

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

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

To obtain the closed-loop transfer function from W to X1-X2, the following commands can be added into the m-file:
numa=conv(conv(numf,nump),denc); dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));

Note that the function "polyadd" is not a Matlab standard function. You will need to copy it to a new m-file to use it. Click here for more information. Refer to the bus modeling page, nump = denf as we can see in the matlab command above. Thus we can simplified this transfer function to be the following:
numa=conv(numf,denc); dena=polyadd(conv(denp,denc),conv(nump,numc));

Plotting the closed-loop response


Let's see what the step response looks like now. Keep in mind that we are using a 0.1 m high step as the disturbance. To simulate this, simply multiply numa by 0.1. Add the following code into the m-file and rerun it. Don't forget to put % mark in front of all bode and margin commands!
t=0:0.01:5; step(0.1*numa,dena,t) axis([0 5 -.01 .01])

and you should see the following plot:

The amplitude of response is a lot smaller than the percent overshoot requirement and the settling time also is less than 5 seconds. Since we can see that an amplitude of the output's response less than 0.0001 m or 1% of input magnitude after 4 seconds. Therefore we can say that the settling time is 4 seconds from the above plot. From the Bode plot above, we see that increasing the gain will increase the crossover frequency and thus make the response faster.

We will increase the gain and see if we can get a better response. Go back to your m-file and change numc to numc=4*conv([3.1483 1],[3.1483 1]). Rerun the m-file and you should get the following plot:

From this plot we can see that the percent overshoot is about 0.15 mm less than the previous plot's and the settling time also less than 5 seconds. This response is now satisfactory and no more design iteration is needed.

Example: A State-space Controller for a Bus Suspension System


Designing the full-state feedback controller Plotting 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, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in Matlab by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands). We need to define the A, B, C, D matrices by entering the following into the m-file:
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; A=[0 0 -(b1*b2)/(m1*m2) -(b1/m1) b2/m2 1 k2/m2 0]; B=[0 1/m1 0 (1/m1)+(1/m2) C=[0 0 1 0]; D=[0 0]; 1 0 0 0 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) -((b1/m1)+(b1/m2)+(b2/m2)) -((k1/m1)+(k1/m2)+(k2/m2))

0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2)];

Designing the full-state feedback controller


First, let's design a full-state feedback controller for the system. Assuming for now that all the states can be measured (this assumption is probably not true but is sufficient for this problem), the schematic of the system should be:

The characteristic polynomial for this closed-loop system is the determinant of (sI-(AB[1,0]'K)). Note that it's not sI-(A-BK) because the controller K can only control the force input u but not the road disturbance W. Recall that our B matrix is a 4 x 2 matrix, and we only need the first column of B to control u. For this example, we have to use integral action to achieve zero steady-state error, so we add an extra state which is . Since in reality the bus will eventually reach an equilibrium that yields a zero steady-state error. New states become X1, Y1, and Y2. Also the state-space matrices, A,B,and C, after adding extra state change to be the following:
Aa=[0 1 0 0 0 -(b1*b2)/(m1*m2) 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) -(b1/m1) 0 b2/m2 0 -((b1/m1)+(b1/m2)+(b2/m2)) 1 0 k2/m2 0 -((k1/m1)+(k1/m2)+(k2/m2)) 0 0 0 0 1 0 0]; Ba=[0 0 1/m1 (b1*b2)/(m1*m2) 0 -(b2/m2) (1/m1)+(1/m2) -(k2/m2) 0 0]; Ca=[0 0 1 0 0]; Da=[0 0];

Actually, there is a shortcut for matlab to achieve the same result.


Aa Ba Ca Da = = = = [[A,[0 0 0 0]'];[C, 0]]; [B;[0 0]]; [C,0]; D;

Add the above matlab code into the m-file. In this case, we treated the problem like PID controller design. The integral control is obtained from the new state. The proportional control is obtained from a gain on Y1 or X1-X2. The direct derivative control for the output isn't possible, since derivative of Y1 or X1-X2 isn't a state. Instead we use the derivative of X1 , which is available for feedback. (While X1 maybe hard to measure, could be obtained by integrating the output of an accelerometer mounted on the bus.) It is similar to adding more damping to velocity of oscillation of the bus. Add the following matlab code for controller K in the m-file:
K = [0 2.3e6 5e8 0 8e6]

We arrive with this value of matrix with trial and error by adjusting gain for derivative of X1,Y1 and integral of Y1, as we previously mentioned.

Plotting the closed-loop response


Looking at the schematic above again, we see that after adding the K matrix into the system, the state-space equations become:

We can now obtain the closed-loop response by simply adding the following code into your m-file. Note that we need to multiply B matrix by 0.1 to simulate 0.1 m high step disturbance:
t=0:0.01:2; step(Aa-Ba(:,1)*K,-0.1*Ba,Ca,Da,2,t) title('Closed-loop response to a 0.1 m step')

Running the m-file in the command window, you should see the following plot:

From the plot we see that the percent overshoot and settling time requirements are satisfied. Moreover the steady-state error approaches zero as well. Therefore, we will determine that the response is satisfactory. Feel free to play around with the gain for matrix K. But you will most likely get the response to have either large percent overshoot or very long settling time. But if you do find a better response, please email us with your results! We are always interested in different ways to solve our examples; we may include your solution in a future version of these tutorials.

Example: State Space Design for Digital Bus Suspension Control


Sampling Time Selection Continuous to Discrete Conversion Designing the Controller Simulating 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 the continuous time model to a discrete time model, and then use the pole placement method to design the controller. From the bus suspension state space modeling page, the state space model of the system is:

Where: * body mass (m1) = 2500 kg, * suspension mass (m2) = 320 kg, * spring constant of suspension system(k1) = 80,000 N/m, * spring constant of wheel and tire(k2) = 500,000 N/m, * damping constant of suspension system(b1) = 350 Ns/m. * damping constant of wheel and tire(b2) = 15,020 Ns/m. * control force (u) = force from the controller we are going to design.

The design requirements are: Overshoot: Output (X1-X2) less than 5% of disturbance (W) Settling time: Less than 5 seconds

Sampling Time Selection


The first step in the design of a discrete-time controller is to convert the continuous plant to its discrete time equivalent. First, we need to pick an appropriate sampling time, T. In this example, selection of sampling time is very important since a step in the road surface very quickly affects the output. Physically, what happens is the road surface suddenly lifts the wheel, compressing the spring, K2, and the damper, b2. Since the suspension mass is relatively low, and the spring fairly stiff, the suspension mass rises quickly, increasing X2 almost immediately. Since the controller can only see the effect of the disturbance after a complete sampling period, we have to pick a sampling time, T, short enough so that the output (X1-X2) does not exceed the 5% requirement in one sampling period. To pick the sampling period, we need to closely examine the beginning of the step response. If you remember from the modeling page, the output quickly goes negative in response to a step disturbance, and then begins to oscillate. We will simulate just the beginning of this response by setting the time vector input to the step function to range from 0 to .005. The response to a .1m step input is simulated by multiplying the B matrix by .1. Create a new m-file and enter the following code:
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; A=[0 0 -(b1*b2)/(m1*m2) -(b1/m1) b2/m2 1 k2/m2 0]; B=[0 1/m1 0 (1/m1)+(1/m2) C=[0 0 1 0]; D=[0 0]; 1 0 0 0 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) -((b1/m1)+(b1/m2)+(b2/m2)) -((k1/m1)+(k1/m2)+(k2/m2))

0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2)];

step(A,.1*B,C,D,2,0:0.0001:.005);

This plot shows that the spring, K1 compresses very quickly, and exceeds our requirement of 5mm in response to a .1m step after only a little more than 0.001s. Therefore, we will set T=.0005s in order to give the controller a chance to respond.

Continuous to Discrete Conversion


Now that we have selected a sampling time, we can convert the plant to discrete time. Matlab can be used to convert the above state space model, A,B,C, and D, to a discrete state space model, Ad,Bd,Cd, and Dd, by using c2dm command. The c2dm command can take six arguments: the four state matrices, the sampling time, T, and the type of hold circuit. In this example we will use zero-order hold ('zoh'). Refer to the Digital Control Tutorials page for more information. Add the following code to your m-file:
T=.0005; [Ad Bd Cd Dd]=c2dm(A,B,C,D,T,'zoh')

Matlab should return the following:


Ad = 1.0000 -0.0035 0.0234 0.7705 Bd = 0.0000 0.0000 0.0000 0.0000 0.0000 0.0035 -0.0234 -0.7705 0.0005 1.0000 0.0000 0.0002 0.0000 -0.0124 0.9760 -0.9112 0.0000 -0.0001 0.0005 0.9998

Cd = 0 Dd = 0 0 0 1 0

which represent the new discrete-time state space model.

Adding an Integrator
In this example, we will need to add an integrator to the system in order to drive the steadystate response to zero. We will add this integrator in series with the plant. This will have the effect of adding another state to the plant. We will add the integrator by representing it in state space and the using the series command. This command takes the A,B,C, and D matrices of the two systems to be connected in series as arguments and returns a new set of A,B,C, and D matrices. An integrator in discrete time state space can be represented as a trapezoidal approximation of integration over each sample period as follows:

To add this, add the following commands in your m-file:


Ai=1; Bi=1; Ci=T; Di=T/2; [Ada,Bda,Cda,Dda]=series(Ad,Bd,Cd,Dd,Ai,Bi,Ci,Di)

Matlab will return a new set of integrator-augmented state matrices, with dimension 5 rather than dimension 4. Unfortunately, the output of these equations is now the new integrated state. We must change the output Cda matrix to output the original output state. Add the following line:
Cda=[Cd 0]

Since the augmented state is the last state, this outputs the same state as the unaugmented equations.

Designing the Controller


The structure of the controller is similar to the structure of the continuous-time state space controller. We will now use the place command to compute the gain matrix, K, which will, in feedback, give us sny desired closed-loop poles. We first need to decide where to place the closed-loop poles. Since we get to place all five of the closed-loop poles, we can be very selective about where to place them. In particular, we can place them to cancel all of the plant zeros, as well as give us the desired response. First, we will find the plant zeros by converting the plant's digital state equations to a transfer function, and then finding the roots of the numerator. We will use the ss2tf command which

takes the state matrices and the selected input as arguments and outputs a transfer function numerator and denominator. Add the following code to your m-file:
[num,den]=ss2tf(Ad,Bd,Cd,Dd,1); zeros=roots(num)

Matlab will return the following:


zeros = 0.9986 + 0.0065i 0.9986 - 0.0065i -0.9929

We will select these three zeros as three of our desired closed-loop poles. One of the other two will be selected at .9992 since a pole there settles in approximately 10000 samples (or 5 seconds). The last pole will be selected at z=.2 since this is sufficiently fast to be insignificant. Add the following code to your m-file:
p1=.97+.13i; p2=.97-.13i; p3=-.87; p1=zeros(1); p2=zeros(2); p3=zeros(3); p4=.9992; p5=.5; K=place(Ada,Bda*[1;0],[p1 p2 p3 p4 p5])

Matlab will return the following:


place: ndigits= 19 K = 1.0e+09 * 0.0548 0.0000 1.0897 0.0011 0.0009

Simulating the Closed-Loop Response


We can use the dstep command to simulate the closed-loop response. Since multiplying the state vector by K in our controller only returns a single signal, u, we need to add a row of zeros to K by multiplying it by [1 0]T. This is identical to what was done in the continuous design to compensate for the fact that there are two inputs to the plant, but only one is a control input. We will simulate with a negative .1m step disturbance in the road to give us a positive deflection of the bus for aesthetic reasons. Enter the following code into your m-file:
yout=dstep(Ada-Bda*[1 0]'*K,-.1*Bda,Cda,-.1*Dda,2,10001); t=0:.0005:5; stairs(t,yout);

You should see the following plot.

We can see in this plot, that the overshoot is less than 5mm, and the response settles well within 5 seconds.

Example: Modeling an Inverted Pendulum


Problem setup and design requirements Force analysis and system equations Matlab representation and the open-loop response

Problem setup and design requirements


The cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F. Determine the dynamic equations of motion for the system, and linearize about the pendulum's angle, theta = Pi (in other words, assume that pendulum does not move more than a few degrees away from the vertical, chosen to be at an angle of Pi). Find a controller to satisfy all of the design requirements given below.

For this example, let's assume that M mass of the cart 0.5 kg m mass of the pendulum 0.5 kg b friction of the cart 0.1 N/m/sec l length to pendulum center of mass 0.3 m I inertia of the pendulum 0.006 kg*m^2 F force applied to the cart x cart position coordinate theta pendulum angle from vertical For the PID, root locus, and frequency response sections of this problem we will be only interested in the control of the pendulums position. This is because the techniques used in these tutorials can only be applied for a single-input-single-output (SISO) system. Therefore, none of the design criteria deal with the cart's position. For these sections we will assume that the system starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its upright position within 5 seconds, and never move more than 0.05 radians away from the vertical.

The design requirements for this system are:


Settling time of less than 5 seconds. Pendulum angle never more than 0.05 radians from the vertical.

However, with the state-space method we are more readily able to deal with a multi-output system. Therefore, for this section of the Inverted Pendulum example we will attempt to control both the pendulum's angle and the cart's position. To make the design more challenging we will be applying a step input to the cart. The cart should achieve it's desired position within 5 seconds and have a rise time under 0.5 seconds. We will also limit the pendulum's overshoot to 20 degrees (0.35 radians), and it should also settle in under 5 seconds. The design requirements for the Inverted Pendulum state-space example are:

Settling time for x and theta of less than 5 seconds. Rise time for x of less than 0.5 seconds. Overshoot of theta less than 20 degrees (0.35 radians).

Force analysis and system equations


Below are the two Free Body Diagrams of the system.

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

Note that you could also sum the forces in the vertical direction, but no useful information would be gained. Summing the forces in the Free Body Diagram of the pendulum in the horizontal direction, you can get an equation for N:

If you substitute this equation into the first equation, you get the first equation of motion for this system: (1) To get the second equation of motion, sum the forces perpendicular to the pendulum. Solving the system along this axis ends up saving you a lot of algebra. You should get the following equation:

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

Combining these last two equations, you get the second dynamic equation: (2) Since Matlab can only work with linear functions, this set of equations should be linearized about theta = Pi. Assume that theta = Pi + ( represents a small angle from the vertical upward direction). Therefore, cos(theta) = -1, sin(theta) = -, and (d(theta)/dt)^2 = 0. After linearization the two equations of motion become (where u represents the input):

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

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

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

then substituting into the second equation:

Re-arranging, the transfer function is:

where,

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

2. State-Space
After a little algebra, the linearized system equations equations can also be represented in state-space form:

The C matrix is 2 by 4, because both the cart's position and the pendulum's position are part of the output. For the state-space design problem we will be controlling a multi-output system so we will be observing the cart's position from the first row of output and the pendulum's with the second row.

Matlab representation and the open-loop response


1. Transfer Function
The transfer function found from the Laplace transforms can be set up using Matlab by inputting the numerator and denominator as vectors. Create an m-file and copy the following text to model the transfer function:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q]

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

-(M+m)*m*g*l/q

Your output should be:


num = 4.5455 den = 1.0000

0 0.1818 -31.1818 -4.4545

To observe the system's velocity response to an impulse force applied to the cart add the following lines at the end of your m-file:
t=0:0.01:5; impulse(num,den,t) axis([0 1 0 60])

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

You should get the following velocity response plot:

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

1. State-Space
Below, we show how the problem would be set up using Matlab for the state-space model. If you copy the following text into a m-file (or into a '.m' file located in the same directory as Matlab) and run it, Matlab will give you the A, B, C, and D matrices for the state-space model and a plot of the response of the cart's position and pendulum angle to a step input of 0.2 m applied to the cart.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies A = [0 1 0 0; 0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0; 0 0 0 1; 0 -(m*l*b)/p m*g*l*(M+m)/p 0] B = [ 0; (i+m*l^2)/p; 0; m*l/p] C = [1 0 0 0; 0 0 1 0] D = [0; 0] T=0:0.05:10; U=0.2*ones(size(T));

[Y,X]=lsim(A,B,C,D,U,T); plot(T,Y) axis([0 2 0 100])

You should see the following output after running the m-file:
A = 0 0 0 0 B = 0 1.8182 0 4.5455 C = 1 0 D = 0 0 0 0 0 1 0 0 1.0000 -0.1818 0 -0.4545 0 2.6727 0 31.1818 0 0 1.0000 0

The blue line represents the cart's position and the green line represents the pendulum's angle. It is obvious from this plot and the one above that some sort of control will have to be designed to improve the dynamics of the system. Four example controllers are included with these tutorials: PID, root locus, frequency response, and state space. Select from below the one you would like to use. Note: The solutions shown in the PID, root locus and frequency response examples may not yield a workable controller for the inverted pendulum problem. As stated previously, when we put this problem into the single-input, single-output framework, we ignored the x position of the cart. The pendulum can be stabilized in an inverted position if the x position is constant or if the cart moves at a constant velocity (no acceleration). Where possible in these examples, we will show what happens to the cart's position when our controller is

implemented on the system. We emphasize that the purpose of these examples is to demonstrate design and analysis techniques using Matlab; not to actually control an inverted pendulum.

Example: Solution to the Inverted Pendulum Problem Using PID Control


Open-loop Representation Closed-loop transfer function Adding the PID controller What 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.

Open-loop Representation
The first thing to do when using PID control in Matlab is to find the transfer function of the system and to check to see if it makes sense. The transfer function found from the Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab by inputting the numerator and denominator as vectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copy the following text to model the transfer function:

M m b i g l

= = = = = =

.5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q]

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

-(M+m)*m*g*l/q

Your output should be:


num = 4.5455 den = 1.0000

0 0.1818 -31.1818 -4.4545

Closed-loop transfer function


The control of this problem is a little different than the standard control problems you may be used to. Since we are trying to control the pendulum's position, which should return to the vertical after the initial disturbance, the reference signal we are tracking should be zero. The force applied to the cart can be added as an impulse disturbance. The schematic for this problem should look like the following.

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

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

Adding the PID controller


This closed-loop transfer function can be modeled in Matlab by copying the following code to the end of your m-file (whether your using the transfer function from the Laplace transforms or from the state-space representation):
kd = 1; k = 1; ki = 1; numPID = [kd k ki]; denPID = [1 0]; numc = conv(num,denPID) denc = polyadd(conv(denPID,den),conv(numPID,num))

Note: Non-standard Matlab commands used in this example are highlighted in green. The function polyadd is not in the Matlab toolbox. You will have to copy it to a new m-file to use it. This transfer function assumes that both derivative and integral control will be needed along with proportional control. This does not have to be the case. If you wanted to start with PI control, just remove the kd term from numPID. If you wanted to start with PD control, just remove the ki term from numPID and change denPID to equal [1]. Assuming you do not change the PID control, you should get the following closed-loop numerator and denominator in the Matlab command window:
numc = 4.5455 denc = 1.0000 0 4.7273 0 -26.6363 0.0910 0

Now we can begin the actual control of this system. First let's see what the impulse response looks like with the numbers we already have. Enter the following code to the end of your mfile:
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. Increase the k variable to see what effect it has on the response. If you set k=100, and set the axis to axis([0, 2.5, -0.2, 0.2]), you should get the following velocity response plot:

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

As you can see, the overshoot has been reduced so that the pendulum does not move more than 0.05 radians away from the vertical. All of the design criteria have been met, so no further iteration is needed.

What happens to the cart's position?


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

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

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

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

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

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page. The pole/zero at the origin cancelled out of the transfer function for Phi, has been put back in. So that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the command window:

M m b i g l

= = = = = =

.5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

q = (M+m)*(i+m*l^2)-(m*l)^2; num1 = [m*l/q 0 0]; den1 = [1 b*(i+m*l^2)/q num2 = [(i+m*l^2)/q den2 = den1 kd = 20; k = 100; ki = 1; numPID = [kd k ki]; denPID = [1 0]; 0

-(M+m)*m*g*l/q -m*g*l/q];

numc = conv(num2,denPID); denc = polyadd(conv(denPID,den2),conv(numPID,num1)); t=0:0.01:5; impulse(numc,denc,t)

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

Example: Solution to the inverted pendulum problem using Root Locus method
Transfer functions Root locus design Lead-lag controller What 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.

Transfer functions
The rlocus command in Matlab can find the root locus for a system described by state-space equations or by a transfer function. For this problem it will be easier in the long run to use a transfer function (the reason for this will become clear later). The transfer function found from the Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab by inputting the numerator and denominator as vectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copy the following text to model the transfer function:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

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

%simplifies input -b*m*g*l/q]

-(M+m)*m*g*l/q

Your output should be:


num = 4.5455 den = 1.0000

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

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

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

Root locus design

This closed-loop transfer function can be modeled in Matlab. The first thing to do is to look at the root locus for the plant by itself, with no compensator. To pick the gain for the proportional control, remember that the settling time has to be less than 5 seconds. This implies that sigma should be more than 4.6/5=0.92. We can put this criteria right on the root locus using the sigrid function, which has to be copied to a m-file. To do this, copy the following code to the end of your m-file (whether your using the transfer function from the Laplace transforms or from the state-space representation): rlocus(num,den) sigrid(0.92)
axis([-6 6 -6 6])

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

Note: Non-standard Matlab commands used in this example are highlighted in green. You should see the following rlocus plot:

As you can see, one of the roots of the closed-loop transfer function is in the right-half-plane. This means that the system will be unstable. Look back at the root locus to see why. Part of the root locus lies between the origin and the pole in the right-half-plane. No matter what gain you chose, you will always have a closed-loop pole in this region, making your impulse response unstable. To solve this problem, we need to add another pole at the origin so all the zeros and poles at the origin will cancel each other out and multiple roots will be created in this right-half-plane region. The multiple roots can then be drawn into the left-half-plane to complete the design. Add the following to your m-file:
p1 = 0;

dentemp = [1 p1]; num2 = num; den2 = conv(den, dentemp);

rlocus(num2,den2) sigrid(0.92)
axis([-10 10 -10 10])

You should get the following root locus plot with multiple roots in the right-half-plane:

Now we can begin trying to draw the branches of the root locus into the left half plane. Enter the following two commands into the command window:
roots(num2) roots(den2)

If you are using the laplace transform transfer function, you should get the following output in the Matlab command window,
ans = 0 ans = 0 -5.6041 5.5651 -0.1428

As you can see there are four poles and only one zero. This means there will be three asymptotes: one along the real axis in the negative direction, and the other two at 120 degree angles from this. For the state-space transfer function there will be one extra pole and zero. This configuration will never have the multiple roots in the left-half-plane. We must reduce the number of asymptotes from three to two by adding one more zero than pole the controller. If just a zero is added, then the intersection of the asymptotes (alpha) would be [(5.6041+5.5651-0.1428+0+0)-(0+0+z2)]/2. This means the two asymptotes will leave the real

axis at roughly -0.1-(1/2)z2. Making z2 as small as possible (assume near the origin) will not pull the multiple roots far enough into the left-half-plane to meet the design requirements (asymptotes will leave at about -0.1).

Lead-lag controller
The solution to this problem is to add another pole far to the left of the other poles and zeros. To keep the right number of asymptotes, another zero should be added as well. The placement of the added pole and zeros is not important except that the pole should be relatively large and the zeros should be relatively small. Try the m-file below to see what effect the poles and zeros have on the root locus. The polyadd function needs to be copied to the directory you are running Matlab in.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q];

q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q z1 p1 z2 p2 = = = = 3; 0; 4; 50;

-(M+m)*m*g*l/q

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]) figure rlocus(num3,den3) sigrid(0.92) axis([-10 10 -10 10]) [k,poles]=rlocfind(num3,den3) figure numc = conv(conv(num,denlead),denlag); denc = polyadd(k*num3,den3); impulse(numc,denc) axis([0 2 -0.05 0.05])

The poles and zeros were found by trial and error. The only things to keep in mind was that one pole had to be at the origin, the other had to be far to the left, and the two zeros had to be

small. Furthermore, I found that if the two zeros were close together and to the right of the farthest left plant pole, the response was better. You should see first the following root locus plot with the zeros and poles listed above:

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

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

The response now meets all of the requirements, so no further iteration is needed.

What happens to the cart's position?


At the beginning on this solution page, the block diagram for this problem was given. The diagram was not entirely complete. The block representing the the position was left out because that part was not being controlled. It would be interesting though, to see what is happening to the cart's position when the controller for the pendulum's angle is in place. To see this, we need to consider the actual system block diagram:

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

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

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

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

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page. The pole/zero at the origin canceled out of the transfer function for Phi, has been put back in. So that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the command window:

M m b i g l

= = = = = =

.5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

q = (M+m)*(i+m*l^2)-(m*l)^2; num1 = [m*l/q 0 0]; den1 = [1 b*(i+m*l^2)/q num2 = [(i+m*l^2)/q den2 = den1 z1 p1 z2 p2 = = = = 3; 0; 4; 50; 0

-(M+m)*m*g*l/q -m*g*l/q];

numlag = [1 z1]; denlag = [1 p1]; numlead = [1 z2]; denlead = [1 p2]; num3 = conv(conv(num1, numlead), numlag); den3 = conv(conv(den1, denlead), denlag); subplot(1,1,1);rlocus(num3,den3) axis([-10 10 -10 10]) [k,poles]=rlocfind(num3,den3) numc = conv(conv(num1,denlead),denlag); denc = polyadd(k*num3,den3); t=0:0.01:6; subplot(2,1,1); impulse(numc,denc,t) axis([0 6 -0.05 0.05]) num4 = conv(num2,den3); den4 = polyadd(conv(den1,den3),k*conv(den1,num3)); subplot(2,1,2); impulse(num4,den4,t) axis([0 6 -0.1 0.1])

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

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

Example: Solution to the Inverted Pendulum Problem Using Frequency Response Method
Open-loop Representation Closed-loop response with no compensation Closed-loop response with compensation What 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 were removed from the transfer function. However, in this example they will be left in for reasons that will become clear later.

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

Settling time of less than 5 seconds. Pendulum should not move more than 0.05 radians away from the vertical.

To see how this problem was originally set up, consult the inverted pendulum modeling page. Note: Before trying to work through this problem, it should be noted that this is a complicated problem to solve with the frequency response method. As you will soon find out, this problem has a pole in the right-half-plane, making it unstable. The frequency response method works best when the system is stable in the open loop. For this reason I would not suggest trying to follow this example if you are trying to learn how to use frequency response. This problem is for people who want to learn how to solve frequency response problems that are more complicated.

Open-loop Representation
The frequency response method uses the bode command in Matlab to find the frequency response for a system described by a transfer function in bode form. The transfer function found from the Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab by inputting the numerator and denominator as vectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copy the following text to model the transfer function:
M m b i = = = = .5; 0.2; 0.1; 0.006;

g = 9.8; l = 0.3; q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0 0] den = [1 b*(i+m*l^2)/q %simplifies input -b*m*g*l/q 0]

-(M+m)*m*g*l/q

Your output should be:


num = 4.5455 den = 1.0000

0 0.1818

0 -31.1818 -4.4545 0

Closed-loop response with no compensation


We will now design an inverted pendulum controller for an impulse force using Nyquist diagrams (we cannot use Bode plots because the system is unstable in open loop). Let's begin by looking at the block diagram for this system:

If you try to model this system in Matlab, you will have all sorts of problems. The best way to use Matlab to solve this problem is to first change the schematic to something that can be modeled much more easily. Rearranging the picture, you can get the following new schematic:

Now we can begin our design. First, we will look at the poles and zeros of this function:
x = roots(num) y = roots(den) x = 0 0 y = 0

-5.6041 5.5651 -0.1428

As you already know, we have a pole-zero cancellation at the origin, as well as one positive, real pole in the right-half plane. This means that we will need one anti-clockwise encirclement of -1 in order to have a stable closed-loop system (Z = P + N; P = 1, N = -1). The following m-file will be very useful for designing our controller. Please note that you will need the function polyadd.m to run this m-file. Copy and paste the function from your browser to a m-file in your directory (make sure the function command starts in the first column of the m-file). Note: Non-standard Matlab commands used in this example are highlighted in green.
function[ ] = pend() clf figure(1) clf %define TF num = [4.5455 den =[1.0000 figure(1) 0 0.1818 -31.1818 0]; -4.4545

0];

%ask numc denc k

user for controller = input('numc?.........'); = input('denc?.........'); = input('K?............');

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

Note: Matlab commands from the control system toolbox are highlighted in red. With this m-file we will now view the uncompensated system's Nyquist diagram by setting the controller numerator, denominator and gain equal to one. Enter pend at the command prompt, and enter 1 for numc, denc, and K. You should see the following plots in your screen:
numc?.........1 denc?.........1

K?............1

Closed-loop response with compensation


The system is unstable in closed loop (no encirclements of -1). Our first step will be to add an integrator to cancel the extra zero at the origin (we will then have two poles and two zeros at the origin). Use the pend command again.
numc?.........1 denc?.........[1 0] K?............1

Notice that the nyquist diagram encircles the -1 point in a clockwise fashion. Now we have two poles in the right-half plane (Z= P + N = 1 + 1). We need to add phase in order to get an anti-clockwise encirclement. We will do this by adding a zero to our controller. For starters, we will place this zero at -1.
numc?.........[1 1] denc?.........[1 0] K?............1

as you can see, this wasn't enough phase. The encirclement around -1 is still clockwise. We are going to need to add a second zero.
numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............1

We still have one clockwise encirclement of the -1 point. However, if we add some gain, we can make the system stable by shifting the nyquist plot to the left, moving the anti-clockwise circle around -1, so that N = -1. help impulse
numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............10

As you can see, the system is now stable. We can now concentrate on improving the response. We can modify the poles of the controller in order to do this. We have to keep in mind that small poles (close to the origin) will affect the response at small frequencies, while larger poles (farther from the origin) will affect the response at high frequencies. When designing via frequency response, we are interested in obtaining simple plots, since they will be easier to manipulate in order to achieve our design goal. Therefore, we will use these concepts to 'flatten' the frequency response (bode plot). At the same time, you will notice that the nyquist diagram will take an oval shape. If we try different combinations of poles and gains, we can get a very reasonable response. (Enter the command axis([0 5 -0.05 0.1]) after the pend command.)

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

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

What happens to the cart's position?


At the beginning on this solution page, the block diagram for this problem was given. The diagram was not entirely complete. The block representing the the position was left out

because that variable was not being controlled. It is interesting though, to see what is happening to the cart's position when the controller for the pendulum's angle is in place. To see this we need to consider the actual system block diagram:

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

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

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

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

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page. The pole/zero at the origin canceled out of the transfer function for Phi, has been put back in. So that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the command window:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

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

-(M+m)*m*g*l/q -m*g*l/q];

k = 10; numcontroller = conv([1 1.1],[1 5]); dencontroller = [1 0]; numc = conv(numx,dencontroller); denc = polyadd(conv(dencontroller,den),k*conv(numcontroller,num)); t=0:0.01:100; impulse(numc,denc,t)

As you can see, the cart moves in the negative direction and stabilizes at about -0.18 meters. This design might work pretty well for the actual controller, assuming that the cart had that much room to move in. Keep in mind, that this was pure luck. We were not trying to design to stabilize the cart's position, and the fact that we have is a fortunate side effect.

Example: State-space design for the inverted pendulum


Open-loop poles LQR design Adding the reference Input Observer 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 design criteria for this example are different. In the other other examples we were dealing with an impulse and not a step input. Also, we were only concerned with the pendulums angle and disregarded the cart's position in the design of the controller. However, for an inverted pendulum it is unrealistic to consider just the single output system. Using state-space methods it is relatively simple to work with a multi-output system, so in this example we will design a controller with both the pendulum angle and the cart position in mind. To see how this problem was originally set up, consult the inverted pendulum modeling page. This problem can be solved using full state feedback. The schematic of this type of control system is shown below:

If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Inverted Pendulum Animation Page after completing this tutorial.

Open-loop poles
In this problem R represents the commanded step input to the cart. The 4 states represent the position and velocity of the cart and the angle and angular velocity of the pendulum. The output y contains both the position of the cart and the angle of the pendulum. We want to design a controller so that when an step input is given to the system, the pendulum should be displaced, but eventually return to zero (i.e. the vertical) and the cart should move to it's new commanded position. To view the system's open-loop response please refer to the inverted pendulum modeling Page The first step in designing this type of controller is to determine the open-loop poles of the system. Enter the following lines of code into a m-file (or a '.m' file located in the same directory as Matlab):

M m b i g l

= = = = = =

0.5; 0.2; 0.1; 0.006; 9.8; 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; 0 0 0 1; 0 -(m*l*b)/p m*g*l*(M+m)/p 0]; B = [0; (i+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0;0]; p = eig(A)

The Matlab command window should output the following text as a result:
p = 0 -0.1428 5.5651 -5.6041

As you can see, there is one right-half-plane pole at 5.5651. This should confirm your intuition that the system is unstable in open loop.

LQR design
The next step in the design process is to assume that we have full-state feedback (i.e. that we can measure all four states), and find the vector K which determines the feedback control law. This can be done in a number of ways. If you know the desired closed-loop poles, you can use the place or acker command. Another option is to use the lqr function; this will give you the optimal controller (under certain assumptions; consult your textbook for more details). The lqr function allows you to choose two parameters, R and Q, which will balance the relative importance of the input and state in the cost function that you are trying to optimize. The simplest case is to assume R=1, and Q=C'*C. You may notice that we are using both outputs (the pendulum's angle and the cart's position). Essentially, the lqr method allows for the control of both outputs. In this case, it is pretty easy to do. The controller can be tuned by changing the nonzero elements in the Q matrix to get a desirable response.

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

To find the structure of Q, enter the following into the Matlab command window:
C'*C

You should see the following in the command window:


ans = 1 0 0 0

0 0 0

0 0 0

0 1 0

0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the 3,3 position will be used to weight the pendulum's angle. The input weighting R will remain at 1. Now that we know what the Q matrix should look like we can experiment to find the K matrix that will give us a good controller. We will go ahead and find the K matrix and plot the response all in one step so that changes can be made in the control and be seen automatically in the response. Enter the following text into your m-file:
x=1; y=1; Q=[x 0 0 0; 0 0 0 0; 0 0 y 0; 0 0 0 0]; R = 1; K = lqr(A,B,Q,R) Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D]; T=0:0.01:5; U=0.2*ones(size(T)); [Y,X]=lsim(Ac,Bc,Cc,Dc,U,T); plot(T,Y) legend('Cart','Pendulum')

You should get the following value for K and a response plot:
K = -1.0000 -1.6567 18.6854 3.4594

The curve in green represents the pendulum's angle, in radians and the curve in blue represents the cart's position in meters. As you can see, this plot is not satisfactory. The pendulum and cart's overshoot appear fine, but their settling times need improvement and the

cart's rise time needs to go down. As I'm sure you have noticed the cart is not near the desired location but has in fact moved in the other direction. This error will be dealt with in the next section and right now we will focus on the settling and rise times. Go back to your m-file and change the x and y variables to see if you can get a better response. You will find that increasing x makes the settling and rise times go down, and lowers the angle the pendulum moves. Using x=5000 and y=100, the following value of K and step response were found:
K = -70.7107 -37.8345 105.5298 20.9238

You may have noted that if you increased x and y even higher, you could improve the response even more. The reason this plot was chosen was because it satisfied the design requirements while keeping x and y as small as possible. In this problem, x and y have been used to describe the relative weight of the tracking error in the cart's position and pendulum's angle versus the control effort. The higher x and y are, the more control effort is used, but the smaller the tracking error. The system response has a settling time under 2 seconds.

Adding the reference input


Now we want to get rid of the steady-state error. In contrast to the other design methods, where we feedback the output and compare it to the reference input to compute an error, with a full-state feedback controller we are feeding back all the states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain K, and use a new value as our reference for computing the input. This can be done by adding a constant gain Nbar after the reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your mfile is in). Delete the lsim line and copy the following to your m-file and run it to view the step response with Nbar added.
Cn=[1 0 0 0]; Nbar=rscale(A,B,Cn,0,K) Bcn=[Nbar*B]; [Y,X]=lsim(Ac,Bcn,Cc,Dc,U,T); plot(T,Y) legend('Cart','Pendulum')

Note: Non-standard matlab commands are highlighted in green.

A different C had to be used because the rscale function will not work for multiple outputs. However, the Nbar found is correct, as you can see from the output below:
Nbar = -70.7107

Now, the steady-state error is within our limits, the rise and settling times are met and the pendulum's overshoot is within range of the design criteria.

Observer design
This response is good, but was found assuming full-state feedback, which most likely will not be a valid assumption. To compensate for this, we will next design a full-order estimator to estimate those states that are not measured. A schematic of this kind of system is shown below, without Nbar:

To begin, we must first find the controller poles. To do this copy the following code to the end of your m-file:
p = eig(Ac)

If you changed the weighting factors x and y above to x=5000 and y=100, you should see the following poles in the Matlab command window:
p = -8.4910 -8.4910 -4.7592 -4.7592 + + 7.9283i 7.9283i 0.8309i 0.8309i

We want to design estimator poles that are about 4-10 times as fast as slowest pole, say at 40. We will use the place command in Matlab to find the L vector (note that acker would also work). Remember that the place command cannot have all the desired poles at the same location. Delete from the lsim command on and enter the following text to the end of your m-file to find the L matrix:
P = [-40 -41 -42 -43]; L = place(A',C',P)'

We are using both outputs (the angle of the pendulum and the position of the cart) to design the observer. The system is not observable using only the angle of the pendulum as output; you can check this in Matlab by computing rank(obsv(A,C(2,:))). This should make sense to you: if you can only measure the angle of the pendulum, you cannot determine what the position of the cart will be. You should see the following in the Matlab window:

L = 1.0e+03 * 0.0826 1.6992 -0.0014 -0.0762 -0.0010 -0.0402 0.0832 1.7604

Now we will combine the control-law design with the estimator design to get the compensator. The response should be similar to the one from the control-law design. To set up the compensator copy the following code to the end of your m-file:
Ace = [A-B*K B*K; zeros(size(A)) (A-L*C)]; Bce = [ B*Nbar; zeros(size(B))]; Cce = [Cc zeros(size(Cc))]; Dce = [0;0]; T = 0:0.01:5; U = 0.2*ones(size(T)); [Y,X] = lsim(Ace,Bce,Cce,Dce,U,T); plot(T,Y) legend('Cart','Pendulum')

After running this m-file, you should output the following step response simulation plot:

This response is about the same as before. All of the design requirements have been met with the minimum amount of control effort, so no more iteration is needed. As you can see, it is much easier to control multi-input or multi-output systems with the state space method than with any other of the methods. If you are interested in running an animation of the inverted pendulum example based on the control techniques used in this tutorial please go to the Inverted Pendulum Animation Page.

Digital Control Example: Inverted Pendulum using State-Space method


Discrete state-space Controllability and Observability Control design via pole placement Reference input Observer design
In this digital control version of the inverted pendulum problem, we are going to use the state-space method to design the digital controller. If you refer to the Inverted Pendulum Modeling page, the state-space equations were derived as

where M mass of the cart 0.5 kg m mass of the pendulum 0.5 kg b friction of the cart 0.1 N/m/sec l length to pendulum center of mass 0.3 m I inertia of the pendulum 0.006 kg*m^2 u step force applied to the cart x cart position coordinate phi pendulum angle from vertical Output are the cart displacement (x in meters) and the pendulum deflection angle (phi in radians). The design requirements are

Settling time for x and phi less than 5 seconds Rise time for x of less than 1 second Overshoot of phi less than 0.35 rad (20 deg) Steady-state error of x and phi less than 2%

Discrete state-space

The first thing to do here is to convert the above continuous state-space equations to discrete state-space. To do this, we are going to use the Matlab function called c2dm. To use this c2dm, we need to specify six arguments: four state-space matrices (A, B, C, and D), sampling time (Ts in sec/sample), and the 'method'. You should already be familiar with how to enter A, B, C, and D matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The method we will use is the zero-order hold ('zoh'). Assuming that the closed-loop bandwidth frequencies are around 1 rad/sec for both the cart and the pendulum, let the sampling time be 1/100 sec/sample. Now we are ready to use c2dm. Enter the following commands to an m-file.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies A = [0 1 0 -(i+m*l^2)*b/p 0 0 0 -(m*l*b)/p B = [ 0; (i+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0; 0]; Ts=1/100; [F,G,H,J]=c2dm (A,B,C,D,Ts,'zoh') 0 (m^2*g*l^2)/p 0 m*g*l*(M+m)/p 0; 0; 1; 0];

Running this m-file in the Matlab command window gives you the following four matrices.
F = 1.0000 0 0 0 G = 0.0001 0.0182 0.0002 0.0454 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000 0.0001 0.0100 1.0016

H = 1 0 J = 0 0 0 0 0 1 0 0

Now we have obtained the discrete state-space model of the form

Controllability and Observability


The next step is to check the controllability and the observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

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 following commands and run it in the command window.
F = [1.0000 0.0100 0.0001 0.0000; 0 0.9982 0.0267 0.0001; 0 0.0000 1.0016 0.0100; 0 -0.0045 0.3119 1.0016]; G = [0.0001; 0.0182; 0.0002; 0.0454]; [1 0 [0; 0]; 0 0 0 1 0; 0];

H = J =

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 state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below.

The next step is to assume that all four state are measurable, and find the control matrix (K). If you refer to the continuous Inverted Pendulum: State-Space page, the Linear Quadratic Regulator (LQR) method was used to find the control matrix (K). In this digital version, we will use the same LQR method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your

control textbook for details. To use this LQR method, we need to find three parameters: Performance index matrix (R), state-cost matrix (Q), and weighting factors. For simplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to H' x H. The weighting factors will be chosen by trial and errors. The state-cost matrix (Q) has the following structure
Q = 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the 3,3 position will be used to weight the pendulum's angle. The weighting factors for the cart's position and the pendulum's angle will be chosen individually. Now we are ready to find the control matrix (K) and see the response of the system. Enter the following commands to an new m-file and run it in the Matlab command window. You should see the following step response.
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]; [1 0 [0; 0]; %weighting factor for the cart position %weighting factor for the pendulum angle 0 0 0 0 0 0 y 0 0; 0; 0; 0]; 0 0 0 1 0; 0];

H = J =

x=1; y=1; Q=[x 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's position in meters. The pendulum's and cart's overshoot appear fine, but their settling times need improvement and the cart's rise time needs to be decreased. Also the cart has, in fact, moved in the opposite direction. For now, we will concentrate on improving the settling times and the rise times, and fix the steady-state error later. Let's increase the weighting factors (x and y) and see if both the settling and rise times decrease. Go back to your m-file and change the x and y to x=5000 and y=100. Running this m-file in the command window gives you the following new step response.

From this plot, we see that all design requirements are satisfied except the steady-state error of the cart position (x). We can easily correct this by introducing a feedforwarding scaling factor (Nbar).

Reference input
Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equals to the input. To obtain the desired output, we need to scale the reference input so that the output equals to the reference. This can be easily done by introducing a feedforwarding scaling factor called Nbar. The basic schematic with the Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can find it from trial and errors. After several trials, the Nbar equals to -61.55 provided the satisfactory response. Try the following m-file and obtain the step response shown below.
T=0:0.01:5; U=0.2*ones(size(T)); F = [1.0000 0 0 0 G = 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

[0.0001; 0.0182; 0.0002; 0.0454]; [1 0 [0; 0]; %weighting factor for the cart position %weighting factor for the pendulum angle 0; 0; 0; 0]; 0 0 0 1 0; 0];

H = J =

x=5000; y=100; Q=[x 0 0 0 0 0 0 0 0 0 y 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)')

Notice that the steady-state error of the cart's position have been eliminated. Now we have designed the system that satisfies all design requirements.

Observer design
The above response satisfies all design requirements; however, it was found assuming all states are measurable. This assumption may not be valid for all systems. In this section, we develop a technique for estimating the states of a plant from the information that is available concerning the plant. The system that estimates the states of another system is called an observer. Thus, in this section we will design a full-order state observer to estimate those states that are not measurable. For further explanation on how an observer works, please consult your control textbooks. A basic schematic of the plant-observer system is shown below.

To design the observer, first, we need to find the L matrix. To find the L matrix, we need to find the poles of the system without the observer (the poles of F-G*K). Copy the following commands to an new m-file and run it in the Matlab command window.
F = [1.0000 0.0100 0.0001 0.0000; 0 0.9982 0.0267 0.0001; 0 0.0000 1.0016 0.0100; 0 -0.0045 0.3119 1.0016]; G = [0.0001; 0.0182; 0.0002; 0.0454]; [1 0 [0; 0]; %weighting factor for the cart position %weighting factor for the pendulum angle 0; 0; 0; 0]; 0 0 0 1 0; 0];

H = J =

x=5000; y=100; Q=[x 0 0 0 0 0 0 0 0 0 y 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 the observer. Let's place the observer poles far left of above poles, say, at [-0.3 -0.31 -0.32 -0.33]. These poles can be changed later, if necessary. We will use the Matlab function place to find the L matrix. Enter the following commands to an new m-file and run it.
F = [1.0000 0.0100 0.0001 0.0000; 0 0.9982 0.0267 0.0001; 0 0.0000 1.0016 0.0100; 0 -0.0045 0.3119 1.0016]; H = [1 0 0 0 0 1 0; 0];

P = [-0.3 -0.31 -0.32 -0.33]; L = place (F',H',P)'

You should see the following L matrix in the command window.


L = 2.6310 172.8146 -0.0129 -2.2954 -0.0105 -1.3468 2.6304 173.2787

Now we will obtain the overall system response including the observer. Once again, create an new m-file and copy the following code.
T=0:0.01:5; U=0.2*ones(size(T)); F = [1.0000 0 0 0 G = 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

[0.0001; 0.0182; 0.0002; 0.0454];

H = J =

[1 0 [0; 0];

0 0

0 1

0; 0];

x=5000; y=100; Q=[x 0 0 0 0 0 0 0 0 0 y 0

%weighting factor for the cart position %weighting factor for the pendulum angle 0; 0; 0; 0];

R = 1; K = dlqr(F,G,Q,R) Nbar = -61.55; L = [2.6310 172.8146 -0.0129 -2.2954 -0.0105; -1.3468; 2.6304; 173.2787; G*K; (F-L*H)];

Fce = [F-G*K zeros(size(F)) Gce = [G*Nbar; zeros(size(G))];

Hce = [H zeros(size(H))]; Jce = [0;0]; [Y,X] = dlsim (Fce,Gce,Hce,Jce,U); stairs (T,Y) legend ('cart (x)','pendulum (phi)')

After running this m-file, you should get the following step response.

As you noticed, this response is about the same as before, and all of the design requirements have been satisfied.

Example: Modeling a Pitch Controller

Photo courtesy: Boeing Physical setup and system equations Design requirements Transfer function and state-space Matlab representation and open-loop response Closed-loop transfer function

Physical setup and system equations


The equations governing the motion of an aircraft are a very complicated set of six non-linear coupled differential equations. However, under certain assumptions, they can be decoupled and linearized into the longitudinal and lateral equations. Pitch control is a longitudinal problem, and in this example, we will design an autopilot that controls the pitch of an aircraft. The basic coordinate axes and forces acting on an aircraft are shown in the figure below:

Assume that the aircraft is in steady-cruise at constant altitude and velocity; thus, the thrust and drag cancel out and the lift and weight balance out each other. Also, assume that change in pitch angle does not change the speed of an aircraft under any circumstance (unrealistic but simplifies the problem a bit). Under these assumptions, the longitudinal equations of motion of an aircraft can be written as:

(1)
Please refer to any aircraft-related textbooks for the explanation of how to derive these equations. Also, click Variables to see what each variable represents. For this system, the input will be the elevator deflection angle, and the output will be the pitch angle.

Design requirements
The next step is to set some design criteria. We want to design a feedback controller so that the output has an overshoot of less than 10%, rise time of less than 2 seconds, settling time of less than 10 seconds, and the steady-state error of less than 2%. For example, if the input is 0.2 rad (11 degress), then the pitch angle will not exceed 0.22 rad, reaches 0.2 rad within 2 seconds, settles 2% of the steady-state within 10 seconds, and stays within 0.196 to 0.204 rad at the steady-state.

Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

Transfer function and the state-space

Before finding transfer function and the state-space model, let's plug in some numerical values to simplify the modeling equations (1) shown above.

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

1. Transfer function
To find the transfer function of the above system, we need to take the Laplace transform of the above modeling equations (2). Recall from your control textbook, when finding a transfer function, zero initial conditions must be assumed. The Laplace transform of the above equations are shown below.

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

2. State-space
Knowing the fact that the modeling equations (2) are already in the state-variable form, we can rewrite them into the state-space model.

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

Matlab representation and open-loop response


Now, we are ready to observe the system characteristics using Matlab. First, let's obtain an open-loop system to a step input and determine which system characteristics need improvement. Let the input (delta e) be 0.2 rad (11 degrees). Create an new m-file and enter the following commands.
de=0.2; num=[1.151 0.1774]; den=[1 0.739 0.921 0];

step (de*num,den) Running this m-file in the Matlab command window should give you the following plot.

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

If you noticed, the above m-file uses the numerical values from the transfer function. To use the state-space model, enter the following commands into a new m-file (instead of the one shown above) and run it in the command window.
de=0.2; A=[-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0]; B=[0.232; 0.0203; 0]; C=[0 0 1]; D=[0];

step(A,B*de,C,D) You should get the same response as the one shown above.

Note: It is possible to convert from the state-space to transfer function, or vice versa using Matlab. To learn more about conversions, see Conversions

Closed-loop transfer function


To solve this problem, a feedback controller will be added to improve the system performance. The figure shown below is the block diagram of a typical unity feedback system.

A controller needs to be designed so that the step response satisfies all design requirements. Four different methods to design a controller are listed at the bottom of this page. You may choose: PID, Root-locus, Frequency response, or State-space.

Example: PID Design method for the Pitch Controller


Proportional control Proportional-Derivative control Proportional-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 pitch angle (theta). The design requirements are

Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds

Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page. Recall from the PID Tutorial page, the transfer function of a PID controller is:

We will implement combinations of proportional (Kp), integral (Ki), and derivative (Kd) controllers in an unity feedback system shown below to study the system output.

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

Proportional control
The first thing in solving this problem using PID control is to find a closed-loop transfer function with a proportional control (Kp). A closed-loop transfer function can be obtained either by hand or using the Matlab function called cloop. Either way, you should obtain the closed-loop transfer function as:

Note: Matlab cannot manipulate symbolic variables. To use the function cloop, enter the following commands to an m-file and run it in the Matlab command window. You should obtain the numerical coefficients of numerator and denominator of the closed-loop transfer function.
Kp=[1]; %Enter any numerical value for the proportional gain num=[1.151 0.1774]; num1=conv(Kp,num); den1=[1 0.739 0.921 0]; [numc,denc]=cloop (num1,den1)

For now, let the proportional gain (Kp) equal 2 and observe the system behavior. We will use the closed-loop transfer function derived by hand. Enter the following commands into a new m-file and run it in the Matlab command window. You should obtain the step response similar to the one shown below:
de=0.2;

Kp=2; numc=Kp*[1.151 0.1774]; denc=[1 0.739 1.151*Kp+0.921 0.1774*Kp]; t=0:0.01:30; step (de*numc,denc,t)

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

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

Using the commands shown below and with several trial-and-error runs, a proportional gain (Kp) of 9 and a derivative gain (Kd) of 4 provided the reasonable response. To comfirm this, change your m-file to the following and run it in the Matlab command window. You should obtain the step response similar to the one shown below:
de=0.2; Kp=9; Kd=4; numc=[1.151*Kd 1.151*Kp+0.1774*Kd 0.1774*Kp]; denc=[1 0.739+1.151*Kd 0.921+1.151*Kp+0.1774*Kd 0.1774*Kp]; t=0:0.01:10; step (de*numc,denc,t)

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

PID Control
Even though all design requirements were satisfied with the PD controller, the integral controller (Ki) can be added to reduce the sharp peak and obtain smoother response. After several trial-and-error runs, the proportional gain (Kp) of 2, the integral gain (Ki) of 4, and the derivative gain (Kd) of 3 provided smoother step response that still satisfies all design requirements. To cofirm this, enter the following commands to an m-file and run it in the command window. You should obtain the step response shown below:

Note: This time we are going to use the function cloop to find the closed-loop transfer function, and then obtain the step response.
de=0.2; Kp=2; Kd=3; Ki=4; numo=[1.151 0.1774]; deno=[1 0.739 0.921 0]; numpid=[Kd Kp Ki]; denpid=[1 0]; num1=conv(numo,numpid); den1=conv(deno,denpid); [numc,denc] = cloop(num1,den1); t=0:0.01:10; step (de*numc,denc,t)

Comments
1. To 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 other two. As a result, you may need to change other two gains as you change one gain. 2. Our system with a PI controller do not provide the desired response; thus, a PI Control section was omitted from this page. You may confirm this by using the mfile shown in PID Control section, and set Kd equals to zero.

Example: Root-Locus Design method for the Pitch Controller


Original root-locus plot Lead compensator Quick 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 pitch angle (theta). The design requirements are

Overshoot: Less than 10%

Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

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

Original root-locus plot


Recall from the Root-Locus Tutorial page, a root-locus plot shows all possible closed-loop pole locations for a pure proportional controller. Since not all poles are acceptable, the Matlab function called sgrid should be used to find an acceptable region of the locus. This sgrid function requires two arguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments can be determined from the rise time, the settling time, and the overshoot requirements and three equations shown below.

where, Wn=Natural frequency zeta=Damping ratio Ts=Settling time Tr=Rise time Mp=Maximum overshoot From these three equations, we can determine that the natural frequency (Wn) must be greater than 0.9 and the damping ratio (zeta) must be greater than 0.52. Let's generate a root-locus plot and use the sgrid to find the acceptable region of the locus. Create a new m-file and enter the following commands:
num=[1.151 0.1774]; den=[1 0.739 0.921 0]; Wn=0.9; zeta=0.52;

rlocus (num,den) sgrid (zeta,Wn)


axis ([-1 0 -2.5 2.5])

Run this m-file in the Matlab command window. You should see the root-locus plot similar to the one shown below:

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

Lead compensator
We need to shift the root-locus more toward the left to get it inside our desired region. If you refer to the Designing Lead and Lag Compensators page, you will notice that the lead compensator can move the root locus to the left. The transfer function of a typical lead compensator is:

Zo=zero Po=pole Zo < Po

In general, the zero is placed in the neighborhood of the natural frequency criterion, and the pole is placed at a distance 3 to 20 times the value of the zero location. Let's place the zero (Zo) at 0.9 and the pole (Zo) at 20. This time, let Matlab functions conv and cloop determine the closed-loop transfer function with the lead compensator. Enter the following commands to an new m-file and run it in the Matlab command window. You should obtain the following root-locus plot:
num1=[1.151 0.1774]; den1=[1 0.739 0.921 0];

num2=[1 0.9]; den2=[1 20]; num=conv(num1,num2); den=conv(den1,den2); Wn=0.9; zeta=0.52;

rlocus (num,den)
axis ([-3 0 -2 2]) sgrid (zeta,Wn)

The root-locus has been generated in our desired region. Now, we are ready to pick a gain (K) and generate the step response corresponding to that gain. Add the following commands to the m-file shown above and run it in the command window. You should see a prompt asking you to pick a point on the root-locus plot. Pick a point close to the zero on the natural frequency criterion, say around -1 on real axis. This point should give you the gain around 200. You should see a step response similar to the one shown below.
[K, poles]=rlocfind (num,den) de=0.2; [numc,denc]=cloop (K*num,den,-1); step (de*numc,denc)

This response satisfies all the design requirements.

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


1. Obtain a root-locus plot with the sgrid using the original plant transfer function. 2. Add a lead (or lag) compensator to bring the root-locus to the desired region, if necessary. 3. Pick a point on the root-locus and obtain the corresponding gain (K). 4. Generate the step response with the chosen gain (K). 5. Determine what needs to be changed from the step response. 6. Add or modify the lead (or lag or lead-lag) compensator. 7. Obtain new root-locus plot with the sgrid command active. 8. Repeat steps 3 to 7 until you obtain a satisfactory result.

Example: Frequency Response Design method for the Pitch Controller


Open-loop response Lead compensator Lag 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 pitch angle (theta). The design requirements are

Overshoot: Less than 10% Rise time: Less than 5 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

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

Open-loop response
Recall from your control textbook that the frequency response design method is most effective for systems with stable open-loop. To check the open-loop stability of our system, create a new m-file, and enter the following commands. Running this m-file in the Matlab command window should give you the step response shown below:
de=0.2; num=[1.151 0.1774]; den=[1 0.739 0.921 0]; step (de*num,den)

Unfortunately, our system is unstable in open-loop; however, we can still design the feedback system via frequency response method (even though this might not be the easiest way). First, let's generate the open-loop Bode plot and see what it looks like. Change the m-file to the following and re-run it in the Matlab command window. You should see a Bode plot similar to the one shown below:
num=[1.151 0.1774]; den=[1 0.739 0.921 0];

bode (num,den)

From our design requirements, we can determine that the natural frequency (Wn) must be greater than 0.9 and the damping ratio (zeta) must be greater than 0.52 (please refer to the Pitch Controller: Root-Locus method for details). Using two equations shown below, we see that the bandwidth frequency and the phase margin must be greater than 0.9 and 52 degrees, respectively.

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. These values 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 new m-file should give you the following closed-loop step response:
[numc,denc]=cloop(num,den,-1); de=0.2; t=0:0.01:10; step (de*numc,denc,t)

As you can see, the transient response is worse that results in long settling time. We will implement a lead compensator to improve the system response.

Lead Compensator
Referring to the "Lead or phase-lead compensator using frequency response" section of Lead and Lag Compensator page, a lead compensator will add a positive phase to the system. An additional positive phase increases the phase margin; thus, increase damping. The settling time should decrease as a result of this increased damping. The transfer function of a typical first-order lead compensator is

We need to find alead, Tlead and Klead. First, the phase margin requirement and the following equation can be used to find alead

Since we are required to have the phase margin of greater than 52 degrees, the alead must be greater than 8.43. Using this alead, the bandwidth frequency requirement of greater than 0.9 and the following equation leads us to have the Tlead of smaller than 0.382.

Let the Klead equal 0.1, alead equal 10, and Tlead equal 0.3 for now and enter the following commands to an new m-file.

num=[1 151 0.1774]; den=[1 0.739 0.921 0]; alead=10; Tlead=0.3; aleadtlead=alead*Tlead; k=0.1; numlead=k*[aleadtlead 1]; denlead=[Tlead 1]; num1=conv(num,numlead); den1=conv(den,denlead); bode(num1,den1) [numc,denc]=cloop(num1,den1,-1); de=0.2; t=0:0.01:10; figure step (de*numc,denc,t)

Running this m-file in the Matlab command window gives you the following Bode and step response plots.

Although both bandwidth frequency and phase margin increased, the response still does not satisfy the design requirements. Let's increase alead and decrease Tlead. After several trial and error runs, an alead of 200, Tlead of 0.0025, and Klead of 0.05 , were found which gave the following lead compensator,

provided the desired transient response. To see the step response and the corresponding Bode plot, enter the following commands to an m-file and run it in the command window. You should see both the Bode plot and the step response shown below:
num=[1 151 0.1774]; den=[1 0.739 0.921 0]; alead=200; Tlead=0.0025; aleadtlead=alead*Tlead; k=0.05; numlead=k*[aleadtlead 1]; denlead=[Tlead 1]; num1=conv(num,numlead); den1=conv(den,denlead); bode(num1,den1) [numc,denc]=cloop(num1,den1,-1); de=0.2; t=0:0.01:10; figure step (de*numc,denc,t)

If you compare the above Bode plot to the original Bode plot, you see both the phase margin and the bandwidth frequency have increased. Increasing both of them improves the rise time, the overshoot, and the settling time, as seen in the above step response plot. To improve the steady-state error, we will add a lag compensator to the system.

Lag compensator
Referring to the "Lag or phase-lag Compensator using frequency response" section of Lead and Lag Compensator page, a lag compensator reduces the steady-state error. The typical first-order transfer function of a lead compensator is

The steady-state error will be reduced by a factor of alag. From the above step response, we see that the steady-state error is roughly 10%. Thus, alag needs to be approximately 0.1. The

Tlag should be greater than alag*Tlag because this compensator must not greatly change the transient response. After several trial and error runs, an alag of 0.1, Tlag of 20, and Klag of 1.5, were found which gave the following lag compensator,

provided the desired response. To see the step response and the corresponding Bode plot, enter the following commands to an new m-file. Running this m-file in the command window should give you the two plots shown below:
num=[1 151 0.1774]; den=[1 0.739 0.921 0]; alead=200; Tlead=0.0025; aleadtlead=alead*Tlead; k=0.05; numlead=k*[aleadtlead 1]; denlead=[Tlead 1]; num1=conv(num,numlead); den1=conv(den,denlead); Tlag=20; alag=0.1; at=alag*Tlag; k2=1.5; numlag=k2/alag*[at 1]; denlag=[Tlag 1]; num2=conv(num1,numlag); den2=conv(den1,denlag);

bode (num2,den2)
[numc2,denc2]=cloop(num2,den2,-1); figure step (0.2*numc2,denc2,t)

If you see the Bode plot, the low frequency gain has increased while keeping the bandwidth frequency the same. This tells us that steady-state error has reduced while keeping the same rise time. The above step response shows that the steady-state error got eliminated. Now all design requirements are satisfied.

Example: State-space method for the Pitch Controller


Controllability and Observability Control design via pole placement Reference 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 pitch angle (theta). The design requirements are

Overshoot: Less than 10% Rise time: Less than 5 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

To see the original problem setup, please refer to the Pitch Controller Modeling page. If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Pitch Controller Animation page after completing this tutorial.

Controllability and Observability


The first thing to do in designing a system via state-space method is to check the controllability and observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3, the rank for both matrices must be 3. The Matlab command rank can give you the ranks of both matrices. Create a new m-file and enter the following commands:
A=[-0.313 -0.0139 0 B=[0.232; 0.0203; 0]; C=[0 0 1]; D=[0]; co=ctrb (A,B); ob=obsv (A,C); Controllability=rank(co) Observability=rank(ob) 56.7 0; -0.426 0; 56.7 0];

If you run this m-file in the Matlab command window, you should see
Controllability = 3 Observability = 3

This proves that our system is both completely state controllable and completely state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below:

where

K=Control matrix x=State matrix (alpha, q, theta) de=-Kx=input R=Reference

Recall from the State-Space Tutorial page, the "pole placement" technique should be used to find the control matrix (K). Since the determinate of [sI-(A-BK)] matrix is a third-order polynomial, there are three poles we can place. In the State-Space Tutorial, the dominant second-order pole placement method was introduced. However for this example, we will use another method called Linear Quadratic Regulator (LQR) method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to find three parameters: performance index matrix (R), state-cost matrix (Q), and weighting factor (p). For simplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to C' x C. The weighting factor (p) will be varied as we see the step response. To see the structure of the Q matrix, type in the following commands to an m-file and run it in the Matlab command window (or you can simply type them directly into the command window).
C=[0 0 1]; Q=C'*C

You should see the following Q matrix in the command window:


Q = 0 0 0 0 0 0 0 0 1

Now we are ready to find the control matrix (K) and see the response of the system. First, let the weighting factor (p) equal 50. Enter the following commands to a new m-file and run it in the Matlab command window.
t=0:0.1:10; de=0.2*ones(size(t)); yo=[0 0 0]; A=[-0.313 -0.0139 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) 56.7 -0.426 56.7 0; 0; 0];

After you run this m-file, you should see the step response similar to the one shown below:

The rise time, overshoot, and settling time looks satisfactory. However, there is a large steady-state error. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference input
Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equal to the input. To obtain the desired output, we need to scale the reference input so that the output equals the reference. This can be easily done by introducing a feed-forwarding scaling factor called Nbar. The basic schematic with the scaling factor (Nbar) is shown below:

We can easily find Nbar from the Matlab function rscale. Since this rscale is a userdefined function, you need to copy and save the rscale m-file to your directory. For further assistance in using user-defined functions, refer to Function. After you have saved the rscale m-file to your directory, enter the following commands to a new m-file and run it in the Matlab command window. You should see the response shown below:
t=0:0.1:10; de=0.2*ones(size(t)); yo=[0 0 0]; A=[-0.313 -0.0139 0 56.7 -0.426 56.7 0; 0; 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. If you are interested in running an animation of the pitch controller example based on the control techniques used in this tutorial please go to the Pitch Controller Animation page.

Digital Control Example: Designing Pitch Controller using State-Space method


Discrete state-space Controllability and observability Control design via pole placement Reference input

In this digital control version of the pitch controller problem, we are going to use the statespace method to design the digital controller. If you refer to the Pitch Controller: Modeling page, the state-space model was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are

Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

Discrete state-space
The first thing to do here is to convert above continuous state-space model to discrete statespace. To do this, we are going to use the Matlab function called c2dm. To use this c2dm, we need to specify six arguments: Four state-space matrices (A, B, C, and D), sampling time (Ts), and the 'method'. You should already be familiar with how to enter A, B, C, and D matrices. The sampling time should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. The method we will use is the zero-order hold. From the closed-loop Bode plot, the bandwidth frequency was determined to be approximately 2 rad/sec (see this yourself) . Thus, to be sure we have small enough sampling time, we are going to use the sampling time of 1/100 sec/sample. Now we are ready to use the function c2dm. Enter the following commands to an m-file.
A = [ -0.313 56.7 -0.0139 -0.426 0 56.7 B = [ 0.232; 0.0203; 0]; 0; 0; 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.0001 0 G = 0.0024 0.0002 0.0001 H = 0 J = 0 0 1 0.05649 0.9957 0.5658 1 0 0

Now we have obtained the discrete state-space model of the form

Controllability and observability


The next step is to check the controllability and the observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3, the rank of both matrices must be 3. The Matlab function rank can give you the rank of each matrices. In an new m-file, enter the following commands and run it.
F = [0.9968 -0.0001 0 G = [0.0024; 0.0002; 0.0001]; H = [0 J = [0]; co = ctrb (F,G); ob = obsv (F,H); Controllability = rank (co) Observability = rank (ob) 0 1]; 0.05649 0.9957 0.5658 0 0 1];

In the command window, you should see


Controllability = 3 Observability = 3

This proves that our discrete system is both completely state controllable and completely state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below.

where

K=Control matrix x=State matrix (alpha, q, theta) de=-Kx=input R=Reference

In the continuous Pitch Controller: State-Space page, the Linear Quadratic Regulator (LQR) method was used to find the control matrix (K). In this digital version, we will use the same LQR method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to find three parameters: Performance index matrix (R), state-cost matrix (Q), and weighting factor (p). For simplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to H' x H. The weighting factor (p) will be chosen by trial and errors. The state-cost matrix (Q) has the following structure
Q = 0 0 0 0 0 0 0 0 1

Now we are ready to find the control matrix (K) and see the response of the system. First, let the weighting factor (p) equals 50. Enter the following commands to a new m-file and run it in the Matlab command window.
t=0:0.01:10; de=0.2*ones(size(t)); F = [0.9968 -0.0001 0 G = [0.0024; 0.0002; 0.0001]; H = [0 J = [0]; p=50; 0 1]; 0.05649 0.9957 0.5658 0 0 1];

Q = [0 0 0 0 0 0 0 0 p]; [K] = dlqr (F,G,Q,1) [x] = dlsim (F-G*K,G,H,J,de);

stairs

(t,x)

After you run this m-file, you should see the control matrix (K) in the command window and the step response similar to the one shown below.

The rise time, the overshoot, and the settling time look satisfactory. However, there is a large steady-state error. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference input
Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equals to the input. To obtain the desired output, we need to scale the reference input so that the output equals to the reference. This can be easily done by introducing a feedforwarding scaling factor called Nbar. The basic schematic with the Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar. But certainly we can find it from trial and errors. After several trials, the Nbar equals to 6.95 provided the satisfactory response. Try the following m-file and obtain the stairstep response shown below.
t=0:0.01:10; de=0.2*ones(size(t)); F = [0.9968 -0.0001 0 G = [0.0024; 0.0002; 0.0001]; H = [0 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 0 1]; 0.05649 0.9957 0.5658 0 0 1];

(F-G*K,G*Nbar,H,J,de);

stairs

(t,x)

From this plot, we see that the Nbar eliminated the steady-state error. Now all design requirements are satisfied.

Note: Assuming all states are measurable, an observer design will not be explained in this page.

Example: Modeling the Ball and Beam Experiment


Problem Setup System Equations Matlab Representation and Open-Loop Response

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

For this problem, we will assume that the ball rolls without slipping and friction between the beam and ball is negligible. The constants and variables for this example are defined as follows: M mass of the ball 0.11 kg R radius of the ball 0.015 m d lever arm offset 0.03 m g gravitational acceleration 9.8 m/s^2 L length of the beam 1.0 m J ball's moment of inertia 9.99e-6 kgm^2 r ball position coordinate alpha beam angle coordinate

theta servo gear angle The design criteria for this problem are:

Settling time less than 3 seconds Overshoot less than 5%

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

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

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

Substituting this into the previous equation, we get:

1. Transfer Function
Taking the Laplace transform of the equation above, the following equation is found:

NOTE: When taking the Laplace transform to find the transfer function initial conditions are assumed to be zero.

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

It should be noted that the above plant transfer function is a double integrator. As such it is marginally stable and will provide a challenging control problem.

2. State-Space
The linearized system equations can also be represented in state-space form. This can be done by selecting the ball's position (r) and velocity (rdot) as the state variables and the gear angle (theta) as the input. The state-space representation is shown below:

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

Note: For this system the gear and lever arm would not be used, instead a motor at the center of the beam will apply torque to the beam, to control the ball's position.

Matlab Representation and Open-Loop Response


1. Transfer Function
The transfer function found from the Laplace transform can be implemented in Matlab by inputting the numerator and denominator as vectors. To do this we must create an m-file and copy the following text into it:
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; printsys(num,den)

Your output should be:


num/den = 0.21 ---------s^2

Now, we would like to observe the ball's response to a step input of 0.25 m. To do this you will need to add the following line to your m-file: step(0.25*num,den) NOTE: Matlab commands from the control system toolbox are highlighted in red.

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

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

2. State-Space
The state-space equations can be represented in Matlab with the following commands (these equations are for the torque control model).
m R g J = = = = 0.111; 0.015; -9.8; 9.99e-6;

H = -m*g/(J/(R^2)+m); A=[0 1 0 0 0 0 H 0 0 0 0 1 0 0 0 0]; B=[0;0;0;1]; C=[1 0 0 0]; D=[0];

The step response to a 0.25m desired position can be viewed by running the command below: step(A,B*.25,C,D) Your output should look like the following:

Like the plot for the transfer function this plot shows that the system is unstable and the ball will roll right off the end of the beam. Therefore, we will require some method of controlling the ball's position in this system. The State-Space example below shows how to implement a controller for this type of system. If you are interested in knowing how to convert state-space representations to transfer function representations, and vice versa, please refer to the Conversions page.

Example: Solution to the Ball & Beam Problem Using PID Control
Closed-loop Representation Proportional Control Proportional-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 Representation
The block diagram for this example with a controller and unity feedback of the ball's position is shown below:

First, we will study the response of the system shown above when a proportional controller is used. Then, derivative and/or integral control will be added if necessary. Recall, that the transfer function for a PID controller is:

Proportional Control
The closed-loop transfer function for proportional control with a proportional gain ( kp) equal to 100, can be modeled by copying the following lines of Matlab code into an m-file (or a '.m' file located in the same directory as Matlab)
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); 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:

numc = 0 denc = 1.0000 0 0.2100 0 0.2100

Now, we can model the system's response to a step input of 0.25 m. Add the following line of code to your m-file and run it:
step(0.25*numc,denc)

You should get the following output:

As, you can see the addition of proportional gain does not make the system stable. Try changing the value of kp and note that the system remains unstable.

Proportional-Derivative Control
Now, we will add a derivative term to the controller. Copy the following lines of code to an m-file and run it to view the system's response to this control method.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); 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 increasing kd we can lower overshoot and decrease the settling time slightly. Therefore, make kd = 20 in your m-file and run it again. Your output should be:

The overshoot criterion is met but the settling time needs to come down a bit. To decrease the settling time we may try increasing the kp slightly to increase the rise time. The derivative gain (kd) can also be increased to take off some of the overshoot that increasing kp will cause. After playing with the gains a bit, the following step response plot can be achieved with kp = 15 and kd = 40:

As you can see from the above plot all the control objectives have been met without the use of an integral controller (settling time for this example is considered achieved when the response is less than 2% of it's final value). Remember, that for a control problem there is more than one solution for the problem. For other methods of controlling the ball and beam example, see the root locus, frequency response, and state-space links below.

Example: Solution to the Ball & Beam Problem Using Root Locus Method
Open-loop Root Locus Lead Controller Selecting a Gain Plotting 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. A schematic of the closed loop system with a controller is given below:

Open-loop Root Locus


The main idea of the root locus design is to estimate the closed-loop response from the openloop root locus plot. By adding zeroes and/or poles to the original system (adding a compensator), the root locus and thus the closed-loop response will be modified. Let us first view the root locus for the plant in open loop. Create an m-file with the following Matlab code in order to model the plant and plot the root locus.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); 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:

As you can see the system has two poles at the origin which go off to infinity along the imaginary axes. The design criteria can also be plotted onto the root locus using the sgrid command. This command generates a grid of constant damping ratio and natural frequency. The damping ratio and natural frequency were found using the following equation, which relates the them to our percent overshoot (PO) and settling time (Ts) requirements:

Note, that the equation with Ts is found by assuming settled is when the response remains within 2% of it's final value. From these equation damping ratio and natural frequency were found to be 0.7 and 1.9 respectively. sgrid(0.70, 1.9)
axis([-5 5 -2 2])

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

Lead Controller
A first order lead compensator tends to shift the root locus into the left-hand plane. For a more detailed description of lead compensators refer to the Lead & Lag Compensator Design page. A lead compensator has the form given below:

where, the magnitude of zo is less than the magnitude of po. Now, let us add the controller to the plant and view the root locus. We will position the zero near the origin to cancel out one of the poles. The pole of our compensator will be placed to the left of the origin to pull the root locus further into the left-hand plane. Add the following lines of Matlab code to your m-file.
zo = 0.01; po = 5; numlead = [1 zo]; denlead = [1 po]; numl = conv(num,numlead); denl = conv(den,denlead);

rlocus(numl,denl) sgrid(0.70, 1.9)

Run your m-file in the Matlab command window and you should see the following:

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

Selecting a Gain
Now that we have moved the root locus into the left-hand plane, we may select a gain that will satisfy our design requirements. We can use the rlocfind command to help us do this. Add the following onto the end of your m-file. [kc,poles]=rlocfind(numl,denl) Go to the plot and select a point near those indicated by the cross mark on the plot below:

You should see in the Matlab command window something similar to the following (your numbers will be slightly different):

selected_point = -2.4988+ 1.2493i kc = 37.3131 poles = -2.4950+ 1.2493i -2.4950- 1.2493i -0.0101

Now, we can plot the response with this gain.

Plotting the Closed-loop Response


This value of kc can be put into the system and the closed-loop response to a step input of 0.25 m can be obtained. Add the following lines to your m-file to perform this analysis.
numl2 = kc*numl; [numcl,dencl] = cloop(numl2,denl); t=0:0.01:5; figure step(0.25*numcl,dencl,t)

Run your m-file and you select a point on the root locus similar to the selected point above. The step response should look like the following:

From this plot we see that when a 0.25m step input is given to the system both the settling time and percent overshoot design criteria are met. Note: A design problem does not necessarily have a unique answer. Using this method (or any other) may result in many different compensators. Try running your m-file several more times selecting a different point each time and study the effect this has on the step response.

For practice you may also want to go back to the original open-loop root locus and try to find other ways to add zeros and poles to get a better response.

Example: Solution to the Ball & Beam Problem Using Frequency Response Method
Open-loop Bode Plot Phase-Lead Controller Adding 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. A schematic of the closed loop system with a controller is given below:

Open-loop Bode Plot


The main idea of frequency based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot, therefore changing the closed-loop response. Let's first draw the bode plot for the original open-loop transfer function. Create an m-file with the following code and then run it in the Matlab command window:
m R g L = = = = 0.111; 0.015; -9.8; 1.0;

d = 0.03; J = 9.99e-6; K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; %simplifies input

bode(num,den) NOTE: Matlab commands from the control system toolbox are highlighted in red.

You should get the following Bode plot:

From this plot we see that the phase margin is zero. Since the phase margin is defined as the change in open-loop phase shift necessary to make a closed-loop system stable this means that our zero phase margin indicates our system is unstable. We want to increase the phase margin and we can use a lead compensator controller to do this. For more information on Phase and Gain margins please refer to the Frequency Response Tutorial.

Phase-Lead Controller
A first order phase-lead compensator has the form given below:

The phase-lead compensator will add positive phase to our system over the frequency range 1/aT and 1/T, which are called the corner frequencies. The maximum added phase for one lead compensator is 90 degrees. For our controller design we need a percent overshoot of 5, which corresponds to a zeta of 0.7. Generally zeta * 100 will give you the minimum phase margin needed to obtain your desired overshoot. Therefore we require a phase margin greater than 70 degrees.

To obtain "T" and "a", the following steps can be used. 1. Determine the positive phase needed: We need at least 70 degrees from our controller. 2. Determine the frequency where the phase should be added (center frequency): In our case this is difficult to determine because the phase vs. frequency graph in the bode plot is a flat line. However, we have a relation between bandwidth frequency (wbw) and settling time (refer to the Bandwidth Frequency page for this equation) which tells us that wbw is approximately 1.92 rad/s. Therefore we want a center frequency just before this. For now we will choose 1. 3. Determine the constant "a" from the equation below, this determines the required space between the zero and the pole for the maximum phase added.

where phi refers to the desired phase margin. For 70 degrees, a = 0.0311. 4. Determine "T" and "aT" from the following equations:

For 70 degrees and center frequency (w) = 1, aT = 0.176 and T = 5.67 Now, we can add our lead controller to the system and view the bode plot. Remove the bode command from your m-file and add the following:
k=1; numlead = k*[5.67 1]; denlead = [0.176 1]; numl = conv(num,numlead); denl = conv(den,denlead);

bode(numl,denl) You should get the following bode plot:

You can see that our phase margin is now 70 degrees. Let's check the closed-loop response to a step input of 0.25m. Add the following to your m-file:
[numcl,dencl] = cloop(numl,denl); t=0:0.01:5; step(0.25*numcl,dencl,t)

You should get the following plot:

Although the system is now stable and the overshoot is only slightly over 5%, the settling time is not satisfactory. Increasing the gain will increase the crossover frequency and make the response faster. Make k = 5, your response should look like:

The response is faster, however, the overshoot is much too high. Increasing the gain further will just make the overshoot worse.

Adding More Phase


We can increase our phase-lead compensator to decrease the overshoot. In order to make the iterative process easier use the following program. Create an m-file and copy the function from your web-browser into it (make sure the function command starts in the first column of the m-file).
function[ ] = phaseball() %define TF m = 0.111; R = 0.015; g = -9.8;

L = 1.0; d = 0.03; J = 9.99e-6; K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; %ask pm = w = k = user for controller information input('Phase Margin?.......'); input('Center Frequency?...'); input('Gain?...............'); %simplifies input

%view compensated system bode plot pmr = pm*pi/180; a = (1 - sin(pmr))/(1+sin(pmr)); T = sqrt(a)/w; aT = 1/(w*sqrt(a)); numlead = k*[aT 1]; denlead = [T 1]; numl=conv(num,numlead); denl=conv(den,denlead); figure bode(numl,denl) %view step response [numcl,dencl]=cloop(numl,denl); t=0:0.01:5; figure step(0.25*numcl,dencl,t)

With this m-file you can choose the phase margin, center frequency, and gain. Run your mfile with the following values and you should see the plots below on your screen.
Phase Margin?.......80 Center Frequency?...1 Gain?...............1

The overshoot is fine but the settling time is just a bit long. Try different numbers and see what happens. Using the following values the design criteria was met.
Phase Margin?.......85 Center Frequency?...1.9 Gain?...............2

Note: A design problem does not necessarily have a unique answer. Using this method (or any other) may result in many different compensators. For practice you may want to go back and change the added phase, gain, or center frequency.

Example: Solution to the Ball & Beam Problem Using the State-space Design Method
Full-State Feedback Controller Reference 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 and ball, here we are controlling alpha-doubledot. By doing this we are essentially controlling a torque applied at the center of the beam by a motor. Therefore, we do not need a gear and lever system. The design criteria for this problem are:

Settling time less than 3 seconds Overshoot less than 5%

To see the derivation of the state-space equations for this problem refer to the ball and beam modeling page. If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Ball & Beam Animation Page after completing this tutorial.

Full-State Feedback Controller


We will design a controller for this physical system that utilizes full-state feedback control. A schematic of this type of system is shown below:

Recall, that the characteristic polynomial for this closed-loop system is the determinant of (sI(A-BK)), where s is the Laplace variable. For our system the A and B*K matrices are both 4x4. Hence, there should be four poles for our system. In designing our full-state feedback controller we can move these poles anywhere we want. For our design we desire an overshoot of less than 5% which corresponds to a zeta of 0.7 (please refer to your textbook for the relationship between overshoot and damping ratio). On a root locus this criterion is represented as a 45 degree line emanating from the origin and extending out into the left-half plane. We want to place our poles on or beneath this line. Our next criterion is a settling time less than 3 seconds, which corresponds to a sigma = 4.6/Ts = 4.6/3 = 1.53, represented by a vertical line at -1.53 on the root locus. Anything beyond this line in the left-half plane is a suitable place for our poles. Therefore we will place our poles at -2+2i and -2-2i. We will place the other poles far to the left for now, so that they will not affect the response too much. To start with place them at -20 and -80. Now that we have our poles we can use Matlab to find the controller (K matrix) by using the place command. Copy the following code to an m-file to model the system and find the K matrix:

NOTE: Matlab commands from the control system toolbox are highlighted in red.
m R g J = = = = 0.111; 0.015; -9.8; 9.99e-6;

H = -m*g/(J/(R^2)+m); A=[0 1 0 0 0 0 H 0 0 0 0 1 0 0 0 0]; B=[0;0;0;1]; C=[1 0 0 0]; D=[0]; p1=-2+2i; p2=-2-2i; p3=-20; p4=-80; K=place(A,B,[p1,p2,p3,p4])

Run your m-file and you should get the following output for the K matrix:
place: ndigits= 15 K = 1.0e+03 * 1.8286 1.0286 2.0080 0.1040

After adding the K matrix, the state space equations now become:

We can now simulate the closed-loop response to a 0.25m step input by using the lsim command. Add the following to your m-file:
T = 0:0.01:5; U = 0.25*ones(size(T)); [Y,X]=lsim(A-B*K,B,C,D,U,T); plot(T,Y)

Run your m-file and you should get the following plot:

From this plot we see that there is a large steady state error for which we will need to add reference input (explained in next section). However, the overshoot and settling time criteria are met. If we wanted to reduce the overshoot further than we would make the imaginary part of the pole smaller than the real part. Also, if we wanted a faster settling time we would move the poles further in the left-half plane. Feel free to experiment with the pole positions to see these trends.

Reference Input
Now we want to get rid of the steady-state error. In contrast to the other design methods, where we feedback the output and compare it to the reference input to compute an error, with a full-state feedback controller we are feeding back both states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain K, and use a new value as our reference for computing the input. This can be done by adding a constant gain Nbar after the reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your mfile 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

Now the steady-state error is gone and all the design criteria are satisfied. Note: A design problem does not necessarily have a unique answer. Using this method (or any other) may result in many different compensators. For practice you may want to go back and try to change the pole positions to see how the system responds. If you are interested in running an animation of the ball & beam example based on the control techniques used in this tutorial please go to the Ball & Beam Animation Page.

Digital Control Example: Ball and Beam problem using PID Control
Digital PID controller Discrete transfer function Open-loop response Proportional control Proportional-Derivative control
In this digital control version of the ball and beam experiment, we are going to use the PID control method to design the digital controller. If you refer to the Ball and Beam Modeling page, the open-loop transfer function was derived as

m mass of the ball 0.11 kg g gravitational acceleration 9.8 m/s^2 d lever arm offset 0.03 m L length of the beam 1.0 m R radius of the ball 0.015 m J ball's moment of inertia 9.99e-6 kgm^2 R(s) ball position coordinate (m) theta(s) servo gear angle 0.25 rad The design criteria for this problem are:

Settling time less than 3 seconds Overshoot less than 5%

Digital PID controller


If you refer to any of the PID control problem for continuous systems, the PID transfer function was expressed as

As you noticed the above transfer function was written in terms of s. For the digital PID control, we use the following transfer function in terms of z.

Discrete transfer function


The first thing to do here is to convert the above continuous system transfer function to discrete transfer function. To do this, we are going to use the Matlab function called c2dm. To use this c2dm, we need to specify four arguments: numerator and denominator matrices, sampling time (Ts), and the 'method'. You should already be familiar with how to enter numerator and denominator matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The method we will use is the zeroorder hold ('zoh'). Assuming that the closed-loop bandwidth frequency is around 1 rad/sec, let the sampling time be 1/50 sec/sample. Now we are ready to use c2dm. Enter the following commands to an m-file.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; Ts = 1/50;

[numDz,denDz]= c2dm (num,den,Ts,'zoh')

Running this m-file in the Matlab command window gives you the following matrices.
numDz = 1.0e-0.4 * 0 denDz = 1 -2 1 0.4200 0.4200

From these matrices, the discrete transfer function can be written as

Open-loop response

Now we will observe the ball's response to a step input of 0.25 m. To do this, enter the following commands to an new m-file and run it in the command window. You should see the following response.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; [x] = dstep (0.25*numDz,denDz,251); t=0:0.02:5; stairs(t,x)

From this plot, it is clear that the open-loop system is unstable causing the ball to roll off from the end of the beam.

Proportianal Control
Now we will add the proportional control (Kp) to the system and obtain the closed-loop system response. For now let Kp equal to 100 and see what happens to the response. Enter the following commands to an new m-file and run it in the command window.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Kp=100; [numDzC,denDzC]=cloop (Kp*numDz,denDz); [x] = dstep (0.25*numDzC,denDzC,251); t=0:0.02:5; stairs(t,x)

As you can see, the addition of proportional control does not make the system stable. You may try to increase the proportional gain (Kp) and confirm that the system remains unstable.

Proportional-Derivative control
Now we will add a derivative term to the controller. Keep the proportional gain (Kp) equal to 100, and let the derivative gain (Kd) equal to 10. Copy the following code to an new m-file and run it to view the system response.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Kp=100; Kd=10; numpd = [Kp+Kd -(Kp+2*Kd) Kd]; denpd = [1 1 0]; numDnew = conv(numDz,numpd); denDnew = conv(denDz,denpd); [numDnewC,denDnewC] = cloop(numDnew,denDnew); [x] = dstep (0.25*numDnewC,denDnewC,251); t=0:0.02:5; stairs(t,x)

Now the system is stable, but the rise time is too long. From the PID Tutorial page, we see that the increasing the proportional gain (Kp) will decrease the rise time. Let's increase the proportional gain (Kp) to 1000 and see what happens. Change Kp in the above m-file from 100 to 1000 and rerun it in the command window. You should see the following step response.

As you can see, all of the design requirements are satisfied. For this particular problem, no implementation of an integral control was needed. But remember there is more than one solution for a control problem. For practice, you may try different P, I and D combinations to obtain a satisfactory response.

You might also like