How to get new double array from sym array

2 views (last 30 days)
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 Comments
동욱
동욱 on 19 Oct 2023
Edited: 동욱 on 19 Oct 2023
As i mentioned below, i just wanted to get 500 point of P1_0. What can i do for that?
Dyuman Joshi
Dyuman Joshi on 19 Oct 2023
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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2023
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 Comments
Walter Roberson
Walter Roberson on 19 Oct 2023
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
Walter Roberson on 19 Oct 2023
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

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 19 Oct 2023
Edited: the cyclist on 19 Oct 2023
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
Elapsed time is 36.606546 seconds.
n
n = 754509006

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!