Sei sulla pagina 1di 129

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, the modeling equations for this system become:

(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];
cruise=tf(num,den);
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 the state-space representation.

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

To use MATLAB to solve this problem, create an new m-file and copy the following
commands:
m = 1000;
b = 50;
u = 500;
A = [-b/m];
B = [1/m];
C = [1];
D = 0;
cruise=ss(A,B,C,D);
No>e: It is possible to convert from the state-space representation to the transfer function
or vice versa using MATLAB. To learn more about the conversion, see the Conversion

Open-loop response
Now let's see how the open-loop system responds to a step input. Add the following
command to the end of your m-file and run it in the MATLAB command window:
step(u*cruise)

You should get the following plot:

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, Root-locus, Frequency response, or State-space.

Example: Modeling a Cruise Control System


in Simulink

Physical setup and system equations


Building the model
Open-loop response
Extracting the Model
Implementing PI control
Closed-loop response

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

Building the Model


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

 Insert an Integrator Block (from the Linear block library) and draw lines to and from its
input and output terminals.
 Label the input line "vdot" and the output line "v" as shown below. To add such a label,
double click in the empty space just above the line.
Since the acceleration (dv/dt) is equal to the sum of the forces divided by mass, we will
divide the incoming signal by the mass.

 Insert a Gain block (from the Linear block library) connected to the integrators input line
and draw a line leading to the input of the gain.
 Edit the gain block by double-clicking on it and change its value to "1/m".
 Change the label of the Gain block to "inertia" by clicking on the word "Gain" underneath
the block.

Now, we will add in the forces which are represented in Equation (1). First, we will add in
the damping force.

 Attach a Sum block (from the Linear block library) to the line leading to the inertia gain.
 Change the signs of this block to "+-".
 Insert a gain block below the inertia block, select it by single-clicking on it, and select Flip
from the Format menu (or type Ctrl-F) to flip it left-to-right.
 Set the gain value to "b" and rename this block to "damping".
 Tap a line (hold Ctrl while drawing) off the integrator's output and connect it to the input
of the damping gain block.
 Draw a line from the damping gain output to the negative input of the Sum Block.
The second force acting on the mass is the control input, u. We will apply a step input.

 Insert a Step block (from the Sources block library) and connect it with a line to the
positive input of the Sum Block.
 To view the output velocity, insert a Scope (from the Sinks block library) connected to the
output of the integrator.

 To provide a appropriate step input of 500 at t=0, double-click the Step block and set the
Step Time to "0" and the Final Value to "u".
Open-loop response
To simulate this system, first, an appropriate simulation time must be set.

 Select Parameters from the Simulation menu and enter "120" in the Stop Time field. 120
seconds is long enough to view the open-loop response.

The physical parameters must now be set. Run the following commands at the MATLAB
prompt:

m=1000;
b=50;
u=500;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished,
double-click on the scope and hit its autoscale button. You should see the following output.

Extracting a Linear Model into MATLAB


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

 Replace the Step Block and Scope Block with an In Connection Block and an Out
Connection Block, respectively (these blocks can be found in the Connections block
library). This defines the input and output of the system for the extraction process.

Save your file as "ccmodel.mdl" (select Save As from the File menu). MATLAB will extract
the linear model from the saved model file, not from the open model window. At the
MATLAB prompt, enter the following commands:
[A,B,C,D]=linmod('ccmodel')
[num,den]=ss2tf(A,B,C,D)
You should see the following output, providing both state-space and transfer function models of
the system.

A =

-0.0500

B =

1.0000e-003

C =

D =

num =

0 0.0010

den =

1.0000 0.0500
To verify the model extraction, we will generate an open-loop step response of the extracted
transfer function in MATLAB. We will multiply the numerator by 500 to simulate a step input of
500N. Enter the following command in MATLAB.

step(500*num,den);
You should see the following plot which is equivalent to the Scope's output.
Implementing PI Control
In the cruise control example a PI controller was designed with Kp=800 and Ki=40 to give the
desired response. We will implement this in Simulink by first containing the open-loop system
from earlier in this page in a Subsystem block.

 Create a new model window.


 Drag a Subsystem block from the Connections block library into your new model window.

 Double click on this block. You will see a blank window representing the contents of the
subsystem (which is currently empty).
 Open your previously saved model of the Cruise Control system, ccmodel.mdl.
 Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or
Ctrl-C).
 Select the blank subsystem window from your new model and select Paste from the Edit
menu (or Ctrl-V). You should see your original system in this new subsystem window.
Close this window.
 You should now see input and output terminals on the Subsystem block. Name this block
"plant model".

Now, we will build a PI controller around the plant model. First, we will feed back the plant
output.

 Draw a line extending from the plant output.


 Insert a Sum block and assign "+-" to it's inputs.
 Tap a line of the output line and draw it to the negative input of the Sum block.

The output of the Sum block will provide the error signal. From this, we will generate
proportional and integral components.

 Insert an integrator after the summer and connect them with a line.
 Insert and connect a gain block after the integrator to provide the integral gain.
 Label this integrator Ki and assign it a value of Ki.
 Insert a new Gain block and connect it with a line tapped off the output of the Sum block.
 Label this gain Kp and assign it a value of Kp.

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

 Insert a summer between the Ki block and the plant model and connect the outputs of the
two gain blocks to the summer inputs.
 Connect the summer output to the input of the plant.

Finally, we will apply a step input and view the output on a scope.

 Attach a step block to the free input of the feedback Sum block.
 Attach a Scope block to the plant output.
 Double-click the Step block and set the Step Time to "0" and the Final Value to "u". This
allows the input magnitude to be changed outside of simulink.
You can download our version of the closed-loop system here.

In this example, we constructed a PI controller from fundamental blocks. As an alternative,


we could have used a Transfer Function block (from the Linear block library) to implement
this in one step, as shown below.

You can download this model here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from
the Simulation menu and enter "10" in the Stop Time field. The design requirements included a
rise time of less than 5 sec, so we simulate for 10 sec to view the output. The physical parameters
must now be set. Run the following commands at the MATLAB prompt:

m=1000;
b=50;
u=10;
Kp=800;
Ki=40;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished,
double-click on the scope and hit its autoscale button. You should see the following output.
Example: DC Motor Speed Modeling
Physical setup and system equations
Design requirements
MATLAB representation and open-loop response

Physical setup and system equations


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

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

