Seth on Simulink

February 26th, 2009

Modeling Mechanical Systems: The Double Pendulum

Do you ever have to model mechanical systems?  Mechanical systems consist of bodies, joints, and force elements like springs.  In this post, I will show you how to model a double pendulum with base Simulink and using SimMechanics.

Pendulum: Equations of Motion

Most of the models I work with are representations of data flow and algorithms.  If you want a model of a mechanical system, you need the equations of motion so you can build the system from base Simulink blocks. Of course, if you don’t know the equations for a pendulum, you must derive them.

Pendulum equations

If you start with that equation, and follow the process described in a previous post about how to draw ODEs, the model of a pendulum looks like this:

Simulink model of a single pendulum

This model is a graphical representation of mathematical operations and algorithm elements.  Simulink solves the differential equation by evaluating the individual blocks according to the sorted order to compute derivatives for the states.  The solver uses numeric integration to compute the evolution of states through time.

Drawing the Mechanical System

SimMechanics provides an alternative to deriving equations and implementing them with base blocks.  Instead of representing a mathematical model of the system, we develop a representation that describes the key components of the mechanical system.  The base units in SimMechanics are physical elements instead of algorithm elements. To build a SimMechanics model, you must break down the mechanical system into the building blocks that describe it.  When you think about the pendulum, it a body connected to a joint, and that joint is connected to some kind of base, we will call that the ground.

Drawing of a pendulum from mechanical elements

The base elements in the SimMechanics library have special names that precisely describe what they are.  I didn’t know this until I started using SimMechanics, but the joint in my pendulum example is called a Revolute.  To build this system, we grab the appropriate blocks and connect them together (kind of like playing with legos!).  The ports on the SimMechanics blocks are connector ports, and the “signals” running between them are connector lines.  These lines do not represent data flow, they represent mechanical connections between elements.

Simulink model using SimMechanics blocks of a single pendulum

These special connection lines and connection ports cannot connect directly to Simulink signals and ports.  Sensors allow you to tap into a mechanical component and measure its physical properties.  In my pendulum model above, I have measured the angular position (ap) and angular velocity (av) of the revolute joint.

When you simulate a SimMechanics model, the process is a little different from regular Simulink data flow.  At initialization, SimMechanics analyzes the mechanical system to determine the topology and geometry of the machine.  At run-time, the external forces and torques are applied to the machine, integrated, and the machine state is updates.  Because the model may contain constraints, the solver checks for the agreement of all the elements of the machine within acceptable tolerances.  The “blocks” that make up the machine do not run one at a time in the simulation loop like regular Simulink blocks.

The Double Pendulum: Equations of Motion

Let’s compare the modeling process for a double pendulum between base Simulink blocks and using SimMechanics.  I don’t know the equations of motion for a double pendulum off the top of my head, so we can derive them.

Derivation of the equations of a double pendulum

Aside from the cramp in my hand from attempting to make my writing legible, the implementation in base blocks is a little more difficult.  There are physical connections between the state variables, and if implemented as written above, you get algebraic loops.

Simulink model of a double pendulum built from base blocks, includeing an algebraic loop

Drawing the Double Pendulum

To make a double pendulum using SimMechanics I just duplicate the first joint and body to make a second arm connected at the end of the first.

Drawing of a double pendulum as the mechanical elements

In literally seconds, I converted the pendulum model to a double pendulum model.

Simulink model of a double pendulum built with SimMechanics blocks

Now It’s Your Turn

How would you model a three-jointed pendulum?  How about N-joints?  Leave a comment here and let us know if you would derive the equations, or reach for SimMechanics.

