For loop in for loop
이전 댓글 표시
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
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
Walter Roberson
2021년 12월 19일
w1=[0;0;tdot1*2*pi/360];
That only has 3 elements
W1=[0;0;w1(i)];
That asks for the i'th element of the vector of length 3, which is going to fail when i becomes 4.
tdot1=k11+2*k21*t(i);
Those are all scalars, so tdot1 is a scalar. Likewise for tdot2
Tdot2=[0;0;tdot2(i)];
There you ask for the i'th element of that scalar.
You do not use tdot1 or tdot2 or tdot3 after the loop, so it is not obvious that you want to store all of the tdot* values as they are calculated. It would seem to make more sense to just use
Tdot2 = [0;0;tdot2];
That would hint that you should do the same for w1(i), just leave off the (i) indexing. But w1 and w2 and w3 are vectors of length 3: does it make sense to calculate W1=[0;0;w1]; where w1 is a vector of length 3? Are you expecting a vector of length 3 result, or are you expecting a vector of length 5 result?
Vinay Basagare
2021년 12월 19일
Vinay Basagare
2021년 12월 19일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!