Guy on Simulink

Simulink & Model-Based Design

Modeling Mechanical Systems: The Double Pendulum 34

Posted by Seth Popinchalk,

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.

Update - 9/14/2015: This blog post uses SimMechanics First Generation. Examples implementing double-pendulum using SimMechanics Second Generation can be found here.

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.

34 CommentsOldest to Newest

Arda replied on : 1 of 34

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?

mohammad amayreh replied on : 3 of 34

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

Aleksandr Mikhailovich Lyapunov replied on : 4 of 34

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

Edward Norton Lorenz replied on : 5 of 34

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

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

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



X replied on : 9 of 34

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.


Guy Rouleau replied on : 10 of 34

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:

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

tesfaye kebede replied on : 11 of 34

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

Guy replied on : 12 of 34

@tesfaye – The SimMechanics documentation explains how to compute the equilibrium configuration of a model. This section is titled “Trimming Mechanical Model” and uses a double pendulum example where the two sections of the pendulum are linked by a rotational spring.

adrian chavarro replied on : 13 of 34

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…

mary replied on : 14 of 34

i have a SimMechanics set of blocks, transferred using Simmechanics Link from solidworks. It worked fine till now. Now, I want to model the full system; it will include motors and sensors.

I want to model my stepper motor into Matlab, and connect it to the readily modeled physical system (using SimMechanics blocks) and actuate my system. I tried to use ‘hybrid stepper motor’ model from SimPowerSystems toolbox but got problems.. Any advice regarding this?

Guy replied on : 15 of 34

@Mary – Typically when you design a controller for a SimMechanics plant, you measure position and velocity signals using sensors. In Simulink your controller computes the torque or force to be fed back to SimMechanics through an actuator. A similar approach can be taken to interface SimPowerSystems to SimMechanics

To learn more about this topic I recommend looking at two demos:

– Simulink Power Window Controller and Detailed Plant Model

In this model the mechanics of a car electric window is modeled in SimMechanics and the electrical motor in SimPowerSystems.

– Hybrid Electric Vehicle (HEV) Power Train Using Battery Model

This demo shows an example of interfacing a SimPowerSystems machine with a SimDriveline power-train. The principle is the same when interfacing with SimMechanics.

As I mentioned above, you need to feed the torque generated by the SimPowerSystems machine to SimMechanics through an actuator. You measure the resulting velocity with a SimMechanics sensor and feed it back to the SimPowerSystems engine.

A final note, SimElectronics motors might be easier to interface with SimMechanics since Interface blocks are provided.

I hope it helps


Iman replied on : 16 of 34

What if we want to add friction to Single pendulum system in Simmechanics?


Guy replied on : 17 of 34

@Iman, To add friction to a joint in SimMechanics you need to add one actuator/sensor port to the Revolute Joint. Then connect to this port the “Joint Spring & Damper” block from the Force Elements section of the library.

rima ghosh replied on : 20 of 34

i have just installed matlab2010b and making a simple pendulum as suggested in your example. the link length is = 15cm.Initial condition is set to -60 deg. CS1 and CGCs have same orientation. when i measure angular position (in radians) it goes from -1.04 deg to -2.09 rad(approx). should it not go from -60 deg to 0 to + 60 deg . the angular position plot is like having a offset to the expected value

mohammad jafari replied on : 21 of 34

how can i model a mechanical sydtem wich has a joint with more than 3 elements?
i mean 3 or more element connect with a joint in one place?

Mike replied on : 22 of 34

Hi, I was wondering if I can have the two models separately, i.e. simulink and simmechanics? I have tried to run them separately but they do not work.

Ramesh Akula replied on : 24 of 34

Hi Seth, very nice posts. I have some doubts. I am trying to create the dynamic masking of the simscape system. can you give a example on that how to create the dynamic masking in simscape.
I’ll explain clearly: suppose consider a finding surface area of the cylinder, rectangle. If user selected Cylinder, it should ask only for Length and radius of the cylinder. Similarly if user selected rectangle, it should prompt for entering the length and width only. Please help me in this problem.

Thanks and regards

moazzam replied on : 25 of 34

Hi… Well, I’m working with 3-link pendulum. For my system, th1 changes from -30 t0 +60, th2 varies from -112 to -4 and th3 changes from -162 to -13 (all angle measurements are in degrees) in just 0.2 second. I’ve generated ref. trajectories using cubic polynomials. I want my simmechanics/maplesim model to follow those trajectories. The problem is I don’t know what excitation/torques must be given to joints in order to get the desired response. I seek immediate help on it.

Ayan replied on : 26 of 34

Mr. Seth Popinchalk,
Sir, you said that the result from two simulation are similar. I ran your simulation with theta1=pi/2; and theta2=pi/(2)^0.5, with unit masses . Initially both models showed same result, but after 28 sec, their results became completely different.Which result is reliable here????

Seth replied on : 27 of 34

@Ayan – As you noted, both simulations agree initially, and after a period of time the results diverge. Which one is correct? Which implementation is giving the right answer? The model built with base blocks that implements the constraint with algebraic loops, or the model built using SimMechanics bodies and joints?

The Answer:

If you are using a variable step solver, both are correct to the tolerance settings you have run them with. The Double Pendulum is a chaotic system. One of the properties of chaotic systems is that they are very sensitive to initial conditions; meaning: small differences can become magnified into big differences. For any two implementations of the same system, there are likely to be very small numerical differences within the tolerance of the calculation (for example +/- eps for a floating point calculation). These small differences can be like perturbations to the solution and over time magnify the difference between the solutions from the different implementations.
Great question!

Ayan replied on : 28 of 34

Mr. Seth Popinchalk,
Thank you sir for your reply.As you’ve mentioned, its long term prediction is not possible due to presence of error. Is there any way to produce almost accurate model of such kind of dynamical system to predict its state atleast for 200sec , other than minimizing the tolerances?

nasim replied on : 29 of 34

Do you connect simmechanic to 3Danimation toolbax?
Do you have a special connection to the body and revolute do ?
Please reply to me?

Seth replied on : 30 of 34

@Ayan – The only way to get the same answer with numerically different approaches is to increase the precision with which you apply those techniques. This means minimizing the error, which requires minimizing tolerances. I think this is the approach you are looking for.

@Nasim – your question is off topic, and very general. Please have a look at the documentation, review some demos, or contact technical support. Good luck!

Andres replied on : 31 of 34

I’ve connected the single joint pendulum on Simulink and ran the simulation with the default values from simulink. The angle is a sine function when I use only close to 0 initial angles, above 3 the amplitude doesnt change, the function just moves umpward the y-axis, not only that but with small angles the period is not the same using the same length and gravity values defying every mathematical analysis given for the pendulum so if u could give me some feedback about this i’d appreciate it, the simmechanics model runs smoothly though.

Charinda replied on : 33 of 34

Hi Mr Popinchalk,

I’m quite new to simulink. I followed the steps on this page and I got the model. In your model, the initial condition blocks have theta_1 and so on in the matrix without them having any specified value that I can see. I was wondering what is happening there. Are they variables defined in MATLAB script that you have imported here and if so how? Or what is happening there? Please any help on this would be very much appreciated.

Seth replied on : 34 of 34

@Charinda – Simulink models usually include PreLoadFcn or OpenFcn or StartFcn which execute MATLAB Code in the base workspace. Take a look at those functions in your model, and you will probably find:
or something similar. Good luck!

Add A Comment

Your email address will not be published. Required fields are marked *


Preview: hide