Download presentation
Presentation is loading. Please wait.
Published byErick Evans Modified over 9 years ago
1
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS 1541 -- Introduction to Computing for the Physical Sciences
2
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 3.14159… i j inf ∞ EECS 1541 -- Introduction to Computing for the Physical Sciences
3
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
4
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
5
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
6
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
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.
8
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
9
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
10
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 1541 -- Introduction to Computing for the Physical Sciences
11
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
12 Example: Relational Expressions: relational operators >> 9 > 8 > 7 > 6 ans = EECS 1541 -- Introduction to Computing for the Physical Sciences 0
13
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
14
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 1541 -- Introduction to Computing for the Physical Sciences
15
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
16 Example: >> 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
17 Example: >> (10 8 ans = Relational Expressions: logical operators Example: >> ‘b’ < ‘c’ - 1 && 3 < 8 ans = 1 0 EECS 1541 -- Introduction to Computing for the Physical Sciences
18
Relational Expressions: logical operators 18 Summary: Truth Table for logical operators: xy~xx || yx && y True FalseTrue False TrueFalse false truefalseFalse EECS 1541 -- Introduction to Computing for the Physical Sciences
19
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.