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

- Simulink Examples
-
Cruise Control |
Motor Speed |
Motor Position |
Bus Suspension |
Inverted Pendulum |
Pitch Controller |
Ball and Beam
- Ball and Beam Examples
-
Modeling |
PID |
Root Locus |
Frequency Response |
State Space |
Digital Control |
Simulink
- Tutorials
-
MATLAB Basics |
MATLAB Modeling |
PID |
Root Locus |
Frequency Response |
State Space |
Digital Control |
Simulink Basics |
Simulink Modeling |
Examples
