Mathematical Modelling and State-Space Control of "Ball-on-
Beam" System
In this document a mathematical model of the "ball-on-beam" system illustrated below is derived and expressed
in form of nonlinear state equations. To this end, Lagrange's approach for modelling mechanical systems is
applied. Later different methods to control this model to balance the ball on the beam is applied.
Definitions and Variable Declarations
clc; % clear display
clearvars; % reset variables
System description and definition of important quantities
The following schematic diagram defines coordinate systems and counting directions of important quantities.
1
In particular, the figure introduces the fixed Cartesian reference frame and the beam-fixed coordinate
system which is rotated by the beam angle with respect to the reference frame. The beam
is pivot-mounted at the common origin of these coordinate systems such that it can freely rotate around the
z-axis. By means of an electric motor, which is of no further interest, the torque can be applied to the
beam, i.e. represents the control input . A force acting on the ball must be taken into account
as disturbance input . It is assumed that the ball rolls on the beam without slipping at all times. All other
friction forces and/or torques are neglected. The measured variables are the beam angle and the ball
position on the beam , which corresponds to the x-value of the ball's center of mass in beam-coordinates.
The meaning of some other parameters and quantities of interest is described by the comments in the following
variable declaration.
Declare symbolic variables
syms x_ball(t) % ball position on beam (in beam coordinate system)
syms beta_beam(t) % beam angle
syms M(t) % motor torque acting on beam
syms F(t) % disturbance force acting on ball
syms r_Ball % radius of ball
syms J_Ball % moment of inertia of ball with respect to its symmetr
syms m_Ball % mass of ball
syms G % gravity of earth
syms J_Beam % moment of inertia of beam with respect to its fixed a
syms q1(t) q2(t) % generalized coordinates
syms x1(t) x2(t) x3(t) x4(t) u(t) z(t) % state variables x, control input u, disturbance input
syms x1dot x2dot x3dot x4dot % auxiliary variables needed to express the state diffe
Specify Generalized Coordinates
It is obvious that the position and orientation of the ball and the beam are completely defined if both and
are known. Thus, the system has two degrees of freedom and the two measured variables can be defined
as generalized coordinates:
x_ball = q1
x_ball(t) =
beta_beam = q2
beta_beam(t) =
Determine Total Kinetic Energy of the System
The "ball-on-beam" system consists of two rigid bodies: the ball and the beam. Consequently, the total kinetic
energy can be expressed as the sum of the kinetic energy of the ball and the kinetic energy of the beam.
Kinetic energy of the ball
2
The overall motion of the ball is rather complex, since it simultaneously performs a rotational and a translational
movement. With respect to its center of mass, however, the kinetic energy of any rigid body can still be stated
as a simple sum of a purely translational and a purely rotational movement in relation to a fixed and non-moving
reference coordinate system. Thus, the total kinetic energy of the ball can be calculated as
with
: absolute value of angular ball speed around its center of mass with respect to fixed reference coordinate
system
: absolute value of translational speed at center of mass with respect to fixed reference coordinate system
Calculation of the translational speed at the center of mass
Position of the ball's center of mass in the fixed Cartesian reference frame as a function of the generalized
coordinates:
xref_ball(t) =x_ball(t)*cos(beta_beam(t)) - r_Ball*sin(beta_beam(t))
xref_ball(t) =
yref_ball(t) =x_ball(t) * sin(beta_beam(t)) + r_Ball * cos(beta_beam(t))
yref_ball(t) =
Absolute value of translational ball speed as a function of and with respect to the fixed reference
coordinate system:
vref_ball(t) = simplify(sqrt(diff(xref_ball(t),t)^2+diff(yref_ball(t),t)^2))
vref_ball(t) =
Calculation of the angular speed around the center of mass
Applying the summation rule for angular velocities (see e.g. [1]), the total angular speed of the ball can
be stated as the sum of the angular speed of the ball with respect to the beam and the angular speed of
the beam with respect to the fixed reference coordinate sytem , i.e. . Since in case
of the "ball-on-beam" system all angular speed vectors are parallel to the z-axis, this vector equation can be
simplified to the scalar relationship where
omegaref_beam(t) = diff(q2(t),t)
omegaref_beam(t) =
3
and follows from the ideal rolling conditions as
omegabeam_ball(t) = -diff(x_ball,t)/r_Ball
omegabeam_ball(t) =
Note that there is a negative sign in the later equation. It is caused by the fact that a negative rotation of the
ball against the "right-hand-rule" is needed to move the system into positive direction (see definition of
coordinate systems and counting direction in the figure above).
Summing both terms up yields
omegaref_ball(t) = omegaref_beam(t) + omegabeam_ball(t)
omegaref_ball(t) =
Calculation of the kinetic energy of the ball
Using the above formula for the kinetic energy expressed with respect to the mass center point finally yields
T_ball(t) = simplify(1/2*m_Ball*vref_ball(t)^2+1/2*J_Ball*omegaref_ball(t)^2)
T_ball(t) =
Kinetic energy of the beam
The beam performs a purely rotational movement around the fixed axis of rotation at the center of the
coordinate system (see figure above). Thus, using as the moment of inertia of the beam with respect
to that axis, the total kinetic energy of the beam is
T_beam(t) = 1/2*J_Beam*omegaref_beam^2
T_beam(t) =
Total kinetic energy of the system
4
The total kinetic energy of the system results from the sum of the kinetic energy of the ball and of the beam as
T(t) = T_ball(t) + T_beam(t)
T(t) =
Determine Total Potential Energy of the System
The impact of gravity can be expressed using a potential energy term. As the beam's center of mass does not
move, however, the total potential energy of the system is solely determined by the ball, i.e. . Moreover,
the ball's potential energy only depends on its y-coordinate in the fixed Cartesian reference coordinate
system. Assuming without loss of generality that would hold if the ball's center of mass was at the
origin of the coordinate system, V can be expressed as
V(t) = G*m_Ball*yref_ball(t)
V(t) =
Lagrangian
The Lagrangian
can now easily be stated as a function of and :
L(t) = simplify(T(t) - V(t))
L(t) =
5
Non-conservative Generalized Forces
In general, the non-conservative generalized forces of a system with N degrees of freedom are given as [1]
where , , and , , represent all non-constraint and non-conservative force
and torque vectors acting on the system and , , as well as , , are the speed and
angular speed vectors of the corresponding contact points.
Applied to the "ball-and-beam" system, the number of degrees of freedom is and the external force
as well as the motor torque (see figure above) represent the only non-conservative quantities acting on the
mechanics. Thus, expressed in reference coordinates the formula above simplifies to the two non-conservative
generalized force terms
Force vector and corresponding speed vector of the contact point in reference coordinates
In beam coordinates the force acts in positive x-direction. Consequently, with respect to the fixed reference
coordinate system, the force vector can be expressed as
Fref_vec(t) = F(t)*[cos(q2);sin(q2);0]
Fref_vec(t) =
The position of the corresponding contact point C (see figure) of the force in beam coordinates is
xbeam_C(t) = x_ball(t)-r_Ball
xbeam_C(t) =
ybeam_C = r_Ball
ybeam_C =
Transformed into the fixed Cartesian reference frame, the contact point C is described by
xref_C(t) = xbeam_C(t)*cos(beta_beam(t))-r_Ball*sin(beta_beam(t))
6
xref_C(t) =
yref_C(t) = xbeam_C(t)*sin(beta_beam(t))+r_Ball*cos(beta_beam(t))
yref_C(t) =
Calculating the time derivative yields the speed vector of the contact point C in reference coordinates:
vref_Cvec(t)=[simplify(diff(xref_C(t),t));simplify(diff(yref_C(t),t));0]
vref_Cvec(t) =
Torque vector and corresponding angular speed vector in reference coordinates
The motor torque acts around the z-axis which represents the fixed axis of rotation for the beam. Thus, we
can state the associated torque vector according to
Mref_vec(t) = M(t)*[0;0;1]
Mref_vec(t) =
and the corresponding angular speed vector as
omegaref_beamvec(t) = omegaref_beam(t)*[0;0;1]
omegaref_beamvec(t) =
with respect to the Cartesian reference frame and considering the counting directions specified in the figure
above.
Non-conservative generalized force associated to
Evaluating
7
results in
Q1_nc(t) = simplify(transpose(Fref_vec(t))*(diff(vref_Cvec(t),diff(q1(t),t)))+transpose(Mref_ve
Q1_nc(t) =
Non-conservative generalized force associated to
Evaluating
results in
Q2_nc(t) = simplify(transpose(Fref_vec(t))*(diff(vref_Cvec(t),diff(q2(t),t)))+transpose(Mref_ve
Q2_nc(t) =
Equations of Motion in Generalized Coordinates
In general, using the Lagrangian L and the non-conservative generalized forces , the equations of motion in
the generalized coordinates are obtained from the N Lagrange's equations (of 2nd kind)
Applied to the "ball-on-beam" system with two degrees of freedom ( ), the evaluation of
results in the following two differential equations in the generalized coordinates:
deq1 = simplify(diff(diff(L(t),diff(q1(t),t)),t) - diff(L(t),q1(t))) == Q1_nc(t)
deq1 =
deq2 = simplify(diff(diff(L(t),diff(q2(t),t)),t) - diff(L(t),q2(t))) == Q2_nc(t)
deq2 =
8
Derive (Nonlinear) State Equations
Define state variables
In order to obtain a state space description of the "ball-on-beam" system from the equations of motion, the
generalized coordinates and their first-order time derivatives are introduced as state variables:
x1_def = q1(t) == x1(t) % x1 --> ball position on beam
x1_def =
x2_def = diff(q1(t),t) == x2(t) % x2 --> translational ball speed relative to beam
x2_def =
x3_def = q2(t) == x3(t) % x3 --> beam angle
x3_def =
x4_def = diff(q2(t),t) == x4(t) % x4 --> angular speed of beam
x4_def =
Define first-order time derivatives of the states
In order to solve automatically for the first time derivatives of the states, the later must also formally be
introduced as variables:
x1dot_def = x2 == x1dot % x1_dot --> x2
x1dot_def(t) =
x2dot_def = diff(q1(t),t,2) == x2dot % x2_dot --> 2nd time derivative of q_1
x2dot_def =
9
x3dot_def = x4 == x3dot % x3_dot --> x4
x3dot_def(t) =
x4dot_def = diff(q2(t),t,2) == x4dot % x4_dot --> 2nd time derivative of q_2
x4dot_def =
Define control input and disturbance input
The motor torque is defined as control input :
udef = M(t) == u(t)
udef =
The force is interpreted as disturbance input :
zdef = F(t) == z(t)
zdef =
Substitute states, control input and disturbance input in equations of motion
Note that the substitutions of the states must be applied in the correct order:
1. Substitute the acceleration variables and first.
2. Substitute the speed variables and second.
3. Substitute the position variables and last.
% Substitute state variables in equations of motion
deq1_in_xuz = deq1;
deq2_in_xuz = deq2;
% substitute q1_2dot --> x2dot
deq1_in_xuz = subs(deq1_in_xuz,lhs(x2dot_def),rhs(x2dot_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x2dot_def),rhs(x2dot_def));
% substitute q1_dot --> x2
deq1_in_xuz = subs(deq1_in_xuz,lhs(x2_def),rhs(x2_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x2_def),rhs(x2_def));
% substitute q1 --> x1
deq1_in_xuz = subs(deq1_in_xuz,lhs(x1_def),rhs(x1_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x1_def),rhs(x1_def));
% substitute q2_2dot --> x4dot
deq1_in_xuz = subs(deq1_in_xuz,lhs(x4dot_def),rhs(x4dot_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x4dot_def),rhs(x4dot_def));
% substitute q2_dot --> x4
10
deq1_in_xuz = subs(deq1_in_xuz,lhs(x4_def),rhs(x4_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x4_def),rhs(x4_def));
% substitute q2 --> x3
deq1_in_xuz = subs(deq1_in_xuz,lhs(x3_def),rhs(x3_def));
deq2_in_xuz = subs(deq2_in_xuz,lhs(x3_def),rhs(x3_def));
% Substitute control input
deq1_in_xuz = subs(deq1_in_xuz,lhs(udef),rhs(udef));
deq2_in_xuz = subs(deq2_in_xuz,lhs(udef),rhs(udef));
% Substitute disturbance input
deq1_in_xuz = subs(deq1_in_xuz,lhs(zdef),rhs(zdef));
deq2_in_xuz = subs(deq2_in_xuz,lhs(zdef),rhs(zdef));
Final equations of motions after substitutions:
deq1_in_xuz
deq1_in_xuz =
deq2_in_xuz
deq2_in_xuz =
Solve for time derivatives of the states and determine state differential equation
These two resulting equations of motion and the two trivial state differential equations
together form a system of four equations. Solving this system of equations for the time derivatives
and results in the vector function which defines the right-hand side of the state differential equation
:
% solve system of equations for first-order time derivatives of the states
aux = solve([x1dot_def,deq1_in_xuz,x3dot_def,deq2_in_xuz],[x1dot,x2dot,x3dot,x4dot]);
% combine solutions to the vector function f(x,u,z)
f = simplify([aux.x1dot;aux.x2dot;aux.x3dot;aux.x4dot])
f =
11
Define measured signals as output variables
The ball position on the beam and the beam angle are measured such that these two signals are
specified as outputs and of the system. Since the states and are defined according to
the output variables can easily be expressed as functions of the states:
With the vector function
h = [x1(t);x3(t)]
h =
the output equation can alternatively be written in vector form as .
Summary: Final State Equations of the Ball-on-beam System
displayFormula('diff(x(t),t)==f')
12
displayFormula('y(t)==h')
RHS Functions without Explicit Statement of Time Dependence
syms x1_ x2_ x3_ x4_ u_ z_ % declare auxiliary variables needed for substituti
f_wo_t = subs(f,[x1(t),x2(t),x3(t),x4(t),u(t),z(t)],[x1_,x2_,x3_,x4_,u_,z_])
f_wo_t =
h_wo_t = subs(h,[x1(t),x2(t),x3(t),x4(t),u(t),z(t)],[x1_,x2_,x3_,x4_,u_,z_])
h_wo_t =
Generate Simulink Function Blocks for Nonlinear Plant Model
13
% open_system('ball_on_beam_nonlinear_model_reference')
% matlabFunctionBlock('ball_on_beam_nonlinear_model_reference/actual_model_x_ball',f_wo_t,'vars
%
% matlabFunctionBlock('ball_on_beam_nonlinear_model_reference/actual_model_beta_beam',h_wo_t,..
% 'vars',{'x1_','x2_','x3_','x4_','u_','z_','r_ball','J_ball','m_ball','g',
Define Parameters for Simulink Simulation
r_ball = 0.0125
r_ball = 0.0125
J_ball = 4*10^-6
J_ball = 4.0000e-06
m_ball = 0.064
m_ball = 0.0640
g = 9.81
g = 9.8100
J_beam = 2.9*10^-3
J_beam = 0.0029
Linearization Around Equilibrium Point
Verify that:
(x = 0, u = 0, z = 0)
represents an equilibrium point of the system
% Define equilibrium point with time functions:
op = [x1; x2; x3; x4; u; z] == [0; 0; 0; 0; 0; 0];
% Define equilibrium point without time functions:
op_wo_t = [x1_; x2_; x3_; x4_; u_; z_] == [0; 0; 0; 0; 0; 0];
% Verify that f(x_op, u_op, z_op) = 0 holds:
f_op = simplify(subs(f, lhs(op), rhs(op)));
f_op_wo_t = simplify(subs(f, lhs(op_wo_t), rhs(op_wo_t)));
f_op
f_op =
14
Linearize the system around the equilibrium point
A = simplify( subs( jacobian(f_wo_t,[x1_,x2_,x3_,x4_]), lhs(op_wo_t),rhs(op_wo_t) ) )
A =
b_u = simplify( subs( jacobian(f_wo_t,[u_]), lhs(op_wo_t),rhs(op_wo_t) ) )
b_u =
b_z = simplify( subs( jacobian(f_wo_t,[z_]), lhs(op_wo_t),rhs(op_wo_t) ) )
b_z =
C = simplify(subs( jacobian(h_wo_t,[x1_,x2_,x3_,x4_]), lhs(op_wo_t),rhs(op_wo_t) ))
C =
Substitute Numerical Parameter Values and Convert Matrices and Vectors to
Standard Numerical Data Type
A_num=double(subs(A,[r_Ball,J_Ball,m_Ball,G,J_Beam],...
[r_ball,J_ball,m_ball,g,J_beam]))
A_num = 4×4
0 1.0000 0 0
-2.7062 0 -7.0071 0
0 0 0 1.0000
-216.4966 0 0 0
15
b_u_num=double(subs(b_u,[r_Ball,J_Ball,m_Ball,G,J_Beam],...
[r_ball,J_ball,m_ball,g,J_beam]))
b_u_num = 4×1
0
4.3103
0
344.8276
b_z_num=double(subs(b_z,[r_Ball,J_Ball,m_Ball,G,J_Beam],...
[r_ball,J_ball,m_ball,g,J_beam]))
b_z_num = 4×1
0
11.1607
0
0
C_num=double(subs(C,[r_Ball,J_Ball,m_Ball,G,J_Beam],...
[r_ball,J_ball,m_ball,g,J_beam]))
C_num = 2×4
1 0 0 0
0 0 1 0
C_num_1 = C_num(1,:)
C_num_1 = 1×4
1 0 0 0
Summary: Linearized State Equations
displayFormula('diff(Delta*x(t),t)==A_num*Delta*x(t)+b_u_num*(Delta*u(t)-0.1)+b_z_num*z(t)')
displayFormula('Delta*y(t)==C_num*Delta*x(t)')
Linearized Plant Model Analysis
1. Stability
eigen_values = eig(A_num)
eigen_values = 4×1 complex
16
-6.1335 + 0.0000i
-0.0000 + 6.3502i
-0.0000 - 6.3502i
6.1335 + 0.0000i
We can observe here that our system is not asmptotically stable, this is because the eigenvalues of
matrix A has a positive real part.
2. Controlability
Q_c = ctrb(A_num, b_u_num)
Q_c = 4×4
103 ×
0 0.0043 0 -2.4279
0.0043 0 -2.4279 0
0 0.3448 0 -0.9332
0.3448 0 -0.9332 0
d_Q_c = det(Q_c)
d_Q_c = 6.9421e+11
The system is completely controlable, because det(Qc != 0)
3. Observability
Q_o = obsv(A_num, C_num)
Q_o = 8×4
1.0000 0 0 0
0 0 1.0000 0
0 1.0000 0 0
0 0 0 1.0000
-2.7062 0 -7.0071 0
-216.4966 0 0 0
0 -2.7062 0 -7.0071
0 -216.4966 0 0
unob = length(A)-rank(Q_o)
unob = 0
The system does not have any unobservabilities. Hence, it is completely observable.
Linear State-Feedback Controller Design
p = [-70, -58, -1.6, -1.4]
p = 1×4
-70.0000 -58.0000 -1.6000 -1.4000
k_fb = place(A_num, b_u_num, p)
k_fb = 1×4
-4.3917 -5.1595 12.9411 0.4444
17
Luenberger Observer Design
p_lo = [-60,-50,-1.75,-1.45]
p_lo = 1×4
-60.0000 -50.0000 -1.7500 -1.4500
k_lo = place(A_num', C_num_1', p_lo)
k_lo = 1×4
103 ×
0.1132 3.3518 -1.4099 -1.3029
l_T = transpose(k_lo)
l_T = 4×1
103 ×
0.1132
3.3518
-1.4099
-1.3029
Luenberger disturbance observer
z1 = -9+12j
z1 = -9.0000 + 12.0000i
z2 = -9-12j
z2 = -9.0000 - 12.0000i
z3 = -12+6j
z3 = -12.0000 + 6.0000i
z4 = -12-6j
z4 = -12.0000 - 6.0000i
p_ldo = [-100 z1 z2 z3 z4]
p_ldo = 1×5 complex
102 ×
-1.0000 + 0.0000i -0.0900 + 0.1200i -0.0900 - 0.1200i -0.1200 + 0.0600i
A_e = zeros(5,5)
A_e = 5×5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
A_e(1:4, 1:4) = A_num
A_e = 5×5
18
0 1.0000 0 0 0
-2.7062 0 -7.0071 0 0
0 0 0 1.0000 0
-216.4966 0 0 0 0
0 0 0 0 0
A_e(1:4, 5) = b_u_num
A_e = 5×5
0 1.0000 0 0 0
-2.7062 0 -7.0071 0 4.3103
0 0 0 1.0000 0
-216.4966 0 0 0 344.8276
0 0 0 0 0
B_e = zeros(5,1)
B_e = 5×1
0
0
0
0
0
B_e(1:4,1) = b_u_num
B_e = 5×1
0
4.3103
0
344.8276
0
C_e=zeros(1,5)
C_e = 1×5
0 0 0 0 0
C_e(1,1:4)=C_num_1
C_e = 1×5
1 0 0 0 0
l_e = place(A_e', C_e', p_ldo)
l_e = 1×5
105 ×
0.0014 0.0503 -0.1421 -1.2930 -0.0168
Conclusion:
• Successful control of the model was achieved.
• Luenberger Observer has been used to estimate the states and implement a state-feedback
Controller.
• Later on the Observer was extended to observe the continuous disturbance on the input and
reduced the estimation errors.
19
• Saturation Block was used as an anti-windup measure in order to avoid negative impacts when
the requested control signal exceeds its feasible range resulting from the restricted motor power
of the laboratory system.
References
1. Kamman, James, "An Introduction to Three-Dimensional Rigid Body Dynamics", ebook, https://kamman-
dynamics-control.org/3d-dynamics-ebook/ (accessed November 6, 2023)
20