# My for loop is running only once

2 views (last 30 days)
Jason Sung on 24 Oct 2020
Commented: Jason Sung on 25 Oct 2020
Hello all! I'm an engineering student and I'm trying to build a for loop that gives me matrix calculations for every single index value defined in its range i.e. 0 to 360.
However, when I run my code, it's only giving me the resulatant matrix for one index value, which is at 360, rather than 361 matrices. I've set it in diefferent ranges and done anything I could think of, but, I couldn't figure it out. I'd appreciate for any help or advice on it. Thanks! Below is my code I've been working on so far:
for theta2 = 0:360 %for theta2 = 0:1:360, theta2 be the index.
%Given:
% Lengths (m)
a = 0.4;
b = 5;
c = 1.2;
%Distance from A to G3:
px = (b*sind(71.7))-1.5; %C to P = 1.5 in x.
py = 0.5;
p = sqrt((px^2)+(py^2));
%36; %theta2 varies from 0 to 360 deg. Randomly selected initial entry (36)
%delta3 (deg):
ac = b*sind(71.7);
bac = acosd(ac/b);
pac = atand(py/px);
delta3 = bac- pac;
w2 = -10; %CW, Constant velocity
alpha2 = 0; %w2 is constant velocity, no acceleration.
% Position analysis: Ch. 4
theta3 = asind((a.*sind(theta2)-c)./b) %180 deg = pi
d = a.*cosd(theta2)-b.*cosd(theta3)
% Velocity analysis=s: Ch. 6
w3 = ((a.*cosd(theta2))./(b.*cosd(theta3))).*w2
ddot = -a.*w2.*sind(theta2)+b.*w3.*sind(theta3) %vB = ddot, slip velocity
% Acceleration Analysis: Ch. 7
% wn^2 terms are for normal accelerations, alphan terms are for tangential
%accelerations.
alpha3 = (a.*alpha2.*cosd(theta2)-a.*(w2.^2).*sind(theta2)+b.*(w3.^2).*sind(theta3))./(b.*cosd(theta3))
dddot = -a.*alpha2.*sind(theta2)-a.*(w2.^2).*cosd(theta2)+b.*alpha3.*sind(theta3)+b.*(w3.^2).*cosd(theta3)
%Mass (kg):
m2 = 0.3;
m3 = 5;
m4 = 0.2;
a2x = -(0.5).*a.*(w2.^2).*cosd(theta2)-(0.5).*a.*alpha2.*sind(theta2);
a2y = -(0.5).*a.*(w2.^2).*sind(theta2)+(0.5).*a.*alpha2.*cosd(theta2);
a3x = -a.*(w2.^2).*cosd(theta2)-a.*alpha2.*sind(theta2)-p.*(w3.^2).*cosd(theta3-180+delta3)-p.*alpha3.*sind(theta3-180+delta3); %Q
a3y = -a.*(w2.^2).*sind(theta2)+a.*alpha2.*cosd(theta2)-p.*(w3.^2).*sind(theta3-180+delta3)+p.*alpha3.*cosd(theta3-180+delta3); %Q
a4x = dddot;
a4y = 0;
g = 9.8; %Q + or -?
%Mass moment of Inertia (kg-m^2):
I2 = 0;
I3 = 15;
%Spring Constant (N/m):
k = 500; %Fs = kd, Q + or - or 0?
dis = d-(-5.2649); % min d = -5.2649
%Position Vectors:
R12x = 0.5.*a.*cosd(theta2+180); %0.5 for CG2 being at the centre of link 2, the rectangular bar link.
R12y = 0.5.*a.*sind(theta2+180);
R32x = 0.5.*a.*cosd(theta2);
R32y = 0.5.*a.*sind(theta2);
R23x = p.*cosd(360+theta3+delta3);
R23y = p.*sind(360+theta3+delta3);
BC = b.*cosd(71.7);
PBx = 1.5;
PBy = BC-py;
thetaBC =180-atand(PBy./PBx)-90;
deltaPB = 90+((71.7-thetaBC) + (-(theta3)));
PB = sqrt((PBy.^2)+(PBx.^2));
R43x = PB.*cosd(270-deltaPB);
R43y = PB.*sind(270-deltaPB);
R3 = sqrt((py.^2)+((1.5).^2));
thetaR3 = 205.609; %x axis to R3 => 205.609 deg.
R3x = R3.*cosd(thetaR3);
R3y = R3.*sind(thetaR3);
%Forces (N):
F3x = 200.*cosd(315);
F3y = 200.*sind(315);
%Force in y direction still exists!
mu = 0; %Smooth surface => zero friction.
%Matrix Form:
A = [1 0 1 0 0 0 0 0;
0 1 0 1 0 0 0 0;
-R12y R12x -R32y R32x 0 0 0 1;
0 0 -1 0 1 0 0 0;
0 0 0 -1 0 1 0 0;
0 0 R23y -R23x -R43y R43x 0 0;
0 0 0 0 -1 0 mu 0;
0 0 0 0 0 -1 1 0];
C = [m2.*a2x + m2.*g;
m2.*a2y;
I2.*alpha2;
m3.*a3x + -F3x + m3.*g;
-F3y + m3.*a3y;
I3.*alpha3 + -R3x.*F3y + R3y.*F3x;
m4.*a4x + k.*dis + m4.*g;
m4.*a4y];
B = inv(A)*C;
F12x = B(1,1);
F12y = B(2,1);
F32x = B(3,1);
F32y = B(4,1);
F43x = B(5,1);
F43y = B(6,1);
F14y = B(7,1);
T12 = B(8,1);
F12 = sqrt((F12x.^2)+(F12y.^2))
F32 = sqrt((F32x.^2)+(F32y.^2))
F43 = sqrt((F43x.^2)+(F43y.^2))
F14 = F14y
T12 = T12
end

per isakson on 25 Oct 2020
"My for loop is running only once" Your loop is running 361 times, but the results of each run overwrites the results of the previous run.
Which is your "the resulatant matrix" ?
This is a relevant example from the documentation
x = zeros(1, 1000000);
for k = 2:1000000
x(k) = x(k-1) + 5;
end

#### 1 Comment

Jason Sung on 25 Oct 2020
Thank you for the explanation!