# Lagrange Equations Using MATLAB

The motion of a building subjected to earthquake excitation is to
be studied using the model shown above. Assume the
foundation mass, m, to be a lumped mass.
Goals: Use Matlab!imulink to "i# derive the equations of
motion for the s\$stem given above using %agrange &quations,
and "ii# to simulate the nonlinear and lineari'ed equations of
motion for the s\$stem and compare results.
1. Kinetic and Potential Energy
Equations
The equation for the kinetic energ\$ is(
( )
2 2 2 2
1 1 1
2 2 2
G G G
T mz M x y I θ · + + +
)
) ) ) (1)
where
cos
G
x z aθ θ · +
)
) ) , sin
G
y aθ θ · −
)
)
This is programmed in MAT%A* as(
syms m M g L cvv cth k K IG a zg zgdot zgdotdot z zdot
zdotdot v vdot vdotdot th thdot thdotdot real
syms x1 x2 x3 x4 u real
% Kinetic nergy
xgdot ! zdot"a#cos\$th%#thdot&
ygdot ! 'a#sin\$th%#thdot&
( ! )*+#m#zdot,2 " )*+#M#\$xgdot,2"ygdot,2%
" )*+#IG#thdot,2&
The equation for the potential energ\$ is(
( )
2
2
1 1
cos
2 2
g
V k z z K Mga θ θ · − + + (2)
which is programmed in MAT%A* as(
% -otential nergy
. ! )*+#k#\$z'zg%,2 " )*+#K#th,2 " M#g#a#cos\$th%&
2. Nonconservative Forces
The non+conservative forces are found as(
( ) ( )
NC v g
W c z z z c
θ
δ δ θ δθ · − − −
)
) ) (3)
,
c
v
/2 c
v
/2
k/2 k/2
m
G
M, I
G
-
g
x
y
a
z
g
z
K
c
-
L
and coded into MAT%A* as(
% /onconservative 0orces
-z ! 'cvv#\$zdot'zgdot%&
-th ! 'cth#thdot&
3. Lagrange’s Equations
The next step is to perform the appropriate calculations to .nd
the %agrange &quations, which are given b\$(
z
d T T V
P
dt z z z
∂ ∂ ∂ ¸ _
− + ·
÷
∂ ∂ ∂
¸ ,
)
(4)
and
d T T V
P
dt
θ
θ θ θ
∂ ∂ ∂ ¸ _
− + ·
÷
∂ ∂ ∂
¸ ,
)
(5)
The chain rule must be used to evaluate the .rst integral in
%agrange/s &quations using Matlab, i.e.(
1
N
j j
j i j i j i
dq dq
d T T T
dt q q q dt q q dt
·
¹ ¹
¸ _ ¸ _ ¸ _ ∂ ∂ ∂ ∂ ∂ ¹ ¹
· +
' ; ÷ ÷ ÷
∂ ∂ ∂ ∂ ∂
¹ ¹ ¸ , ¸ , ¸ ,
¹ ¹

)
) ) ) )
(6)
As an example,
d T
dt z
∂ ¸ _
÷

¸ ,
)
is written as(
d T T dz T dz T d T d
dt z z z dt z z dt z dt z dt
θ θ
θ θ
∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂
· + + +
∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂
¸ _ ¸ _ ¸ _ ¸ _ ¸ _
÷ ÷ ÷ ÷ ÷
¸ , ¸ , ¸ , ¸ , ¸ ,
)
)
)
) ) ) ) ) )
&ach term in &q. "0# can be coded in MAT%A* as follows(
T
z

