Decision-Making Programs

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

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.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
ENGR-25_Programming-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
MATLAB Loops and Branching.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Chapter 8 Branching Statements and Program Design.
Matlab Basics IEF 217a: Lecture 2 Fall 2002 Sigmon and Davis, chapters 1-7.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
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.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 4 Programming with MATLAB PowerPoint to accompany Copyright © The.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Flow of Control Part 1: Selection
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 4 Programming with MATLAB. Algorithms and Control Structures Algorithm: an ordered sequence of precisely defined instructions that performs some.
Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
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.,
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
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.
1 Structured Programming EEN170 Programming in MATLAB.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
Introduction to MATLAB for Engineers, Third Edition
ECE 1304 Introduction to Electrical and Computer Engineering
Making Choices with if Statements
Control Statements in Matlab
Selection Statements by Ahmet Sacan
MATLAB DENC 2533 ECADD LAB 9.
Introduction to MATLAB
Lecture 3 MATLAB programming (1)
Conditional Statements
Data Types, Identifiers, and Expressions
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Vectorized Code, Logical Indexing
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Logical Operations In Matlab.
Matlab Basics.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Decision-Making Programs Module 5 Decision-Making Programs

CW 5.1 (1/2 sheet;No name) Provide your response to the following questions on a ½ sheet of paper. 1. What aspects of the class interested you? 2. What helped you understand the ideas discussed? 3. What do you think is useful for your future study? 2

CW 5.1 cont’d 4. What would you change about this class in the immediate future to make it a more enjoyable or satisfying learning experience for you? Be as specific as possible please. 5. What ideas did you encounter in the last semester that you would have difficulty? And why? 3

Short-term Sensory Store A Model of the Information Processing System (IPS) Short-term Sensory Store Input Memory Loss Rehearsal Working Memory Long-term Memory Storage Retrieval Attention Elaboration Organization 4 4

Relational Operators < Less than <= Less than or equal to > Meaning < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to

Examples of Relational Ops >> x = 2; y = 5; >> z = x < y % or >> z = (x < y) >> u = x == y % or >> u = (x == y) 6

Relational Ops on Arrays >> x = [6 3 9]; y = [14 2 9]; >> z = (x < y) >> u = (x ~= y) >> v = (x > 8) >> w = x (x<y) 7

Relational and Arithmetic Ops Arithmetic Ops have precedence over Relational Ops What are the differences below? >> z = 5 > 2 + 7 >> z = 5 > (2 + 7) >> z =(5 > 2) + 7 8

Precedence among Relational Ops MATLAB evaluates Relational Ops from left to right What are the differences below? >> z = 5 > 3 ~= 1 >> z = (5 > 3) ~= 1 9

Logical Class in MATLAB logical variables only hold the values 1 (true) and 0 (false) Below w is a numeric array and k is a logical array >> x = [ -2 : 2 ] >> k = (abs(x) > 1) >> z = x(k) >> w = [1 0 0 0 1] >> v = x(w) 10

Logical Function return an array that is used for logical indexing or logical tests if A is a numeric array, then >> B = logical(A) returns a logical array B. Back to the question before, >> w = logical([1 0 0 0 1]) >> v = x(w) 11

Accessing Arrays using Logical Arrays >> B = logical(eye(3)) >> C = A(B) Now try >> D = A(eye(3)) 12

Logical (Boolean) Operators Name Definition ~A NOT return a new array of same size, has ones where A is zero and zeros where A is nonzero A&B AND return a new array of same size, has ones where A and B are nonzero and zeros where either A or B is zero | OR return a new array of same size, has ones where A and/or B are nonzero and zeros where both A and B are zero 13

Logical Operators && Short-Circuit AND Name Definition && Short-Circuit AND return true if both A and B true return false if both A and B false || Short-Circuit OR return true if either A or B or both true 14

Order of Precedence Highest Parentheses; start fr innermost pair Operator Type Highest Parentheses; start fr innermost pair Higher Arithmetic ops and logical NOT (~); left to right Medium Relational ops; left to right Lower Logical AND Lowest Logical OR 15

Examples of Logical Op ~ >> x = [0 3 9]; y = [14 -2 9]; >> a = ~x >> b = ~x > y >> c = ~(x > y) >> d = (x <= y) 16

Examples of Logical Op & Compare two arrays of the same dim >> z = 0&3 >> z = 2&3 >> z = 0&0 >> z = [5 -3 0 0]&[2 4 0 5] >> z = 1&2+3 >> z = 5<6&1 17

Examples of Logical Op & … >> x=[6 3 9];y=[14 2 9];a=[4 3 12]; >> z = (x>y) & a >> z = (x>y)&(x>a) In math, 5 < x < 10. In MATLAB, >> (5<x) & (x< 10) 18

Examples of Logical Op | >> z = 0|3 >> z = 0|0 >> z = [5 -3 0 0]|[2 4 0 5] >> z = 3<5|4==7 >> z = (3<5) | (4==7) 19

Examples of Logical Op | … >> z = 1|0&1 >> z = (1|0)&1 >> z = 1|0&0 >> z = 1|(0&0) >> z = ~3==7|4==6 >> z = ((~3)==7)|(4==6) 20

Exclusive OR (xor) fcn xor(A,B) = 1 if either A or B is nonzero but not both = 0 if A and B are both zero or both nonzero In MATLAB, Function z = xor(A,B) z = (A|B) & ~ (A&B); 21

Examples of xor fcn >> a = xor([3 0 6], [5 0 0]) >> b =[3 0 6] | [5 0 0] 22

Truth Table x y ~x x|y x&y xor(x,y) T F 23

