1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS 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: Constant namesMeaning/value pi π or … i j inf ∞ EECS Introduction to Computing for the Physical Sciences
Constants 3 What will be the final answer of the following expression? EECS Introduction to Computing for the Physical Sciences >> 2 * pi pi ans =
Random numbers 4 Several built-in functions in MATLAB to generate random numbers such as: The simplest built-in random function is “ rand ” EECS 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 = 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 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 Introduction to Computing for the Physical Sciences Example: >> fix(3.1415) ans = 3 Example: >> floor( ) ans = - 4
7 Rounding functions EECS Introduction to Computing for the Physical Sciences Example: >> ceil(3.1415) ans = 4 Example: >> round( ) 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 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 Introduction to Computing for the Physical Sciences
Relational Expressions: relational operators 10 The relational operators in MATLAB are: Operatorname > greater than >= greater than or equal to < less than <= less than or equal to == equal to ~= not equal to EECS Introduction to Computing for the Physical Sciences
11 Example: Relational Expressions: relational operators Example: >> 10 < ans = EECS 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 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 Introduction to Computing for the Physical Sciences Example: >> ‘a’ < ‘d’ ans = 1
Relational Expressions: logical operators 14 The logical operators in MATLAB are: Operatorname || 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 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 Introduction to Computing for the Physical Sciences
16 Example: >> 3 8 ans = Relational Expressions: logical operators Example: >> 10 < && 3 < 8 ans = 1 0 EECS Introduction to Computing for the Physical Sciences
17 Example: >> (10 8 ans = Relational Expressions: logical operators Example: >> ‘b’ < ‘c’ - 1 && 3 < 8 ans = 1 0 EECS Introduction to Computing for the Physical Sciences
Relational Expressions: logical operators 18 Summary: Truth Table for logical operators: xy~xx || yx && y True FalseTrue False TrueFalse false truefalseFalse EECS 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 Introduction to Computing for the Physical Sciences We will look into more details about plotting graphs in MATLAB in Chapter 3 and Lab #2.