6/22/2015ENGR 111A – Fall 20041 MatLab – Palm Chapter 4, Part 1 Relational Operators, Logical...

Post on 21-Dec-2015

217 views 0 download

Transcript of 6/22/2015ENGR 111A – Fall 20041 MatLab – Palm Chapter 4, Part 1 Relational Operators, Logical...

04/18/23 ENGR 111A – Fall 2004 1

MatLab – Palm Chapter 4, Part 1Relational Operators, Logical Operators and Conditional Statements

Class 9.1Chapter 4

Sections: 4.1, 4.2, 4.3

04/18/23 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

04/18/23 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 4.3-4 (p. 198).

04/18/23 ENGR 111A – Fall 2004 4

4.2 Relational Operators (p. 191)

MATLAB has 6 relational operators to make comparisons between variables. See Table 4.2-1 (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

04/18/23 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”

04/18/23 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,uz = 1u = 0Here z and u are “logical” variables (p.

192).

04/18/23 ENGR 111A – Fall 2004 7

4.3 Logical Operators MATLAB has five logical operators ( also called

Boolean operators) shown in Table 4.3-1 (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).

04/18/23 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 Evaluated from left to right.

Fourth: logical AND. Fifth: logical OR.

04/18/23 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.

04/18/23 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) > yz = [0, 0, 0]>>z = ~( x > y) z = [1, 0, 1]

04/18/23 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])

04/18/23 ENGR 111A – Fall 2004 12

Practice

For the arrays x and y in the previous example, show using MATLAB thatz = ~( x > y) is equivalent to z = ( x <= y )

(Recall x = [6, 3, 9]; y = [14, 2, 9])

04/18/23 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.

04/18/23 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].

04/18/23 ENGR 111A – Fall 2004 15

ORDER OF PRECEDENCE FOR OPERATOR TYPES (AGAIN)

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 Evaluated from left to right.

Fourth: logical AND. Fifth: logical OR.

04/18/23 ENGR 111A – Fall 2004 16

INDIVIDUAL EXERCISE (3 min).Show that(a) z1 = 1&2 + 3

is equivalent toz2 = 1&( 2 + 3) and,

(b) z3 = 5 < 6&1is equivalent toz4 = ( 5 < 6) &1

What about z5 = 5 < (6&1)? Are these parentheses necessary?

04/18/23 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.

04/18/23 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.

04/18/23 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);

04/18/23 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].

04/18/23 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 4.3-4 (p. 198)

04/18/23 ENGR 111A – Fall 2004 22

Comparisons Note: Truth Table in Table 4.3-3 (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.

04/18/23 ENGR 111A – Fall 2004 23

Example Average daytime highs in College

Station for this week are:t_ave = [79.5 79.3 79 78.6 78.7 78.3 77]

Forecasted highs are:t_now = [78 83 79 86 77 72 71]

How many days will the temperature be above average or average?

How many days will the temperature be either below or above average?

04/18/23 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.

04/18/23 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)

04/18/23 ENGR 111A – Fall 2004 27

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

04/18/23 ENGR 111A – Fall 2004 28

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.