Info
This question is closed. Reopen it to edit or answer.
diary() output problem with large analytical expression
1 view (last 30 days)
Show older comments
Context:
The following scipt is used to generate an analytical expression that will be placed in a matlabFunction block for use in a Simulink simulation. Attempting to generate the expression within Simulink was unsuccessful so this is the best method that could be found for using large analytical functions.
Expectation:
The scipt generates an analytical expression with the variable(s) qn(t) to represent position vector in cartesian coordinates. Then the first and second derivatives are taken using the function diff(). The diary() function writes the resulting analytical expressions from diff() to a series of *.txt files that are then ammended to matlabFunctions within Simulink manually. The diary files are expected to be one row of the derivatives per file. When viewing the files in Notepad, the text should be represented as "1 line" in the lower right corner.
Actual:
The derivatives execute using the diff() command and the diary files are generated. The first derivative (v_CM) is produced as 1 line per *.txt file for the three output files. The second derivative (a_CM) produces 4 lines per file for the three output files. I am unsure why more than 1 line is being produced. The lines are not continuations of the analytical expression. I know this because there are no mathematical operataors between the line segments in the output.
Question:
How do I get the complete analytical expression for "a_CM" correctly represented from the diary output?
tic
clc
clear
syms t q1(t) q2(t) q3(t) q4(t) q5(t) q6(t) q7(t) real
P_CM = [[-0.00312696 0.00015337 0.03467746 1];[-0.06018364 0.02006104 -0.00363283 1];[0.00001092 0.04270058 0.02340470 1];[0.00000000 0.12253790 0.00106375 1];[0.00003873 0.03407722 0.02337290 1];[-0.00024936 0.13895161 -0.00005063 1];[-0.0012889 0.02169988 0.01899712 1];[-0.01011566 -0.00000243 -0.03315674 1]];
m = [1.56412565 5.93894617 3.20983758 2.10160170 1.87815030 1.62635986 1.21239939 0.38325487];
n = 7;
% symbolic variables
q = [q1(t) q2(t) q3(t) q4(t) q5(t) q6(t) q7(t)];
x = [-pi/2 -pi/2 -pi/2 -pi/2 -pi/2 -pi/2 0]; % change in angle ... (rad)
d = [0.317 0.1925 0.4 0.1685 0.4 0.1363 0.13375]; %primary displacement (actual) in (m)
a = [0.081 0 0 0 0 0 0]; %secondary displacement (actual) in (m)
for i = 1:7
Z = [cos(q(i)) -sin(q(i)) 0 0; sin(q(i)) cos(q(i)) 0 0; 0 0 1 0; 0 0 0 1]; % rotation about z-axis
X = [1 0 0 0; 0 cos(x(i)) -sin(x(i)) 0;0 sin(x(i)) cos(x(i)) 0; 0 0 0 1]; % rotation about x-axis
A = [1 0 0 a(i); 0 1 0 0 ; 0 0 1 0; 0 0 0 1]; % secondary displacement does not occur along the y-axis so the y-axis value is set to 0
D = [1 0 0 0; 0 1 0 0; 0 0 1 d(i); 0 0 0 1]; % displacement along z-axis
Q{i} = Z*D*A*X; % each links transformation is stored in a cell array, 1-7
end
T_01 = Q{1};
T_02 = Q{1}*Q{2};
T_03 = Q{1}*Q{2}*Q{3};
T_04 = Q{1}*Q{2}*Q{3}*Q{4};
T_05 = Q{1}*Q{2}*Q{3}*Q{4}*Q{5};
T_06 = Q{1}*Q{2}*Q{3}*Q{4}*Q{5}*Q{6};
T_07 = Q{1}*Q{2}*Q{3}*Q{4}*Q{5}*Q{6}*Q{7};
T_c = {T_01 T_02 T_03 T_04 T_05 T_06 T_07};
P_CM = sym(P_CM);
for i = 1:7
COM(1:4,1) = P_CM(1)';
COM(1:4,i+1) = T_c{i}*P_CM(i+1,:)';
end
COM = COM(1:3,:);
X_COM = (m(1)*COM(1,1)+m(2)*COM(1,2)+m(3)*COM(1,3)+m(4)*COM(1,4)+m(5)*COM(1,5)+m(6)*COM(1,6)+m(7)*COM(1,7)+m(8)*COM(1,8))/sum(m);
Y_COM = (m(1)*COM(2,1)+m(2)*COM(2,2)+m(3)*COM(2,3)+m(4)*COM(2,4)+m(5)*COM(2,5)+m(6)*COM(2,6)+m(7)*COM(2,7)+m(8)*COM(2,8))/sum(m);
Z_COM = (m(1)*COM(3,1)+m(2)*COM(3,2)+m(3)*COM(3,3)+m(4)*COM(3,4)+m(5)*COM(3,5)+m(6)*COM(3,6)+m(7)*COM(3,7)+m(8)*COM(3,8))/sum(m);
p_CM = [X_COM; Y_COM; Z_COM];
for i = 1:1
name = sprintf('v%d.txt',i);
diary (name)
v_CM(i,1) = diff(p_CM(i,1))
diary off
clc
name = sprintf('a%d.txt',i);
diary (name)
a_CM(i,1) = diff(v_CM(i,1))
diary off
clc
end
%%
toc
0 Comments
Answers (0)
This question is closed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!