Download presentation

Presentation is loading. Please wait.

Published byMarjorie Reed Modified over 4 years ago

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 loops ii. for loops.

3
A 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.

4
1.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

5
4. 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

6
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);

7
The 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 for loop.

8
for ii = 1:10 Statement 1... Statement n end 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 to 10 after the loop finishes executing.

9
for ii = 1:2:10 Statement 1... Statement n end 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.

10
for ii = [5 9 7] Statement 1... Statement n end 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.

11
for ii = [1 2 3;4 5 6] Statement 1... Statement n End 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.

12
To 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 = 0 N! = 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 = 1 for ii = 1:n n_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

13
% 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 value x = 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

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

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google