∂)
1(1zdot ! di22\$(3zdot%&
d T
dt z
∂ ¸ _
÷

¸ ,
)
ddt1(1zdot ! di22\$1(1zdot3z%#zdot " ***
di22\$1(1zdot3zdot%#zdotdot " ***
di22\$1(1zdot3th%#thdot " ***
di22\$1(1zdot3thdot%#thdotdot&
T
z

1(1z ! di22\$(3z%&
V
z

1.1z ! di22\$.3z%&
&ach term in &q. "1# is coded, using MAT%A*, as shown below(
T
θ

)
1(1thdot ! di22\$(3thdot%&
d T
dt θ
∂ ¸ _
÷

¸ ,
)
ddt1(1thdot ! di22\$1(1thdot3z%#zdot " ***
di22\$1(1thdot3zdot%#zdotdot " ***
di22\$1(1thdot3th%#thdot " ***
di22\$1(1thdot3thdot%#thdotdot&
T
θ

1(1th ! di22\$(3th%&
V
θ

1.1th ! di22\$.3th%&
2ombining these terms together \$ields the two equations of
motion(
% (he e4uation o2 motion 2or 5z5 \$e4z ! )%
e4z ! sim1le\$ddt1(1zdot ' 1(1z " 1.1z ' -z%&
% (he e4uation o2 motion 2or 5th5 \$e4th ! )%
e4th ! sim1le\$ddt1(1thdot ' 1(1th " 1.1th ' -th%&
3
4. Equations of Motion in elative
!oordinates
The equations of motion are written with respect to the absolute
coordinates. To get the equations of motion in relative
coordinates, we substitute
g
z v z · + into the &4Ms. The result
will give us the appropriate equations from which to derive the
state space representation of this s\$stem. The code to achieve
all of this is(
% 6e7rite the 8M using relative coordinates \$i*e*3
% v ! z ' zg%
z ! v " zg&
zdot ! vdot " zgdot&
zdotdot ! vdotdot " zgdotdot&
%(he e4uation o2 motion 2or 9v: \$4v1!)%
4v1 ! sim1le\$su;s\$e4z%%&
%(he e4uation o2 motion 2or 9th: \$4th1!)%
4th1 ! sim1le\$su;s\$e4th%%&
21rint2\$<=n%s=n<3<(he 8M in relative coordinates*<%
1retty\$4v1%
1retty\$4th1%
5or future reference, let us denote the equations found above
as(
v
Eq 6 4v1 and Eq
θ
6 4th1
". #tate #\$ace For%ulation
To simulate the s\$stem response, the &4M must be put into
state space form. 7f we de.ne our state vector as(
{ } { }
'
'
1 2 3 4
v v x x x x θ θ · · x
)
)
then the nonlinear state space representation as follows(
( )
( )
( )
( )
3 1
1 2 3 4 2
4 3
1 2 3 4 4
, , , ,
, , , ,
, , , ,
, , , ,
v g
v
g
v
x x v
f v v z
f x x x x u x v
d
x x dt
f x x x x u x
f v v z θ
θ
θ θ
θ θ
θ
θ θ
1
1 ¹ ¹ ¹ ¹
1
1 ¹ ¹ ¹ ¹
1
¹ ¹ ¹ ¹
1
· · · ·
' ; ' ; 1
1
¹ ¹ ¹ ¹ 1
1
¹ ¹ ¹ ¹ 1
¹ ¹ ¹ ¹ ¸ ]
¸ ]
x
)
) )
)
) ))
) ))
) )
)
))
) )
) ))
(7)
To .nd ( )
, , , ,
v g
f v v z θ θ
)
) )) and ( )
, , , ,
g
f v v z
θ
θ θ
)
) )) , the two equations,
0
v
Eq · and 0 Eq
θ
· , are solved for v))and
θ
))
. 7n MAT%A*, this is
achieved b\$(
% >olve 2or vdotdot and thdotdot 2or the state s1ace
2ormulation*
>ol ! solve\$4v134th13<vdotdot3thdotdot<%&
>ol*vdotdot ! sim1le\$>ol*vdotdot%&
>ol*thdotdot ! sim1le\$>ol*thdotdot%&
5inall\$, we substitute into these solutions the names of the state
"x,, x3, x8, and x0# and the input " u #.
2v!su;s\$>ol*vdotdot3?v3vdot3th3thdot3zgdotdot@3
?x13x23x33x43u@%
2th!su;s\$>ol*thdotdot3? v3vdot3th3thdot3zgdotdot@3
?x13x23x33x43u@%
&. Lineari'ed #tate #\$ace
e\$resentation
The lineari'ed state space representation is given b\$(
u · + x Ax B ) ()
!tart from the nonlinear equations found in !ection 1, 0
v
f · and
0 f
θ
· . A Ta\$lor !eries approximation will be used to lineari'e
the equations of motion.
8
Taylor Series Approximation
( ) ( ) ( )
j j
u u
m
j j
i
j j
x x
n
j j
i
m n i
u u
u
f
x x
x
f
u u x x f
j j j j

+ −

·
·
·
·
·
∑ ∑
1 1
1 1
, , , , ,  
The s\$stem must be lineari'ed with respect to an operating
point, t\$picall\$ a state of either stable or unstable equilibrium. 7n
this case, we will lineari'e the s\$stem about its rest position.
[ ] [ ] 0 , 0 , 0 , 0 , 0 , , , ,
4 3 2 1
· u x x x x
Appl\$ing a Ta\$lor !eries approximation, \$ou will .nd that the
state+space can be written in a form as below.
u
u
f
u
f
x
x
x
x
x
f
x
f
x
f
x
f
x
f
x
f
x
f
x
f
x
x
x
x
v
v v v v
1
1
1
1
1
]
1

¸

+
1
1
1
1
]
1

¸

1
1
1
1
1
1
]
1

¸

·
1
1
1
1
]
1

¸

θ θ θ θ θ
0
0
1 0 0 0
0 0 1 0
4
3
2
1
4 3 2 1
4 3 2 1
4
3
2
1

&ach terms can be calculated as follows
12v1x1!su;s\$di22\$2v3x1%3?x13x23x33x43u@3?)3)3)3)3)@%
12v1x2!su;s\$di22\$2v3x2%3?x13x23x33x43u@3?)3)3)3)3)@%
12v1x3!su;s\$di22\$2v3x3%3?x13x23x33x43u@3?)3)3)3)3)@%
12v1x4!su;s\$di22\$2v3x4%3?x13x23x33x43u@3?)3)3)3)3)@%
12v1u!su;s\$di22\$2v3u%3?x13x23x33x43u@3?)3)3)3)3)@%
12th1x1!su;s\$di22\$2th3x1%3?x13x23x33x43u@3?)3)3)3)3)@%
12th1x2!su;s\$di22\$2th3x2%3?x13x23x33x43u@3?)3)3)3)3)@%
12th1x3!su;s\$di22\$2th3x3%3?x13x23x33x43u@3?)3)3)3)3)@%
12th1x4!su;s\$di22\$2th3x4%3?x13x23x33x43u@3?)3)3)3)3)@%
12th1u!su;s\$di22\$2th3u%3?x13x23x33x43u@3?)3)3)3)3)@%
The s\$stem is now in a linear state+space form, and can be used