Error using mupadengin​e/feval2sy​m_NaNsingu​larity First argument must be an arithmetical expression.

71 views (last 30 days)
clc
close all
clear all
%Declaración de Variables Simbólicas longitudes, masas, pos, vel y acel.
syms L1 L2 m1 m2 g
syms q1 q2 qd1 qd2 qdd1 qdd2
%Definicion de vectores de posición para las articulaciones
r11 = [0;0;0;1];
r22 = [0;0;0;1];
g0 = [0,0,-g,0];
%Hallar matrices de transformación
A01 = [cos(q1),-sin(q1), 0, L1*cos(q1);
sin(q1), cos(q1), 0, L1*sin(q1);
0, 0, 1, 0;
0, 0, 0, 1];
A12 = [cos(q2),-sin(q2), 0, L2*cos(q2);
sin(q2), cos(q2), 0, L2*sin(q2);
0, 0, 1, 0;
0, 0, 0, 1];
A02 = A01*A12;
%Obtener matrices parciales Uij
U11=simplify(A01,q1);
U12=simplify(A01,q2);
U21=simplify(A02,q1);
U22=simplify(A02,q2);
%Obtener matrices Uijk
U111 = simplify(diff(U11,q1));
U112 = simplify(diff(U11,q2));
U121 = simplify(diff(U12,q1));
U122 = simplify(diff(U12,q2));
U211 = simplify(diff(U21,q1));
U212 = simplify(diff(U21,q2));
U221 = simplify(diff(U22,q1));
U222 = simplify(diff(U22,q2));
%Obtener Matrices Ji (Tomadas desde el origen de las coordenadas).
J1 = [0,0,0,0;
0,0,0,0;
0,0,0,0;
0,0,0,m1];
J2 = [0,0,0,0;
0,0,0,0;
0,0,0,0;
0,0,0,m2];
%Obtener la matriz de inercias i=1 j=1 k=1:2
d11 = trace(U11*J1*transpose(U11)) + trace(U21*J2*transpose(U21));
%i=1 j=2 k=2
d12 = trace(U22*J2*transpose(U21));
%i=2 j=1 k=2
d21 = trace(U22*J1*transpose(U12));
%i=2 j=2 k=2
d22 = trace(U22*J2*transpose(U22));
D=[d11,d12;
d21,d22];
D=simplify(D)
%Obterner los términos hikm
%i=1 k=1 m=1 j=1:2
h111 = trace(U111*J1*transpose(U11)) + trace(U211*J2*transpose(U21));
%i=1 k=1 m=2 j=2
h112 = trace(U212*J2*transpose(U21));
%i=1 k=2 m=1 j=2
h121 = trace(U221*J2*transpose(U12));
%i=1 k=2 m=2 j=2
h122 = trace(U212*J2*transpose(U12));
%i=2 k=1 m=1 j=2
h211 = trace(U211*J2*transpose(U21));
%i=2 k=1 m=2 j=2
h212 = trace(U212*J2*transpose(U21));
%i=2 k=2 m=1 j=2
h221 = trace(U221*J2*transpose(U22));
%i=2 k=2 m=2 j=2
h222 = trace(U222*J2*transpose(U22));
%Matriz Coriolis y Centrípeta
%i=1 k=1:2 m=1:2
h1 = (h111*qd1*qd1 + h112*qd1*qd2) + (h121*qd2*qd1 + h122*qd2*qd2);
%i=1 k=1:2 m=1:2
h2 = (h211*qd1*qd1 + h212*qd1*qd2) + (h221*qd2*qd1 + h222*qd2*qd2);
H = simplify([h1;h2])
%Hallar la matriz de fuerzas de gravedad
%i=1 j=1:2
c1 = (-m1*g0*U11*r11) + (-m2*g0*U21*r22);
%i=2 j=1:2
c2 = (-m1*g0*U12*r11) + (-m2*g0*U22*r22);
C = simplify([c1;c2])
%Hallar el torque o ecuación dinámica del sistema
qdd = [qdd1;qdd2];
tau = D*qdd+H+C
I have this code to check the euler-lagrange method, but it does not generate the solutions, in the definition of U11 it generates this error...

Answers (1)

