How to add calculated column to existing matrix in for loop
4 views (last 30 days)
Show older comments
Hey all, I have this code here that calculates out a 5x1 matrix. I have a number of inputs. The most important ones are theta2, theta3, theat4, theta5. I would like these to iterate through all the t values from 0 to 5. For every iteration, I would like the newly calculated 5x1 matrix to add itself onto the first matrix. So after 5 iterations, my 5x1 matrix should be a 5x6 matrix all filled with the calculations from each iteration. How do I do this? I've been racking my brain to figure this out and I don't know what to do. Please help and thank you so much!
clear; clc;
r2 = 40/1000;
r3 = 120/1000;
r4 = 70/1000;
r5 = 60/1000;
%t = linspace(0,5,500);
t = (0:1:5); % 50 steps
N = length(t);
for i = 1:N
theta2 = 20*cosd(9.425*(t-0.52))+57.029;
theta3 = 20.675*cosd(9.397*(t-0.60))-68.768;
theta4 = 18.517*cosd(9.425*(t-0.52))+57.029;
theta5 = 20.682*cosd(9.425*(t-0.68))-68.767;
phic = theta5 - theta3;
Finx = -250;
Finy = -433.013;
C = Finx * r5 * sind(theta3 + phic) - Finy * r5 * cosd(theta3 + phic);
mat1 = zeros(5);
mat1 = [0 0 -r4*sind(theta4) r4*cosd(theta4) 0;
1 0 -1 0 0;
0 1 0 -1 0;
0 0 r3*sind(theta3) -r3*cosd(theta3) 0;
r2*sind(theta2) -r2*cosd(theta2) 0 0 1;];
mat3 = [0; -Finx; -Finy; C; 0;];
% mat2 = [F23x; F23y; F34x; F34y; T;]; % This each variable in mat2 is
mat2 = mat1 \ mat3;
end
0 Comments
Accepted Answer
Walter Roberson
on 10 Dec 2023
Edited: Walter Roberson
on 10 Dec 2023
r2 = 40/1000;
r3 = 120/1000;
r4 = 70/1000;
r5 = 60/1000;
%t = linspace(0,5,500);
t = (0:1:5); % 50 steps
N = length(t);
for i = 1:N
theta2 = 20*cosd(9.425*(t(i)-0.52))+57.029;
theta3 = 20.675*cosd(9.397*(t(i)-0.60))-68.768;
theta4 = 18.517*cosd(9.425*(t(i)-0.52))+57.029;
theta5 = 20.682*cosd(9.425*(t(i)-0.68))-68.767;
phic = theta5 - theta3;
Finx = -250;
Finy = -433.013;
C = Finx * r5 * sind(theta3 + phic) - Finy * r5 * cosd(theta3 + phic);
mat1 = zeros(5);
mat1 = [0 0 -r4*sind(theta4) r4*cosd(theta4) 0;
1 0 -1 0 0;
0 1 0 -1 0;
0 0 r3*sind(theta3) -r3*cosd(theta3) 0;
r2*sind(theta2) -r2*cosd(theta2) 0 0 1;];
mat3 = [0; -Finx; -Finy; C; 0;];
% mat2 = [F23x; F23y; F34x; F34y; T;]; % This each variable in mat2 is
mat2(:,i) = mat1 \ mat3;
end
mat2
plot(t, mat2)
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!