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.
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,
Top-down Program Design, Relational and Logical Operators
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
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.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
CPS120: Introduction to Computer Science Operations Lecture 9.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Week 8: Decisions Bryan Burlingame 21 October 2015.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CSE123 Lecture 3 Files and File ManagementScripts.
Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
UMBC CMSC 104 – Section 01, Fall 2016
Section 7.1 Logical Operators
Conditional Statements
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Chapter 3: Introduction to Problem Solving and Control Statements
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
MATLAB Logical Expressions
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Selection—Making Decisions
Chapter 3: Selection Structures: Making Decisions
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
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 4/17/2017 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 4/17/2017 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). 4/17/2017 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 4/17/2017 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” 4/17/2017 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). 4/17/2017 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). 4/17/2017 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. 4/17/2017 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. 4/17/2017 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] 4/17/2017 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]) 4/17/2017 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]) 4/17/2017 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. 4/17/2017 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]. 4/17/2017 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. 4/17/2017 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? 4/17/2017 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. 4/17/2017 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. 4/17/2017 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); 4/17/2017 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]. 4/17/2017 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) 4/17/2017 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. 4/17/2017 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? 4/17/2017 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. 4/17/2017 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) 4/17/2017 ENGR 111A – Fall 2004

4.4 Conditional Statements The MATLAB conditional statements enable us to write programs that make decisions. Understanding the IF-THEN-ELSE logic is fundamental to all software development. Make sure that you understand: if statement on p. 201. else statement on p. 202. elseif statement on p. 205. Will cover more in the next class. 4/17/2017 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 4/17/2017 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. 4/17/2017 ENGR 111A – Fall 2004