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.
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:
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.
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.
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.
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.
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.
In literally seconds, I converted the pendulum model to a double pendulum model.
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
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?
I posted a comparaison of the algorithm in Simulink blocks and in SimMechanics in the same model:
You will see that results from the two implementation are similar.
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
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!
Aleksandr Mikhailovich Lyapunov
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
It is a wonderful tool Mr. Seth Popinchalk. I would like to know if I can do the same for Electronic and Electric systems?
realy nice, but now i have build a mobile robot motion models, can you help me??
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.
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
Dr.that is a nice model,but u have to show the equilibrium configuration too.
@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.
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…
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?
@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
What if we want to add friction to Single pendulum system in Simmechanics?
@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.
please tell me TCSC model to subsynchronous resonance in power systems
@Mahaboob Basha Qureshi – I reccomend you post your question to the MATLAB Newsgroup.
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
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?
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.
@Mike – “Do not work” is a very general condition. If you would like further help on this, I suggest posting to MATLAB Answers. Good luck.
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
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.
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????
@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?
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.
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?
Do you connect simmechanic to 3Danimation toolbax?
Do you have a special connection to the body and revolute do ?
Please reply to me?
@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!
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.
I need simulink for Sub-synchronous Resonance in HVDC LINK
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.
@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!