Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.

Similar presentations

Presentation on theme: "Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161."— Presentation transcript:

1 Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161

2 Introduction to Chapter 8 Learn how to include logic in a program to allow choices depending upon the input data or intermediate calculations. In the process we will add a number of new MATLAB commands to our repertoire of commands. We begin by exploring the concept of relational and logical operators, we will look at flowcharting, the find command and then various control structures. Mastering these concepts will move you to a new level as a MATLAB user.

3 Relational and Logical Operators To get started incorporating statement control structures in our programs, we need to understand MATLAB’s relational and logical operations. Consider, >> x = 5; >> x < 3% < is a relational operator ans = 0 The statement x < 3 is asking the question, Is x < 3?; MATLAB returns with ans = 0 which is read as false, i.e., x is not less than 3. If x was less than 3, MATLAB would have returned with ans = 1, or true. The result of a relational operation is always true (1) or false (0).

4 Relational and Logical Operators II Relational operators applied to matrices yield comparisons term by term, the matrices must be the same order, consider >> x = 1 : 5; >> y = [2, 2, 1, 6, 4]; >> y >= x% read as great than or equal to ans = 1 1 0 1 0 In this case, x and y are row vectors of 5 elements each. MATLAB compares term by term and determines that y >= x for the 1 st, 2 nd, and 4 th terms only.

5 Relational and Logical Operators III MATLAB’s Relational Operators < read as less than <= read as less than or equal to >read as greater than >=read as greater than or equal to ==read as equal to ~=read as not equal to Now remember the result of a relational operation is always true (1) or false (0); x is greater than y or it is not, x is equal to 3 or it is not.

6 Relational and Logical Operators IV MATLAB’s Logical Operators &read as the logical operation of and ~read as the logical operation of not |read as the logical operation of or Logical operators allow us to combine relational operators to form more complex operations, for example, (x > y) & ( y <= 3 ) asks the question, is x greater than y and y less or equal to 3. If so, MATLAB returns with true (1) else false (0).

7 Flowcharting and Pseudocode Flowcharting is a graphical technique for creating your coding plan, Pseudocode is a verbal description of your plan. Both allow you to organize your thoughts and generate a description of your program without worrying about the formality of the MATLAB statements used to create your program. Both techniques let you focus on the logical flow of your program first before translating into MATLAB. These techniques minimize the chance for logical errors in your program.

8 Flowcharting Four symbols facilitate developing flowcharts, Start/Stop Input/Output Decision Calculations

9 Start Conversion Enter Temp in degrees C Convert to degrees F Output converted Temp Stop

10 MATLAB’s find command The find command searches a matrix, row or column vector and identifies which elements in the matrix that meet a given criterion. height = [63, 67, 65, 72, 69, 78, 75]; % height in inches x = find(height >= 66) x = 2 4 5 6 7 x is a five element row matrix of the indices of height that satisfy the criterion of being greater than or equal to 66

11 MATLAB’s find command II  If you want then to know the actual values (not the indices) that satisfy the criterion, consider accept = height(x) which would return accept = 67 72 69 78 75

12 MATLAB’s find command III The find command can be used with mxn matrices, either ask for the row and column or use the default number scheme, by column left to right. A = some mxn matrix of temperatures [row, column] = find(A > 100) % row/column indices or temps = find(A>100)% single index number

13 Statement Level Control Structures MATLAB programs are generally organized as one of or as a combination of three control structures Sequences: Here MATLAB commands are executed one after the other in a linear manner from beginning to end. Selection: Here at some point in a program based upon some logical condition, one branch of the program is selected and followed as opposed to some other branch. Repetition: Here a sequence of commands is executed some number of times, from 0 to 1 to 2 to n times. The repetition continues until some logical condition is met, then the repetition ceases and the program continues.

14 if, else and elseif if, else, and elseif statements help us use the selection type of control structure. Here some condition is tested, if the condition is true one sequences of commands is selected, if false another sequence of commands is selected. The form of the if statement is if comparison% the comparison is a relational operation such as statement 1% x 10 or x z statement 2 etc. end Upon encountering the if statement, MATLAB determines if the comparison is true or false, when it is true, then the statements between the if and the end statements are executed, when false they are not executed. Now let’s incorporate the else statement with the if statement.

15 if, else and elseif Incorporating the else statement, if comparison% some relational operation statement 1 statement 2% first set of commands etc. else statement a% second set of commands statement b etc. end Here when the comparison is true, the first set of statements is executed, the second set will not be. But when the comparison is false, the second set of commands is executed, the first set is not. One of the sets will be executed but not both.

