loops are matlab constructs that permit us to execute a sequence of statements more than once. there...

14

Upload: marjorie-reed

Post on 24-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while
Page 2: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

Looping StatementsLoops are MATLAB constructs that permit us to execute a sequence of statements more than once.

There are two basic forms of loop constructs: i. while loops ii. for loops.

Page 3: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

while LoopA while loop is a block of statements that are repeated indefinitely as long as some condition is satisfied.

while expression...... Code block...End

The controlling expression produces a logical value. If the expression is true, the code block will be executed, and then control will return to the while statement. If the expression is still true, the statements will be executed again. This process will be repeated until the expression becomes false.

When control returns to the while statement and the expression is false, the program will execute the first statement after the end.

Page 4: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

Example of while Loop1. State the Problem

Calculate the average and the standard deviation of a set of measurements, assuming that all of the measurements are either positive or zero and assuming that we do not know in advance how many measurements are included in the data set. A negative input value will mark the end of the set of measurements.

2. Define the inputs and outputs The inputs required by this program are an unknown number of positive or zero numbers. The outputs from this program are a printout of the mean and the standard deviation of the input data set. In addition, we will print out the number of data points input to the program

3. Top down Design Accumulate the input data Calculate the mean and standard deviation Write out the mean, standard deviation, and number of points

Page 5: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

Example of while Loop4. Pseudo code

1. Initialize n, sum_x, and sum_x2 to 0 2. Prompt user for first number 3. Read in first x 4. while x >= 0 5. n <- n + 1 6. sum_x <- sum_x + x 7. sum_x2 <- sum_x2 + x^2 8. Prompt user for next number 9. Read in next x 10.end 11. x_bar <- sum_x / n 12.std_dev <- sqrt((n*sum_x2 - sum_x^2) / (n*(n-1))) 13.Write out the mean value x_bar 14.Write out the standard deviation std_dev 15.Write out the number of input data points n

Page 6: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

Example of while Loop

5. MATLAB Code % Initialize sums. n = 0; sum_x = 0; sum_x2 = 0; % Read in first value x = input('Enter first value: '); % While Loop to read input values. while x >= 0 % Accumulate sums. n = n + 1; sum_x = sum_x + x; sum_x2 = sum_x2 + x^2; % Read in next value x = input('Enter next value: '); end % Calculate the mean and standard deviation x_bar = sum_x / n; std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); % Tell user. fprintf('The mean of this data set is: %f\n', x_bar); fprintf('The standard deviation is: %f\n', std_dev); fprintf('The number of data points is: %f\n', n);

Page 7: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For LoopThe for loop is a loop that executes a block of statements a specified number of times. The for loop has the form

for index = expr...... Body...end

Index is the loop variable (also known as the loop index) and expr is the loop control expression. The expression usually takes the form of a vector in shortcut notation first:incr:last.The statements between the for statement and the end statement are known as the body of the loop. They are executed repeatedly during each pass of the forloop.

Page 8: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop Control Expressionfor ii = 1:10

Statement 1...Statement nend

The loop index ii will be 1 the first time,2 the second time, and so on. The loop index will be 10 on the last pass through the statements. When control is returned to the for statement after the tenth pass, there are no more columns in the control expression, so execution transfers to the first statement after the end statement. Note that the loop index ii is still set to10 after the loop finishes executing.

Page 9: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop Control Expression

for ii = 1:2:10Statement 1...Statement nend

The loop index ii will be 1 the first time, 3 the second time, and so on. The loop index will be 9 on the fifth and last pass through the statements. When control is returned to the for statement after the fifth pass, there are no more columns in the control expression, so execution transfers to the first statement after the end statement. Note that the loop index ii is still set to 9 after the loop finishes executing.

Page 10: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop Control Expression

for ii = [5 9 7]Statement 1...Statement nend

Loop will be executed three times with the loop index set to 5 the first time, 9 the second time, and 7 the final time. The loop index ii is still set to 7 after the loop finishes executing.

Page 11: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop Control Expression

for ii = [1 2 3;4 5 6]Statement 1...Statement nEnd

The loop index ii will be the column vector the first time [1; 4], the second time [2 ; 5], and the third time [3 ; 6]. The loop index ii is still set to [3 ; 6] after the loop finishes executing. This example illustrates the fact that a loop index can be a vector.

Page 12: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop ExampleTo illustrate the operation of a for loop, we will use a for loop to calculate the factorial function. The factorial function is defined as

N! = 1 N = 0N! = N * (N-1) * (N-2) * ... * 3 * 2 * 1 N > 0

The MATLAB code to calculate N factorial for positive value of N would be

n_factorial = 1for ii = 1:nn_factorial = n_factorial * ii;end

Suppose that we wish to calculate the value of 5!. If n is 5, the for loop control expression would be the row vector [1 2 3 4 5]. This loop will be executed 5 times, with the variable ii taking on values of 1, 2, 3, 4, and 5 in the successive loops.The resulting value of n_factorial will be 1 x 2 x 3 x 4 x 5 = 120

Page 13: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

For Loop Example% Initialize sums.sum_x = 0; sum_x2 = 0;% Get the number of points to input.n = input('Enter number of points: ');% Loop to read input values.for ii = 1:n% Read in next valuex = input('Enter value: ');% Accumulate sums.sum_x = sum_x + x;sum_x2 = sum_x2 + x^2;end% Now calculate statistics.x_bar = sum_x / n;std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) );% Tell user.fprintf('The mean of this data set is: %f\n', x_bar);fprintf('The standard deviation is: %f\n', std_dev);fprintf('The number of data points is: %f\n', n);end

Page 14: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while

Continuation…..Till this lecture is the syllabus for Midterm i.e. on 26th October 2012.Loops will be continued after the exam……………