Title: Week 6 - Programming I
1Week 6 - Programming I
- So far, weve looked at simple programming via
scripts programs of sequentially evaluated
commands - Today, extend features to
- additional operators
- branches to control operations
- loops to repeat operations
- Textbook chapter 7, pages 165-179, 182-187
(sections 7.1 7.2.2, 7.4, 7.4.1)
2Relational Operators the Idea
- In formal English someone might ask you
Is your age greater than or equal to 21? - Answers include
- Yes, of course
- Heres my ID card
- Im 18
- I knew this would happen if I forgot my ID
- No
3- Using mathematical notation, we test or compute
the relation - age 21 or age gt 21
- And expect 1 of only 2 answers
- Yes or True
- No or False
4Relational Operators in MATLAB
- A operator B
- A and B can be
- Variables or constants or expressions to compute
- Scalars or arrays (match the sizes on arrays!)
- Numeric or string
- Operators gt gt lt lt
- Result is true (1) or false (0) perhaps an
array
5Note value and class
6- More examples
- expression result
- 5 lt 7 1
- 3, 5, 2 gt 1, 0, 12 1 1 0
- max( 16 ) lt 7 1
- 3, pi, -12 gt 1 1 1 0
- 'Tom' 'Bob' 0 1 0
- 'Tom' 'm' 0 0 1
Note arrays and strings need to be the same
size
7- Notes
- Can compute using the result e.g.
- how many of a particular letter in a state
name?
8- Dont confuse and
- Round off errors can impact
- sind(0) 0 1
- sind(180) 0 0
-
- instead, test for small values
- abs( sind(180) ) lt eps 1
-
9Matlab has Logical Operators as Well
- A operator B
- A and B can be
- Variables or constants or expressions to compute
- Scalars or arrays, numeric or string
- A and B are interpreted as logical (binary)
- Numeric 0 is interpreted as false
- All else is interpreted as true (equal to 1)
- Result is true (1) or false (0) perhaps an array
10- Basic operators and or
- xor not
-
-
A B AB AB xor(A,B) A
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
truth table
unary operator
11- Examples
- Are you between 25 and 30 years old?
- (agegt25) (agelt30)
- Is it winter?
- (month12 daygt22) (month1) (month2)
(month3 daylt21)
12- Array example
- Score 70, 55, 88, 98, 80, 73, 90 C
(Score gt 70) (Score lt 81) - C 0 0 0 0 1 1 0
- Useful in counting how many entries satisfy a
condition - B_grades sum( Scorelt91 Scoregt80 )
13- Text examples
- 'Tom' 'm' 'Tom' 'o' 0 1 1
- name input('enter name','s')
- name 'Tom' name 'Bob'
- Rolling dice
- roll sum(ceil(6rand(1,2)))
- roll 7 roll 11
14- Other useful logical operators
- Extend and from binary to arrays
- any(X) all(X)
- To check array size, value, and data type
isempty(A) - isinf(A) isnan(A)
- ischar(A) isnumeric(A)
- To find the locations
- of events find( )
-
15Operator Precedence (left to right)
- Parentheses ( )
- Transpose(') and power(.)
- Negation (-) and logical negation ()
- Multiplication (.) and division (./),
- Addition () and subtraction (-)
- Colon operator ()
- Relational operators (lt, lt, gt, gt, , )
- Logical AND ()
- Logical OR ()
16Branches, Conditional Statements
- Commands to select and execute certain blocks of
code, skipping other blocks. - Three types in Matlab
- if/else
- switch
- try/catch
this week
17If/Else
- if expression
- commands if true
- else
- commands if false
- end
- Use relational and logical operators to determine
what commands to execute -
evaluate this
use of blue in editor also, auto indentation on
commands
18Example output whether a variable x is
positive or not x computed somehow
if x gt 0 disp('the value is
positive') else disp('the value is negative
or zero') end
19Example output a warning if the variable x
is negative (note that there is no else
portion in this example) x computed
somehow if x lt 0 disp('Warning negative
value') end
the else component is not required
20Example ask if a plot should be drawn x
input('Plot now? ', 's') if x 'yes' x
'YES' plot( .. ) end
more complicated expression to evaluate
21Example Write a script to put 2 numbers in
numerical order
22Loops
- Commands to repeatedly execute certain blocks of
code - Two types in Matlab
- for
- while
this week
23The for Loop
- Used for a specific number of repetitions of a
- group of commands
- for index array
- commands to be repeated go here
- end
- Rules
- One repetition per column of array
- index takes on the corresponding columns values
24- Example collect 7 digits of a telephone number
and store in an array -
-
7 repetitions since the array is 1 2 3 4 5 6 7
digit cycles through the 7 values to create the 1
by 7 array number
25(No Transcript)
26- Example calculating interest for 10 years
command num2str converts numerical variables to
string variables for concatenating with other
strings
27(No Transcript)
28Example implement a count down timer (in
seconds)
29(No Transcript)
30- Example a general vector for array
31Example a matrix for array
32- Example even a string array
33Errors in Your Scripts
Note red text bad news
But tells you where
34- Run-time errors inf or NaN results
Note black text okay, just a warning
35- Logical errors in your program hard to find
- Example quadratic equation solver
- But x22x1 (x1)2 ? x 1
- Use the built-in debugger
Missing parentheses around 2a
36Matlab Data Files (not in the text)
- Types
- .asv auto save
- ascii regular text files
- .mat Matlabs proprietary format (multiple
variables)