On using Lagrangian mechanics, and how to use it to simulate complex systems and vehicle dynamics.
The motivation for this article once again comes from the MIT Motorsports team. I have always wanted to model the full transient performance of the car, and I have enjoyed investing time into designing and developing a variety of models for this task.
The previous article showed a method to analyse very generic dynamical systems by automatically identifying degrees of freedom, and then evaluating the dynamics in these degrees of freedom using an extension of D'Alemberts principle. However, I decided to move away from this method for the task of vehicle design for a few reasons:
To get around these issues, I decided to use a simpler model that models all complexities of suspension e.t.c. as simple springs. This (for now) does not account for progressive motion ratios and other higher level complexities, but still provides quite a comprehensive and easy to tweak model of the full car.
However, I want to emphasize that this article is not an article on vehicle design. This is an article on physics and numerical computation, so I will not go into deep details on how the car model itself works (maybe some other time!). This is an article on Lagrangian mechanics, a powerful tool to analytically evaluate dynamic systems, and how it can be applied in a computational context.
Lagrangian mechanics is incredibly powerful in dynamics evaluation, described as "the sledgehammer of dynamics". However, a lot of the information on this topic is heavily fragmented and doesn't, in my opinion, give satisfying explanations on how the Euler-Lagrange equations work. In this first section, I will look at the Euler-Lagrange equations and how they can be derived from first principles.
One thing many mechanics students may not know is that the Euler-Lagrange equations are actually a result from variational calculus, and can be derived without a drop of physics. Consider some once-differentiable variable
The goal of the calculus of variations in this case in this case is to find some function
subject to the condition that
Here,
This might seem a bit abstract, so to provide some intuition, you can think of
This paradigm of minimizing a cost over a route shows up sometimes in physics. One that many readers may be familiar with is Fermat's principle, which states that light takes the path which minimizes the time taken between two points. (This doesn't immediately work with this formulation, as the "time variable" becomes distance, but you get the general jist)The Euler-Lagrange equations don't allow you to find a global minimum (calculus in general can't do that very well) but they can give some information on a stationary point of
The Euler Lagrange equations are actually derived by following this exact idea numerically. Suppose that
This means that for small
Bring the derivative inside the integral (as the integral is in a different variable) and then differentiate
Some comments on the final line:
To proceed from here, note that the integration by parts can be used on the second term inside the integral:
However! note that from our assumption that
The final step technically requires more proof as it is the fundamental lemma of the calculus of variations, but it does feel intuitive. As a reminder, our goal is to find a
As
This is the Euler-Lagrange equation! No physics, no nothing, just finding minimizing paths. You will often see it in the following form:
Great question! It is also one that I feel I may not yet be qualified to answer. However, as previously mentioned, one perspective is that the Lagrangian finds the route a system takes that minimizes some cost function. In mechanics, the function
This function is then called a Lagrangian. The "total cost over time"
Nevertheless, I personally can't find myself sold by the whole argument. The Lagrangian is simply unintuitive. I also believe that the idea that "systems somehow "know" where they are in the future and just take the minimal path to get there" to be completely ridiculous; my personal philosophy is that physical laws should apply microscopically to generate macroscopic consequences, not macroscopic constraints dictating microscopic behaviour. Maybe I am too immature in the world of physics and I will have to come back and rewrite this article (and if someone wants to teach me please reach out!), but for now, this idea is not for me.
However, not all is lost. There is a sense in which you can go from fundamental, Newtonian laws of motion, and use those to recover the Euler-Lagrange equations. Following all of the logic backwards, we can use Newtonian mechanics to show the "principle of least action", instead of going the other way.
Deriving Lagrangian mechanics from D'Alembert's principle actually ends up being more powerful, as it allows you to consider the effects of external forces that do not come from conservative energies.
D'Alembert's principle is the follows: Consider some system made up of
Next, consider any infinitesimal, kinematically permissible variation in the system. This means we move the points around in the system somehow, but we continue to satisfy the constraints. In our rod example, the two points could move a bit to the left, or rotate them a bit, but they could not get further apart from each other. Then, d'Alembert's principle states that for any variations of this form:
Here:
This feels more intuitive! It looks a lot more like Newton's laws, what with the
D'Alembert's principle relies on constraint forces always being perpendicular to permissible virtual displacements.
Each particle in the system experiences forces from two sources: the external forces on the system, and the forces due to constraints. For any arbitrary particle (say the
Then, we can rearrange and sum across all particles to obtain
The statement above is true for any displacement for the particles, and the inclusion of the the
And we once again recover d'Alembert's principle:
However, why are constraint forces necessarily perpendicular to permissible virtual displacements? In fact, this is not necessarily true: it is very possible to construct convoluted examples where this doesn't hold (imagine a particle constrained to be on a treadmill, but if the particle is pulled upwards the treadmill moves forwards, for some strange reason). However, constraint forces usually do no work. Imagine a fully rigid link again: it cannot store or dissipate energy, and so in any permissible displacement, the forces it exerts overall do no work, and thus
Okay! We are now ready to do what we set out to do: get Lagrangian mechanics back from d'Alembert's principle, which we just showed was a simple consequence of Newtonian mechanics. I acknowledge Douglas Cline for the derivation in this section, whose work I have rephrased and reformatted, along with writing my own expositions on the steps taken.
To get started, we must first declare a set of generalised coordinates, that can fully and minimally describe the system. In other words, no generalised coordinates can be redundant and it should be possible to perturb any of the generalised coordinates independently of each other. This is our state vector,
Using generalised coordinates, we can rewrite d'Alembert's principle, as
and then d'Alembert's principle becomes
However, from the way we defined generalised coordinates, any perturbation in the generalised coordinates is permissible. This means that we can set any one of the
We begin by removing the external forcing from the system, by defining the generalised force
Sidenote: this is a generalised force in the sense that multiplying this quantity by
This takes d'Alembert's principle to
Now, we prepare for a neat algebraic trick by writing
by the product rule.
Now, look more closely at the first term. A not completely unreasonable thing to do is to try to write down
And therefore
This lets us write
This looks promising! A term has appeared that is just the kinetic energy of the i'th particle!
It keeps getting better, as we turn our attention to the yellow term. Note that
Now we can write
With the work we have done, we can finally get back to the statement of d'Alembert's principle:
We can now rearrange the summation
But
This should be very exciting! This looks just like the Euler-Lagrange equations, but
Recall our definition of generalised force:
However, we can divide
where
We can define a non-conservative generalised force, that encapsulates all forces not from potential fields.
Now we can write
Substituting this back into the original equation,
We can continue with
Finally, if the potential energy is not dependant on the generalised velocities,
But if we define
This is exactly the same expression as we got from the Euler-Lagrange formulation!
If you remember from earlier, we accomplished we set out to do. Instead of simply defining action and saying that the system follows the route that minimizes the action integral to obtain the above equation, we obtained the above equation from d'Alembert's principle, derived straight from Newtonian mechanics. We can now work backwards and conclude that in the case of no non-conservative forces, the principle of least action holds. This is important as later in physics, assuming the principle of least action is useful, for example in quantum mechanics problems.
However, we have done something even more powerful. We have also proved a framework to work with non-conservative external forces, and this is going to be incredibly important in the next section: using these equations for simulation purposes.
To model vehicle dynamics, we need to start with some model of the car. In the interests of keeping our team's competitive edge, I will not be sharing the exact model here. However, for exemplary purposes, I will be showing a simpler model on which this method can be used.
This image depicts a single rigid body for a frame, and four springs representing the suspension. There are now 6 generalised coordinates for the system, describing the position and rotation (I use Euler angles) of the full frame.
However, to write the Lagrangian, we need to write the energy stored in the springs and the kinetic energy of the system in terms of the generalised coordinates. The expressions in this will become absolutely horrible and completely intractable by any human. However, using a symbolic algebra package like SymPy, we can directly write down these equations. This is a mostly uninteresting endeavour, but it can definitely be done! The general strategy for this is to write symbolic expressions for the positions of key tracked points, and then writing expressions for the distances of these points to work out spring lengths and thus energy.
Armed with a symbolic expression of the Lagrangian, we can now generate one equation of motion for each of the generalised coordinates using the Euler-Lagrange equations from before. But how does this allow us to time evolve the system? Let's take a look at the equation from before:
The first point of action is to deal with the non-conservative generalised forces
These forces can represent multiple things in this model, including:
The most efficient way to implement this in the program is to implement this as some set of forces with known direction, but unknown magnitude. Write each non-conservative force as
As an example of this formulation, to represent arbitrary tire-ground forces, declare two non-conservative forces for each tire, one for the longitudinal component and one for the lateral component. If one of our generalised coordinates is the yaw of the car (
Now for each
We can now write a symbolic expression for each
It is now time to deal with the left hand side of the expression. Before we proceed though, let's think more about the specific Lagrangian in question for most mechanical systems.
As we established earlier, we can write each
Thus, the Lagrangian is
This equation implicitly puts the equations of motion in a vector, to slightly simplify the notation.
The main point of this is to show that for some functions
And for a single equation of motion,
This seems meaningless, but its consequences are immense. This opens a window into our strategy to time evolve the system: write the motion simply as a first order ODE. If we can somehow compute
So, let's get to it! Each equation of motion is now written as
We can continue to use our symbolic algebra program to compute
which can be done once again by using SymPy. Once we have computed all
We can simply do this by substituting all
Now each equation of motion is in the form
To be able to solve for
This then creates the overall equation
But this is now just an equation of the form
Now to compute
To summarise, here is how the overall time evolution of the system works:
The power of this method is in the sheer complexity that a single model can encapsulate. We no longer have to model cars with simple bicycle models with steady state formulae; we have a generalised method to evolve models as complex as they get. The use of generalised coordinates also allows us to directly track values of interest, such as the yaw rate of the car or the roll of the car. The use of symbolic algebra expressions allow other values of interest (e.g. shock length, tire normal forces) to be easily computed on top of this. To showcase the power of this model, here is a graph showing load transfer as a driver enters a turn following a step steering input:
And here is a video of a simulated car drifting!
No project is truly done without thinking about where you can go next!
But overall, I am super happy with model and its power, and I can't wait to see where the project goes next!