* 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 variab 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)];
motor=tf(num,den);
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(motor,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;
motor_ss=ss(A,B,C,D);

step(motor_ss)
Run this m-file in the MATLAB command window, and you should get the same output as
the one shown above.

Example: DC Motor Speed Modeling in


Simulink
Physical setup
Building the model
Open-loop response
Extracting the Model
Implementing PI control
Closed-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.

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

Building the Model


This system will be modeled by summing the torques acting on the rotor inertia and
integrating the acceleration to give the velocity. Also, Kirchoff's laws will be applied to the
armature circuit.

 Open Simulink and open a new model window.

First, we will model the integrals of the rotational acceleration and of the rate of change of
armature current.
 Insert an Integrator block (from the Linear block library) and draw lines to and from
its input and output terminals.
 Label the input line "d2/dt2(theta)" and the output line "d/dt(theta)" as shown
below. To add such a label, double click in the empty space just above the line.
 Insert another Integrator block above the previous one and draw lines to and from
its input and output terminals.
 Label the input line "d/dt(i)" and the output line "i".

Next, we will start to model both Newton's law and Kirchoff's law. These laws applied to
the motor system give the following equations:

The angular acceleration is equal to 1/J multiplied by the sum of two terms (one pos., one
neg.). Similarly, the derivative of current is equal to 1/L multiplied by the sum of three
terms (one pos., two neg.).
 Insert two Gain blocks, (from the Linear block library) one attached to each of the
integrators.
 Edit the gain block corresponding to angular acceleration by double-clicking it and
changing its value to "1/J".
 Change the label of this Gain block to "inertia" by clicking on the word "Gain"
underneath the block.
 Similarly, edit the other Gain's value to "1/L" and it's label to Inductance.
 Insert two Sum blocks (from the Linear block library), one attached by a line to
each of the Gain blocks.
 Edit the signs of the Sum block corresponding to rotation to "+-" since one term is
positive and one is negative.
 Edit the signs of the other Sum block to "-+-" to represent the signs of the terms in
Kirchoff's equation.

Now, we will add in the torques which are represented in Newton's equation. First, we will
add in the damping torque.

 Insert a gain block below the inertia block, select it by single-clicking on it, and
select Flip from the Format menu (or type Ctrl-F) to flip it left-to-right.
 Set the gain value to "b" and rename this block to "damping".
 Tap a line (hold Ctrl while drawing) off the rotational integrator's output and
connect it to the input of the damping gain block.
 Draw a line from the damping gain output to the negative input of the rotational
Sum block.

Next, we will add in the torque from the armature.


 Insert a gain block attached to the positive input of the rotational Sum block with a
line.
 Edit it's value to "K" to represent the motor constant and Label it "Kt".
 Continue drawing the line leading from the current integrator and connect it to the
Kt gain block.

Now, we will add in the voltage terms which are represented in Kirchoff's equation. First,
we will add in the voltage drop across the coil resistance.

 Insert a gain block above the inductance block, and flip it left-to-right.
 Set the gain value to "R" and rename this block to "Resistance".
 Tap a line (hold Ctrl while drawing) off the current integrator's output and connect
it to the input of the resistance gain block.
 Draw a line from the resistance gain output to the upper negative input of the
current equation Sum block.

Next, we will add in the back emf from the motor.

 Insert a gain block attached to the other negative input of the current Sum block
with a line.
 Edit it's value to "K" to represent the motor constant and Label it "Ke".
 Tap a line off the rotational integrator output and connect it to the Ke gain block.
The third voltage term in the Kirchoff equation is the control input, V. We will apply a step
input.

 Insert a Step block (from the Sources block library) and connect it with a line to the
positive input of the current Sum block.
 To view the output speed, insert a Scope (from the Sinks block library) connected to
the output of the rotational integrator.
 To provide a appropriate unit step input at t=0, double-click the Step block and set
the Step Time to "0".
You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "3" in the Stop Time field. 3 seconds is long enough to
view the open-loop response. The physical parameters must now be set. Run the following
commands at the MATLAB prompt:
J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.

Extracting a Linear Model into MATLAB


A linear model of the system (in state space or transfer function form) can be extracted
from a Simulink model into MATLAB. This is done through the use of In and Out
Connection blocks and the MATLAB function linmod. First, replace the Step Block and
Scope Block with an In Connection Block and an Out Connection Block, respectively
(these blocks can be found in the Connections block library). This defines the input and
output of the system for the extraction process.
Save your file as "motormod.mdl" (select Save As from the File menu). MATLAB will
extract the linear model from the saved model file, not from the open model window. At the
MATLAB prompt, enter the following commands:

[A,B,C,D]=linmod('motormodel')
[num,den]=ss2tf(A,B,C,D)
You should see the following output, providing both state-space and transfer function
models of the system.
A =

-10.0000 1.0000
-0.0200 -2.0000

B =

0
2

C =

1 0

D =

num =

0 0.0000 2.0000
den =

1.0000 12.0000 20.0200


To verify the model extraction, we will generate an open-loop step response of the
extracted transfer function in MATLAB. Enter the following command in MATLAB.
step(num,den);
You should see the following plot which is equivalent to the Scope's output.

Implementing Lag Compensator Control


In the motor speed control root locus example a Lag Compensator was designed with the
following transfer function.

To implement this in Simulink, we will contain the open-loop system from earlier in this
page in a Subsystem block.

 Create a new model window in Simulink.


 Drag a Subsystem block from the Connections block library into your new model
window.
 Double click on this block. You will see a blank window representing the contents
of the subsystem (which is currently empty).
 Open your previously saved model of the Motor Speed system, motormod.mdl.
 Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit
menu (or Ctrl-C).
 Select the blank subsystem window from your new model and select Paste from the
Edit menu (or Ctrl-V). You should see your original system in this new subsystem
window.
 Close this window. You should now see input and output terminals on the
Subsystem block.
 Name this block "plant model".

Now, we will insert a Lag Compensator into a closed-loop around the plant model. First,
we will feed back the plant output.

 Draw a line extending from the plant output.


 Insert a Sum block and assign "+-" to it's inputs.
 Tap a line of the output line and draw it to the negative input of the Sum block.
The output of the Sum block will provide the error signal. We will feed this into a Lag
Compensator.

 Insert a Transfer Function Block after the summer and connect them with a line.
 Edit this block and change the Numerator field to "[50 50]" and the Denominator
field to "[1 0.01]".
 Label this block "Lag Compensator".

Finally, we will apply a step input and view the output on a scope.

 Attach a step block to the free input of the feedback Sum block and attach a Scope
block to the plant output.
 Double-click the Step block and set the Step Time to "0".
You can download our version of the closed-loop system here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "3" in the Stop Time field. The design requirements
included a settling time of less than 2 sec, so we simulate for 3 sec to view the output. The
physical parameters must now be set. Run the following commands at the MATLAB
prompt:
J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.
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 variab, 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];
motor=tf(num,den);
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(motor,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];
motor=ss(A,B,C,D);
The step response is obtained using the command
step(motor)

Example: DC Motor Position Modeling in


Simulink
Physical setup
Building the model
Open-loop response
Extracting a digital model
Implementing digital control
Closed-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

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

Building the Model


This system will be modeled by summing the torques acting on the rotor inertia and
integrating the acceleration to give the velocity, and integrating velocity to get position.
Also, Kirchoff's laws will be applied to the armature circuit. Open Simulink and open a
new model window. First, we will model the integrals of the rotational acceleration and of
the rate of change of armature current.

 Insert an Integrator block (from the Linear block library) and draw lines to and from
its input and output terminals.
 Label the input line "d2/dt2(theta)" and the output line "d/dt(theta)" as shown
below. To add such a label, double click in the empty space just above the line.
 Insert another Integrator block attached to the output of the previous one and draw a
line from its output terminal.
 Label the output line "theta".
 Insert a third Integrator block above the first one and draw lines to and from its
input and output terminals.
 Label the input line "d/dt(i)" and the output line "i".
Next, we will start to model both Newton's law and Kirchoff's law. These laws applied to
the motor system give the following equations:

The angular acceleration is equal to 1/J multiplied by the sum of two terms (one pos., one
neg.). Similarly, the derivative of current is equal to 1/L multiplied by the sum of three
terms (one pos., two neg.).

 Insert two Gain blocks, (from the Linear block library) one attached to each of the
leftmost integrators.
 Edit the gain block corresponding to angular acceleration by double-clicking it and
changing its value to "1/J".
 Change the label of this Gain block to "inertia" by clicking on the word "Gain"
underneath the block.
 Similarly, edit the other Gain's value to "1/L" and it's label to Inductance.
 Insert two Sum blocks (from the Linear block library), one attached by a line to
each of the Gain blocks.
 Edit the signs of the Sum block corresponding to rotation to "+-" since one term is
positive and one is negative.
 Edit the signs of the other Sum block to "-+-" to represent the signs of the terms in
Kirchoff's equation.
Now, we will add in the torques which are represented in Newton's equation. First, we will
add in the damping torque.

 Insert a gain block below the inertia block, select it by single-clicking on it, and
select Flip from the Format menu (or type Ctrl-F) to flip it left-to-right.
 Set the gain value to "b" and rename this block to "damping".
 Tap a line (hold Ctrl while drawing) off the first rotational integrator's output
(d/dt(theta)) and connect it to the input of the damping gain block.
 Draw a line from the damping gain output to the negative input of the rotational
Sum block.

Next, we will add in the torque from the armature.

 Insert a gain block attached to the positive input of the rotational Sum block with a
line.
 Edit it's value to "K" to represent the motor constant and Label it "Kt".
 Continue drawing the line leading from the current integrator and connect it to the
Kt gain block.

Now, we will add in the voltage terms which are represented in Kirchoff's equation. First,
we will add in the voltage drop across the coil resistance.

 Insert a gain block above the inductance block, and flip it left-to-right.
 Set the gain value to "R" and rename this block to "Resistance".
 Tap a line (hold Ctrl while drawing) off the current integrator's output and connect
it to the input of the resistance gain block.
 Draw a line from the resistance gain output to the upper negative input of the
current equation Sum block.
Next, we will add in the back emf from the motor.

 Insert a gain block attached to the other negative input of the current Sum block
with a line.
 Edit it's value to "K" to represent the motor constant and Label it "Ke".
 Tap a line off the first rotational integrator's output (d/dt(theta)) and connect it to the
Ke gain block.

The third voltage term in the Kirchoff equation is the control input, V. We will apply a step
input.

 Insert a Step block (from the Sources block library) and connect it with a line to the
positive input of the current Sum block.
 To view the output speed, insert a Scope (from the Sinks block library) connected to
