File Exchange

## Euler-Lagrange tool package

version 1.5.0.0 (75.4 KB) by Mischa Kim

### Mischa Kim (view profile)

Use the Euler-Lagrange equation to derive differential equations

Updated 24 Oct 2016

Use the Euler-Lagrange tool to derive differential equations based on the system Lagrangian. The Lagrangian is defined symbolically in terms of the generalized coordinates and velocities, and the system parameters. Additional inputs are the vector of generalized forces and a Rayleigh-type dissipation function.

The function returns the vector field description of the differential equations and the associated MATLAB function and Simulink block. The MATLAB function and Simulink block are readily available in a form to be used with e.g. ode45 or in a Simulink model.

To check out the exact syntax and function definition type

>> help eulerlagrange

at the MATLAB command prompt, copy-paste one of the examples into the command window, and execute. As an example:

syms th thd g m l k
L = m*l^2*thd^2/2 + m*g*l*cos(th); % Lagrangian
X = {th thd}; % Vector of generalized coordinates
Q_i = {0}; Q_e = {0}; % No generalized forces
R = k*thd^2/2; % Friction term
par = {g m l k}; % System parameters
% Create symbolic differential equations …
% …and the corresponding 'm'ATLAB function and 's'imulink block
VF = EulerLagrange(L,X,Q_i,Q_e,R,par,'m','s');

Included in the tool package are two ready-made demos, a pendulum and an inverted rotary pendulum system. The pendulum demo shows the usage of the Euler-Lagrange tool from beginning to end. The rotary system is illustrated with a MATLAB and a Simulink demo. If the Simulink 3D Animation toolbox is available the system dynamics can be visualized in a VR world.

Note: Only MATLAB and the Symbolic Math Toolbox are required to use the tool. Simulink, Simulink 3D Animation, and the Aerospace blockset are recommended to run the demos with all the bells and whistles.

Compatibility: This function has been successfully tested with the following releases
- R2013a
- R2014a through R2016b

### Cite As

Mischa Kim (2020). Euler-Lagrange tool package (https://www.mathworks.com/matlabcentral/fileexchange/49796-euler-lagrange-tool-package), MATLAB Central File Exchange. Retrieved .

Agree with @Johannes Geisler. I faced the same problem and his solution worked. Thank you.

ram ram

### ram ram (view profile)

Symbolic computation of EL is good

Amitava Biswas

PRITAM OJHA

### PRITAM OJHA (view profile)

Index exceeds
array bounds.

Error in
EulerLagrange
(line 117)
subs(Q_i{ii}
+
Q_e{ii},X2n,Xt2n)];

Error in
kin_of_rov_with_energy
(line 458)
VF =
EulerLagrange(L,X,Q_i,Q_e,R,par,'m','s');
---------------------------------------------------------------------------

can anyone explain why this error is repeatedly showing up??
what might be the problem??

Pratik Pandya

### Pratik Pandya (view profile)

i am getting an error in the reshape function (line 89).
X2n = reshape(X,[ ],2); %this line.
any help out there?

Johannes Geisler

### Johannes Geisler (view profile)

Works really good, had to change a little:
I had to swap in line 133 the subs to : VF = subs(VF,Y(indqqd(:)),Y);
Otherwise my coordinates didn't came out the way I threw them in.

Also I had to change in line 128 to ind(length(ind)/2+1:length(ind)); and in line 131 to ['[Y[kk] \$ kk = 1..',num2str(length(ind)),']'];
so I wouldnt get an error when some functions turned out to be zero and thrown away by odeToVectorField

dmitry luchinskii

### dmitry luchinskii (view profile)

why would you try to use Functional Derivative of Vector of Functionals

https://www.mathworks.com/help/symbolic/functionalderivative.html

Index exceeds matrix dimensions.

Error in EulerLagrange (line 116)
DE = [DE; simplify(L_qdotdt - L_qt + R_qtdot) == ...

Error in mathmodel (line 257)
Laga=EulerLagrange(L,XX,T_ext,Q_e,R,par,'m')

I got this error while running. In my case i have use 10-variables in X cell

SOURAV MISHRA

### SOURAV MISHRA (view profile)

I have an issue. I am using MATLAB 2012b.
I am trying to solve the differential equations of a 3 DOF PUMA robot. I have the joint torques and I need to obtain the joint angles and joint velocities.
I have formulated the Lagrangian and while running the function, I am getting the following error:
------------------------------------------------------------------------------------------------------
Operands to the || and && operators must be convertible to logical scalar values.

Error in sym/subs>evalableY (line 231)
s = isnumeric(Y) || (iscell(Y) && all(cellfun(@(y)isnumeric(y)||isempty(y),Y)));

Error in sym/subs>tryFunctionHandle (line 177)
if ~evalableY(F,Y)

[G,worked] = tryFunctionHandle(F,X,Y);

Error in sym/subs (line 119)

Error in EulerLagrange (line 116)
L_qtdot = subs(L_qdot,X2n,Xt2n);

Error in ABCD (line 11)
vf = EulerLagrange(L,X,Q_i,Q_e,R,par,'m');
---------------------------------------------------------------------------------------------
I would appreciate any help. I am in urgent need of a solution to this issue.

Seyhan Özen

Mischa Kim

### Mischa Kim (view profile)

Eric, thanks for your feedback. What is the earlier version you are referring to?

Eric Eveleigh

### Eric Eveleigh (view profile)

Works great. For earlier MATLAB versions I find that I need to slightly modify EulerLagrange.m for it to work:
Line 86 initially has,
arrayfun(@(ii) symfun(['q' int2str(ii) '(t)']),t),1:numcoor,'UniformOutput',false);
The symfun part needs to be changed to,
symfun(sym(['q' int2str(ii) '(t)']))
for the code to work in older MATLAB versions.

Fiona

### Fiona (view profile)

Is it possible to input generalized forces, Q, that are functions of the generalized coordinates, X? I tried to do so, but the code throws an error unless the Q matrix is made up of variable names. I tried to assign the Q matrix later on, but then ode45 cannot properly solve the system of equations. Thank you!

@Johannes

### @Johannes (view profile)

Nice and easy to use!

Johann Wiens

Anton Semechko

### Anton Semechko (view profile)

Uber useful! Thanks!

Sebastian

### Sebastian (view profile)

Very nice tool!

 21 Oct 2016 1.5.0.0 Improved backward compatibility. This function now works with the following releases: R2013a, and R2014a through R2016b. 1 Sep 2016 1.4.0.1 Replaced code fragments that will be removed in future MATLAB releases (creating symbolic variables from strings). Added support for internal and external generalized forces. 22 Jul 2015 1.4.0.0 Minor update: the tool now accounts for the fact that odeToVectorField performs substitutions during conversion. As a result, the order of the created differential equations now matches the order of the state vector components. 13 May 2015 1.3.0.0 Fixed another typo 4 May 2015 1.2.0.0 This version also accounts for Rayleigh-type dissipation functions 28 Feb 2015 1.1.0.0 Added tool picture.
##### MATLAB Release Compatibility
Created with R2016b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux