Download presentation

Presentation is loading. Please wait.

Published byImogen Campbell Modified about 1 year ago

1
Module 5 Decision-Making Programs

2
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?

3
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?

4
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

5
Relational Operators Relational operator Meaning

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

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

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

9
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

10
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 = [ ] >> v = x(w)

11
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([ ]) >> v = x(w)

12
Accessing Arrays using Logical Arrays >> A = [5 6 7; ; ] >> B = logical(eye(3)) >> C = A(B) Now try >> D = A(eye(3))

13
Logical (Boolean) Operators Opera tor NameDefinition ~ANOTreturn a new array of same size, has ones where A is zero and zeros where A is nonzero A&BANDreturn a new array of same size, has ones where A and B are nonzero and zeros where either A or B is zero |ORreturn a new array of same size, has ones where A and/or B are nonzero and zeros where both A and B are zero

14
Logical Operators Opera tor NameDefinition &&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 return false if both A and B false

15
Order of Precedence Precedenc e Operator Type HighestParentheses; start fr innermost pair HigherArithmetic ops and logical NOT (~); left to right MediumRelational ops; left to right LowerLogical AND LowestLogical OR

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

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

18
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

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

20
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)

21
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);

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

23
Truth Table xy~xx|yx&yxor(x,y) TTFTTF TFFTFT FTTTFT FFTFFF

24
CW Determine the answers by hand. Use MATLAB to check your answer. a. If x = [ ] and y = [ ] a = ~y > xb = x&y c = x|yd = xor(x,y) b. If x=[ ] and y=[ ] e = (x y) g = (x ~= y)h = (x == y) i = (x > 2)

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

26
Logical Fcns Lgc fcnDefinition 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

27
Logical Fcns Lgc fcnDefinition [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 isempt y(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’

28
Logical Fcns Lgc fcnDefinition 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

29
Examples of logical fcns >> x = [-2 0 4]; >> y = find(x) >> x = [ ]; y = [ ]; >> values = x (x

30
Examples of logical fcns >> x = [ ]; y = [ ]; >> z = find(x&y) >> values = y (x&y) >> how_many = length(values)

31
Conditional Statements MATLAB cond stmts include if else elseif end

32
if Statement Basic form if logical expression statements end

33
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

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

35
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

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

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

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

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

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

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

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

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

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

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

46
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.

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

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

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

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

51
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

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

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google