the output of the second rotational integrator (theta).
 To provide a appropriate unit step input at t=0, double-click the Step block and set
the Step Time to "0".
You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "0.2" in the Stop Time field. 0.2 seconds is long
enough to view the open-loop response. Also in the Parameters dialog box, it is helpful to
change the Solver Options method. Click on the field which currently contains "ode45
(Dormand-Prince)". Select the option "ode15s (stiff/NDF)". Since the time scales in this
example are very small, this stiff system integration method is much more efficient than the
default integration method.
The physical parameters must now be set. Run the following commands at the MATLAB
prompt:
J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.

Extracting a Digital Model into MATLAB


A linear digital model of this continuous-time system (in state space or transfer function
form) can be extracted from a Simulink model into MATLAB. Conversion to a discrete-time
(digital) system is done with Zero-Order Hold blocks on both the inputs and outputs of the
system, which act as both D/A (sample-and-hold) and A/D devices. The extraction of a
model makes use of In and Out Connection blocks and the MATLAB function dlinmod. We
will start with the model which we just build. You can download a complete version here.
We will first group all of the system components (except for the Step and Scope which
aren't really part of the system) into a Subsystem block.

 Drag the mouse from one corner of your model window to the other to highlight all
of the components. If possible, avoid highlighting the Step and Scope blocks, but if
you do, hold the shift key and single click on either of the Step and Scope blocks to
un-highlight them. Your model window should appear as shown below.

 Select Create Subsystem on the Edit menu (or hit Ctrl-G). This will group all of the
selected blocks into a single block. Your window should appear as shown below.
 Change the label of the Subsystem block to "Continuous Plant". If you like, you can
resize this block so the words "In1" and "Out1" inside of it don't overlap. To resize
a block, highlight it by single clicking it and drag the corners to the desired size.
 Replace the Step Block and Scope Block with Zero Order Hold blocks (from the
Discrete block library). One Zero Order Hold block is used to convert a discrete-
time signal to a stepwise-constant continuous signal. The other Zero Order Hold
block is used to take discrete samples of the output from the plant.
 Edit the Zero Order Hold blocks and set the Sample Time fields to 0.001 (this is fast
compared to the desired step response in the MATLAB tutorial.)
 Connect an In Connection Block to the input of the first Zero Order Hold block, and
an Out Connection Block to the output of the second Zero Order Hold block. (these
blocks can be found in the Connections block library). This defines the input and
output of the system for the extraction process.
 Drag each block in your model so that they are arranged in a line.
Save your file as "motorpos.mdl" (select Save As from the File menu). MATLAB will
extract the linear model from the saved model file, not from the open model window. At the
MATLAB prompt, enter the following commands:

[A,B,C,D]=dlinmod('motorposmodel',.001)
[num,den]=ss2tf(A,B,C,D)
The extra parameter in dlinmod provides the sample time for the discrete conversion. You
should see the following output, providing discrete time models of the system both in state-
space and transfer function form.
A =

1.0000 0.0000 0.0010


0 0.0000 -0.0065
0 0.0055 0.9425

B =

0.0010
0.2359
2.0589

C =

1 0 0

D =

num =

0 0.0010 0.0010 0.0000

den =

1.0000 -1.9425 0.9425 0


As noticed in above results, both numerator and denominator of the discrete transfer
function have one extra root at z = 0. These cancel each other, and the discrete-time transfer
function to the motor position output from the voltage input is:

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

[x1] = dstep(num,den,201);
t=0:0.001:0.2;
stairs(t,x1)
You should see the following plot which is equivalent to the Scope's output.

Implementing Digital Control


In the motor speed control digital example a digital controller was designed with the
following transfer function.

 Bring up the model window containing the digital system which was just extracted
into MATLAB. (You can download our version here)
 Delete the "In" and "Out" blocks.

We will first feed back the plant output.


 Insert a Sum block and assign "+-" to it's inputs.
 Tap a line of the output line of the output Zero Order Hold line and draw it to the
negative input of the Sum block.

The output of the Sum block will provide the error signal. We will feed this into the digital
controller.

 Insert a Discrete Transfer Function Block (from the Discrete block library) after the
summer and connect them with a line.
 Edit this block and change the Numerator field to "450*conv([1 -.85],[1 -.85])", the
Denominator field to "conv([1 .98],[1 -.7])", and the Sample Time to ".001".
 Label this block "Controller" and resize it to view the entire contents.

Finally, we will apply a step input and view the output on a scope.

 Attach a Step block to the free input of the feedback Sum block and attach a Scope
block to the plant output.
 Double-click the Step block and set the Step Time to "0".
You can download our version of the closed-loop system here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "0.05" in the Stop Time field. The design requirements
included a settling time of less than 0.04 sec, so we simulate for 0.05 sec to view the
output. The physical parameters must now be set. Run the following commands at the
MATLAB prompt:
J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.
Example: Modeling a Bus Suspension System
using Transfer Functions
Physical setup
Design requirements
Equations of motion
Transfer function equation
Entering equations into MATLAB
Open-loop response
Control Block Diagram
State Space Method

Physical setup

Designing an automatic suspension system for a bus turns out to be an interesting control
problem. When the suspension system is designed, a 1/4 bus model (one of the four wheels) is
used to simplify the problem to a one dimensional spring-damper system. A diagram of this
system is shown below:
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 wheel goes 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, is 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 the control 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 the disturbance input W(s) only, we set U(s) = 0. Thus we get
the transfer function G2(s) as the following:

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];
P=tf(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];
G2=tf(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(P)
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 an unacceptably long
time to reach the steady state (the settling time is very large). The solution to this problem
is to add a feedback controller to the system.

step(G2)
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: Bus Suspension Modeling in


Simulink
Physical setup
Building the model
Open-loop response
Extracting the Model
Implementing Full State Feedback
Closed-loop response

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

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.

Building the Model


This system will be modeled by summing the forces acting on both masses (body and
suspension) and integrating the accelerations of each mass twice to give velocities and
positions. Newton's law will be applied to each mass. Open Simulink and open a new
model window. First, we will model the integrals of the accelerations of the masses.

 Insert an Integrator block (from the Linear block library) and draw lines to and from
its input and output terminals.
 Label the input line "a1" (for acceleration) and the output line "v1" (for velocity) To
add such a label, double click in the empty space just above the line.
 Insert another Integrator block connected to the output of the first.
 Draw a line from its output and label it "x1" (for position).
 Insert a second pair of Integrators below the first with lines labeled "a2", "v2", and
"x2".

Next, we will start to model Newton's law. Newton's law for each of these masses can be
expressed as:
These equations can be represented with gain blocks (for 1/M1 and 1/M2) and two
summation blocks.

 Insert two Gain blocks, (from the Linear block library) one attached to the inputs of
each of the integrator pairs.
 Edit the gain block corresponding to M1 by double-clicking it and changing its
value to "1/m1".
 Change the label of this Gain block to "Mass 1" by clicking on the word "Gain"
underneath the block.
 Similarly, edit the other Gain's value to "1/m2" and it's label to "Mass 2". (You may
want to resize the gain blocks to view the contents. To do this, single click on the
block to highlight it, and drag one of the corners to the desired size.)

There are three forces acting on M1 (one spring, one damper, and the input, u) and five
forces acting on M2 (two springs, two dampers, and the input, u).

 Insert two Sum blocks (from the Linear block library), one attached by a line to
each of the Gain blocks.
 Edit the signs of the Sum block corresponding to M1 to "+--" to represent the three
forces (two of which will be negative)
 Edit the signs of the other Sum block to "++-++" to represent the five forces, one of
which will be negative.
Now, we will add in the forces acting on each mass. First, we will add in the force from
Spring 1. This force is equal to a constant, k1 times the difference X1-X2.

 Insert a sum block after the upper pair of integrators.


 Edit its signs to "+-" and connect the "x1" signal to the positive input and the "x2"
signal to the negative input.
 Draw a line leading from the output of the Sum block.
 Insert a Gain block above the "Mass1" block.
 Flip it left-to-right by single-clicking on it and selecting Flip Block from the Format
menu (or hit Ctrl-F).
 Edit the value of this gain to "k1" and label the block "Spring 1".
 Tap a line off the output of the last Sum block and connect it to the input of this
gain block.
 Connect the output of this gain block (the spring force) to the second input of the
Mass 1 Sum block. This input should be negative since the Spring 1 pulls down on
Mass 1 when X1 > X2.
 Tap a line off the spring force line and connect it to the second input of the Mass 2
Sum block. This input is positive since Spring 1 pulls up on Mass 2.
Now, we will add in the force from Damper 1. This force is equal to b1 times V1-V2.

 Insert a sum block below the Mass 1's first integrator.


 Flip it left-to-right, and edit it's signs to "+-".
 Tap a line off the "v1" line and connect it to the positive input of this Sum block.
 Tap a line off the "v2" line and connect it to the negative input of this Sum block.
 Insert a Gain block to the left of this Sum block and flip it left-to-right.
 Edit it's value to "b1" and label it "Damper 1".
 Connect the output of the new Sum block to the input of this gain block.
 Connect the output of this gain block (the damper force) to the third input of the
Mass 1 Sum block. This input is negative, similar to Spring 1's force on Mass 1.
 Tap a line off Damper 1's force line and connect it to the first input (which is
positive) of Mass 2's Sum block.
Now we will add in the force from Spring 2. This force acts only on Mass 2, but depends
on the ground profile, W. Spring 2's force is equal to X2-W.

 Insert a Step block in the lower left area of your model window. Label it "W".
 Edit it's Step Time to "0" and it's Final Value to "0". (We will assume a flat road
surface for now).
 Insert a Sum block to the right of the W Step block and edit its signs to "-+".
 Connect the output of the Step block to the positive input of this Sum block.
 Tap a line off the "x2" signal and connect it to the negative input of the new Sum
block.
 Insert a Gain block to the right of this Sum block and connect the Sum's output to
the new Gain's input.
 Change the value of the gain to "k2" and label it "Spring 2".
 Connect the output of this block (Spring 2's force) to the fourth input of Mass 2's
Sum block. This force adds in in the positive sense.
Next, we will add in the force from Damper 2. This force is equal to b2 times V2-d/dt(W).
Since there is no existing signal representing the derivative of W we will need to generate
this signal.

 Insert a Derivative block (from the Linear block library) to the right of the W step
block.
 Tap a line of the Step's output and connect it to the input of the Derivative block.
 Insert a Sum block after the Derivative block and edit it's signs to "+-".
 Connect the Derivative's output to the positive input of the new Sum block.
 Tap a line off the "v2" line and connect it to the negative input of this Sum block.
 Connect the output of this Sum block (Damper 2's force) to the fifth input of Mass
2's Sum block. This force also adds in with positive sign.
The last force in the input U acting between the two masses.

 Insert a Step block in the upper left of the model window.


 Connect it's output to the remaining input of Mass 1's Sum block (with positive
sign).
 Tap a line off this signal and connect it to the remaining input of Mass 2's Sum
block (with negative sign).
 Edit this Step block's Step Time to "0" and leave its Final Value "1".
 Label this Step block "U".
 Finally, to view the output (X1-X2) insert a Scope connected to the output of the
rightmost Sum block.
You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "50" in the Stop Time field. 50 seconds is long enough
to view the open-loop response. The physical parameters must now be set. Run the
following commands at the MATLAB prompt:
m1=2500;
m2=320;
k1=80000;
k2=500000;
b1 = 350;
b2 = 15020;
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.
Extracting a Linear Model into MATLAB
A linear model of the system (in state space or transfer function form) can be extracted
from a Simulink model into MATLAB. This is done through the use of In and Out
Connection blocks and the MATLAB function linmod. We will extract only the model from
the input U to the output X1-X2.

 First, replace the U Step block with an In Connection Block.


 Also, replace the Scope block with an Out Connection Block. (These blocks can be
found in the Connections block library). This defines the input and output of the
system for the extraction process.
Save your file as "suspmod.mdl" (select Save As from the File menu). MATLAB will extract
the linear model from the saved model file, not from the open model window. At the
MATLAB prompt, enter the following commands:

[A,B,C,D]=linmod('suspmodel')
[num,den]=ss2tf(A,B,C,D)
You should see the following output, providing both state-space and transfer function
models of the system.
A =

1.0e+003 *

0 0 0 0.0010
0 0 0.0010 0
0.2500 -1.8125 -0.0480 0.0011
-0.0320 0.0320 0.0001 -0.0001

B =

0
0
-0.0031
0.0004

C =

1 -1 0 0
D =

num =

0 0.0000 0.0035 0.0188 0.6250

den =

1.0e+004 *

0.0001 0.0048 0.1851 0.1721 5.0000


To verify the model extraction, we will generate an open-loop step response of the
extracted transfer function in MATLAB. Enter the following command in MATLAB.
step(num,den);
You should see the following plot which is equivalent to the Scope's output.

Implementing Full State Feedback


In the Bus Suspension Control State Space example a full-state feedback controller was
designed feeding back the following five states:

The controller used the following feedback gain matrix:

To implement this in Simulink, we will contain the open-loop system from earlier in this
page in a Subsystem block.
 Create a new model window.
 Drag a Subsystem block from the Connections block library into your new model
window.

 Double click on this block. You will see a blank window representing the contents
of the subsystem (which is currently empty).
 Open your previously saved model of the Bus Suspension system, suspmod.mdl.
 Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit
menu (or Ctrl-C).
 Select the blank subsystem window from your new model and select Paste from the
Edit menu (or Ctrl-V). You should see your original system in this new subsystem
window (you may need to use the scroll bars to center on it).
 Label the In Connection block "U", and the Out Connection block "y1".
 Replace the W Step block with an In Connection block and label this block "W".
Now we will generate the other state outputs from the subsystem.

 Insert an Out block below the "y1" block and label it "d/dt(y1)", Tap a line off the
line leading into the Damper 1 gain block (V1-V2) and connect it to the d/dt(y1)
Out block.
 Insert another Out block below the "d/dt(y1)" Out block and label it "x1".
 Tap a line off the "x1" line and connect it to this Out block.
 Insert another Out block below the "x1" Out block and label it "d/dt(x1)".
 Tap a line off the "v1" line and connect it to this Out block.
The final, extra, state needs to be generated, which is the integral of Y1.

 Insert an Integrator block above the "y1" Out block and connect its input with a line
tapped of the input to the "y1" Out block.
 Insert an Out block, label it "int(y1)", and connect it to the output of the new
integrator.
Since the state outputs will be used to form a vector, it is important that they be numbered
in the right order.

 Edit the "x1" Out block and change its Port Number to "1".
 Similarly, change the "d/dt(x1)" Out block's port number to "2", "y1" Out's port
number to "3", "d/dt(y1)" Out's port number to "4", and "int(y1)" Out's port number
to "5".
 The In blocks should be numbered such that "U" is "1" and "W" is "2". Some of
these numbers may already be correct.
 Close the Subsystem window. You should now see input and output terminals on
the Subsystem block.
 Name this block "Suspension Model".
 You should resize this block so that you can read all of the labels. To do this,
highlight it by single-clicking on it and drag one of the highlighted corners to the
right size. Notice that the model has two inputs and five outputs. Each input and
output is a scalar signal in this model.
Now, we will build a full-state feedback controller around the plant model. First, we need
to create a vector signal out of the five scalar outputs in order to multiply by the feedback
gain matrix K.

 Insert a Mux block (from the Connections block library) to the right of the
Suspension Model block. The Mux takes multiple inputs and combines them into a
vector signal. By default, the Mux has three inputs.
 Edit the Mux block and change the Number of Inputs to "5".
 Resize the Mux so that it is the same height as the Suspension Model block.
 Connect each of the Suspension Model's outputs to the Mux's inputs in order.

Now, we will close the loop.

 Insert a Matrix Gain block (from the Linear block library) below the Suspension
Model block.
 Flip the Matrix Gain left-to-right and edit its value to "K".
 Insert a Sum block to the left of the Suspension Model block.
 Edit its signs to "+-".
 Connect the output of the Matrix Gain to the negative input of the Sum block.
 Connect the output of the Sum block to the "U" input of the Suspension Model.
 Insert a Step block and connect it to the positive input of the Sum block.
 Label the step block "r" and edit its Step Time to "0" and its Final Value to "0" (we
are commanding the bust to stay level).
 Insert a Step block and connect it to the "W" input of the Suspension Model.
 Edit its Step Time to "0" and its Final Value to "-.1" (we are now assuming a 10cm
deep pothole).
 Insert a Scope block and tap a line off the "y1" output of the Suspension Model and
connect it to the Scope.

You can download our version of the closed-loop system here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters
from the Simulation menu and enter "2" in the Stop Time field. The design requirements
included a settling time of less than 5 sec, and the system actually settles in 2 sec. The
physical parameters must now be set. Run the following commands at the MATLAB
prompt:
m1=2500;
m2=320;
k1=80000;
k2=500000;
b1 = 350;
b2 = 15020;
The last step is to assign values to the feedback gain matrix K. Execute the following
command at the MATLAB prompt.
K= [ 0 2.3e6 5e8 0 8e6 ];
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is
finished, double-click on the scope and hit its autoscale button. You should see the
following output.
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.2 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 pendulum's 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 its 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:
Since we will be looking at the angle Phi as the output of interest, solve the first equation
for X(s),

then substitute into the second equation, and re-arrange. 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 = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

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

num = [m*l/q 0];


den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q];
pend=tf(num,den)
Your output should be:
Transfer function:
4.545 s
----------------------------------
s^3 + 0.1818 s^2 - 31.18 s - 4.455
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(pend,t)
axis([0 1 0 60])
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.

Although the output amplitude increases past 60 radians (10 revolutions), the model is only
valid for small . In actuality, the pendulum will stop rotating when it hits the cart ( =90
degree).

2. State-Space

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

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


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]
pend=ss(A,B,C,D);
T=0:0.05:10;
U=0.2*ones(size(T));
[Y,T,X]=lsim(pend,U,T);
plot(T,Y)
axis([0 2 0 100])
You should see the following output after running the m-file:

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. Several example controllers are
included with these tutorials; 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: Modeling an Inverted Pendulum in