Abhas
Abhas on 28 Mar 2024
Hi Juan,
The error you're encountering is due to an incorrect use of the "simplify" function. The "simplify" function simplifies expressions, not matrices, with respect to a variable. In your code, instead of trying to simplify matrices 'U11', 'U12', 'U21', and 'U22' with respect to 'q1' and 'q2', you should simply call "simplify" on the matrix without specifying a variable.
The correct MATLAB code using simplify function is as follows:
clc
close all
clear all
%Declaración de Variables Simbólicas longitudes, masas, pos, vel y acel.
syms L1 L2 m1 m2 g
syms q1 q2 qd1 qd2 qdd1 qdd2
%Definicion de vectores de posición para las articulaciones
r11 = [0;0;0;1];
r22 = [0;0;0;1];
g0 = [0,0,-g,0];
%Hallar matrices de transformación
A01 = [cos(q1),-sin(q1), 0, L1*cos(q1);
sin(q1), cos(q1), 0, L1*sin(q1);
0, 0, 1, 0;
0, 0, 0, 1];
A12 = [cos(q2),-sin(q2), 0, L2*cos(q2);
sin(q2), cos(q2), 0, L2*sin(q2);
0, 0, 1, 0;
0, 0, 0, 1];
A02 = A01*A12;
%Obtener matrices parciales Uij
U11 = simplify(A01);
U12 = simplify(A01);
U21 = simplify(A02);
U22 = simplify(A02);
%Obtener matrices Uijk
U111 = simplify(diff(U11,q1));
U112 = simplify(diff(U11,q2));
U121 = simplify(diff(U12,q1));
U122 = simplify(diff(U12,q2));
U211 = simplify(diff(U21,q1));
U212 = simplify(diff(U21,q2));
U221 = simplify(diff(U22,q1));
U222 = simplify(diff(U22,q2));
%Obtener Matrices Ji (Tomadas desde el origen de las coordenadas).
J1 = [0,0,0,0;
0,0,0,0;
0,0,0,0;
0,0,0,m1];
J2 = [0,0,0,0;
0,0,0,0;
0,0,0,0;
0,0,0,m2];
%Obtener la matriz de inercias i=1 j=1 k=1:2
d11 = trace(U11*J1*transpose(U11)) + trace(U21*J2*transpose(U21));
%i=1 j=2 k=2
d12 = trace(U22*J2*transpose(U21));
%i=2 j=1 k=2
d21 = trace(U22*J1*transpose(U12));
%i=2 j=2 k=2
d22 = trace(U22*J2*transpose(U22));
D=[d11,d12;
d21,d22];
D=simplify(D)
D = 
%Obterner los términos hikm
%i=1 k=1 m=1 j=1:2
h111 = trace(U111*J1*transpose(U11)) + trace(U211*J2*transpose(U21));
%i=1 k=1 m=2 j=2
h112 = trace(U212*J2*transpose(U21));
%i=1 k=2 m=1 j=2
h121 = trace(U221*J2*transpose(U12));
%i=1 k=2 m=2 j=2
h122 = trace(U212*J2*transpose(U12));
%i=2 k=1 m=1 j=2
h211 = trace(U211*J2*transpose(U21));
%i=2 k=1 m=2 j=2
h212 = trace(U212*J2*transpose(U21));
%i=2 k=2 m=1 j=2
h221 = trace(U221*J2*transpose(U22));
%i=2 k=2 m=2 j=2
h222 = trace(U222*J2*transpose(U22));
%Matriz Coriolis y Centrípeta
%i=1 k=1:2 m=1:2
h1 = (h111*qd1*qd1 + h112*qd1*qd2) + (h121*qd2*qd1 + h122*qd2*qd2);
%i=1 k=1:2 m=1:2
h2 = (h211*qd1*qd1 + h212*qd1*qd2) + (h221*qd2*qd1 + h222*qd2*qd2);
H = simplify([h1;h2])
H = 
%Hallar la matriz de fuerzas de gravedad
%i=1 j=1:2
c1 = (-m1*g0*U11*r11) + (-m2*g0*U21*r22);
%i=2 j=1:2
c2 = (-m1*g0*U12*r11) + (-m2*g0*U22*r22);
C = simplify([c1;c2])
C = 
%Hallar el torque o ecuación dinámica del sistema
qdd = [qdd1;qdd2];
tau = D*qdd+H+C
tau = 
You may refer to the following documentation links to have a better understanding on working with simplify function and MATLAB Euler-Lagrange library:
  1. https://www.mathworks.com/help/symbolic/simplify.html
  2. https://www.mathworks.com/matlabcentral/fileexchange/86563-matlab-euler-lagrange-library

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!