Use of while loop, MATLAB in Engineering

Use of While loop:

Here is an illustration of calling the function, passing 5000 for the value of the input argument high.

>> factgthigh(5000)

ans =

5040

The iterator variable i is initialized to 0, and the running product variable fac that will store the factorial of each and every value of i, is initialized to 1. The first time the while loop is executed, the condition is theoretically true: 1 is less than or equal to 5000. Therefore, the action of the loop is executed, that is to increment i to 1 and fac to 1 (1 * 1). Afterward execution of the action of the loop, the condition is computed again. As it will still be true, the action is executed: i is incremented to 2, and fac get the value 2 (1 * 2). The value 2 is still   <= 5000, therefore the action will be executed again: i will be incremented to 3, & fac will get the value 6 (2 * 3). This continues till the first value of fac is found which is bigger than 5000. As soon as fac gets to this value, the condition become false and the while loop will end. At this point the factorial is assigned to the output argument that returns the value. The reason at which i is initialized to 0 instead of 1 is that the first time the loop action is executed, i will be 1 and fac becomes 1 so we have 1 and 1!, that is 1. Note that the output of all the assignment statements is suppressed in the function.


Posted Date: 10/19/2012 6:05:12 AM | Location : United States







Related Discussions:- Use of while loop, Assignment Help, Ask Question on Use of while loop, Get Answer, Expert's Help, Use of while loop Discussions

Write discussion on Use of while loop
Your posts are moderated
Related Questions
Creating the structure Variables: Creating a structure variable can be accomplished by simply storing the values in fields by using assignment statements, or by using the stru

Program to Counting in a while loop: The script initializes variable counter to 0. Then, in the while loop action, each and every time the user successfully enter a number, th

Uses of Function handles: The Function handles can also be generated for functions other than anonymous functions, both built-in & user-defined functions. For illustration, th

Referring to and Showing Cell Array Elements and Attributes: Just as with the other vectors, we can refer to individual elements of the cell arrays. The only difference is tha

Example of Exponential function modular program: In order to view the distinction in the approximate value for e as n increases, the user kept choosing Limit & entering larger

Plotting from a Function: The following function creates a Figure Window as shown in figure, which shows various types of plots for similar y vector. The vector is passed as a

Dot Product: The dot or inner product of two vectors a and b is written as a • b and is defined as  In another words, this is like matrix multiplication when multiplyi

Function strncmp: The function strncmp compares only the first n characters in the strings and ignores the rest. The initial two arguments are strings to compare, and third ar

Illustration of Sound files: For illustration, the following script generates a subplot which shows the signals from chirp and from train, which is as shown in figure:

Function cirarea - Anonymous functions: The function handle name is cirarea. The one argument is passed to the input argument radius. The body of the function is an expression