Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3

Similar presentations


Presentation on theme: "Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3"— Presentation transcript:

1 Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
MatLab – Palm Chapter 4, Part 1 Relational Operators, Logical Operators and Conditional Statements Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3 1/3/2019 ENGR 111A – Fall 2004

2 RAT 9.1 Take out a piece of paper, write your name and team number, today’s date and RAT 9.1. In 2-minutes, Individually translate the following English logical statement into MATLAB and write the MATLAB statement on your paper. Assign the variable named Z with the value that results from: x greater than 5 or x less than 1 Assume that x exists in the workspace Keep for your own notes. Answer: Z = x>5|x<1 1/3/2019 ENGR 111A – Fall 2004

3 Learning Objectives Students should be able to:
Use relational operators to develop relational expressions (i.e., x<y). Use logical operators to develop logical expressions (i.e., (x<y) & (a>b) ). Use logical functions given in Table (p. 198). 1/3/2019 ENGR 111A – Fall 2004

4 4.2 Relational Operators (p. 191)
MATLAB has 6 relational operators to make comparisons between variables. See Table (p. 191). < less than > greater than <= less than or equal to >= greater than or equal to == equal to (You will get this one wrong in the exam!) ~= not equal to 1/3/2019 ENGR 111A – Fall 2004

5 RELATIONAL OPERATORS Results of comparison using relational operators:
ZERO, if comparison is false. False = 0 ONE, if comparison if true. True = 1 If comparing numbers, any non-zero is considered “true” 1/3/2019 ENGR 111A – Fall 2004

6 EXAMPLE >> x=2; >> y=3;
>> z=x<y; % same as z = (x<y) >> u=x==y; % same as u = (x==y) >> z,u z = 1 u = Here z and u are “logical” variables (p. 192). 1/3/2019 ENGR 111A – Fall 2004

7 4.3 Logical Operators MATLAB has five logical operators ( also called Boolean operators) shown in Table (p. 194): ~ (NOT): z = ~x. & (AND): used to link logical expressions: z =(x<y) & (a>b). | (OR): used to link logical expressions: q =(x<y) | (a>b). && (Short-Circuit AND): used for operations on 2 scalar logical expressions. || (Short-Circuit OR): used for operations on 2 scalar logical expressions. (Note | is the shift-\ key, above Enter). xor (exclusive OR) is used to link logical expressions: w = xor(A, B). 1/3/2019 ENGR 111A – Fall 2004

8 ORDER OF PRECEDENCE SEE TABLE 4.3-2 (p. 195)
First: parenthesis, innermost first. Second: arithmetic operators and logical NOT (~) Evaluated from left to right. Third: relational operators Fourth: logical AND. Fifth: logical OR. 1/3/2019 ENGR 111A – Fall 2004

9 THE NOT OPERATOR ( ~ ) Given an array A:
~A returns an array of the same dimension. ~A has ones where A is zero and zeros where A is nonzero. 1/3/2019 ENGR 111A – Fall 2004

10 EXAMPLE of the ~ Operator
>> x = [6, 3, 9]; y = [14, 2, 9]; >>z = ~x z = [0, 0, 0] >>z = ~x > y % same as z = (~x) > y >>z = ~( x > y) [1, 0, 1] 1/3/2019 ENGR 111A – Fall 2004

11 EXAMPLE continued What would z = (x>~y) return?
How about z = (x~>y)? How about z = (x~=y)? After you write down what you think they return, type them into Matlab (Recall x = [6, 3, 9]; y = [14, 2, 9]) 1/3/2019 ENGR 111A – Fall 2004

12 Practice For the arrays x and y in the previous example, show using MATLAB that z = ~( x > y) is equivalent to z = ( x <= y ) (Recall x = [6, 3, 9]; y = [14, 2, 9]) 1/3/2019 ENGR 111A – Fall 2004

13 THE AND OPERATOR ( & ) The & operator compares two arrays A and B of the same dimension and returns an array of the same dimension. Returns ONE when both of the corresponding elements of A and B are non-zero. Returns ZERO when either or both of the corresponding elements of A and B are zero. 1/3/2019 ENGR 111A – Fall 2004

14 EXAMPLE of the & Operator
z = 0 & 3 returns z = 0. z = 2 & 3 returns z = 1. z = 0 & 0 returns z = 0. z = [ 5, -3, 0, 0] & [ 2, 4, 0, 5] returns z = [1, 1, 0, 0]. 1/3/2019 ENGR 111A – Fall 2004