Simulink
Problem setup and design requirements
Force analysis and system equation setup
Building the model
Open-loop response
Extracting a linearized model
Implementing PID control
Closed-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 = 0 (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 0). 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.2 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

In this example, we will implement a PID controller which can only be applied to a single-
input-single-output (SISO) system,so we will be only interested in the control of the
pendulums angle. Therefore, none of the design criteria deal with the cart's position. 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.

Force analysis and system equation setup


Below are the two Free Body Diagrams of the system.

This system is tricky to model in Simulink because of the physical constraint (the pin joint)
between the cart and pendulum which reduces the degrees of freedom in the system. Both
the cart and the pendulum have one degree of freedom (X and theta, respectively). We will
then model Newton's equation for these two degrees of freedom.

It is necessary, however, to include the interaction forces N and P between the cart and the
pendulum in order to model the dynamics. The inclusion of these forces requires modeling
the x and y dynamics of the pendulum in addition to its theta dynamics. In the MATLAB
tutorial pendulum modeling example the interaction forces were solved for algebraically.
Generally, we would like to exploit the modeling power of Simulink and let the simulation
take care of the algebra. Therefore, we will model the additional x and y equations for the
pendulum.
However, xp and yp are exact functions of theta. Therefore, we can represent their
derivatives in terms of the derivatives of theta.

These expressions can then be substituted into the expressions for N and P. Rather than
continuing with algebra here, we will simply represent these equations in Simulink.

Simulink can work directly with nonlinear equations, so it is unnecessary to linearize these
equations as it was in the MATLAB tutorials.

Building the Model in Simulink


First, we will model the states of the system in theta and x. We will represent Newton's
equations for the pendulum rotational inertia and the cart mass.

 Open a new model window in Simulink, and resize it to give plenty of room (this is
a large model).
 Insert two integrators (from the Linear block library) near the bottom of your model
and connect them in series.
 Draw a line from the second integrator and label it "theta". (To insert a label,
double-click where you want the label to go.)
 Label the line connecting the integrators "d/dt(theta)".
 Draw a line leading to the first integrator and label it "d2/dt2(theta)".
 Insert a Gain block (from the Linear block library) to the left of the first integrator
and connect its output to the d2/dt2(theta) line.
 Edit the gain value of this block by double clicking it and change it to "1/I".
 Change the label of this block to "Pendulum Inertia" by clicking on the word
"Gain". (you can insert a newline in the label by hitting return).
 Insert a Sum block (from the Linear block library) to the left of the Pendulum
Inertia block and connect its output to the inertia's input.
 Change the label of this block to Sum Torques on Pend.
 Construct a similar set of elements near the top of your model with the signals
labeled with "x" rather than "theta". The gain block should have the value "1/M"
with the label "Cart Mass", and the Sum block should have the label "Sum Forces
on Cart".
 Edit the Sum Forces block and change its signs to "-+-". This represents the signs of
the three horizontal forces acting on the cart.
Now, we will add in two of the forces acting on the cart.

 Insert a Gain block above the Cart Mass block. Change its value to "b" and its label
to "damping".
 Flip this block left-to-right by single clicking on it (to select it) and selecting Flip
Block from the Format menu (or hit Ctrl-F).
 Tap a line off the d/dt(x) line (hold Ctrl while drawing the line) and connect it to the
input of the damping block.
 Connect the output of the damping block to the topmost input of the Sum Forces
block. The damping force then has a negative sign.
 Insert an In block (from the Connections block library) to the left of the Sum Forces
block and change its label to "F".
 Connect the output of the F in block to the middle (positive) input of the Sum
Forces block.

Now, we will apply the forces N and P to both the cart and the pendulum. These forces
contribute torques to the pendulum with components "N L cos(theta) and P L sin(theta)".
Therefore, we need to construct these components.
 Insert two Elementary Math blocks (from the Nonlinear block library) and place
them one above the other above the second theta integrator. These blocks can be
used to generate simple functions such as sin and cos.
 Edit upper Math block's value to "cos" and leave the lower Math block's value "sin".
 Label the upper (cos) block "Vertical" and the lower (sin) block "Horizontal" to
identify the components.
 Flip each of these blocks left-to-right.
 Tap a line off the theta line and connect it to the input of the cos block.
 Tap a line of the line you just drew and connect it to the input of the sin block.
 Insert a Gain block to the left of the cos block and change its value to "l" (lowercase
L) and its label to "Pend. Len."
 Flip this block left-to-right and connect it to the output of the cos block.
 Copy this block to a position to the left of the sin block. To do this, select it (by
single-clicking) and select Copy from the Edit Menu and then Paste from the Edit
menu (or hit Ctrl-C and Ctrl-V). Then, drag it to the proper position.
 Connect the new Pend. Len.1 block to the output of the sin block.
 Draw long horizontal lines leading from both these Pend. Len. blocks and label the
upper one "l cos(theta)" and the lower one "l sin(theta)".
Now that the pendulum components are available, we can apply the forces N and P. We
will assume we can generate these forces, and just draw them coming from nowhere for
now.

 Insert two Product blocks (from the Nonlinear block library) next to each other to
the left and above the Sum Torques block. These will be used to multiply the forces
N and P by their appropriate components.
 Rotate the left Product block 90 degrees. To do this, select it and select Rotate
Block from the Format menu (or hit Ctrl-R).
 Flip the other product block left-to-right and also rotate it 90 degrees.
 Connect the left Product block's output to the lower input of the Sum Torques block.
 Connect the right Product block's output to the upper input of the Sum Torques
block.
 Continue the l cos(theta) line and attach it to the right input of the left Product
block.
 Continue the l sin(theta) line and attach it to the right input of the right Product
block.
 Begin drawing a line from the open input of the right product block. Extend it up
and the to the right. Label the open end of this line "P".
 Begin drawing a line from the open input of the left product block. Extend it up and
the to the right. Label the open end of this line "N".
 Tap a line off the N line and connect it to the open input of the Sum forces block.
Next, we will represent the force N and P in terms of the pendulum's horizontal and vertical
accelerations from Newton's laws.

 Insert a Gain block to the right of the N open ended line and change its value to "m"
and its label to "Pend. Mass".
 Flip this block left-to-right and connect it's to N line.
 Copy this block to a position to the right of the open ended P line and attach it to the
P line.
 Draw a line leading to the upper Pend. Mass block and label it "d2/dt2(xp)".
 Insert a Sum block to the right of the lower Pend. Mass block.
 Flip this block left-to-right and connect its output to the input of the lower Pend.
Mass block.
 Insert a Constant block (from the Sources block library) to the right of the new Sum
block, change its value to "g" and label it "Gravity".
 Connect the Gravity block to the upper (positive) input of the newest Sum block.
 Draw a line leading to the open input of the new Sum block and label it
"d2/dt2(yp)".
Now, we will begin to produce the signals which contribute to d2/dt2(xp) and d2/dt2(yp).

 Insert a Sum block to the right of the d2/dt2(yp) open end.


 Change the Sum block's signs to "--" to represent the two terms contributing to
d2/dt2(yp).
 Flip the Sum block left-to-right and connect it's output to the d2/dt2(yp) signal.
 Insert a Sum block to the right of the d2/dt2(xp) open end.
 Change the Sum block's signs to "++-" to represent the three terms contributing to
