# How to find the nonlinear equations of motion of a seesaw-cart electromechanical system?

So what I have here is a single pendulum with a cart system. The pendulum is composed of a set of rigidly connected parts integrated in a seesaw-like structure which can freely pivot around the axis on its bottom. The weight attached to the vertical rod of the pendulum is an additional element to the pendulum.

What I need to do is to derive equations of motion and state-space representation of this system and put it all into MATLAB to model this system. I am just getting to learn Newton's and Langrange's method and I have no way of knowing if my solution would remotely be correct.

The two figure below represent the described model, with the second being a more simplified model of the seesaw model.

The system's constant parameters necessary to model the dynamics of the system are as follows:

• a height of center of mass of the pendulum, excluding the weight B (i.e., point A in the figure = center of mass of the light gray shaded parts),
• b position of the weight B on the vertical rod )the weight is modelled a point concentrated mass at point B),
• d height of interconnection points of ropes which connect multiple pendulums (this is irrelevant to the given problem since I want to model the system for a single pendulum),
• c height of the center of mass of the cart (point C in the figure),
• h height of the rack for cart propulsion,
• J moment of inertia of the pendulum, excluding the weight B,
• m1 mass of the pendulum, excluding the weight B,
• m2 mass of the weight in point B,
• m3 mass of the cart.

The cart is driven by a DC motor.

Any help with this I would greatly appreciate.

• Welcome to Robotics Marx. This looks like a homework question, and on stack exchange, questions asking for homework help must include a summary of the work you've done so far to solve/understand the problem, and a description of the difficulty you are having solving/understanding it. Please edit your question to add this information and take a look at How to Ask and tour for more information on how stack exchange works. For advice on how to write a good question, see the Robotics question checklist.
– Ben
Nov 30, 2021 at 14:38
• Hello Ben, thank you for the welcome. This is kind of a homework question but not exactly. It's actually something I'm working on for my undergrad thesis and it's been rather difficult because I can't seem to find anything similar to this problem online. Still, I will try to solve this problem and post if I feel it could help someone in the future! :) Nov 30, 2021 at 20:06

Took some time but this right here might be the solution to the problem. I did this by hand so I hope the handwriting is not a problem. Thanks to Chuck for the help! :)

This is a really complex example to start with if you're just getting into deriving equations of motion; I would think any course would have you do lots of examples with much simpler systems and build towards working on something like this.

That said, when I do work like this in Matlab I try to start with positional equations. You've got drawings there that show things like $$c$$, $$h$$, $$\phi$$, etc. Which of those are time-varying, and which aren't?

Perturb the system and make your positional equations from there, like the second drawing. Look at examples for compound pendulums and see how they've setup their positional equations. Remember that ultimately you're trying to go for kinetic and potential energies, so you'll need to get positions to all the centers of mass.

When you create the equations in Matlab, use the symbolic toolbox. You can define constants with the sym command, like

sym b;


and time-varying parameters by putting (t) after the variable, like

sym s(t);


then, when you write your equations, you can just use the variables b and s and Matlab understands that s varies as a function of time and b does not. This is important because, once you get your position equations completed, you can take derivatives with the diff command. You can just do diff(myEqn) and Matlab understands to take the first derivative with respect to t, or you can be explicit about it like diff(myEqn, t). There's are a few other specifics you can read about in the documentation, but those are the basics for diff.

The Euler-Lagrange equation has the "q" terms, which is just the "variable in question" at the time, so you repeat the process for each variable and wind up with your system of equations. I think you would use $$s$$ as a q once and then use $$\phi$$ as a q once, which should end you up with some set of equations for $$\ddot{s}$$ and $$\ddot{\phi}$$, which you'd use for your equations of motion. If you're going to put an actuator on $$b$$ or something to change the bob weight then you'd want to use that, too.

$$L = K - U \\$$

and you can do that with $$U = mgh$$ and $$K = (1/2)mv^2$$. You'll get your velocity $$v$$ term by taking the time derivative of your position equation, with diff.

Then, when you get $$L$$, you can find the Euler-Lagrange equation, like

$$\frac{\partial L}{\partial q} L(t) - \frac{d}{dt}\frac{\partial L}{\partial \dot{q}} L(t) = 0 \\$$

So, once you define what $$L$$ is, you need to again go through each time for a different "q" variable ($$s$$, $$\phi$$, etc.) and get your Euler-Lagrange equation. Again, you can kind of leverage Matlab's diff function for this but it becomes more difficult because Matlab does a pretty poor job at handling partial derivatives of derivatives.

What I do is to use the subs command to pull my derivatives out and replace them with something easier to reference, like

syms sDot;
L2 = subs(L, diff(s,t), sDot);


That would give you L2 as a function of sDot instead of diff(s, t). I do this because again the partial derivative is kind of a pain to do. What you're going for is taking the derivative of L with respect to $$\dot{s}$$, but what that looks like in Matlab would be:

dLdqDot = diff(L, diff(s,t));


and it's been long enough that I can't remember exactly what the issue was, but I remember Matlab not being able to intelligently handle that. By swapping out your diff(s, t) with a singular variable like sDot you can instead do something like:

dLdqDot = diff(L, sDot);


and get better results. Once you're done with that, you'll need to subs back in your version as a function of time so you can take the time derivative of that, the $$d/dt$$ of the $$d/dt(\partial L / \partial \dot{q})$$ term.

dLdqDot_t = subs(dLdqDot, sDot, diff(s, t));


Then you get your right hand side:

eulerLagrange_RHS = diff(dLdqDot_t, t);


and then finally one more round of subs to get your variables back out to be sortable again:

syms sDdot;
el_RHS = subs(eulerLagrange_RHS, diff(s, t, t), sDdot);
el_RHS = subs(el_RHS, diff(s, t), sDot);


and it's important to note that you need to do the second derivative FIRST or you'll wind up with something like diff(sDot, t) instead of diff(s, t, t) or sDdot. After you do the second derivative you can do the first derivative.

Then, once you're done with the (d/dt)(dL/dqDot) then you can work on doing the same for the dL/dq as the left-hand side, and then you can setup your equation:

equationOfMotion(1) = el_LHS - el_RHS == 0;


and then you can solve(equationOfMotion(1), sDdot) and get your equation of motion for $$\ddot{s}$$. Then you do it all over again for $$\phi$$. And yes, it's super, super tedious, but again you should have had at least one full semester and probably two full semsters of doing homework basically like this at least once a week, classes like statics, dynamics, system dynamics, etc.

This is all more about how to wrangle Matlab than the specifics about your problem, but do your work in a script in Matlab and you can make all the pretty plots you want along the way to check your work.

I would strongly, strongly recommend you parameterize your variables at the end and use a variety of plot commands to visualize what you're working with, to recreate the scene through various values of $$\phi$$, etc., to make sure you haven't gone wrong somewhere.

If you try working your way through this and have more issues, please come back and show your work with the problem as far as you've gotten and a description of why you think it's wrong or what has you stuck.

Also a disclaimer - I don't actually have access to a copy of Matlab any more or any of my prior work (changed jobs wooo) so this is all from memory; you'll probably find some mistakes in the syntax or something but the steps should be generally accurate.

• Sorry for the somewhat late response. Thank you for your comment, I've had somewhat progress with this problem. Getting to where I think I can start getting the partial derivative so I finally get my Lagrange. This is what I have so far ibb.co/RgBh8FG Dec 7, 2021 at 3:54
• I'm discouraged to continue right now because I don't know if I'm on the right track, but that's my work so far. I will surely continue once I double-check my so far results. Dec 7, 2021 at 4:01
• @Marx - I think you need the parallel axis theorem for your A kinetic energy, because the axis of rotation isn't coincident with point A, but otherwise I think looks really good. Didn't look too hard at your cart KE because trig derivatives get messy in a hurry, but that and your partial derivatives were what I was trying to help with in my answer here. Define your cart's position using variables that are defined to be functions of time where appropriate and then Matlab's diff command will give you the velocity for free.
– Chuck
Dec 7, 2021 at 11:57
• Some other pro commands in matlab are simplify and expand. You might get a ton of terms when you take the derivative of position, but some of them might reduce via trig identities, etc. Simplify will find those identities, combine fractions, etc. to simplify your expression. I use it with expand, which forces matlab to multiply out all the terms, because sometimes matlab will fail to recognize terms "hidden" in exponential parenthesis when you just call simplify. Try simplify(expand(yourExpression)) on any messy-looking symbolic outputs you get.
– Chuck
Dec 7, 2021 at 12:07
• Thank you so much for the help, I posted my answer to this problem in case anyone found themselves with a similar problem. Still have to convert these non-linear ODE's into state-space form which has proven to be a challenge (at least for me) but I have progress which is good. :) Dec 20, 2021 at 0:42