While loops & multiple variables

조회 수: 4 (최근 30일)
conor meaney
conor meaney 2015년 2월 21일
편집: Andrew Newell 2015년 2월 22일
Hi There,
I've run into an issue whilst using a while loop in my script involving iterations. The loop must end once a certain number of variables in the script converges to a pre-defined error value/limiing criterion between the nth and n-1th iteration.
i.e.
INPUT
while abs(xn - xn-1) >= limiting criteron
SCRIPT HERE
end
OUTPUTS
That's fine for one variable and works as wanted.. as soon as the variable on the left become less than the limiting criterion, it ends.
What I wanted to do is the same idea but with multiple variables requiring this limiting criterion i.e. I want it to keep iterating so that multiple variables converge to less than this value. (assuming convergence exists).
My attempt is something along the lines of:
INPUTS
while [abs(xn - xn-1), abs(yn - yn-1), abs(zn-zn-1)] <= [limiting criterion, limiting criterion, limiting criterion]
SCRIPT HERE
end
OUTPUTS
What appears to be happening is as soon as any of the variables becomes less than this limiting criterion, the loop ends, but what I want to happen is it to keep iterating until all the variables are less than the limiting criterion, again assuming convergence exists.
Any ideas?
Thanks in advance
Conor.
  댓글 수: 2
Andrew Newell
Andrew Newell 2015년 2월 21일
I presume you intended >= in the second example?
conor meaney
conor meaney 2015년 2월 21일
Yes apologies, it should read '>=' in the second example!

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

답변 (2개)

Andrew Newell
Andrew Newell 2015년 2월 21일
Your while condition returns a logical vector, and if any of its components is false the whole thing is false and the loop ends. What you need is
while any([abs(xn - xn-1), abs(yn - yn-1), abs(zn-zn-1)] >= [limiting criterion, limiting criterion, limiting criterion])
  댓글 수: 1
conor meaney
conor meaney 2015년 2월 21일
Perfect, works like a treat and a simple edit to the script.
Thanks!
Conor.

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


Image Analyst
Image Analyst 2015년 2월 21일
Try
continueWithLoop = true;
while continueWithLoop
% Some code....
% Check if we can quit loop or need to continue
continueWithLoop = abs(x(n) - x(n-1)) > limitingCriterion || ...
abs(y(n) - y(n-1)) > limitingCriterion || ...
abs(z(n) - z(n-1)) > limitingCriterion
end
  댓글 수: 5
Image Analyst
Image Analyst 2015년 2월 22일
  1. xn should be x(n)
  2. xn-1 should be x(n-1)
  3. yn should be y(n)
  4. yn-1 should be y(n-1)
  5. zn should be z(n)
  6. zn-1 should be z(n-1)
  7. Each of the three instances of "limiting criterion" should be "limitingCriterion" since variables can't have spaces in their names.
Andrew Newell
Andrew Newell 2015년 2월 22일
편집: Andrew Newell 2015년 2월 22일
Oh. I was viewing it as pseudocode and assuming the actual code was mostly correct.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by