diff command in matlab

조회 수: 13 (최근 30일)
Milad Ghafary Shirvan
Milad Ghafary Shirvan 2020년 7월 7일
답변: Walter Roberson 2020년 7월 8일
Hi everyone
I'm trying to derive the dynamic equations of a robot with lagrange method in matlab
when I define the lagrangian function and I want to derivate this function about theta_1, theta_2, theta_3 and theta_4, matlab shows this error :
Error using sym/diff (line 70)
Second argument must be a variable or a nonnegative integer specifying the number of
differentiations.
could anyone help me to solve this?
thank you
I have attached the codes below
first I run the Forward_Kinematics and the Forward_Dynamics
%% Forward_Kinematics
clc
clear all
close all
syms t L1 L2 L3 L4 L5 L6 L7
digits 15;
t5 = 0;
%%
t1 = str2sym('t1(t)');
a0 = 0;
al0 = 90;
d1 = L3;
T10 = [cos(t1) -sin(t1)*cosd(al0) sin(t1)*sind(al0) a0*cos(t1);
sin(t1) cos(t1)*cosd(al0) -cos(t1)*sind(al0) a0*sin(t1);
0 sind(al0) cosd(al0) d1;
0 0 0 1];
%%
t2 = str2sym('t2(t)');
a1 = L5;
al1 = 0;
d2 = 0;
T21 = [cos(t2) -sin(t2)*cosd(al1) sin(t2)*sind(al1) a1*cos(t2);
sin(t2) cos(t2)*cosd(al1) -cos(t2)*sind(al1) a1*sin(t2);
0 sind(al1) cosd(al1) d2;
0 0 0 1];
%%
t3 = str2sym('t3(t)');
a2 = L6;
al2 = 0;
d3 = 0;
T32 = [cos(t3) -sin(t3)*cosd(al2) sin(t3)*sind(al2) a2*cos(t3);
sin(t3) cos(t3)*cosd(al2) -cos(t3)*sind(al2) a2*sin(t3);
0 sind(al2) cosd(al2) d3;
0 0 0 1];
%%
t4 = str2sym('t4(t)');
a3 = L7;
al3 = 0;
d4 = 0;
T43 = [cos(t4) -sin(t4)*cosd(al3) sin(t4)*sind(al3) a3*cos(t4);
sin(t4) cos(t4)*cosd(al3) -cos(t4)*sind(al3) a3*sin(t4);
0 sind(al3) cosd(al3) d4;
0 0 0 1];
%%
a4 = 0;
al4 = -90;
d5 = 0;
T54 = [cos(t5) -sin(t5)*cosd(al4) sin(t5)*sind(al4) a4*cos(t5);
sin(t5) cos(t5)*cosd(al4) -cos(t5)*sind(al4) a4*sin(t5);
0 sind(al4) cosd(al4) d5;
0 0 0 1];
%%
T50 = T10*T21*T32*T43*T54;
T51 = T21*T32*T43*T54;
T52 = T32*T43*T54;
T53 = T43*T54;
A = inv(T10);
B = inv(T21);
C = inv(T32);
D = inv(T43);
E = inv(T54);
F = C*B*A;
G = F*T50;
for i = 1:3
for j = 1:3
R10(i,j) = T10(i,j);
end
end
for i = 1:3
for j = 1:3
R21(i,j) = T21(i,j);
end
end
for i = 1:3
for j = 1:3
R32(i,j) = T32(i,j);
end
end
for i = 1:3
for j = 1:3
R43(i,j) = T43(i,j);
end
end
px = T50(1,4);
py = T50(2,4);
pz = T50(3,4);
P = [px;py;pz];
save('P');
save('T10');
save('T21');
save('T32');
save('T43');
save('R10');
save('R21');
save('R32');
save('R43');
%% Forward_Dynamics
clc
clear all
close all
syms xc1 yc1 zc1 xc2 yc2 zc2 xc3 yc3 zc3 xc4 yc4 zc4 t1(t) t2 t3 t4...
Ix1 Iy1 Iz1 Ix2 Iy2 Iz2 Ix3 Iy3 Iz3 Ix4 Iy4 Iz4 m0 m1 m2 m3 m4 g
load('T10'); load('T21'); load('T32'); load('T43');
load('R10'); load('R21'); load('R32'); load('R43');
%%
r11 = [xc1;yc1;zc1;1];
r10 = T10*r11;
r10 = r10(1:3,:);
d11 = [0;0;0;1];
d10 = T10*d11;
d10 = d10(1:3,:);
%%
r22 = [xc2;yc2;zc2;1];
r20 = T10*T21*r22;
r20 = r20(1:3,:);
d22 = [0;0;0;1];
d20 = T10*T21*d22;
d20 = d20(1:3,:);
%%
r33 = [xc3;yc3;zc3;1];
r30 = T10*T21*T32*r33;
r30 = r30(1:3,:);
d33 = [0;0;0;1];
d30 = T10*T21*T32*d33;
d30 = d30(1:3,:);
%%
r44 = [xc4;yc4;zc4;1];
r40 = T10*T21*T32*T43*r44;
r40 = r40(1:3,:);
d44 = [0;0;0;1];
d40 = T10*T21*T32*T43*d44;
d40 = d40(1:3,:);
%% Angular Velocity of Links
w10 = [0;0;diff(t1)];
w21 = [0;0;diff(t2)];
w32 = [0;0;diff(t3)];
w43 = [0;0;diff(t4)];
w20 = w10 + R10*w21;
w30 = w20 + R10*R21*w32;
w40 = w30 + R10*R21*R32*w43;
%% Moments of Inertia of Links about Base Frame
I11 = [Ix1,0,0;0,Iy1,0;0,0,Iz1];
I22 = [Ix2,0,0;0,Iy2,0;0,0,Iz2];
I33 = [Ix3,0,0;0,Iy3,0;0,0,Iz3];
I44 = [Ix4,0,0;0,Iy4,0;0,0,Iz4];
I10 = R10*I11;
I20 = R10*R21*I22;
I30 = R10*R21*R32*I33;
I40 = R10*R21*R32*R43*I44;
%% The Velocity of Mass Centers and Joints
v10 = diff(r10);
v20 = diff(r20);
v30 = diff(r30);
v40 = diff(r40);
d_d10 = diff(d10);
d_d20 = diff(d20);
d_d30 = diff(d30);
d_d40 = diff(d40);
%% Kinetic Energy of The Manipulator
K = [(1/2)*m1*v10.*v10] + [(1/2)*m2*v20.*v20] + [(1/2)*m3*v30.*v30] + ...
[(1/2)*m4*v40.*v40] + [(1/2)*m0*d_d30.*d_d30] + [(1/2)*I10*w10.*w10] + ...
[(1/2)*I20*w20.*w20] + [(1/2)*I30*w30.*w30] + [(1/2)*I40*w40.*w40];
%% Potential Energy of The Manipulator
U = m2*g*r20(3) + m3*g*r30(3) + m4*g*r40(3) + m0*g*d40(3);
%% Lagrangian of The Manipulator
L = K - U;
Q0 = diff(diff(L,diff(t1))) - diff(L,t1);
Q1 = diff(diff(L,diff(t2))) - diff(L,t2);
Q2 = diff(diff(L,diff(t3))) - diff(L,t3);
Q3 = diff(diff(L,diff(t4))) - diff(L,t4);
  댓글 수: 2
