For loop in for loop

2 views (last 30 days)
Vinay Basagare
Vinay Basagare on 19 Dec 2021
Commented: Vinay Basagare on 19 Dec 2021
k11=28.21;
k21=-4.23;
k12=-18.133;
k22=2.59;
k13=20.815;
k23=-2.97;
t=0:0.1:3;
num_t=length(t);
t4=0;
t3=-0.3588;
t2=0.2433;
t1=-0.81;
R21 = [cos(t2) -sin(t2) 0;
sin(t2) cos(t2) 0;
0 0 1];
R32=[cos(t3) -sin(t3) 0;
sin(t3) cos(t3) 0;
0 0 1];
D4=[0 0 0.450+0.092];
D3=[0.440 0 0];
D2=[0; 0.33; 0];
D1=[0.05; 0; 0];
t = 0:.1:3;
num_t = length(t);
for i=1:num_t
tdot1=k11+2*k21*t(i);
tdot2=k12+2*k22*t(i);
tdot3=k13+2*k23*t(i);
t2dot1=2*k21;
t2dot2=2*k22;
t2dot3=2*k23;
w1=[0;0;tdot1*2*pi/360];
w2=R21*w1+[0;0;tdot2*2*pi/360];
w3=R32*w2+[0;0;tdot3*2*pi/360];
uW1(i)=w1(3,1);
uW2(i)=w2(3,1);
uW3(i)=w3(3,1);
v2=R21*cross(w1,D1);
v3=R32*(v2+cross(w2,D2));
uV2(i)=norm(v2);
uV3(i)=norm(v3);
W1=[0;0;w1(i)];
alfa1=t2dot1;
Tdot2=[0;0;tdot2(i)];
alfa2=t2dot2+R21*alfa1+R21*cross(W1,Tdot2);
W2=[0;0;w2(i)];
Tdot3=[0;0;tdot3(i)];
alfa3=t2dot3+R32*alfa2+R32*cross(W2,Tdot3);
ua2(i)=alfa2(3,1);
ua3(i)=alfa3(3,1);
end
Index exceeds the number of array elements. Index must not exceed 1.
hold on
figure(1)
plot(t,uW1,'-*')
xlabel('time[s]');
ylabel('w1[rad/s]');
figure(2)
plot(t,uW2,'*')
xlabel('time[s]');
ylabel('w2[rad/s]');
figure(3)
plot(t,uW3,'*')
xlabel('time[s]');
ylabel('w3[rad/s]');
figure(4)
plot(t,uV2,'.')
xlabel('time[s]');
ylabel('v2[m/s]');
figure(5)
plot(t,uV3,'.')
xlabel('time[s]');
ylabel('v3[m/s]');
hold on
figure(1)
plot(t,ua2,'*')
xlabel('time[s]');
ylabel('alfa2[rad/s^2]');
figure(2)
plot(t,ua3,'*')
xlabel('time[s]');
ylabel('alfa3[rad/s^2]');
Should I be using another For loop for the plot of Alpha vs time? I am unable to understand how can I get rid of the error at line 48.Please help me. Thanks in advance.
  3 Comments
Vinay Basagare
Vinay Basagare on 19 Dec 2021
I understood and I am expecting a vector of length 3.
Vinay Basagare
Vinay Basagare on 19 Dec 2021
I just want to take it as the first element of w(i) should be taken when the first element of tdot1 is taken and thats why i thought to use it that way. but, i got your point. Thanks for the whole explaination. I really apprecaite it.

Sign in to comment.

Accepted Answer

VBBV
VBBV on 19 Dec 2021
k11=28.21;
k21=-4.23;
k12=-18.133;
k22=2.59;
k13=20.815;
k23=-2.97;
t=0:0.1:3;
num_t=length(t);
t4=0;
t3=-0.3588;
t2=0.2433;
t1=-0.81;
R21 = [cos(t2) -sin(t2) 0;
sin(t2) cos(t2) 0;
0 0 1];
R32=[cos(t3) -sin(t3) 0;
sin(t3) cos(t3) 0;
0 0 1];
D4=[0 0 0.450+0.092];
D3=[0.440 0 0];
D2=[0; 0.33; 0];
D1=[0.05; 0; 0];
t = 0:.1:3;
num_t = length(t);
for i=1:num_t
tdot1=k11+2*k21*t(i);
tdot2=k12+2*k22*t(i);
tdot3=k13+2*k23*t(i);
t2dot1=2*k21;
t2dot2=2*k22;
t2dot3=2*k23;
w1=[0;0;tdot1*2*pi/360];
w2=R21*w1+[0;0;tdot2*2*pi/360];
w3=R32*w2+[0;0;tdot3*2*pi/360];
uW1(i)=w1(3,1);
uW2(i)=w2(3,1);
uW3(i)=w3(3,1);
v2=R21*cross(w1,D1)
v3=R32*(v2+cross(w2,D2))
uV2(:,i)=norm(v2)
uV3(:,i)=norm(v3)
W1=[0;0;w1];
alfa1=t2dot1;
Tdot2=[0;0;tdot2];
alfa2=t2dot2+R21*alfa1+R21*cross(w1,Tdot2);
W2=[0;0;w2];
Tdot3=[0;0;tdot3];
alfa3=t2dot3+R32*alfa2+R32*cross(w2,Tdot3);
ua2(i)=alfa2(3,1)
ua3(i)=alfa3(3,1)
end;
figure(1)
plot(t,uW1,'-*')
xlabel('time[s]');
ylabel('w1[rad/s]');
figure(2)
plot(t,uW2,'*')
xlabel('time[s]');
ylabel('w2[rad/s]');
figure(3)
plot(t,uW3,'*')
xlabel('time[s]');
ylabel('w3[rad/s]');
figure(4)
plot(t,uV2,'.')
xlabel('time[s]');
ylabel('v2[m/s]');
figure(5)
plot(t,uV3,'.')
xlabel('time[s]');
ylabel('v3[m/s]');
figure(6)
plot(t,ua2,'*')
xlabel('time[s]');
ylabel('alfa2[rad/s^2]');
figure(7)
plot(t,ua3,'*')
xlabel('time[s]');
ylabel('alfa3[rad/s^2]');
You might not need additional for loop
  1 Comment
Vinay Basagare
Vinay Basagare on 19 Dec 2021
Thanks a lot for the answer and your time.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!