15 ORDER OF PRECEDENCE FOR OPERATOR TYPES (AGAIN)
SEE TABLE (p. 195) First: parenthesis, innermost first. Second: arithmetic operators and logical NOT (~) Evaluated from left to right. Third: relational operators Fourth: logical AND. Fifth: logical OR. 1/3/2019 ENGR 111A – Fall 2004

16 INDIVIDUAL EXERCISE (3 min).
Show that (a) z1 = 1&2 + 3 is equivalent to z2 = 1&( 2 + 3) and, (b) z3 = 5 < 6&1 z4 = ( 5 < 6) &1 What about z5 = 5 < (6&1)? Are these parentheses necessary? 1/3/2019 ENGR 111A – Fall 2004

17 THE OR OPERATOR ( | ) The | operator compares two arrays A and B of the same dimension and returns an array of the same dimension. Returns ONE when either or both of the corresponding elements of A and B are non-zero. Returns ZERO when both of the corresponding elements of A and B are zero. 1/3/2019 ENGR 111A – Fall 2004

18 EXAMPLE of the | OPERATOR
z = 0|3 returns z = 1. z = 0|0 returns z = 0. z = [ 5, -3, 0, 0] | [2, 4, 0, 5] returns z = [ 1, 1, 0, 1]. What value does the expression below assign to z? z = 3 < 5 | 4 ==7 Test in Matlab. Write out how to solve this problem by hand. 1/3/2019 ENGR 111A – Fall 2004

19 XOR (Exclusive OR) xor(A,B) return zeros where A and B are either both nonzero or both zero. It returns ones where either A or B is nonzero, but not both. This can be written as a function: function z = xor(A,B) z = (A|B) & ~(A&B); 1/3/2019 ENGR 111A – Fall 2004

20 Example of xor and | Operators
z = xor([3,0,6], [5,0,0]); Returns z = [0,0,1]. z = ([3,0,6] | [5,0,0]); Returns z = [1,0,1]. 1/3/2019 ENGR 111A – Fall 2004

21 RELATIONAL AND LOGICAL FUNCTIONS
MATLAB provides additional relational and logical functions: any(x) all(x) find(x) finite(x) etc. SEE TABLE (p. 198) 1/3/2019 ENGR 111A – Fall 2004

22 Comparisons Note: Truth Table in Table (p. 196). A good way to deal with complex logic problems. Some people do not use it much and prefer to combine switch, if and while structures. 1/3/2019 ENGR 111A – Fall 2004

23 Example Average daytime highs in College Station for this week are: t_ave = [ ] Forecasted highs are: t_now = [ ] How many days will the temperature be above average or average? How many days will the temperature be either below or above average? 1/3/2019 ENGR 111A – Fall 2004

24 Example 4.3-1 & T4.3-2 REVIEW Example 4.3-1 (p. 199)
Work T4.3-2 (p. 200) as a script file: Do the plot if you can. Don’t forget the comments at the top to identify your work, etc. Make the format compact and short. Make sure you check the answers. 1/3/2019 ENGR 111A – Fall 2004

25 T4.3-2 Solution % Program TYU4p3_2.m
% Solves Test Your Understanding problem T4.3-2 on p. 200. % Set the values for initial speed, gravity, and angle. v0 = 20; g = 9.81; A = 40*pi/180; % Compute the time to hit. t_hit = 2*v0*sin(A)/g; % Compute the arrays containing time, height, and speed. t = 0 : t_hit/100 : t_hit; h = v0*t*sin(A) - 0.5*g*t.^2; v = sqrt(v0^2 - 2*v0*g*sin(A)*t + g^2*t.^2); % Determine when the height is less than 4m or the speed is greater than % 17m/s. u = find(~(h<4 | v>17)); % Compute the corresponding times. t_1 = (u(1) - 1)*(t_hit/100); t_2 = u(length(u) - 1)*(t_hit/100); % Plot to check your answer. figure(1) ; plot(t,h) ; figure(2) ; plot(t,v) 1/3/2019 ENGR 111A – Fall 2004

26 Assessment Take out a sheet of paper and Individually, write a brief description of the MATLAB topic that is the “muddiest” to you so far in Chapter 4. Then, as a team, agree on a topic you would like for me to cover in more detail next time. Turn in your paper with your team number 1/3/2019 ENGR 111A – Fall 2004

27 Assignment #8 Individual assignment.
Palm Chapter 4: (starting on p. 241) Problems #5, 10, 11 & 13. Submit a clear write-up of your problems DUE: Nov. 2, 2004 Reading Assignment: Sections 4.4 and 4.6. 1/3/2019 ENGR 111A – Fall 2004


Download ppt "Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3"

Similar presentations


Ads by Google