Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.

Similar presentations


Presentation on theme: "Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL."— Presentation transcript:

1 Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL define the key terms in logical statements including: –relational operators, –logical operators, –1 and 0 as true and false determine the truth of relational statements determine the truth of relational statements using logical operators text sections: 4.2 & 4.3

2 Quiz – individual please The equation for an ellipse can be written as follows: Develop a script that can plot an ellipse on Cartesian axes with axes of a = 5 m and b=10 m. where a, b are the major and minor axis lengths of the ellipsis and  varies from zero to 2 . Make sure that the real geometry of the ellipses is shown, i.e., a circle should look like a circle. Please print out your script and the resulting graph.

3 Changing Graph Interactively in Excel interactive options – try: Right Click on the Axes and choose “Format Axis” note the options –you can change ranges, tick spacing... –you can also choose log spacing for any axis –you can change the position of the axis numbers. Click on the series –set points, lines and colors Click on titles –to edit and change fonts – use home ribbon. Click on blank area of graph –If in 03 get rid of the gray background

4 Putting a function on the graph plot the fit to our data: the fitted equation was ht=29 – 0.028*raw. need some x values (what range): 350-950 Create an x series in spreadsheet (a column of x values) – can use one of the existing series. add a formula to the first cell in the next column and copy down to to create y series. add this series to graph as before. click on the series and change to lines instead of points.

5 Excel: Some Potential pitfalls The line graph: –start the graph wizard and choose line graph –select one set of x-y data –plot the points –Result: the points are plotted in order with no other meaning to the x axis. Trying to plot all at once –highlight the entire block of data –start plot wizard and create an x-y plot –Result: all series are plotted against the first series – not really what we wanted. You can plot multiple series at once but only when the first is the x values for all series and the remaining columns are matching y-series

6 Relational Operators greater than = less than or equal == equal  must be distinguished from the assignment operator ~= not equal summarized in Table 4.2-1 in text

7 Scalar comparison For x = 6 try z= (x < 10) z=(x==10) z=(x>=4) z=(x~=7) Notice class of z in workspace window

8 Handout: Relational and Logical Operators Handout Have students break into pairs for the problems that follow. Have students refer to as they work on the following examples

9 Scalar to a Vector  Result: logical vector (array) same size as original  Compares the scalar to each value in the vector  Returns a true or false for each comparison e.g., >> y = (2 >= [ 1 2 3]) y = [1 1 0] (true, true, false)

10 Scalar to a Vector Problem A stock price history over 10 days is: price_A = [ 19, 18, 22, 21, 25, 19, 17, 21, 27, 29 ]; Use MATLAB to calculate the number of days were the price is over 20. Can you do this in one line of MATLAB code?

11 Scalar to Vector Comparison Set up vector and compare to scalar boundary >> price_A = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]; >> z = price_A > 20 z = 0 0 1 1 1 0 0 1 1 1 Notice the vector shows us which days meet the condition. We can simply sum this vector to find the number of days. >> sum(z) ans = 6 In one line >> sum(price_A > 20) ans = 6

12 Logical Comparison of Two Vectors  Two vectors must be the same length  Results in a logical vector of the same length  Compares the two vectors element by element e.g. z = ([1 2 3] ~= [3 2 1] )  z =[1 0 1] (true, false, true)

13 Vector vs. Vector Problem price_A = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29] price_B = [22, 17, 20, 19, 24, 18, 16, 25, 28, 27] The arrays price_A and price_B given above contain the price in dollars of two stocks over several days. Use MATLAB to determine how many days the price of stock A was above the price of stock B.

14 Vector to Vector Comparison Set up vectors and compare, notice price_A is the same as above so we only need to set up vector price_B. >> price_B = [22, 17, 20, 19, 24, 18, 16, 25, 28, 27] price_B = 22 17 20 19 24 18 16 25 28 27 >> z = price_A > price_B z = 0 1 1 1 1 1 1 0 0 1 Again the vector shows us which days meet the condition & we can use sum to find the number of days. >> sum(z) ans = 7 In one line: >> sum(price_A > price_B)

15 Text Comparisons Create variables: a = ‘o’, b=‘Mastodons’, c=‘mastodons’ Try the following comparisons x = (b == a) y = (b == c) z = (b < c) text strings  can be defined by enclosing in single quotes (literals)  vectors of type char  ASCII value is compared

16 text comparisons: MATLAB session > a = 'o'; b='Mastodons'; c='mastodons'; >> x = (b == a) x = 0 0 0 0 1 0 1 0 0 >> y = (b == c) y = 0 1 1 1 1 1 1 1 1 >> z = (b < c) z = 1 0 0 0 0 0 0 0 0 notice >> double('M'), double('m') ans = 77 ans = 109 Note: relational operators have equal precedence, L-to-R execution

17 Basic Logical Operators NameMATLAB operator Function and&true if both connected statements are true or| (shift \) true of either connected statement is true not~ (shift `) makes a true statement false or vise- versa xorxor(A,B)true A & B differ, false if both are the same

18 Page 2 of Handout Have students try and develop truth table by hand (section III on Handout) Go through the following example truth table Have them follow create a truth table in MATLAB following instructions is section IV of the handout Order of Precedence is Handout section V.

19 III. Truth Table ABNot ~A Or A | B And A & B Exclusive or XOR(A,B) True(1) True(1))False(0) True(1) False(0) True(1)

20 IV. Creating Truth Table in MATLAB >> A = [1 1 0 0]’; % notice a transpose is used >> B = [1 0 1 0]’; >> Ttable = [A, B, ~A, A | B, A & B, xor(A, B)] Ttable = 1 1 0 1 1 0 1 0 0 1 0 1 0 1 1 1 0 1 0 0 1 0 0 0 Matches table in previous slide

21 V. Order of Precedence 1.Parenthesis () 2.arithmetic and logical NOT +-/* ~ 3.relational operators <>== 4.logical AND & 5.logical OR | last two new with MATLAB 6 and above

22 Logical of a double vector Try the following >> a = [ -2, 3, 0.001, 0, 4, 0] >> logical(a) What happens when double is converted to logical? Now try >> find(a) What does this do?

23 Handout part VI. >> x = [-3,0,0,2,5,8]; >> y = [-5,-2,0,3,4,10]; >> z = y <~ x z = 1 1 1 0 0 0 >> zb = x & y zb = 1 0 0 1 1 1 >> zc = x | y zc = 1 1 0 1 1 1 >> zd = xor(x, y) zd = 0 1 0 0 0 0 What are the indices of the true values for z in part a. >> zi=find(z) zi = 1 2 3 What are the x & y values for the true cases in b. >> x(find(zb)) ans = -3 2 5 8 >> y(find(zb)) ans = -5 3 4 10

24 Discuss: Short cut operators – same result, speeds up execution for scalars (not for vectors) –A&&B => if A = 0, evaluates to false immediately –A||B => if A=1, evaluates to true immediately The find function => returns the indices of the nonzero elements e.g., z=find(x&y) … VII. If time allows, review other logical functions from Table 4.3-4 –All => many component and –Any => many component if –Ischar, isempty, isinf …


Download ppt "Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL."

Similar presentations


Ads by Google