CW 5.2 1. Determine the answers by hand. Use MATLAB to check your answer. a. If x = [5 -3 18 4] and y = [-9 13 7 4] a = ~y > x b = x&y c = x|y d = xor(x,y) b. If x=[-9 -6 0 2 5] and y=[-10 -6 2 4 6] e = (x < y) f = (x > y) g = (x ~= y) h = (x == y) i = (x > 2) 24

CW 5.2 2. Follow the MATLAB instructions below. Compare your results with the Truth Table. >> x = [1 1 0 0]’ >> y = [1; 0; 1; 0] >> Truth_Table=[x,y,~x,x|y, x&y, xor(x,y)] 25

Logical Fcns all(x) a scalar, 1 or 0. 1 if all elements are nonzero Lgc fcn Definition all(x) a scalar, 1 or 0. 1 if all elements are nonzero all(A) a row vector having the same # of columns as A. 1 if all elements in a column are nonzero any(x) a scalar, 1 or 0. 1 if any element is nonzero any(A) a row vector having the same # of columns as A. 1 if any element in a column is nonzero find(A) an array having the indices of nonzero elements of array A 26

Logical Fcns [u,v,w]= find(A) Lgc fcn Definition [u,v,w]= find(A) arrays u & v contain row & col indices of nonzero elements of A; w contain the nonzero elements; w is optional finite(A) an array same dim as A w/ ones where element of A is finite ischar(A) a scalar 1 if A is a character array isempty(A) A scalar 1 if A is an empty array isinf(A) An array same dim as A w/ones where element of A is ‘inf’ 27

Logical Fcns Lgc fcn Definition isnan(A) an array same dim as A w/ ones where element of A is ‘NaN’ isnumeric(A) a scalar, 1 or 0. 1 if A is a numeric array isreal(A) a scalar, 1 or 0. 1 if all elements of A are NON imaginary logical(A) Convert all elements of A into logical values xor(A,B) Exclusive or on each element ‘pair’of A and B 28

Examples of logical fcns >> y = find(x) >> x = [6 3 9 11]; y = [14 2 9 13]; >> values = x (x<y) >> how_many = length(values) >> indices = find(x<y) 29

Examples of logical fcns >> x = [5 -3 0 0 8]; y = [2 4 0 5 7]; >> z = find(x&y) >> values = y (x&y) >> how_many = length(values) 30

Conditional Statements MATLAB cond stmts include if else elseif end 31

if Statement Basic form if logical expression statements end 32

if Example 1 Math: y = only if x ≥ 0 English: If x is greater than or equal to zero compute y from y = MATLAB: >> if x >= 0 >> y = sqrt(x) >> end 33

if Example 1 Shortened form is allowed but less readable >> if x >= 0, y = sqrt(x), end 34

if Example 2 >> x = 5; y = 2; >> z = 0; >> if (x>0) & (y>0) z = sqrt(x) + sqrt(y) w = log(x) - 3 * log(y) end 35

else Statement Basic form if logical expression statements 1 else end 36

else Example 1 Suppose that y = for x ≥ 0 and that y = ex – 1 for x < 0 >> if x >= 0 y = sqrt(x) else y = exp(x) – 1 end 37

else Example 2 Consider the following. Predict what should be the response. >> x = [4 -9 25]; if x < 0 disp(‘some elements are –ve’) else y = sqrt(x), end 38

else Example 2 Now consider the following. >> x = [4 -9 25]; if x >= 0 y = sqrt(x) else disp(‘some elements are –ve’) end 39

elseif Statement if logical expression 1 statements 1 elseif logical expression 2 statements 2 else statements 3 end 40

elseif Example 1 Suppose that y = ln x if x ≥ 5 and that y = if 0 ≤x < 5 41

elseif Example 1 >> if x >= 5 y = log(x) else if x >= 0 y = sqrt(x) end 42

elseif Example 1 improved >> if x >= 5 y = log(x) elseif x >= 0 y = sqrt(x) end 43

CW 5.3 (attach all printed codes) Suppose that x = [-4 -1 0 2 10] and y = [-5 -2 2 5 9]. Find the values and the indices of the elements in x that are greater than the corresponding elements in y 44

CW 5.3 2. Suppose that y = ln x for x > 10 y = for 0 ≤ x ≤ 10 and y = ex -1 for x < 0 Write the shortest codes using if/else/elseif stmts Test value using x = -3, 5 and 12 45

CW 5.4 (Lab) 1. Given a number x and the quadrant q (q = 1, 2, 3, 4), write a program to compute sin-1(x) in degrees, taking into account the quadrant. The program should display an error message if |x| > 1. 46

String variable Contains variable >> number = 123; >> street_num = ‘123’; What is the difference? 47

Addressing string variable Consider >> sch_name = ‘Mark Keppel High’ >> length(sch_name) >> sch_name(4:6) 48

Prompt for response >> reply = input(‘Continue? Y/N [Y]: ’, ‘s’); >> if (isempty(reply))|reply==‘Y’|reply==‘y’) reply = ‘Y’ else reply = ‘N’ end 49

for Loops Repeating a calculation a number of times Typical structure >> for loop_variable = start : step : end stmts end 50

for Loops example >> for k = 5 : 10 : 35 x = k^2 end Short but not that readable >> for k = 0 : 2 : 10, y = sqrt(k), end 51

CW 5.5 1. Write a script file to compute the sum of the first 15 terms in the series 5k2 – 2k, where k = 3, 4, … , 18 2. Write a script file to plot 15 + 10 x ≥ 9 y = 10 x + 10 0 ≤ x < 9 10 x < 0 52