Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Decision-Making Programs
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Spring 2004ENGR 111A1 MATLAB – Palm Chapter 3, Part 2 Function Files Class 6.1 Chapter 3: Section 2.
If Statements & Relational Operators Programming.
MatLab – Palm Chapter 4, Part 3 For and While Loops
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Top-down Program Design, Relational and Logical Operators
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
ENGR 111A - Spring MatLab – Palm Chapter 4, Part 4 Review and Debugging Class 12.1: Palm Chapters &
Program Design, Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Boolean Algebra & Logic Gates
ECE 1304 Introduction to Electrical and Computer Engineering
UMBC CMSC 104 – Section 01, Fall 2016
Introduction to Programming for Mechanical Engineers (ME 319)
Section 7.1 Logical Operators
Introduction to MATLAB for Engineers, Third Edition
Other Kinds of Arrays Chapter 11
Introduction to MATLAB
Linear Inequalities and Absolute Value
Conditional Statements
Data Types, Identifiers, and Expressions
If statement.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Dr. Clincy Professor of CS
Use of Mathematics using Technology (Maltlab)
Relational Operators Operator Meaning < Less than > Greater than
Logical Operations In Matlab.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Using Decision Structures
Herbert G. Mayer, PSU CS Status 7/19/2015
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Compound Conditionals
Selection—Making Decisions
Topics discussed in this section:
Selection Statements Chapter 3.
Chapter 3: Selection Structures: Making Decisions
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
The boolean type and boolean operators
Presentation transcript:

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

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

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). 1/3/2019 ENGR 111A – Fall 2004

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 1/3/2019 ENGR 111A – Fall 2004

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

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

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). 1/3/2019 ENGR 111A – Fall 2004

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

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

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

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

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

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

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

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 Fourth: logical AND. Fifth: logical OR. 1/3/2019 ENGR 111A – Fall 2004

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

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

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

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

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

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) 1/3/2019 ENGR 111A – Fall 2004

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. 1/3/2019 ENGR 111A – Fall 2004

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? 1/3/2019 ENGR 111A – Fall 2004

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

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

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

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