Gas station without pumps

2016 October 15

Having trouble learning Lagrangian mechanics

Filed under: Uncategorized — gasstationwithoutpumps @ 22:51
Tags: , ,

For the past few days, I’ve been trying to teach myself enough Lagrangian mechanics that I can derive systems of ordinary differential equations for the sorts of simple systems that come up in control-theory classes.

I think I’ve kind of got it for mechanical systems, and maybe for electronic ones, but I can’t seem to wrap my head around electromechanical systems.  I’m going to dump out a little of my understanding here, to clarify it in my own head, and to get corrections or suggestions from my readers, many of whom are far better at physics than me, having actually taken it in college. My son and I studied simple physics using the textbook Matter and Interactions a few years ago, but the book doesn’t get into Lagrangian mechanics, and I don’t think I really understand magnetic fields intuitively well enough to keep the mathematical abstractions straight.

Part of my problem in reading introductions to Lagrangian mechanics (like the Wikipedia one) is that they use almost impenetrable abstract notation and immediately jump to very general cases, leaving me with no intuition about what they are doing.

As I understand it, Lagrangian mechanics starts with the idea of a conserved scalar quantity (like energy), and provides a way of setting up equations of motion by taking partial derivatives with respect to generalized coordinates, which are in turn functions of time.

For mechanical systems the generalized coordinates are generally positions of point masses or angles of joints (velocity and angular velocity are time derivatives of the coordinates).  For electronic systems, the generalized coordinates are either charges or voltages and currents, depending whose formulation you read.  The charge-based formulation has made a bit more sense to me, as currents are the time-based derivatives of charge.  We look at the charge on capacitors and current through inductors to compute energy in the system.

One problem with a lot of the descriptions of Lagrangian mechanics is that they do everything with purely conservative systems, then tack in dissipation as an afterthought, but all real systems that need control have dissipation of energy as a fundamental part of the modeling process.  I’ll try to include the dissipation terms in the basic formulation, rather than adding them  at the end.

Some notation:

  • \mathcal{L} is the difference between kinetic and potential energy of the system.  In a conservative system, it would always be 0, with energy sloshing back and forth between kinetic and potential forms, but never increasing or decreasing.Correction based on comments: I screwed up here—it is the sum of kinetic and potential energy (the Hamiltonian) not the difference (the Lagrangian) that is constant in a conservative system.
  • \mathcal{P} is the power dissipated by the system.
  • \mathcal{F} is the vector input to the system needed to make the energy balance work out.   The units for this vector depend on what the generalized coordinates are.  For mechanical systems, Cartesian coordinates will need forces, and angles will need torques.  For electronic systems using charges as coordinates, the units are volts.

With this notation, the basic formula is

\frac{d}{dt}\frac{\partial \mathcal{L}}{\partial \dot x} - \frac{\partial \mathcal{L}}{\partial x} + \frac{\partial \mathcal{P}}{\partial \dot x} = F_{x}~.

I may be missing some critical conditions on when this can be applied, but I did manage to work out the equations of motion for an inverted pendulum on a cart from it.

So here is the inverted-pendulum example:

We have two coordinates: the horizontal position of the cart x, and the angle of the inverted pendulum (clockwise from upright) \theta.  The cart has mass m_{c} and the pendulum m_p, with a distance from the pivot on the cart to the center of  mass for the pendulum l.  Furthermore, the pendulum has moment of inertia J, and there is viscous drag on the cart with a force -b\dot x.  The pivot is assumed to be frictionless.

Let’s use p to designate the location of the center of mass of the pendulum: p= \left(x+l\sin(\theta), l\cos(\theta) \right).

The potential energy in the system is just due to the height of the pendulum mass: m_{p}g l \cos(\theta).  The kinetic energy is m_{c} \dot x^2/2 + m_p \dot p^2/2 + J \dot\theta^2/2. Combining these gives us

\mathcal{L} = m_{c} \dot x^2/2 + m_p \dot p^2/2 + J \dot\theta^2/2 - m_{p}g l \cos(\theta)~.

The power dissipated due to friction is \mathcal{P} = b \dot x^2/2.

It would be good to get rid of the extra variable p, using just x and \theta.  The derivative is \dot p=\left(\dot x +l \cos(\theta)\dot\theta, -l \sin(\theta)\dot\theta\right), and its square is \dot p^2 = \dot x^2 + 2l\cos(\theta) \dot x \dot \theta + l^2\dot \theta^2.  Substituting that into our previous formula gives us the Lagrangian in terms of just the generalized coordinates and their time derivatives:

\mathcal{L} = (m_{c} +m_p) \dot x^2/2 + m_p l \cos(\theta)\dot x\dot \theta + (J +m_{p}l^2)\dot\theta^2/2 - m_{p}g l \cos(\theta)~.

