MATLAB While LOOP
In contrast to the For loop, which calculates the value of a group of commands with a fixed number of times, the While LOOP calculates the value of a group of statements with an indefinite number of times. The general form of the While loop is:
While expression
{Commands}
End
If all the elements in the expression are true, execute the {commands} between the while and end statements }.Generally,ExpressionReturns a scalar value, but the array value is also valid. In an array, all elements of the obtained array must be true. Consider the following example:
» Num = 0; EPS = 1;
» While (1 + EPS)> 1
EPS = EPS/2;
Num = num + 1;
End
» Num
Num =
53
» EPS = 2 * EPS
EPS =
2.2204e-016
This example shows how to calculate the special MATLAB value.EpsIs a method that can be added to 1, and the result is a minimum value with a finite precision greater than 1. Here we use uppercase lettersEPSThereforeEpsWill not be overwritten. In this example,EPSStart with 1. As long as (1 +EPS)> 1 is true (non-zero), and the command value in the While loop is always obtained. BecauseEPSConstantly divided by 2,EPSGradually becoming smaller so thatEPS+ 1 is not greater than 1. (Remember, this happens because the computer uses a fixed number to represent the number. MATLAB uses 16 bits, so we can only expectEPSClose to 10-16 .) At this point, (1 +EPS)> 1 is false (zero), so the While loop ends. Finally,EPSMultiply with 2 because the last division of 2 makesEPSToo small.