Example: Solution to the Inverted Pendulum Problem Using
Frequency Response Method
Open-loop transfer function
Closed-loop response with no compensation
Closed-loop response with compensation
What happens to the cart's position?
The state equations for this problem are:

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 transfer function
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. Matlab can find the transfer function of the system from
the state equations. The set of state equations for this problem can be
changed into a transfer function by creating the following m-file (or by creating a '.m' file located
in the same directory as Matlab):
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;
p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies
A = [0 1 0 0;
0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;
0 0 0 1;
0 -(m*l*b)/p m*g*l*(M+m)/p 0];
B = [0; (i+m*l^2)/p; 0; m*l/p];
C = [1 0 0 0;
0 0 1 0];
D = [0;0];
[num,den] = ss2tf(A,B,C,D)
By running this m-file, the numerator and denominator of the transfer
function should be shown in the Matlab command window. The following
lines should be displayed:
num =
0 -0.0000 1.8182 0.0000 -44.5455
0 0.0000 4.5455 0.0000 0
den =
1.0000 0.1818 -31.1818 -4.4545 0
The first thing to notice is that the numerator has two rows. The first
row represents the cart's position and second represents the pendulum's angle.
For this problem, only the pendulum's angle is of
interest. Therefore, the first row will be ignored for now.
The next thing to notice is that in the second row of the numerator, the
first and last element are 0, while the second and fourth element are
0.0000. If you look closer at each of these elements, you will find that
they are not zero, but in fact some very small number. These numbers are
not really supposed to be part of the transfer function, and should be
eliminated. The page on conversions will give you more information
on this topic. The numerator can be corrected by
adding the following line of code to the end of your m-file:
num = [num(2,3) 0 num(2,5)];
The transfer function numerator now reflects only the output of interest,
and the zeros at infinity are not causing errors.
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
The first thing we will notice is that 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).
function[ ] = pend()
%define TF
num = [4.5455 0 0];
den =[1.0000 0.1818 -31.1818 -4.4545 0];
figure(1)
%ask user for controller
numc = input('numc?.........');
denc = input('denc?..........');
k = input('K?.........');
%view compensated system bode
bode(k*conv(numc,num), conv(denc,den))
%view compensated system nyquist
figure(2)
subplot (2,1,1)
nyquist(k*conv(numc,num), conv(denc,den))
%view compensated CL system impulse response
subplot(2,1,2)
clnum = conv(num,denc);
temp1 = k*conv(numc,num);
temp2 = conv(denc, den);
clden = polyadd(temp1,temp2);
impulse (clnum,clden)
pause
clf
figure(1)
clf
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.
(After entering the pend command, enter axis([0 10 -0.05
0.1]) to change the axis on the step response plot.)
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 1 -0.05 0.1])
after the pend command.)
numc?.........conv([1 .14],[1 20])
denc?..........[1 0]
K?.........10


Our overshoot is still a little bit high, let's add some more gain and
observe what happens. (Again, enter the command axis([0 1 -0.05
0.1]) after the pend command.)
numc?.........conv([1 .14],[1 20])
denc?..........[1 0]
K?.........15


Our response is even better than we hoped for! 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:

Now that we have the transfer function, let's take a look at the
response. Create a new m-file that looks like the following:
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;
p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies
A = [0 1 0 0;
0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;
0 0 0 1;
0 -(m*l*b)/p m*g*l*(M+m)/p 0];
B = [ 0;
(i+m*l^2)/p;
0;
m*l/p];
C = [1 0 0 0;
0 0 1 0];
D = [0;
0];
[num,den] = ss2tf(A,B,C,D);
numx =[num(1,3) 0 num(1,5)];
num = [num(2,3) 0 num(2,5)];
k = 15;
numcontroller = conv([1 .14],[1 20]);
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.24 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.
User Survey
Your anonymous answers to the following questions will help us to
improve future versions of these tutorials.
-
Frequency Response Examples
-
Cruise Control |
DC Motor |
Bus Suspension |
Inverted Pendulum
-
Inverted Pendulum Examples
-
Modeling |
PID |
Root Locus |
Frequency Response |
State Space
-
Tutorials
-
Basics |
Modeling |
PID |
Root Locus |
Frequency Response |
State Space |
Examples

8/28/96 JDP