Applying the basic formula gives us

F_x = \frac{d \left((m_c+m_p)\dot x +m_p l \cos(\theta)\dot \theta\right)}{dt} + b \dot x

F_x = (m_c+m_p)\ddot x + m_p l \cos(\theta)\ddot\theta -m_p l \sin(\theta) {\dot\theta}^2+ b \dot x

F_\theta =\frac{d \left(\left(m_p l \cos(\theta) \dot x + (J+m_pl^2)\dot\theta\right)\right)}{dt}  +m_p l \sin(\theta)\dot x{\dot\theta}^2+ g m_p l \sin(\theta)

F_\theta = m_p l \cos(\theta) \ddot x - m_p l \sin(\theta)\dot x\dot\theta + (J+m_pl^2)\ddot\theta+m_p l \sin(\theta)\dot x{\dot\theta}^2+ g m_p l \sin(\theta)  

We can linearize this around \theta=0 (the inverted pendulum straight up), by setting \cos(\theta)\approx 1 and \sin(\theta)\approx 0, except for the gravitational term, where we use \sin(\theta)\approx\theta:

F_x = (m_c+m_p)\ddot x + m_p l \ddot\theta+ b \dot x

F_\theta = m_p l \ddot x  + (J+m_pl^2)\ddot\theta+ g m_p l \theta

These equations of motion can be used to design a controller for the cart and inverted-pendulum system, as long as you don’t let the pendulum get too far from the vertical.  I think that the linearized equations are ok, but I may have made some calculus errors in the nonlinear equations I simplified them from.

I’ll stop here, but try to do another (electronic) example in a later blog post.


  1. Good start so far, and certainly right in my wheelhouse so far as well (I’ve never used a Lagrangian formalism in electronics or control theory but I do a ton with mechanics). I don’t think I agree with your statement that L should always be zero. It’s integral over the whole interaction should be minimized, but it doesn’t have to be zero.

    Comment by Andy "SuperFly" Rundquist — 2016 October 16 @ 09:17 | Reply

    • You’re right. I don’t know what I was thinking \mathcal{L} is unlikely to ever be 0. It is the difference of the derivatives that is usually given in the intros to be 0 (no dissipation and no external forces).

      Comment by gasstationwithoutpumps — 2016 October 16 @ 09:59 | Reply

  2. I agree with Andy, not only is the value of L not zero, it’s not going to be constant (for all but the most trivial systems). The value of the Hamiltonian (which is often, but not always, T + V) will be constant in a conservative system, and can be taken to be zero, with the appropriate choice for the zero of potential energy.

    I wouldn’t say that dissipation is added to Lagrangian systems “as an afterthought”. I would say that not all dissipation can be shoe-horned into the Lagrangian formulation. With that said, I would be wary of trying to include dissipation as part of L, for fear that I would be limiting my model unnecessarily. But I haven’t done Lagrangians for electronics, so …

    Comment by Chris Goedde — 2016 October 16 @ 09:39 | Reply

    • I agree with Andy and you, there is no reason for \mathcal{L} to be 0.

      I put the dissipation into a separate power term, which seemed to be the most common way to tack it in to the Lagrangian formulation in the intros I tried to understand. Models for control theory usually need to include some handling of dissipation, though often rather crudely approximated (like assuming viscous drag, rather than static friction). In electronics every resistor is a source of dissipation, and neglecting them would really screw up the models!

      Comment by gasstationwithoutpumps — 2016 October 16 @ 10:02 | Reply

      • Drag that is linear in the velocity is used because (1) it is easy to solve and (2) it is identical to R*I damping in AC circuits making for two birds with one stone. If you know one you immediately grasp the other, plus you can solve the mechanical system with an analog electrical system, an approach that was quite common in the olden days before cheap digital computers.

        Comment by CCPhysicist — 2016 October 16 @ 19:46 | Reply

        • I figured that drag linear in velocity was popular because of the ease of modeling. Does it have any resemblance to real forces?

          I know that aerodynamic drag on bicycles is usually modeled as proportional to v^2, but that rolling resistance is often modeled as proportional to v.

          Hmm, explains that something moving slowly and causing no turbulence will have drag linearly proportional to velocity. That implies to me that neither the v nor the v^2 models are very good in a region of medium speed, where modelers switch from one model to the other. Or is it a sudden transition (onset of turbulence, for example) that switches between which model is appropriate?

          Comment by gasstationwithoutpumps — 2016 October 16 @ 20:14 | Reply

  3. […] post is a continuation of Having trouble learning Lagrangian mechanics, looking at electronic systems rather than mechanical ones.  Again, this is not intended as a […]

    Pingback by Lagrangian mechanics for linear electronics | Gas station without pumps — 2016 October 16 @ 13:27 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: