SUNY Geneseo Department of Mathematics
Friday, February 24
Math 230 02
Spring 2017
Prof. Doug Baldwin
Yu-Min Chung
Eleni Panagiotou
Both speakers are faculty candidates
Extra credit for writing a paragraph or so summarizing connections you make between either or both talks and your own interests.
Monday (Feb. 27)
Material since the beginning of the semester (e.g., Matlab arithmetic, functions, variables, scripts, vectors, plotting, matrices, etc.)
3 to 5 short-answer questions (i.e., questions requiring about a paragraph of prose, 5 - 10 lines of code, etc. to answer)
You’ll have the whole class period to do the test
Open book, open notes, open computer to the extent you use the computer as a reference source. But you can’t communicate in real time with anyone except me during the test.
Comparisons. Given a vector of numbers, count how many of them are negative.
Logical indexing. Given a vector of numbers, pick out the ones that are negative.
Ideas from reading:
Matlab code that finds, extracts, and counts the negative numbers in a vector:
>> % Count negative numbers in a vector.
>> numbers = [ 1, 5, -3, 0, -9, 6, -3 ]
numbers =
1 5 -3 0 -9 6 -3
>> numNeg = numbers < 0
numNeg =
0 0 1 0 1 0 1
>> negValues = numbers( numNeg )
negValues =
-3 -9 -3
>> % Beware that users can't enter logical values as 0 or 1, users have to
>> % use true and false:
>> numbers( [0 0 0 0 1 0 1] )
Subscript indices must either be real positive integers or logicals.
>> numbers( [ true, true, false, false, false, true, true ] )
ans =
1 5 6 -3
>> numNeg
numNeg =
0 0 1 0 1 0 1
>> sum( numNeg )
ans =
3
Truth tables (Try 5.2.1). Write Matlab commands that print the final columns of truth tables for and, or, not, and exclusive or.
Reading ideas:
Matlab code for the the truth tables:
>> % Start with and
>> true & false
ans =
0
>> [ false false true true ] & [ false true false true ]
ans =
0 0 0 1
>> % or
>> [ false false true true ] | [ false true false true ]
ans =
0 1 1 1
>> % exclusive or
>> xor( [ false, false, true, true ], [false true false true] )
ans =
0 1 1 0
>> % not
>> ~ [false true]
ans =
1 0
Challenge. Given a vector of numbers, compute a vector of absolute values without using the abs
function.
Ideas: we already used the <
operator to find places where there are negative numbers. Multiplying those numbers by -1 would yield their absolute values. But we can’t just multiply by the result of the less-than comparison, because that zeros out the positive numbers. Instead, we need to multiply everything that’s not negative by 1. The logical negation of the less-than results provides these ones, which we can add to the vector of negative ones to get something we can multiply the numbers by:
>> numbers
numbers =
1 5 -3 0 -9 6 -3
>> numNeg
numNeg =
0 0 1 0 1 0 1
>> negators = - numNeg
negators =
0 0 -1 0 -1 0 -1
>> numbers .* negators
ans =
0 0 3 0 9 0 3
>> numPos = ~ numNeg
numPos =
1 1 0 1 0 1 0
>> flip = negators + numPos
flip =
1 1 -1 1 -1 1 -1
>> numbers .* flip
ans =
1 5 3 0 9 6 3
(After exam)
“If” statements
Read sections 11.1 - 11.3 (11.2 and 11.3 are reviews of relational and logical operators, but with examples using “if” statements)