Effacer les filtres
Effacer les filtres

How do I create a for loop that can use multiple changing variables?

1 vue (au cours des 30 derniers jours)
Allison Bushman
Allison Bushman le 5 Déc 2018
Modifié(e) : Stephen23 le 6 Déc 2018
I am trying to create a for loop in which multiple circles move at the same time; however, each circle has a specific angle that it needs to move. How do I create a for loop to match this movement? I have already tried nested for loops and that did not provided that wanted response. Thank you.
P1=[-15,0];
P2=[-5,0];
plot([P1(1) P2(1)],[P1(2) P2(2)],'LineWidth',5,'Color','black');
A=[0,0];
circle=viscircles(A,5,'LineWidth',2,'Color','black');
axis equal
axis off
for b=0:pi/80:pi/8,c=0:pi/40:pi/4,d=0:3*pi/80:3*pi/8,e=0:pi/20:pi/2
pause(.5)
B=A+[10*cos((-pi/8)+b), 10*sin((-pi/8)+b)];
circle2=viscircles(B,5,'LineWidth',2,'Color','green');
C=B+[10*cos(-(pi/4)+c), 10*sin(-(pi/4)+c)];
circle3=viscircles(C,5,'LineWidth',2,'Color','blue');
D=C+[10*cos(-(3*pi/8)+d), 10*sin(-(3*pi/8)+d)];
circle4=viscircles(D,5,'LineWidth',2,'Color','red');
E=D+[10*cos(-(pi/2)+e), 10*sin(-(pi/2)+e)];
circle5=viscircles(E,5,'LineWidth',2,'Color','yellow');
end
axis equal
axis off

Réponse acceptée

Stephen23
Stephen23 le 5 Déc 2018
...
b = 0:pi/80:pi/8;
c = 0:pi/40:pi/4;
d = 0:3*pi/80:3*pi/8;
e = 0:pi/20:pi/2;
for k = 1:numel(b)
pause(0.5)
B = A+[10*cos(b(k)-pi/8),10*sin(b(k)-pi/8)];
viscircles(B,5,'LineWidth',2,'Color','green');
C = B+[10*cos(c(k)-pi/4),10*sin(c(k)-pi/4)];
viscircles(C,5,'LineWidth',2,'Color','blue');
D = C+[10*cos(c(k)-3*pi/8),10*sin(d(k)-3*pi/8)];
viscircles(D,5,'LineWidth',2,'Color','red');
E = D+[10*cos(e(k)-pi/2),10*sin(e(k)-pi/2)];
viscircles(E,5,'LineWidth',2,'Color','yellow');
end
  2 commentaires
Allison Bushman
Allison Bushman le 5 Déc 2018
Thank you! That worked perfectly.
Stephen23
Stephen23 le 6 Déc 2018
Modifié(e) : Stephen23 le 6 Déc 2018
@Allison Bushman: then please accept my answer!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB 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!

Translated by