Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CSCI-1190
Beginning Programming for Engineers
Today’s class
o Lecture 3
Assignment
o Homework 3 on Grader
o zyBook 3 Activity on zyBooks
Lab 3 on Thursday
o Available at the end of the lecture time
1
CSCI-1190 Dr. Liu
Lecture 3
CSCI BP Liu 2
1
3/24/2019
CSCI BP Liu 3
CSCI BP Liu 4
2
3/24/2019
https://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html
Function Example
function [c] = pythagf (a, b)
% The pythagf calculates hypotenuse
c = sqrt(a^2 + b^2);
end
CSCI BP Liu 7
3
3/24/2019
CSCI BP Liu 8
4
3/24/2019
CSCI BP Liu 10
A function must define return variable(s) for the caller to use the
result from the function.
CSCI BP Liu 11
5
3/24/2019
6
3/24/2019
CSCI BP Liu 15
CSCI BP Liu 16
7
3/24/2019
CSCI BP Liu 17
8
3/24/2019
CSCI BP Liu 19
9
3/24/2019
CSCI BP Liu 21
10
3/24/2019
Question
CSCI BP Liu 23
11
3/24/2019
CSCI BP Liu 26
Only one of
them is
displayed.
CSCI BP Liu 27
12
3/24/2019
https://www.smartdraw.com/flowchart/flowchart-symbols.htm
CSCI BP Liu 28
13
3/24/2019
“ if ” Statement
● Keyword if must pair with end
if condition 1
● A condition is a relational or logical expression
statement 1
resulting in a logical value: true or false
elseif condition 2
● Could have multiple conditions by using elseif
statement 2
● The nth condition is checked only when the
elseif condition n conditions before it (1 to n -1) are all false
statement 3
● If the nth condition is true, the conditions after it
... will not be evaluated
else ● The last else doesn’t have a conditional expression
statement 4 ● Only one or none statement is executed
end (None statement is executed if all conditions result
CSCI BP in
Liu a false value and else is not used) 30
if Statement Example
CSCI BP Liu 31
14
3/24/2019
if Statement Example
score =input('input your score number: ')
if score >=90
display('Excellent!')
elseif score >=80 & score <90 % do not need to evaluate score < 90
display('Very Good!')
elseif score >=70 & score <80 % do not need to evaluate score < 80
display('Not to bad')
elseif score >=60 & score <70 % do not need to evaluate score < 80
display(‘Passed')
else
display('Why?')
end CSCI BP Liu 32
Relational Operators
Relational Interpretation
Operators
a == b a equal to b ?
a ~= b a not equal to b ?
a<b a less than b ?
a <= b a less than or equal to b ?
a>b a greater than b ?
a >= b a greater than or equal to b ?
The two operands of a relational operator, such as, a and b above, must have
equal size matrices. 33
CSCI BP Liu 33
15
3/24/2019
CSCI BP Liu 35
16
3/24/2019
CSCI BP Liu 36
17
3/24/2019
Logical Operators
Logical operators operate on logical data type
Logical operations can combine multiple relational
expressions
Example: a > 5 & a < 10
Logical operators
Operator Interpretation
& and
| or
~ not
CSCI BP Liu 38
18
3/24/2019
19
3/24/2019
CSCI BP Liu 42
CSCI BP Liu 43
20
3/24/2019
Example 1:
A mathematic expression: 16 < usrAge < 25
MATLAB code implementation of the above formula
o Must uses logical operators to combine relational operations
(usrAge > 16) & (usrAge < 25)
Example 2:
Mathematic expressions:
a x b, or ab
MATLAB code:
a*b
CSCI BP Liu 44
21
3/24/2019
https://www.mathworks.com/help/matlab/ref/switch.html
CSCI BP Liu 46
22
3/24/2019
CSCI BP Liu 49
23
3/24/2019
save 'filename'
Saves all variables in the workspace to filename.mat
save 'filename' x y z
Saves the variable x, y, and z to filename.mat
whos
Lists all the variables in the current workspace.
whos -file 'filename'
Lists all the variables save in filename.mat
CSCI BP Liu 50
load 'filename'
Load all the variables stored in filename.mat into the
workspace. (Existing Workspace variables not stored in
filename.mat are unchanged.)
load 'filename' x y z
Load the variables x, y, and z from filename.mat into the
workspace.
CSCI BP Liu 51
24
3/24/2019
Examples:
inclass(1).m or inclass(2).m, …, filename(n).m
The above file names are Invalid in MATLAB. You must remove
the (#) portion by renaming it, or some other ways.
CSCI BP Liu 52
25