d2/dt2(xp).
 Flip the Sum block left-to-right and connect it's output to the d2/dt2(xp) signal.
 The first term of d2/dx2(xp) is d2/dx2(x). Tap a line off the d2/dx2(x) signal and
connect it to the topmost (positive) input of the newest Sum block.
Now, we will generate the terms d2/dt2(theta)*lsin(theta) and d2/dt2(theta)*lcos(theta).

 Insert two Product blocks next to each other to the right and below the Sum block
associated with d2/dt2(yp).
 Rotate the left Product block 90 degrees.
 Flip the other product block left-to-right and also rotate it 90 degrees.
 Tap a line off the l sin(theta) signal and connect it to the left input of the left Product
block.
 Tap a line off the l cos(theta) signal and connect it to the right input of the right
Product block.
 Tap a line off the d2/dt2(theta) signal and connect it to the right input of the left
Product block.
 Tap a line of this new line and connect it to the left input of the right Product block.
Now, we will generate the terms (d/dt(theta))^2*lsin(theta) and (d/dt(theta))^2*lcos(theta).

 Insert two Product blocks next to each other to the right and slightly above the
previous pair of Product blocks.
 Rotate the left Product block 90 degrees.
 Flip the other product block left-to-right and also rotate it 90 degrees.
 Tap a line off the l cos(theta) signal and connect it to the left input of the left
Product block.
 Tap a line off the l sin(theta) signal and connect it to the right input of the right
Product block.
 Insert a third Product block and insert it slightly above the d/dt(theta) line. Label
this block "d/dt(theta)^2".
 Tap a line off the d/dt(theta) signal and connect it to the left input of the lower
Product block.
 Tap a line of this new line and connect it to the right input of the lower Product
block.
 Connect the output of the lower Product block to the free input of the right upper
Product block.
 Tap a line of this new line and connect it to the free input of the left upper Product
block.

Finally, we will connect these signals to produce the pendulum acceleration signals. In
addition, we will create the system outputs x and theta.

 Connect the d2/dt2(theta)*lsin(theta) Product block's output to the lower (negative)


input of the d2/dt2(yp) Sum block.
 Connect the d2/dt2(theta)*lcos(theta) Product block's output to the lower (negative)
input of the d2/dt2(xp) Sum block.
 Connect the d/dt(theta)^2*lcos(theta) Product block's output to the upper (negative)
input of the d2/dt2(yp) Sum block.
 Connect the d/dt(theta)^2*lsin(theta) Product block's output to the middle (positive)
input of the d2/dt2(xp) Sum block.
 Insert an Out block (from the Connections block library) attached to the theta signal.
Label this block "Theta".
 Insert an Out block attached to the x signal. Label this block "x". It should
automatically be numbered 2.
Now, save this model as pend.mdl.

Open-loop response
To generate the open-loop response, it is necessary to contain this model in a subsystem
block.

 Create a new model window (select New from the File menu in Simulink or hit
Ctrl-N).
 Insert a Subsystem block from the Connections block library.
 Open the Subsystem block by double clicking on it. You will see a new model
window labeled "Subsystem".
 Open your previous model window named pend.mdl. Select all of the model
components by selecting Select All from the Edit menu (or hit Ctrl-A).
 Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit
Ctrl-C).
 Paste the model into the Subsystem window by selecting Paste from the Edit menu
(or hit Ctrl-V) in the Subsystem window
 Close the Subsystem window. You will see the Subsystem block in the untitled
window with one input terminal labeled F and two output terminals labeled Theta
and x.
 Resize the Subsystem block to make the labels visible by selecting it and dragging
one of the corners.
 Label the Subsystem block "Inverted Pendulum".

Now, we will apply a unit impulse force input, and view the pendulum angle and cart
position. An impulse can not be exactly simulated, since it is an infinite signal for an
infinitesimal time with time integral equal to 1. Instead, we will use a pulse generator to
generate a large but finite pulse for a small but finite time. The magnitude of the pulse
times the length of the pulse will equal 1.

 Insert a Pulse Generator block from the Sources block library and connect it to the F
input of the Inverted Pendulum block.
 Insert a Scope block (from the Sinks block library) and connect it to the Theta
output of the Inverted Pendulum block.
 Insert a Scope block and connect it to the x output of the Inverted Pendulum block.
 Edit the Pulse Generator block by double clicking on it. You will see the following
dialog box.
 Change the Period value to "10" (a long time between a chain of impulses - we will
be interested in only the first pulse).
 Change the Duty Cycle value to ".01" this corresponds to .01% of 10 seconds, or
.001 seconds.
 Change the Amplitude to 1000. 1000 times .001 equals 1, providing an approximate
unit impulse.
 Close this dialog box. You system will appear as shown below.

We now need to set an appropriate simulation time to view the response.

 Select Parameters from the Simulation menu.


 Change the Stop Time value to 2 seconds.
 Close this dialog box

You can download a version of the system here. Before running it, it is necessary to set the
physical constants. Enter the following commands at the MATLAB prompt.
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;
Now, start the simulation (select Start from the Simulation menu or hit Ctrl-t). If you look
at the MATLAB prompt, you will see some error messages concerning algebraic loops. Due
to the algebraic constraint in this system, there are closed loops in the model with no
dynamics which must be resolved completely at each time step before dynamics are
considered. In general, this is not a problem, but often algebraic loops slow down the
simulation, and can cause real problems if discontinuities exist within the loop (such as
saturation, sign functions, etc.)

Open both Scopes and hit the autoscale buttons. You will see the following for theta (left)
and x (right).
Notice that the pendulum swings all the way around due to the impact, and the cart travels
along with a jerky motion due to the pendulum. These simulations differ greatly from the
MATLAB open loop simulations because Simulink allows for fully nonlinear systems.

Extracting the linearized model into MATLAB


Since MATLAB can't deal with nonlinear systems directly, we cannot extract the exact
model from Simulink into MATLAB. However, a linearized model can be extracted. This is
done through the use of In and Out Connection blocks and the MATLAB function linmod. In
the case of this example, will use the equivalent command linmod2, which can better
handle the numerical difficulties of this problem.

To extract a model, it is necessary to start with a model file with inputs and outputs defined
as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as
pend.mdl. In this model, one input, F (the force on the cart) and two outputs, theta
(pendulum angle from vertical) and x (position of the cart), were defined. When linearizing
a model, it is necessary to choose an operating point about which to linearize. By default,
the linmod2 command linearizes about a state vector of zero and zero input. Since this is
the point about which we would like to linearize, we do not need to specify any extra
arguments in the command. Since the system has two outputs, we will generate two transfer
functions.

At the MATLAB prompt, enter the following commands

[A,B,C,D]=linmod2('pend')
[nums,den]=ss2tf(A,B,C,D)
numtheta=nums(1,:)
numx=nums(2,:)
You will see the following output (along with algebraic loop error messages) providing a
state-space model, two transfer function numerators, and one transfer function denominator
(both transfer functions share the same denominator).
A =

0 0 1.0000 0
0 0 0 1.0000
31.1818 0.0000 0.0000 -0.4545
2.6727 0.0000 0.0000 -0.1818

B =

0
0
4.5455
1.8182

C =

1 0 0 0
0 1 0 0

D =

0
0

nums =

0 0.0000 4.5455 0.0000 0.0000


0 0.0000 1.8182 0.0000 -44.5455

den =

1.0000 0.1818 -31.1818 -4.4545 0.0000

numtheta =

0 0.0000 4.5455 0.0000 0.0000

numx =

0 0.0000 1.8182 0.0000 -44.5455


To verify the model, we will generate an open-loop response. At the MATLAB command
line, enter the following commands.
t=0:0.01:5;
impulse(numtheta,den,t);
axis([0 1 0 60]);
You should get the following response for the angle of the pendulum.
Note that this is identical to the impulse response obtained in the MATLAB tutorial
pendulum modeling example. Since it is a linearized model, however, it is not the same as
the fully-nonlinear impulse response obtained in Simulink.

Implementing PID control


In the pendulum PID control example, a PID controller was designed with proportional,
integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this, we
will start with our open-loop model of the inverted pendulum. And add in both a control
input and the disturbance impulse input to the plant.

 Open your Simulink model window you used to obtain the nonlinear open-loop
response. (pendol.mdl)
 Delete the line connecting the Pulse Generator block to the Inverted Pendulum
block. (single-click on the line and select Cut from the Edit menu or hit Ctrl-X).
 Move the Pulse Generator block to the upper left of the window.
 Insert a Sum block to the left of the Inverted Pendulum block.
 Connect the output of the Sum block to the Inverted Pendulum block.
 Connect the Pulse generator to the upper (positive) input of the Sum block.
