MATLAB WHILE LOOP

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.