How to create an infinite loop until condition is met?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Using specifically the "FOR" command. Not the "While." I must use iteration.
To allow you understand...I have a program that creates a trajectory and using a separate function builds up several vectors (t,x,y,vx,vy...). These become lists as the function returns values such as 'tn' that i add. It breaks when the projectile hits the ground. An example of my code:
t(1)=0
for B = 1:length(t)
%MY FUNCTION GOES HERE AND TAKES 't(B)' AND RETURNS 'tn' for example and an yn(height) position.
t = [t tn]
if yn <= 0 break end
I thought this would keep going until it returned a value that hits the ground. But it only does the first two values. When i switched for B = 1:1000....it returned all values
But B = 1:1000 is technically not true...since...a projectile with enough speed will surpass these many iterations in t,x,y values. I thought that since length(t) is BUILDING up...the loop keeps going on.
B = 1: length(t) apparently doesn't change when used to describe the range vector for the "FOR" function..
MY QUESTION: is there any way you see to use the fact the vectors are building up...to advance one by one.
when I do B = 1:Inf (it tells me the 'loop index' is too large)
Something similar to 'while true' statements...but i can't use the 'while' command.
thanks!
0 comentarios
Respuestas (1)
dpb
el 1 de Mzo. de 2014
1) length(t) ... doesn't change when used to describe the range [in] "FOR"...
Indeed. The upper limit of the FOR loop is computed from the expression and not updated even if the value used in the expression is changed in the loop. This is documented behavior (and much like most other languages, btw).
2) Use "Something similar to 'while true' statements...but i can't use the 'while' command."
Of course you can...just compute the index manually since it's not convenient in your problem to use a counted loop (ie, for)
y=yLimStart; % an initial condition
ix=0; % also initialize an index and time
t=0;
while yn>0
ix=ix+1;
y(ix) = function(t)
t=t+dt;
end
Or you could use
while true
...
if y(ix)<=0, break,end
end
where the other bookkeeping is indicated by the ellipses (...)
2 comentarios
dpb
el 2 de Mzo. de 2014
Editada: dpb
el 2 de Mzo. de 2014
i meant i'm not "allowed" to use the while statement ...
That's a stupid reqm't for the specific problem even if it is homework and intended to make a workaround given the facilities in Matlab. If teaching some other languages such as Fortran one could see the point in that there the obvious other solution would be sotoo--
looper: continue
ix=ix+1
y(ix) = function(t)
if y(ix)<=0.0 goto breakout
t=t+dt
goto looper
breakout: continue
to emphasize the utility of while but w/o a GOTO in Matlab it's absurd imo to use such fruitless exercises and teach the perversion. But, sometimes one has to do what one has to do, no matter how distasteful it may be.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!