ans

advertisement
EECS 1541 -- Introduction to Computing for the Physical Sciences
Week 2: Variables and Assignment Statements
•
READING: 1.4 – 1.6
1
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
• 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:
Constant names
pi
Meaning/value
π or 3.14159…
i
j
inf
∞
2
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
• What will be the final answer of the following expression?
>> 2 * pi + - + pi
ans =
3.1416
3
returns an n-by-n matrix of pseudorandom normal values
EECS 1541 -- Introduction to Computing for the Physical Sciences
Random numbers
• Several built-in functions in MATLAB to generate random numbers
such as:
rand
randi(max)
randi([min,max])
- generate a random number between 0 and 1.
- generate a random integer: 1 ≤ x ≤ max.
- generate a random integer: min ≤ x ≤ max.
• The simplest built-in random function is “rand”
4
EECS 1541 -- Introduction to Computing for the Physical Sciences
Random numbers
• Example:
>> rand
ans =
0.8715
• Note that there is no input argument required for the “rand”
function
• 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)?
5
EECS 1541 -- Introduction to Computing for the Physical Sciences
Rounding functions
• Rounding functions:
fix
floor
ceil
round
• Example:
-
Round
Round
Round
Round
towards
towards
towards
towards
>> fix(3.1415)
ans =
zero.
minus infinity.
plus infinity.
nearest integer.
• Example:
>> floor(-3.1415)
ans =
3
- 4
6
EECS 1541 -- Introduction to Computing for the Physical Sciences
Rounding functions
• Rounding functions:
fix
floor
ceil
round
• Example:
-
Round
Round
Round
Round
towards
towards
towards
towards
>> ceil(3.1415)
ans =
zero.
minus infinity.
plus infinity.
nearest integer.
• Example:
>> round(-3.1415)
ans =
4
- 3
7
EECS 1541 -- Introduction to Computing for the Physical Sciences
Random numbers
• Recall: how do we generate a random integer greater than or equal
to 0 but less than 10 (i.e. 0 ≤ x < 10)?
• One method: We can combine the “fix” and “rand” functions
>> fix(rand*10)
rand*10 gives a random number
between 0 and 10
fix rounds “down” the random number to an integer
8
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions
• 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
9
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: relational operators
• 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
10
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: relational operators
• Example:
>> 10 < 8 - 5
ans =
0
• Example:
>> (10 < 8) - 5
ans =
-5
11
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: relational operators
• Example:
>>
9 > 8 > 7 > 6
ans =
0
12
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions
• Comparing characters (e.g. a, b, c) is also possible. Characters
are compared using their ASCII equivalent value
• Example:
>> ‘a’ < ‘d’
ans =
1
13
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
• 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.
14
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
• The || and && operators in MATLAB are also known as shortcircuit 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.
15
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
• Example:
>> 3 < 8 || 3 > 8
ans =
1
• Example:
>> 10 < 8 - 5 && 3 < 8
ans =
0
16
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
• Example:
>> (10 < 8) -5 || 3 > 8
ans =
1
• Example:
>> ‘b’ < ‘c’ - 1 && 3 < 8
ans =
0
17
EECS 1541 -- Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators
• 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
18
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Plotting functions
• Plotting 2-D or 3-D graphs is a powerful function provided in
MATLAB.
• One of the popular built-in functions in MATLAB is the “plot”
function:
>> help plot
• We will look into more details about plotting graphs in MATLAB in
Chapter 3 and Lab #2.
19
Download