Why WHILE loop doesn't advance?

function cam()
clc
clear all
close all
N_i = input('N de intervalos:');
k=1;
while(k<=N_i)
k
beta_i(k) = input('Beta do intervalo:');
L(k) = input('Lift do intervalo:');
mov(k) = input('Movimento do intervalo:');
k=k+1;
end
R_0 = input('Raio base:');
tb_i=0;
tb_f=1;
teta=0;
passo=1;
n=1;
T(n)=teta;
j=1;
while(j<=N_i)
j
switch mov(j)
case 1
while(teta<=beta_i(j))
y=0.5*L(j)*(1-cos(pi*teta/beta_i(j)));
dy=((pi*L(j))/(2*beta_i(j)))*sin(pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
A(n,:,j)=[y,dy,j]
P(n,:,j)=[u,v,j]
n=n+1;
teta=teta+passo;
end
case 2
while(teta<=beta_i(j))
y=L(j)*((teta/beta_i(j))-sin(2*pi*(teta/beta_i(j))));
dy=(L(j)/beta_i(j))*(1-cos(2*pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
A(n,:,j)=[y,dy,j]
P(n,:,j)=[u,v,j]
n=n+1;
teta=teta+passo;
end
end
j=j+1;
end
end
Why the "while(j<=N_i)" loop only computes A and P for j=1 and doesn't advance after that? Can't understand...

5 个评论

Well, for a start we have no idea what N_i is since it is a user input.
For example when N_i=2 it only computes for j=1 but not for j=2 and I wonder why.
Can you give typical numbers for all 5 user inputs, not just one, so we know what to enter and can try your code?
Try this:
N de intervalos:2
k =
1
Beta do intervalo:180
Lift do intervalo:3
Movimento do intervalo:1
k =
2
Beta do intervalo:90
Lift do intervalo:2
Movimento do intervalo:2
Raio base:0.025

请先登录,再进行评论。

 采纳的回答

Walter Roberson
Walter Roberson 2016-11-1

1 个投票

I notice that you do not initialize teta between the two iterations of the while loop. Your while test is based upon teta which is going to continue to have the value it had after the first iteration of the loop, so the end condition might already be satisfied.

更多回答(1 个)

It worked for me:
N de intervalos:2
k =
1
Beta do intervalo:3
Lift do intervalo:3
Movimento do intervalo:4
k =
2
Beta do intervalo:5
Lift do intervalo:6
Movimento do intervalo:7
Raio base:0
j =
1
j =
2

3 个评论

Try with this inputs instead:
N de intervalos:2
k =
1
Beta do intervalo:180
Lift do intervalo:3
Movimento do intervalo:1
k =
2
Beta do intervalo:90
Lift do intervalo:2
Movimento do intervalo:2
Raio base:0.025
That also does both j. Look:
N de intervalos:2
k =
1
Beta do intervalo:180
Lift do intervalo:3
Movimento do intervalo:1
k =
2
Beta do intervalo:90
Lift do intervalo:2
Movimento do intervalo:2
Raio base:0.025
j =
1
j =
2
It doesn't compute vectors A and P for j=2...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by