Clear Filters
Clear Filters

How to add calculated column to existing matrix in for loop

4 views (last 30 days)
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
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Accepted Answer

Walter Roberson
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
mat2 = 5×6
178.4505 178.4954 176.3301 172.0828 166.0154 158.5185 156.7834 156.7579 158.0741 160.8101 165.1170 171.1783 -71.5495 -71.5046 -73.6699 -77.9172 -83.9846 -91.4815 -276.2296 -276.2551 -274.9389 -272.2029 -267.8960 -261.8347 -5.5384 -5.5418 -5.3740 -5.0379 -4.5401 -3.8908
plot(t, mat2)

More Answers (0)

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!