myphysicslab – pendulum and cart
in this simulation there is a cart moving along a horizontal track. from the cart a pendulum is suspended. a spring connects the cart to a wall. friction acts on the cart and on the pendulum.
you can change parameters such as gravity, mass, pendulum length, spring stiffness, and friction (damping). you can drag the cart or pendulum with your mouse to change the starting position. click the "reset" button to start from a resting position. if you don't see the simulation try instructions for enabling java. scroll down to see the math!
there are two ways to find the equations of motion for a system like this: the direct newtonian way or the indirect lagrangian way. we show both methods here.
kinematics
for both direct and indirect methods, we first need to get the kinematics right. kinematics means the relations of the parts of the device, without regard to forces. in kinematics we are only trying to find expressions for the position, velocity, & acceleration in terms of whatever variables we have chosen. the variables here are:
 x = position of cart (0 = spring unstretched)
 θ = angle of pendulum (0 = pendulum hanging straight down)
 r = length of rod (constant)
we'll need the standard unit vectors,
i, j, k. we use bold and overline to indicate a vector.
 i = (1,0,0) unit vector in horizontal direction
 j = (0,1,0) unit vector in vertical direction
 k = (0,0,1) unit vector in z direction (out of the page)
the kinematics for the cart are simple
position = x i
velocity = x' i
acceleration = x'' i
the kinematics for the pendulum are more complex. the position is derived by a fairly simple application of trigonometry. the velocity and acceleration are then the first and second derivatives of the position.
position = x i + r sin θ i − r cos θ j
velocity = x' i + r θ' cos θ i + r θ' sin θ j
acceleration = x'' i + r θ'' cos θ i −
r θ' ^{2} sin θ i +
r θ'' sin θ j +
r θ' ^{2} cos θ j
forces
we treat the cart and the pendulum bob as point particles. begin by drawing the free body diagram for the cart and writing expressions for the forces acting on them. define the following parameters and forces:
 n = normal force (from the track the cart is on)
 t = tension in the rod
 m = mass of cart
 m = mass of pendulum
 g = gravitational constant
 d = cart friction constant
 b = pendulum friction constant
 k = spring stiffness
