Professional Documents
Culture Documents
N Link Pendulum
N Link Pendulum
For the single pendulum case from the previous post we derived the equations of motion
manually, but for the n-link case it is much easier to derive them programmatically. Ill show one
way to do this step by step, using a Lagrangian mechanics formulation. If you prefer, you could
easily skip ahead from here to the control theory portion of the post.
The n + 1 generalized coordinates describing the n-link pendulum depicted above are the cart
position x(t) and the angles of each arm i(t):
The n + 1 generalized velocities are the time derivatives of the coordinates:
Together these comprise the dynamical variables of the n-link pendulum. For the double (2link) pendulum, for example, the dynamical variables are:
The n + 1 generalized external forces are just the control force f(t) acting on the cart and zero
forces acting on each of the other n generalized coordinates:
For example, the generalized external forces acting on the triple pendulum are:
Now we need to compute the Lagrangian of the n-link pendulum. It helps to define a couple of
other properties of the system first, starting with the location of the hinge of each of the n arms.
The hinge of the first arm is at the horizontal position of the cart, with a height of zero:
The location of the hinge of each other arm depends on the location of the previous hinge in the
following simple way:
For example, the hinge of the second arm is at the end of the first arm:
We assume the arms have uniform linear density, so the center of mass of the ith arm is halfway
along it:
The kinetic energy of the ith arm is 1/2 mi v.v, where v is the velocity of its center of mass:
In our dimensionless units in which the gravitational acceleration g = 1, the potential energy of
the ith arm is its mass multiplied by the height of its center of mass:
(The 2 gives the second component of armcenter[i], which is the height.)
The Lagrangian of the cart is just its kinetic energy, cx(t)22, so the overall Lagrangian of the nlink pendulum is:
Now we are ready to apply the EulerLagrange equations and get the equations of motion:
So far, so good. For n = 1 we get the same equations of motion as we used in the first post in this
series:
The equations become much longer for larger n, but we will happily use them without ever
looking at them.
(If you do want to see the equations of motion for, say, a quadruple pendulum printed out in full,
just download the Computable Document Format (CDF) file for this post and evaluate
equations[4]. The output is about five pages long on-screen, which is exactly why you dont want
to derive these things by hand.)
We used symbolic math and programming to set up the equations of motion. Now we can
immediately use Mathematicas numerics and control systems features to simulate and stabilize
them. This is whats great about having all these areas integrated into one system!
Well investigate the concrete case c = mi = di = 1:
(The mi_ means mi for all values of i.)
Its handy to define a function initial[n] that generates some slightly non-upright initial
conditions at t = 0 for an n-link pendulum. For example, here are slightly non-upright initial
conditions for a single pendulum:
Note that the pendulum is exactly upright when each i = 2 1.5708. (Download the CDF file
to see the definition of initial[n], or to substitute your own.)
Well start by simulating a double pendulum with no control force, f(t) = 0, corresponding to
making no attempt to balance the pendulum:
Using this function, heres a triple pendulum falling over, with no attempt to control it:
It gets messy fast, but well be able to steady it!
First, we need a state space model of the n-link pendulum, linearized around the equilibrium
point. The function StateSpaceModel automatically generates this for us from our nonlinear
equations of motion:
The function equilibrium[n] just gives the unstable (upright) equilibrium point for an n-link
pendulum: i = 2; everything else = 0. For example, for a double pendulum:
As we discussed in the previous post, each column in this augmented matrix describes the
immediate effect of one of the dynamical variables (or the control force) on each of the other
dynamical variables.
We can now compute a linear control force for this system. For a double pendulum, for example,
the linear control force will have the form
The coefficients in the quadratic cost function may represent actual costs of deviations of the
system from the desired equilibrium state, or they may be chosen according to other criteria,
such as robustness against certain types of perturbations. The cost coefficients Ive used here are
a guess, based on which dynamical variables I suspect are the most important for keeping the
pendulum from falling over.
Here is a generalization of my guessed cost coefficients to an n-link pendulum:
(Download the CDF file to see the definitions of h1(t) and h2(t), or to substitute your own.)
Heres the bumped double pendulum:
Its also interesting to directly plot the generalized coordinates x(t), 1(t), and 2(t), and compare
with the forces h1(t) and h2(t) and the control force f(t):
With sufficiently large perturbations, linear control systems such as the ones we have been
exploring will fail to recover (and will spiral out of control). Moreover, for n-link pendulums,
larger values of n require smaller angular perturbations to knock them over, and in real life there
are always perturbations (sound, heat, wind ). In practice its hard to build physical
demonstrations of n-link pendulums with more than about three or four links.
Nevertheless, its very interesting to explore these idealized systems. There are lots of things you
can try with the downloadable CDF file for this post. For example, with different choices for the
cost function, you can make linear control systems for inverted n-link pendulums that can
recover from different (including larger) initial perturbations. Or, with different lengths and
masses of the pendulum arms (and no control force), you can explore the dynamics of n-link
pendulums, which are very interesting by themselves. Try it out!
In the next post in this series, well look at some more generalizations and variations on this
problem, including inverted pendulums in 3D.
Download the CDF file
Posted in: Other Application Areas
Newer
Innovating Interactive Web Publishing with Wolfram Demonstrations
Older
Optimizing Financial Modeling with Mathematica
RELATED POSTS
Wolfram Community Featured Posts: Reddit's 60-Second Button, Raspberry Pi, and More
July 21, 2015
Wolfram Language Artificial Intelligence: The Image Identification Project
May 13, 2015
Instant Apps for the Apple Watch with the Wolfram Language
April 28, 2015
10 Comments
Whod have thought you could use the EL equations and M to teach you breakdance?
Great post, Andrew, again!
Posted by Mooniac March 1, 2011 at 1:56 pm
Reply
Hi Nicolas, We could have alternatively derived the equations by adding the overall
translational kinetic energy to the rotational kinetic energy. We would end up with the same
equations. Including friction is something we will try in the next post in this series!
Posted by Andrew Moylan March 1, 2011 at 4:42 pm
Reply
Hi Andrew,
yes the equations are right, but the hypothesis that all the arm mass is located in its middle,
and has no rotational inertia is just unrealistic. You may change the drawing to make it fit your
hypothesis. This means to not display arms by solid beams but rather by thin lines with a
(disk) mass in the middle. In this case the animation will be realistic, nevertheless the real
experiment would appear less realistic unless you use carbon fiber rods and lead masses
located in their middle.
If you are not convinced, take a ruler by its middle between two fingers, move it to any
direction and you can fill its translational inertia. If the ruler would have all its mass in the
middle, you would fill exactly the same, right ? Now use your finger to give a torque to the
ruler, you feel its rotational inertia, this important dynamic effect is missing when you assume
that all the mass is located in the middle point.
If you would like to add rational inertial effect, you may create a lumped approximation by a
repartition of fraction of masses along each arm (at least two), and use the same equations, or
formally include rotational inertia of the arms in the Lagrangian.
Posted by Nicolas March 2, 2011 at 5:06 am
Reply
Hi Nicolas, youre right! Thats more realistic, especially considering my diagrams. To make
this change we have to add the following term to the definition of ke[i_]:
+ (Subscript[m, i] Subscript[d, i]^2)/24 Subscript[\[Theta], i][t]^2
I will update the downloadable notebook for this post to include this option. Thanks!
Posted by Andrew Moylan March 3, 2011 at 3:28 am
Reply
Fantastic post! I really liked that you were able to showcase the symbolic power of
Mathematica along with the new features of v8. Great job.
Posted by Nicholas M August 29, 2011 at 1:03 am
Reply
This blog post was phenomenal! Thank you so much. It made me see how powerful
Mathematica can be.
Posted by Roy December 9, 2014 at 7:32 pm
Reply
plist = Join[{p},
Table[p +=
Evaluate[{Sin[
Subscript[\[Theta], i][t]], -Cos[
Subscript[\[Theta], i][t]]}], {i, 1, 10}]];
Show[Graphics[{Blue, Thick, Line[linex], Blue, Thick, Line[liney],
Red, Line[plist], Black, Map[Disk[#, 0.15] &, plist]}],
PlotRange -> 10*1 {{-1, 1}, {-1, 1}},
AspectRatio -> Automatic], {t, 0.0, 25.0, 0.03}];
ListAnimate[fr]
Now ,I use NDSolveValue , it not work , Why ? Where my mistake :
Clear[initial]
initial[n_] :=
Flatten[{x[0] == x[0] == 0,
Table[{Subscript[\[Theta], i][0] == N[ \[Pi]/2 1/35 (Sqrt[2])^i],
Subscript[\[Theta], i][0] == RandomReal[{-0.1, 0.1}]}, {i, n}]}]
k = 10;
linex = {{-10, 5}, {10, 5}};
liney = {{0, 5}, {0, -15}};
Join[equations[3], initial[3]] /. parameters /. f[t] -> Sin[2 t]
{x, Subscript[\[Theta], 1], Subscript[\[Theta], 2], \
Subscript[\[Theta], 3], Subscript[\[Theta], 4], Subscript[\[Theta], \
5], Subscript[\[Theta], 6], Subscript[\[Theta], 7], \
Subscript[\[Theta], 8], Subscript[\[Theta], 9], Subscript[\[Theta], \
{x, Subscript[\[Theta], 1], Subscript[\[Theta], 2], \
Subscript[\[Theta], 3], Subscript[\[Theta], 4], Subscript[\[Theta], \
5], Subscript[\[Theta], 6], Subscript[\[Theta], 7], \
Subscript[\[Theta], 8], Subscript[\[Theta], 9], Subscript[\[Theta], \
{x, Subscript[\[Theta], 1], Subscript[\[Theta], 2], \
Subscript[\[Theta], 3], Subscript[\[Theta], 4], Subscript[\[Theta], \
5], Subscript[\[Theta], 6], Subscript[\[Theta], 7], \
Subscript[\[Theta], 8], Subscript[\[Theta], 9], Subscript[\[Theta], \
{x,Subscript[\[Theta], 1],Subscript[\[Theta], 2],Subscript[\[Theta], 3],Subscript[\[Theta],
4],Subscript[\[Theta], 5],Subscript[\[Theta], 6],Subscript[\[Theta], 7],Subscript[\[Theta],
8],Subscript[\[Theta], 9],Subscript[\[Theta], 10]} =
NDSolveValue[
Join[equations[k], initial[k]] /. parameters /. f[t] -> Sin[2 t],
coordinates[k], {t, 0, 25},
Method -> {EquationSimplification -> Residual}];
fr = Table[p = {x[t], 5};
plist = Join[{p},
Table[p +=
Evaluate[{Sin[
Subscript[\[Theta], i][t]], -Cos[
Subscript[\[Theta], i][t]]}], {i, 1, 10}]];
Show[Graphics[{Blue, Thick, Line[linex], Blue, Thick, Line[liney],
Red, Line[plist], Black, Map[Disk[#, 0.15] &, plist]}],
PlotRange -> 10*1 {{-1, 1}, {-1, 1}},
AspectRatio -> Automatic], {t, 0.0, 25.0, 0.03}];
ListAnimate[fr]
Please , help me . Thank you very much
Posted by Nguyen Tan Ly May 4, 2015 at 11:28 pm
Reply