Rik
Rik 2020년 7월 7일
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Have a read here and here. It will greatly improve your chances of getting an answer.
Why are you using clear all and close all? And why are you saving the entire workspace with different names? Have you read the documentation for the save function?
Milad Ghafary Shirvan
Milad Ghafary Shirvan 2020년 7월 8일
thank you for your help.
I,m new in MATLAB and I didn't know the subjects that you have mentioned.
I'll consider this next time

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 7월 8일
Q0 = diff(diff(L,diff(t1))) - diff(L,t1);
diff(t1) will generally be an expression, seldom happening to be a scalar variable. You then try to differentiate L with respect to that expression. You cannot differentiate with respect to an expression in MATLAB.
Then after that you have another diff() level around that, but not specifying the name of the variable. It helps readers when you specify the name of the variable to differentiate with respect to (and you can run into subtle problems when the variable you though you were differentiating with respect to happened to vanish from the equation, in which case if you did not specify a variable, diff() will pick another variable to differentiate with respect to...)
Generally speaking, when you differentiate a function with respect to an unknown function, you are typically making a mistake in classic calculus, and you need Calculus of Variations instead.
The problem is that when you differentiate with respect to an unknown function, you do not know that what you are differentiating is independent of what you are differentiating with respect to.
However, you can hypothesize that there is an independent solution, and proceed to solve under that hypothesis, and then afterwards go back and cross-check that the functions really were independent.
To run the hypothesis, you would subs() in symbols for the function, do the derivative with respect to the symbol. For example,
syms DT1
dt1 = diff(t1);
DLT1 = diff(subs(L, dt1, DT1), DT1);
%then
dLT1 = subs(DLT1, DT1, dt1); %recover the differential form
Q0 = diff(dlT1) - diff(L,t1); %which variable is the first one with respect to??

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by