cart forces
pendulum forces
just by looking at the freebody diagram we can fairly easily figure out some of the forces on the cart. (the pendulum friction force is trickier, it's explained in the next section).
 spring force = −k x i
 normal force = n j
 gravity = −m g j
 tension in the rod = −t cos θ j + t sin θ i
 cart friction = −d x' i
 pendulum friction = ^{b}⁄_{r} θ' (cos θ i + sin θ j)
the forces on the pendulum bob are:
 tension in the rod = t cos θ j − t sin θ i
 gravity = −m g j
 pendulum friction = −^{b}⁄_{r} θ' (cos θ i + sin θ j)
torque force from pendulum friction
torque force of friction
we consider the torque from friction of the pendulum to be a vector perpendicular to the plane where the pendulum and cart move. that is, the torque is in the direction of the unit vector k which extends out of the plane. the magnitude of the torque is given by −b θ' where θ' is the angular velocity. we can regard the force from the torque as being equivalent to a force applied at the end of the pendulum rod (see illustration). from newton's third law (every action has an equal and opposite reaction) we know that the same force is applied at the other end of the rod, but in opposite direction.
a torque that is the result of a force f applied at the end of the rod r is given by the cross product
r × f = (r_{x}, r_{y}, 0) × (f_{x}, f_{y}, 0) =
(r_{x} f_{y} − r_{y} f_{x}) k
here we are showing a 3space vector as (x, y, z) which is the same as x i + y j + z k. we regard the force f as being perpendicular to the rod, so that
f = f (−r_{y}, r_{x}, 0) ⁄ r
where f is the unknown magnitude of f and r is the length of r so that r = √(r_{x}^{2} + r_{y}^{2}). the direction of f is chosen so that positive torque leads to increase in θ. then the cross product becomes
r × f = (r_{x} f_{y} − r_{y} f_{x}) k =
f (r_{x}^{2} + r_{y}^{2}) ⁄ r k =
f r k
now we can find f:
−b θ' k = r × f = f r k
f = ^{−b}⁄_{r} θ'
this result makes sense: consider that to produce a given amount of torque, you would need a stronger force applied at the end of a short rod, or a weaker force with a longer rod. the force is then given by
f = f (−r_{y}, r_{x}, 0) ⁄ r =
^{−b}⁄_{r} θ' (cos θ i + sin θ j)
note that r = r(sin θ, −cos θ, 0) which justifies part of the above equation. this torque force f is applied at the pendulum bob, and its opposite is applied at the cart.
direct method for finding equations of motion
we add up the forces described above to find the net force on the cart
f = n j − m g j − t cos θ j + t sin θ i −
k x i −
d x' i +
^{b}⁄_{r} θ' (cos θ i + sin θ j)
now using newton's law
f = m a and the cart acceleration we found earlier, we have

n j − m g j − t cos θ j + t sin θ i − k x i +
^{b}⁄_{r} θ' (cos θ i + sin θ j) =
m x'' i
 (1) 
adding the forces on the pendulum gives
f = t cos θ j − t sin θ i − m g j −
^{b}⁄_{r} θ' (cos θ i + sin θ j)
using newton's law
f = m a and the pendulum acceleration we found earlier, we have

t cos θ j − t sin θ i − m g j −
^{b}⁄_{r} θ' (cos θ i + sin θ j) =
m(x'' i + r θ'' cos θ i −
r θ' ^{2} sin θ i +
r θ'' sin θ j +
r θ' ^{2} cos θ j)
 (2) 
now we can write the vector components of the above equations (1) and (2) as separate equations. this gives us 4 simultaneous equations.

t sin θ − k x − d x' + ^{b}⁄_{r} θ' cos θ = m x''
 (3) 
n − m g − t cos θ + ^{b}⁄_{r} θ' sin θ = 0

− t sin θ − ^{b}⁄_{r} θ' cos θ =
m(x'' + r θ'' cos θ −
r θ' ^{2} sin θ)
 (4) 

t cos θ − m g − ^{b}⁄_{r} θ' sin θ =
m(r θ'' sin θ +
r θ' ^{2} cos θ)
 (5) 
(we ignore the second of the four equations, but we could use it to find the unknown normal force
n on the cart). next do some algebraic manipulations to eliminate the unknown tension
t. adding together equations (3) and (4) and rearranging we get:
(m + m)x'' = m r θ' ^{2} sin θ − m r θ'' cos θ − k x −
d x'
 (6) 
multiply equation (5) by
sin θ
t sin θ cos θ − m g sin θ − ^{b}⁄_{r} θ' sin^{2} θ =
m r θ'' sin^{2} θ +
m r θ' ^{2}sin θ cos θ
use equation (4) to replace
t sin θ.
−^{b}⁄_{r} θ' cos^{2} θ − m x'' cos θ − m r θ'' cos^{2}θ +
m r θ' ^{2}sin θ cos θ −
m g sin θ −
^{b}⁄_{r} θ' sin^{2} θ =
m r θ'' sin^{2}θ +
m r θ' ^{2}sin θ cos θ
using the trig identity
cos^{2}θ + sin^{2}θ = 1 this simplifies to
^{b}⁄_{r} θ' + m x'' cos θ + m r θ'' + m g sin θ = 0
 (7) 
equations (6) and (7) are the equations of motion. we have two equations in the two variables
x,θ and their derivatives. we will need to do some further manipulations of these two equations to get them into a form suitable for the rungekutta numerical analysis method (see below).
indirect (energy) method for finding equations of motion
the indirect method is based on the energy of the system. (a good textbook that covers this is
fundamentals of applied dynamics by james h. williams jr. you will find the same "mass and plane pendulum dynamic system" discussed on page 234 of the 1996 edition.) our first step is to find the lagrangian of the system which is the kinetic energy minus the potential energy.
l = t − v
 l = the lagrangian
 t = kinetic energy
 v = potential energy
for the kinetic energy we need the velocity of the cart and pendulum, which we calculated earlier
(see
kinematics above). kinetic energy is given by
^{1}⁄_{2}m v^{2}. note that we use the vector dot product to square a vector,
v^{2} = v · v. so we have
t = ^{1}⁄_{2} m (x' i)^{2} +
^{1}⁄_{2} m (x' i + r θ' cos θ i + r θ' sin θ j)^{2}
applying the dot product rule to square the vectors we get
t = ^{1}⁄_{2} m x' ^{2} +
^{1}⁄_{2} m ((x' + r θ' cos θ)^{2} + (r θ' sin θ)^{2})
there are two stores of potential energy in this system: the spring and the gravitational potential of the pendulum.
v_{spring} = ^{1}⁄_{2} k x^{2}
v_{gravity} = m g height = m g(r − r cos θ)
so the lagrangian for the system is
l = ^{m}⁄_{2} x' ^{2} +
^{m}⁄_{2} (x' + r θ' cos θ)^{2} +
^{m}⁄_{2} (r θ' sin θ)^{2} −
^{k}⁄_{2} x^{2} −
m g r(1 − cos θ)
generalized forces: friction
to deal with the friction forces we calculate the amount of nonconservative work done by tiny movements in the variables. note that we consider only nonconservative forces like friction here  conservative forces like gravity or springs are handled by the potential energy calculations in the lagrangian.
a tiny change of
δx in
x results in an amount of nonconservative work from friction given by
δw = −d x' δx so we define the "generalized force"
w_{x} = −d x'
similarly, a tiny change of
δθ in
θ results in an amount of nonconservative work from friction
δw = −b θ' δθ, so we define another generalized force:
w_{q} = −b θ'
equations of motion from lagrangian
next we will derive the equations of motion from lagrange's equation. for the
x variable lagrange's equation is

w_{x} =
 (8) 
note that
x' and
x are treated as separate variables when taking partial derivatives. here are the partial derivatives:
∂ l

= m x' + m(x' + r θ' cos θ)

∂ x'

taking the derivative with respect to time of
and subtracting
equation (8) becomes
− d x' = m x'' + m x'' + m r θ'' cos θ −
m r θ' ^{2} sin θ +
k x
which we can rearrange to be identical to equation (6) found above in the direct method:
(m + m)x'' = m r θ' ^{2} sin θ −
m r θ'' cos θ −
k x −
d x'
for the
θ variable lagrange's equation is

w_{q} =
 (9) 
here are the partial derivatives:
∂ l

= m(x' + r θ' cos θ)r cos θ + m θ' r^{2} sin^{2}θ

∂ θ'

∂ l

= −m(x' + r θ' cos θ)r θ' sin θ +
m r^{2} θ' ^{2}sin θ cos θ −
m g r sin θ

∂ θ

(notice that we can factor out
m r from each of these terms.) taking the derivative with respect to time of
and subtracting
equation (9) becomes
− b θ' = m r [(x'' + r θ'' cos θ − r θ' ^{2} sin θ)cos θ −
θ' sin θ(x' + r θ' cos θ) +
θ'' r sin^{2}θ +
2 θ' ^{2}r sin θ cos θ +
(x' + r θ' cos θ)θ' sin θ −
r θ' ^{2}sin θ cos θ +
g sin θ]
after lots of cancellation and using the identity
sin^{2}θ + cos^{2}θ = 1 this reduces to equation (7) from the direct method:
^{b}⁄_{r} θ' + m x'' cos θ + m r θ'' + m g sin θ = 0
which technique do you like better, direct or indirect? it seems that the indirect method is the choice of professionals. it requires more calculus, but the calculations are a lot easier. compare what was done here to the
earlier calculations for frictional torque in the direct method. on the other hand, the direct method is necessary for figuring out the forces within the system, like the tension in the rod, or the normal force of the track on the cart.
numerical solution
to solve the equations of motion numerically, so that we can drive the simulation, we use the
rungekutta method for solving sets of ordinary differential equations.
to use the rungekutta method, we need to get equations (6) and (7) into the following form:
x'' = f(x, x', θ, θ')
θ'' = g(x, x', θ, θ')
where
f, g are functions to be determined. so we want to solve for each second derivative in terms of lower derivative variables. if you look back at equations (6) and (7), you can see that they need some manipulation to isolate the second derivatives over to the left hand side. after solving for
θ'' and
x'' (either by hand or with an algebra program like mathematica) we get:
x'' =

m r θ' ^{2} sin θ + m g sin θ cos θ − k x − d x' + ^{b}⁄_{r} θ' cos θ

m + m sin^{2}θ

θ'' =

−m r θ' ^{2} sin θ cos θ − (m+m)g sin θ + k x cos θ + d x' cos θ − (1 + ^{m}⁄_{m})^{b}⁄_{r} θ'

r (m + m sin^{2}θ)

if we define a couple of new variables, we can change this into four firstorder equations (that is, four equations with only first derivatives).
 v = velocity of cart
 ω = angular velocity of pendulum
x' = v
θ' = ω
v' =

m r ω^{2} sin θ + m g sin θ cos θ − k x − d v + ^{b}⁄_{r} ω cos θ

m + m sin^{2}θ

ω' =

−m r ω^{2} sin θ cos θ − (m+m)g sin θ + k x cos θ + d v cos θ − (1 + ^{m}⁄_{m})^{b}⁄_{r} ω

r (m + m sin^{2}θ)

this is now exactly the form needed to plug in to the rungekutta method for numerical solution of the system.