16 if, else and elseif Incorporating the elseif statement, if comparison #1 statements elseif comparison #2 statements elseif comparison #3 statements else statements end Here we allow for several comparisons, when #1 is true the statements following the if statement are executed up to the first elseif statement, for #1 false, the second comparison is tested. If #2 is true, the statements following the second comparison are executed, and so on. If none of the comparisons are true, then the statements following the else statement are executed. One and only one set of statements will be executed.

17 switch/case and the menu functions switch/case is also a selection type of control structure and very similar to the if-elseif type of structure. Often if-elseif is used with numerical conditions while switch/case structures are used with strings, In either case, both let you choose one of several paths through your program based upon some criterion. The menu function often in conjunction with the switch/case function lets you use a graphical interface for selecting your desired condition.

18 switch/case example city = input(‘Enter name of city; ‘, ‘s’); switch city case ‘Boston’ disp(‘$345’) case ‘Denver’ disp(‘$150’) case ‘Honolulu’ disp(‘Stay at Home’) otherwise disp(‘Error: Incorrect Entry; Not on file.’) end

19 menu function Used usually in conjunction with the switch/case function, the menu function causes a menu box to appear on the screen with a series of buttons that the user can select. The buttons are determined by the program. Upon the selection by the user, the program continues. In using the menu function, the programmer specifies the title for the menu box and the names of the various buttons. From our previous example

20 menu function example city = menu(‘Select a city from the menu;’, ‘Boston’, ‘Denver’, ‘Honolulu’); switch city case 1 disp(‘$345’) case 2 disp(‘$150’) case 3 disp(‘Stay at Home’) otherwise disp(‘Error: Incorrect Entry; Not on file.’) end

21 Looping Looping is the third type of control structure, here some set of statements is executed a number of times until some condition is met. There are two types, for loops and while loops. In general, try to avoid loops, they slow down MATLAB programs, but it is always best to first get a program that works and then improve upon is speed of execution later, i.e., optimize it after you get it working.

22 for Loops Incorporating for statements, for index = expression statement 1 statement 2 etc. end A typical for statement might look like, “for k = 1:10”. Here k is initially assigned a value of 1 and the set of statements is executed. Then k is assigned a value of 2 and the statements are repeated. This continues until k = 10. This is the last time the statements will be executed, next when k would be assigned a value of 11, the loop terminates. A more general form for the expression would be initial_value:increment:final_value but the idea is the same.

23 while Loops Incorporating while statements, while expression statement 1 statement 2 etc. end Here the expression is a relational operation. Upon encountering this while loop, MATLAB tests the expression. When the expression is true, the statements between the while statement and end statement are executed. Then the expression is retested. If it continues to be true, the statements are executed again. Note you can get trapped here unless the statements in the while loop modify the variables in the expression. If they don’t you are in an endless loop, use Ctrl c to get out of an endless loop.

24 Break and Continue commands Break command is used to terminate a loop pre-maturely based upon some condition. Continue command is used to stop the current pass through the loop and start a new pass. Here the loop continues whereas for the Break command, the loop completely terminates. Break and Continue are commonly used within an if statement within a loop, that is, if the condition is true, the either the loop terminates (Break) or the current pass terminates (Continue). Examples on pages 328 and 329 illustrate the usage of these two statements.

25 Midpoint Break Loops The examples shown on pages 328 and 329 are illustration of midpoint break loops. Consider the following; while(1)... do some calculations using x as a variable if (x < 0.001) break end... do some more calculations... end

26 Final Touches In MATLAB it is better to avoid loops if you can. Sometimes you can’t, then go ahead a use them. Watch out for the endless loop. When using if, else, elseif, switch/case, for, and while statements, indent your statements and internal control structures so you can see the control structures more clearly. It helps in debugging and understanding the flow of the program. Seeing where they start and end easily is a great help. Use comment statements liberally to help you understand your control structures. Don’t forget to end each control structure with the “end” statement. It is generally easier on the mind to deal with scalar variables in relational expressions than vector or matrix variables. Try to keep it simple. When you forget the syntax of a statement, use the help feature in the Command Window to see the online documentation, help if.

27 Chapters 8 and 9 Assignments The following assignments will let you practice using MATLAB control structures and various commands. Assignments 8.4, 8.5, 8.14, 9.8, 9.13, 9.15, 9.16 (problem numbers for the 3rd edition text.)

Download ppt "Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161."

Similar presentations

Ads by Google