View
212
Download
0
Category
Tags:
Preview:
Citation preview
1
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
2
• Recall that variables are used to store values that might change
• Constants are values that cannot be changed at any time. Some constants that are pre-defined in MATLAB are:
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
3
• What will be the final answer of the following expression?
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> 2 * pi + - + pi
ans =
3.1416
Random numbers
4
• Several built-in functions in MATLAB to generate random numbers such as:
• The simplest built-in random function is “rand”
EECS 1541 -- Introduction to Computing for the Physical Sciences
rand - generate a random number between 0 and 1.
randi(max) - generate a random integer: 1 ≤ x ≤ max.
randi([min,max]) - generate a random integer: min ≤ x ≤ max.
returns an n-by-n matrix of pseudorandom normal values
Random numbers
5
• Example:
• Note that there is no input argument required for the “rand” function
>> rand
ans =
0.8715
• Since “rand” returns a random real number between 0 and 1, how do we generate a random integer greater than or equal to 0 but less than 10 (i.e. 0 ≤ x < 10)?
EECS 1541 -- Introduction to Computing for the Physical Sciences
Rounding functions
6
• Rounding functions:
fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> fix(3.1415)
ans =
3
• Example: >> floor(-3.1415)
ans =
- 4
7
Rounding functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> ceil(3.1415)
ans =
4
• Example: >> round(-3.1415)
ans =
- 3
• Rounding functions:
fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
Random numbers
8
• Recall: how do we generate a random integer greater than or equal to 0 but less than 10 (i.e. 0 ≤ x < 10)?
>> fix(rand*10)
• One method: We can combine the “fix” and “rand” functions
rand*10 gives a random number between 0 and 10
fix rounds “down” the random number to an integer
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions
9
• Expressions that are conceptually either true or false are called relational expressions, or Boolean or logical expressions
• “true” is represented by the logical value 1, and “false” is represented by the logical value 0
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: relational operators
10
• The relational operators in MATLAB are:
Operator name
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
~= not equal to
EECS 1541 -- Introduction to Computing for the Physical Sciences
11
• Example:
Relational Expressions: relational operators
• Example:
>> 10 < 8 - 5
ans =
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> (10 < 8) - 5
ans =
-5
0
12
• Example:
Relational Expressions: relational operators
>> 9 > 8 > 7 > 6
ans =
EECS 1541 -- Introduction to Computing for the Physical Sciences
0
Relational Expressions
13
• Comparing characters (e.g. a, b, c) is also possible. Characters are compared using their ASCII equivalent value
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> ‘a’ < ‘d’
ans =
1
Relational Expressions: logical operators
14
• The logical operators in MATLAB are:
Operator name
|| or
&& and
~ not
• The “or” logical operator will output a true value if either or both of the operands are true.
• The “and” logical operator will output a true value only if both of the operands are true.
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
15
• The || and && operators in MATLAB are also known as short-circuit operators.
• This means that if the result of the expression can be determined from the first part, then the second part will not even be evaluated.
EECS 1541 -- Introduction to Computing for the Physical Sciences
16
• Example: >> 3 < 8 || 3 > 8
ans =
Relational Expressions: logical operators
• Example: >> 10 < 8 - 5 && 3 < 8
ans =
1
0
EECS 1541 -- Introduction to Computing for the Physical Sciences
17
• Example: >> (10 < 8) -5 || 3 > 8
ans =
Relational Expressions: logical operators
• Example: >> ‘b’ < ‘c’ - 1 && 3 < 8
ans =
1
0
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
18
• Summary: Truth Table for logical operators:
x y ~x x || y x && y
True True False True True
True False False True False
false false true false False
EECS 1541 -- Introduction to Computing for the Physical Sciences
19
• One of the popular built-in functions in MATLAB is the “plot” function:
Built-in Functions: Plotting functions
>> help plot
• Plotting 2-D or 3-D graphs is a powerful function provided in MATLAB.
EECS 1541 -- Introduction to Computing for the Physical Sciences
• We will look into more details about plotting graphs in MATLAB in Chapter 3 and Lab #2.
Recommended