Now, we will feed back the angle output.

 Insert a Sum block to the right and below the Pulse Generator block.
 Change the signs of the Sum block to "+-".
 Insert a Constant block (from the Sources block library) below the Pulse generator.
Change its value to "0". This is the reference input.
 Connect the constant block to the upper (positive) input of the second Sum block.
 Tap a line off the Theta output of the Inverted Pendulum block and draw it down
and to the left. Extend this line and connect it to the lower (negative) input of the
second Sum block.

Now, we will insert a PID controller.

 Double-click on the Blocksets & Toolboxes icon in the main Simulink window.
This will open a new window with two icons.
 In this new window, double-click on the SIMULINK extras icon. This will open a
window with icons similar to the main Simulink window.
 Double-click on the Additional Linear block library icon. This will bring up a
library of Linear blocks to augment the standard Linear block library.
 Drag a PID Controller block into your model between the two Sum blocks.
 Connect the output of the second Sum block to the input of the PID block.
 Connect the PID output to the first Sum block's free input.

 Edit the PID block by doubleclicking on it.


 Change the Proportional gain to 100, leave the Integral gain 1, and change the
Derivative gain to 20.
 Close this window.

You can download our version of the closed-loop system here

Closed-loop response
We can now simulate the closed-loop system. Be sure the physical parameters are set (if
you just ran the open-loop response, they should still be set.) Start the simulation, double-
click on the Theta scope and hit the autoscale button. You should see the following
response:
This is identical to the closed-loop response obtained in the MATLAB tutorials. Note that
the PID controller handles the nonlinear system very well because the angle is very small
(.04 radians).
Example: Modeling a Pitch Controller
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 steady-state error of less than 2%. For example, if the input is
0.2 rad (11 degrees), then the pitch angle will not exceed 0.22 rad, reaches 0.2 rad within 2
seconds, settles to 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 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];
pitch=tf(num,den);
step(de*pitch)
axis([0 15 0 0.8])
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];
pitch=ss(A,B,C,D);
step(de*pitch)
axis([0 15 0 0.8])
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.
Several different methods to design a controller are listed at the bottom of this page.

Example: Modeling a Pitch Controller in


Simulink
Physical setup and system equations
Building the State-Space Model
Open-loop response
Extracting the model into MATLAB
Building a full-state feedback controller
Closed-loop response

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. In this example, we will begin with the linearized state-space equation
model which was obtained in the MATLAB Tutorials: pitch control example This model,
with numerical values substituted in, is as follows:

in state-space form, the equations are as follows:


Which can be written as:

Building the state-space model


We will model these state equations explicitly in Simulink taking advantage of vector lines
to carry the state vector signal X. Vector lines are produced automatically in Simulink
when blocks input or output vector signals. First, we will represent the derivative of the
state.

 Open a new model window in Simulink.


 Insert an Integrator block from the Linear block library.
 Draw a line from the output of the Integrator and label it "X". To label a line,
double click near the line where you want the label to appear.
 Draw a line leading to the input of the Integrator and label it "d/dt(X)"

Now, we will represent the first state equation, which is d/dt(X)=Ax+Bu.

 Insert a Matrix Gain block from the Linear block library below the integrator.
 Flip this block left-to-right. To do this, select it with the mouse (single-click) and
select Flip Block from the Format menu (or hit Ctrl-F).
 Edit the Matrix Gain block (by double clicking). Change the Gain Matrix field to
"A" (which we will define later in MATLAB) and close the dialog box.
 Change the label of this block from Matrix Gain to "A" by single clicking on the
existing label.
 Tap a line off the state signal, X (hold Ctrl while drawing the line to tap) and
connect it to the input of the A block. This creates the signal Ax.
 Insert a Sum block (from the Linear block library) to the left of the integrator and
connect it's output to the line leading to the Integrator's input.
 Draw a line from the output of the A matrix block to the lower (positive) input of
the Sum block.
 Insert another Matrix Gain block to the left of the Sum block. Change it's Matrix
Gain value to "B" and change it's label to "B".
 Connect the B matrix output to the other (positive) input of the Sum block.
 Draw a line leading to the input of the Sum block. This is the input signal, u.

Now we will form the output signal which is equal to Cx+Du.

 Insert a Matrix Gain block to the right of the integrator and attach the integrator's
output to the input of the Matrix Gain.
 Change the Matrix Gain value to "C" and change it's label to "C".
 Insert another Matrix Gain block below the "C" block, and change both it's value
and label to "D".
 Tap a line off the u signal (the input line of the B matrix block) and attach it to the
input of the "D" matrix block.
 Insert a Sum block to the right of the C matrix block.
 Connect the outputs of the C and D matrix blocks to the inputs of the Sum block.
Next, we will apply inputs and extract outputs from this system. We will use In and Out
blocks for this purpose. These blocks allow the system to be extracted into MATLAB and
they allow the system to be placed into a Subsystem block for use within another model.

 Insert an In block (from the Connections block library) and connect it to the open
line which connects to the input of the B matrix block.
 Change the label of the In block to "deltac" (corresponding to the angle of the
elevator).
 Insert an Out block (from the Connections block library) and connect it to the output
of the rightmost Sum block.
 Change the label of this Out block to "theta" (corresponding to the pitch angle of the
plane).
 Insert an Out block above the Theta block, and change it's label to "X"
corresponding to the state vector of the system. (This will be used later to
implement full-state feedback.
 Tap a line off the X signal (leading from the second integrator) and connect it to the
X Out block.

Save this model as "pitch.mdl" (or download ours here.)


Open-loop response
To generate the open-loop response, it is first necessary to contain this model in a
subsystem block.

 Create a new model window (select New from the File menu in Simulink or hit
Ctrl-N).
 Insert a Subsystem block from the Connections block library.
 Open the Subsystem block by double clicking on it. You will see a new model
window labeled "Subsystem".
 Open your previous model window named pitch.mdl. Select all of the model
components by selecting Select All from the Edit menu (or hit Ctrl-A).
 Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit
Ctrl-C).
 Paste the model into the Subsystem window by selecting Paste from the Edit menu
(or hit Ctrl-V) in the Subsystem window
 Close the Subsystem window. You will see the Subsystem block in the untitled
window with one input terminal labeled deltac and two output terminals labeled
theta and x.
 Resize the Subsystem block to make the labels visible by selecting it and dragging
one of the corners.
 Label the Subsystem block "Plane Pitch Model".

 Insert a Step block (from the Sources block library) and connect it to the input of the
Plane Pitch Model.
 Edit the Step block (by double clicking on it to bring up the dialog box) and change
the Step Time value to 0. Close the Step block dialog box.
 Insert a Scope block (from the Sinks block library) and connect it to the theta output
of the Plane Pitch Model.
Before obtaining a step response, we must set the constants in the matrices A, B, C, and D.
Enter the following commands at the MATLAB prompt.

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];
We are now ready to run the simulation. If you like, you can download our version of the
open-loop system here. Start the simulation by selecting Start from the Simulation menu (or
hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit
the Scope's autoscale button. You will see the following response.

Extracting the model into MATLAB


The Simulink model can be extracted into an equivalent state-space model in MATLAB.
This is done through the use of In and Out Connection blocks and the MATLAB function
linmod.

To extract a model, it is necessary to start with a model file with inputs and outputs defined
as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as
pitch.mdl. In this model, one input, deltac (the elevator angle) and two outputs, theta (pitch
angle) and X (the state vector), were defined. We must truncate the output and feedthrough
matrices (Co and Do) to provide only the first input.

At the MATLAB prompt, enter the following commands

[Ao,Bo,Co,Do]=linmod('pitch');
Ao
Bo
Co=Co(1,:)
Do=Do(1,:)
You will see the following output providing the open-loop model of the system.
Ao =

-0.3130 56.7000 0
-0.0139 -0.4260 0
0 56.7000 0

Bo =

0.2320
0.0203
0

Co =

0 0 1

Do =

0
Note that the matrices Ao, Bo, Co, and Do are the same at the original matrices A, B,C, and
D.

Building a full-state feedback controller


In the CTMS Example: Pitch Controller -- State Space Method page, a full-state feedback
controller was designed using the LQR method. We will now construct this controller in
Simulink.

 Bring up your open-loop Pitch model window (or download ours here)
 Insert a Matrix Gain block below the Plane Pitch Model and change both its Matrix
Gain value and it's label to "K".
 Flip the K matrix block left-to-right.
 Insert a Sum block to the left of the Plane Pitch Model and change it's value to "+-".
 Connect the state vector X output of the Plane Pitch Model to the input of the K
matrix block.
 Connect the output of the K matrix block to the second (negative) input of the Sum
block.
 Connect the Step block to the first (positive) input of the Sum block.
 Connect the Sum block's output to the input of the Plane Pitch Model.

You can download our version of the closed-loop model here.

Closed-loop response
Before simulating the closed-loop system, we must first determine the gain matrix K using
the LQR method. Execute the following commands at the MATLAB prompt.
p=50;
Q=[0 0 0;
0 0 0;
0 0 p];
[K]= lqr (A,B,Q,1)
Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You
should see the following response.
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 horizontal 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 variab 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 second derivative of the input angle alpha actually affects the second derivative of r.
However, we will ignore this contribution. The Lagrangian equation of motion for the ball
is then 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:
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 variab 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 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 = 0.111;
R = 0.015;
g = -9.8;
L = 1.0;
d = 0.03;
J = 9.99e-6;

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

num = [-K];
den = [1 0 0];
ball=tf(num,den)
Your output should be:
Transfer function:
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*ball)
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 = 0.111;
R = 0.015;
g = -9.8;
J = 9.99e-6;

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

