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

1 回表示 (過去 30 日間)
Allison Bushman
Allison Bushman 2018 年 12 月 5 日
編集済み: Stephen23 2018 年 12 月 6 日
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

採用された回答

Stephen23
Stephen23 2018 年 12 月 5 日
...
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 件のコメント
Allison Bushman
Allison Bushman 2018 年 12 月 5 日
Thank you! That worked perfectly.
Stephen23
Stephen23 2018 年 12 月 6 日
編集済み: Stephen23 2018 年 12 月 6 日
@Allison Bushman: then please accept my answer!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by