For loop in for loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
VBBV
le 19 Déc 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!