How to get new double array from sym array
2 次查看(过去 30 天)
显示 更早的评论
I'm doing assigenment for Foward kinematics. But the loop doesn't stop. What do i have to do? The code is below. Thank you for your help.
syms a1 a2 a4 a5;
d1=100;d2=250;d3=50;d4=250;d5=100;
% define transformation matrix
T_01=[sin(a1) 0 cos(a1) 0; -cos(a1) 0 sin(a1) 0; 0 -1 0 d1; 0 0 0 1];
T_12=[cos(a2) 0 sin(a2) d2*cos(a2); sin(a2) 0 -cos(a2) d2*sin(a2); 0 1 0 0; 0 0 0 1];
T_23=[-1 0 0 0; 0 0 1 0; 0 1 0 d3; 0 0 0 1];
T_34=[cos(a4) -sin(a4) 0 d4*cos(a4); sin(a4) cos(a4) 0 d4*sin(a4); 0 0 1 0; 0 0 0 1];
T_4H=[cos(a5) -sin(a5) 0 d5*cos(a5); sin(a5) cos(a5) 0 d5*sin(a5); 0 0 1 0; 0 0 0 1];
% Calculate center of E.E. frame
T_0H=T_01*T_12*T_23*T_34*T_4H;
P = transpose([0 0 0 1]);
P1_0=T_0H*P;
% Get array of center point
for a1 = 0:pi/1000:pi/2
for a2 = 0:-pi/500:-pi
for a4 = 0:pi/1000:pi/2
for a5 = 0:-pi/1000:-pi/2
P1_0;
end
end
end
end
3 个评论
Dyuman Joshi
2023-10-19
If you mean from the points from the for loops, then they are not 500 points, they are ~63 billion points together.
And as Walter has mentioned in his answer - you might not have enough storage for that amount of data.
采纳的回答
Walter Roberson
2023-10-19
syms a1 a2 a4 a5;
d1=100;d2=250;d3=50;d4=250;d5=100;
% define transformation matrix
T_01=[sin(a1) 0 cos(a1) 0; -cos(a1) 0 sin(a1) 0; 0 -1 0 d1; 0 0 0 1];
T_12=[cos(a2) 0 sin(a2) d2*cos(a2); sin(a2) 0 -cos(a2) d2*sin(a2); 0 1 0 0; 0 0 0 1];
T_23=[-1 0 0 0; 0 0 1 0; 0 1 0 d3; 0 0 0 1];
T_34=[cos(a4) -sin(a4) 0 d4*cos(a4); sin(a4) cos(a4) 0 d4*sin(a4); 0 0 1 0; 0 0 0 1];
T_4H=[cos(a5) -sin(a5) 0 d5*cos(a5); sin(a5) cos(a5) 0 d5*sin(a5); 0 0 1 0; 0 0 0 1];
% Calculate center of E.E. frame
T_0H=T_01*T_12*T_23*T_34*T_4H;
P = transpose([0 0 0 1]);
P1_0=T_0H*P;
% Get array of center point
for a1 = 0:pi/1000:pi/2
for a2 = 0:-pi/500:-pi
for a4 = 0:pi/1000:pi/2
for a5 = 0:-pi/1000:-pi/2
this_entry = double(subs(P1_0))
end
end
end
end
You have 500*500*500*500 loop iterations so this could would evaluate the 4 x 1 symbolic array 6 1/4 billion times, creating the 4 x 1 double scalar this_entry each time and then discarding it. The end result would be that this_array would be what was appropriate for the last iteration of each loop -- a point near (pi/2, -pi, pi/2, -pi/2)
If you were to recode to save all of the outputs, it would require 500*500*500*500*4 entries each 8 bytes, for a total of 2 terabytes of storage in a single array. You probably do not have that much ram.
3 个评论
Walter Roberson
2023-10-19
Are you wanting to get one output for [0, 0, 0, 0], a second output for [pi/1000, -pi/500, pi/1000, -pi/1000], a third for 2*[pi/1000, -pi/500, pi/1000, -pi/1000], a third for 3*[pi/1000, -pi/500, pi/1000, -pi/1000] and so on? Each element in combination only with the "corresponding" element in the other vectors?
Or are you saying that you want to run through all combinations of values for a1, a2, a4, creating a 4 x 500 array of values like A1(J), A2(K), A4(L), but all all a5 values in one go? And you will do something with that 4 x 500 array and then will move on to the next combination of a1, a2, a4 values? So 500 * 500 * 500 times you would generate a 4 x 500 array that you would do something with?
Walter Roberson
2023-10-19
syms a1 a2 a4 a5;
d1=100;d2=250;d3=50;d4=250;d5=100;
% define transformation matrix
T_01=[sin(a1) 0 cos(a1) 0; -cos(a1) 0 sin(a1) 0; 0 -1 0 d1; 0 0 0 1];
T_12=[cos(a2) 0 sin(a2) d2*cos(a2); sin(a2) 0 -cos(a2) d2*sin(a2); 0 1 0 0; 0 0 0 1];
T_23=[-1 0 0 0; 0 0 1 0; 0 1 0 d3; 0 0 0 1];
T_34=[cos(a4) -sin(a4) 0 d4*cos(a4); sin(a4) cos(a4) 0 d4*sin(a4); 0 0 1 0; 0 0 0 1];
T_4H=[cos(a5) -sin(a5) 0 d5*cos(a5); sin(a5) cos(a5) 0 d5*sin(a5); 0 0 1 0; 0 0 0 1];
% Calculate center of E.E. frame
T_0H=T_01*T_12*T_23*T_34*T_4H;
P = transpose([0 0 0 1]);
P1_0=T_0H*P;
a5 = 0:-pi/1000:-pi/2;
% Get array of center point
for a1 = 0:pi/1000:pi/2
for a2 = 0:-pi/500:-pi
for a4 = 0:pi/1000:pi/2
%at this point a1, a2, a4 are scalars, a5 is a 1 x 500 vector
%and P1_0 is a 4 x 1 symbolic vector. The result will be 4 x 500
these_entries = double(subs(P1_0));
%now do something with the 4 x 500 array these_entries
end
end
end
更多回答(1 个)
the cyclist
2023-10-19
编辑:the cyclist
2023-10-19
I expect it does stop, eventually. In the code below, I changed the loop over a5, so that it executes only 1/100th as many iterations, and the code took 36 seconds. I expect your code would have taken about one hour to complete. Did you wait that long?
Note that I did not spend any time looking at whether your code is sensible or not.
syms a1 a2 a4 a5;
d1=100;d2=250;d3=50;d4=250;d5=100;
% define transformation matrix
T_01=[sin(a1) 0 cos(a1) 0; -cos(a1) 0 sin(a1) 0; 0 -1 0 d1; 0 0 0 1];
T_12=[cos(a2) 0 sin(a2) d2*cos(a2); sin(a2) 0 -cos(a2) d2*sin(a2); 0 1 0 0; 0 0 0 1];
T_23=[-1 0 0 0; 0 0 1 0; 0 1 0 d3; 0 0 0 1];
T_34=[cos(a4) -sin(a4) 0 d4*cos(a4); sin(a4) cos(a4) 0 d4*sin(a4); 0 0 1 0; 0 0 0 1];
T_4H=[cos(a5) -sin(a5) 0 d5*cos(a5); sin(a5) cos(a5) 0 d5*sin(a5); 0 0 1 0; 0 0 0 1];
% Calculate center of E.E. frame
T_0H=T_01*T_12*T_23*T_34*T_4H;
P = transpose([0 0 0 1]);
P1_0=T_0H*P;
tic
n = 0;
% Get array of center point
for a1 = 0:pi/1000:pi/2
for a2 = 0:-pi/500:-pi
for a4 = 0:pi/1000:pi/2
for a5 = 0:-pi/10:-pi/2
P1_0;
n = n+1;
end
end
end
end
toc
n
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!