13 Responses to “Modeling Mechanical Systems: The Double Pendulum”

  1. Arda replied on :

    I am fascinated with SimMechanics. It must be annoying that all the effort to built a base-simulink model is just a trash!
    Anyway, now that you had 2 models, could you please post a comparison of the outputs?

  2. Guy Rouleau replied on :

    @Arda,

    I posted a comparaison of the algorithm in Simulink blocks and in SimMechanics in the same model:

    http://www.mathworks.com/matlabcentral/fileexchange/23126

    You will see that results from the two implementation are similar.

  3. mohammad amayreh replied on :

    this is a wonderfull work from you sir i will certainly use simMechanics but knowing how to model the systen by hand is als very important

  4. Aleksandr Mikhailovich Lyapunov replied on :

    Dear Mr. Seth Popinchalk.

    I could not rest in peace any longer. This blog post trivializes my life’s work! My second method allows us to determine stability of system without solving model’s ODE! SimMechanics gives no insight into stability! Why you use it!? What if, for some obscure initial condition not likely to be simulated, your system is unstable? how will you know? You make mistake to simulate system and not check stability? You must check stability! Checking stability means knowing dynamics! Mr Seth and Mr Guy, your attempt to destroy my work has failed! Read my book: The general problem of the stability of motion.

    I agree with mr. Amayreh’s comments. I add this to it: it is important to model system with pencil, paper and simulate using abacus only! No shortcut.

    This is good education time, not LEGOS play time!

    SimMechanics is Blasphemy!

    Good bye.
    Aleksandr Mikhailovich Lyapunov

  5. Edward Norton Lorenz replied on :

    My, my…it seems that our friend Dr. Lyapunov is a bit agitated with the marvellous SimMechanics.

    Perhaps I can offer a few words in your favour, Mr Guy, to alleviate my colleagues acerbic, vituperative vitriolic remarks.

    The value of your tool seems to lie in exactly the characteristic in question. Namely, that it does not need dynamic analysis of the system being modelled. Perhaps if this tool was available to us in my youth, I would not have been sidetracked by a pesky initial condition in my Lorenz Equation describing the weather for 50 years.

    My detailed 12th order model of the weather system failed to produce the results expected-all that modelling for nothing-proving that building mathematical models may not be worth it sometimes.

    If I had not been trying to discover the chaotic behavior arising from my equations 50 years ago, perhaps today we could have solved global warming….and that, my friend, would have become known as the REAL butterfly effect!

    Former Professor at MIT
    Dr. Ed Lorenz

  6. Alejandro P. Gómez R. replied on :

    It is a wonderful tool Mr. Seth Popinchalk. I would like to know if I can do the same for Electronic and Electric systems?

    Sincerely

    Alejandro

  7. Seth replied on :

    @Alejandro - There are a few tools you should look at: SimScape, SimElectronics and SimPowerSystems. Your choice of which tool to use will depending on what you are trying to model. Good luck!

  8. hendry replied on :

    realy nice, but now i have build a mobile robot motion models, can you help me??

  9. X replied on :

    I would say “blasphemy” is to use a man’s good name and reputation to rant in such an insulse manner.

    News seem to travel slowly in some parts of the world, sadly,

    Edward Norton Lorenz passed away on April 16th, 2008.
    Aleksandr Mikhailovich Lyapunov passed away on November 3rd 1918.

    It’s everybody’s guess what they would have thought of SimMechanics.

    X

  10. Guy Rouleau replied on :

    Hi Hendry,

    It is possible to implement a mobile robot using SimMechanics. To begin, you have to assemble the main parts of the robot, like wheels, suspension and main body. After it is done, the most complex part is to define the interaction of the rover with the ground. One simple solution is to represent the ground as a stiff spring-damper system. Many contact dynamics models have been developed in the past and can be implemented in Simulink, for example the “magic formula” for tires. I can also mention that The MathWorks website provide a list of third party products and some of them are specialized in wheel contact dynamics:

    http://www.mathworks.com/products/connections/

    To give an example of what a rover model can look like, I just submitted a file on the MATLAB Central. Please look for my profile to find it.

    I hope it helps

    Guy Rouleau

  11. tesfaye kebede replied on :

    Dr.that is a nice model,but u have to show the equilibrium configuration too.

  12. Guy replied on :

    @tesfaye - The SimMechanics documentation explains how to compute the equilibrium configuration of a model. This section is titled “Trimming Mechanical Model” and can be found online at:

    http://www.mathworks.com/access/helpdesk/help/toolbox/physmod/mech/ug/f0-6194.html

    This section uses a double pendulum example where the two sections of the pendulum are linked by a rotational spring.

  13. adrian chavarro replied on :

    Great tool, for educational and sicentific, simulation.

    I would like to know where can i place a motor for controlling position on inverterted pendulum….thanks…

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • Mohamamd: Hi Suth, I try to simulate a load tap-changing transformer in simulink but its control part has to be...
  • Han Geerligs: Hello Guy, thanks for the clarificaton and link. However in the documentation I am missing the...
  • Guy: @Han, you probably already know, but I think it is good to share with everyone. To zoom in use the key...
  • Han Geerligs: Hi Seth, Once again I’d like to point out that my biggest accelerator is using mouse and keyboard...
  • XaL: Hi, thanks for the tips. As someone wrote in http://blogs.mathwor ks.com/seth/2009/03/ 13/new-%C2%A0rele...
  • Uba osy: Hi, in the introductory example for fuzzy logic toolbox it was noted that using non fuzzy means, you could...
  • Prashant: How can I have same example but instead AC(1 to 10V 50 or 60Hz) and DC(0.5 to 10 V) then adding AC+DC but...
  • adrian chavarro: Great tool, for educational and sicentific, simulation. I would like to know where can i place a...
  • Ashish Sadanandan: @wei, I was talking about the case where the compiler would perform the ‘model_Xdim...
  • wei: @Ashish, I agree with your observation on compiler optimization but fail to see why Han’s code would be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.