필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by