# Possibilities of using for end loop in code

조회 수: 1 (최근 30일)
Zahra . 2023년 8월 2일
답변: Chunru . 2023년 8월 2일
I want to build an articulated pendulum, let say up to 15 pendulums. Here, I define the first two pendulums as:
x1 = (r1/2)*cos(theta1);
y1 =(r1/2)*sin(theta1);
x2 = (r2/2)*cos(theta2)+ r1*cos(theta1);
y2 = (r2/2)*sin(theta2) + r1*sin(theta1);
y=[x1 0 y1 0 theta1 0 x2 0 y2 0 theta2 0];
but I'm not sure how to write it by using for end loop so that I could build it until 15 pendulums. Also, should there is another way to do it, I would highly appreciated to learn about that. Thanks in advance.
##### 댓글 수: 1없음 표시없음 숨기기
Dyuman Joshi 2023년 8월 2일
What are the values of theta1, theta2, .....?
And what is the formula/relation for (xn,yn)?
A better idea will be to put data for each pendulum in a row, so that is easy to access -
out=[x1 0 y1 0 theta1 0;
x2 0 y2 0 theta2 0;
...
xn 0 yn 0 thetan 0];

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Chunru 2023년 8월 2일
In MATLAB, you can do it without loop (preferred approach):
theta = [0 10 20 30 50]';
r = [9 8 7 6 3]';
y = [(r/2).*cos(theta), (r/2).*sin(theta), zeros(size(theta))]
y = 5×3
4.5000 0 0 -3.3563 -2.1761 0 1.4283 3.1953 0 0.4628 -2.9641 0 1.4474 -0.3936 0

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!