A=[0 1 0 0
0 0 H 0
0 0 0 1
0 0 0 0];
B=[0;0;0;1];
C=[1 0 0 0];
D=[0];
ball=ss(A,B,C,D);
The step response to a 0.25m desired position can be viewed by running the command
below:
step(0.25*ball)
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, see Conversions.

Example: Modeling the Ball and Beam


Experiment in Simulink
Problem Setup and System Equations
Building the Simulink Model
Open-Loop Response
Extracting the Model into MATLAB
Constructing a Lead Compensator Controller
Closed-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 horizontal 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 variab 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%
The second derivative of the input angle alpha actually affects the second derivative of r.
However, we will ignore this contribution. The Lagrangian equation of motion for the ball
is then given by the following:

The beam angle (alpha) can be expressed in terms of the angle of the gear (theta).

Building the Model in Simulink


In this example, rather than express all the forces and geometric constraints (which is
difficult to model in Simulink for dynamic systems with constraints) we will model the
nonlinear Lagrangian equation of motion directly. This equation gives d/dt(r) as a function
of the state and input variab, r, d/dt(r), alpha, and d/dt(alpha). We will make use of the
Nonlinear Function Block to express this function. First, we must express the derivatives of
the output, r.

 Open a new model window in Simulink.


 Insert an Integrator block from the Linear block library.
 Insert a second Integrator to the right of the first, and connect the two with a line.
 Label the line connecting the two "d/dt(r)". To label a line, double-click near the
line where you want the label (in this case, just below the line)
 Draw a line from the second Integrator and label it "r".
 Insert an Out block from the Connections block library and connect it to the "r"
signal line. This will form the output of the system.
 Change the label of the Out block to "r" by single-clicking on the existing "Out"
label.
Now, we will insert the function which takes the vector [r d/dt(r) alpha d/dt(alpha)] and
returns d/dt(r).

 Insert a Fcn block from the Nonlinear library and connect its output to the input of
the first Integrator.
 Edit the Fcn block by double clicking it, and change it's function to the following:

(-1/(J/(R^2)+m))*(m*g*sin(u[3])-m*u[1]*(u[4])^2)

This function block takes an input vector, u, where each component is referred to as
u[1], u[2], etc. In our case, u[1]=r, u[2]=d/dt(r), u[3]=alpha, and u[4]=d/dt(alpha).

 Close the dialog box and change the label of the Fcn block to "Ball-Beam
Lagrangian Model" (you can add newlines in the label by hitting return).

Now, we will begin to construct the function input vector u by feeding back the state
signals from the integrators and forming a vector from them with a Mux block.

 Insert a Mux block from the Connections block library and connect its output to the
input of the Ball-Beam block.
 Edit the Mux block (by double-clicking on it) and change its number of inputs to 4.
The Mux block should now have four inputs.
 Tap a line off the d/dt(r) signal (hold Ctrl while drawing) and connect it to the
second input of the Mux block.
 Tap a line of the r signal and connect it to the first input of the Mux block.
Now we will construct the signals alpha and d/dt(alpha) from the input theta.

 Insert an In block on the left side of your model window. Change its label to "theta".
 Insert a Gain block and connect it to the theta block. Change its gain value (double-
click on it) to "d/L".
 Connect the output of the gain block to the third input of the Mux block. Label this
line "alpha".
 Insert a Derivative block from the Linear block library and place it underneath the
alpha signal line.
 Tap a line off the output of the Gain block and connect it to the input of the
Derivative block.
 Connect the output of the Derivative block to the fourth input off the Mux block.

Save your model as "ball.mdl". You can download ours here. Open Loop Response To
generate the open-loop response, it is first necessary to contain this model in a subsystem
block.

 Create a new model window (select New from the File menu in Simulink or hit
Ctrl-N).
 Insert a Subsystem block from the Connections block library.
 Open the Subsystem block by double clicking on it. You will see a new model
window labeled "Subsystem".
 Open your previous model window named ball.mdl. Select all of the model
components by selecting Select All from the Edit menu (or hit Ctrl-A).
 Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit
Ctrl-C).
 Paste the model into the Subsystem window by selecting Paste from the Edit menu
(or hit Ctrl-V) in the Subsystem window
 Close the Subsystem window. You will see the Subsystem block in the untitled
window with one input terminal labeled theta and one output terminal labeled r.
 Resize the Subsystem block to make the labels visible by selecting it and dragging
one of the corners.
 Label the Subsystem block "Ball and Beam Model".

 Insert a Step block (from the Sources block library) and connect it to the input of the
Ball and Beam Model.
 Edit the Step block (by double clicking on it to bring up the dialog box) and change
the Step Time value to 0. Close the Step block dialog box.
 Insert a Scope block (from the Sinks block library) and connect it to the output of
the Ball and Beam Model.

Before obtaining a step response, we must set the physical parameters Enter the following
commands at the MATLAB prompt.
m = 0.111;
R = 0.015;
g = -9.8;
L = 1.0;
d = 0.03;
J = 9.99e-6;
We are now ready to run the simulation. If you like, you can download our version of the
open-loop system here. Start the simulation by selecting Start from the Simulation menu (or
hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit
the Scope's autoscale button. You will see the following response.
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. Later in this tutorial, we will implement a lead compensator.

Extracting the Model into MATLAB


The Simulink model can be extracted into an equivalent state-space or transfer function
model in MATLAB. This is done through the use of In and Out Connection blocks and the
MATLAB function linmod.

To extract a model, it is necessary to start with a model file with inputs and outputs defined
as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as
ball.mdl. In this model, one input, theta (the input crank angle) and one output, r (ball
position), were defined.

At the MATLAB prompt, enter the following commands

[A,B,C,D]=linmod('ball')
[num,den]=ss2tf(A,B,C,D)
You will see the following output providing the open-loop model of the system.
A =

0 1
0 0

B =

0
0.2100

C =

1 0
D =

num =

0 0 0.2100

den =

1 0 0
We can verify this model by obtaining an open-loop step response. Enter the following
command at the MATLAB prompt:
step(num,den);
You will see the following open-loop response:

Building a Lead Compensator Controller


In the CTMS Example: Solution to the Ball & Beam Problem Using Root Locus Method a
lead compensator was designed with a zero at -0.01 and a pole at -5, with a gain of 37.1.
We will now construct this controller in Simulink.

 Bring up your open-loop Ball and Beam model window (or download ours here)
 Delete the line which connects the Step block to the Ball and Beam model block.
 Insert a Transfer Function block from the linear block library to the left of the Ball
and Beam block, and connect its output to the input of the Ball and Beam block.
 Edit the Transfer Function block and change its numerator to "[1 0.01]" and its
denominator to "[1 5]".
 Change the label of the Transfer Function block to "Lead Compensator".
 Insert a Gain block to the left of the Lead Compensator and connect its output to the
Lead compensator's input.
 Change the Gain value to "37.1".
 Insert a Sum block to the left of the Gain block and change it's value to "+-".
Connect the output of the Sum to the input of the Gain block.
 Tap a line off the output of the Ball and Beam model and connect it to the negative
input of the Sum.
 Connect the Step block to the positive input of the Sum block

You can download our version of the closed-loop model here.

Closed-Loop Response
Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You
should see the following response.

Potrebbero piacerti anche