0% found this document useful (0 votes)
21 views

Week 1-2 (2)

The document provides an overview of various rounding functions in MATLAB, including fix(), floor(), ceil(), and round(), along with examples of their usage. It also covers random number generation techniques, including the use of rand and randi functions, and introduces basic mathematical functions available in MATLAB. Additionally, it discusses relational expressions, logical operators, and the structure of vectors and matrices in MATLAB.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Week 1-2 (2)

The document provides an overview of various rounding functions in MATLAB, including fix(), floor(), ceil(), and round(), along with examples of their usage. It also covers random number generation techniques, including the use of rand and randi functions, and introduces basic mathematical functions available in MATLAB. Additionally, it discusses relational expressions, logical operators, and the structure of vectors and matrices in MATLAB.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

1

Rounding functions
fix() - Round to the nearest integer towards zero.
floor() - Round to the nearest integer towards minus infinity.
ceil() - Round to the nearest integer towards plus infinity.
round() - Round towards nearest integer.

• Example 1: >> fix(3.1415) • Example 2: >> floor(-3.1415)

ans = ans =
3 - 4
• Example 3: >> ceil(3.1415) • Example 4: >> round(-3.1415)

ans = ans =

4 - 3

2
Rounding functions
fix() - Round to the nearest integer towards zero.
floor() - Round to the nearest integer towards minus infinity.
ceil() - Round to the nearest integer towards plus infinity.
round() - Round towards nearest integer.

• Example 5: >> fix(-2.98) • Example 6: >> floor(0.1415)

ans = ans =
- 2 0
• Example 7: >> round(0.5)  In the case of a tie, where an
element has a fractional part of
ans = exactly 0.5, the round function
rounds away from zero to the
1 integer with larger magnitude.
3
Random numbers

• The simplest built-in random function is “rand”

rand - generate a random number between 0 and 1:(0,1)


open interval

• How do we generate a random real number in the range from 0


to 8, exclusive?

>> rand*8  To generate a random real number


in the open interval (0,N):
ans =

7.3070
rand * N

4
Random numbers

• The simplest built-in random function is “rand”

rand - generate a random number between 0 and 1:(0,1)


open interval

• How do we generate a random integer in the range from 0 to 10,


inclusive?
>> round(rand*10)
round rounds “up” the
ans = random number to an integer

5
Random numbers

• Other built-in random functions

randi(max) - generate a random integer: 1 ≤ x ≤ max.


randi([min,max]) - generate a random integer: min ≤ x ≤ max.

• Example: Generate an integer in the inclusive range from 1 to 100:

>> randi(100)

ans =

93

6
Random numbers

• Other built-in random functions

randi(max) - generate a random integer: 1 ≤ x ≤ max.


randi([min,max]) - generate a random integer: min ≤ x ≤ max.

• Example: Generate an integer in the inclusive range from 20 to 35:

>> randi([20,35])

ans =

27

7
Some Functions in elfun
>> help elfun

Function Description
sin, cos, tan sine, cosine, tangent in radians

sind, cosd, tand sine, cosine, tangent in degree


fix, floor, ceil, rounding functions
round
log(x) natural logarithm (base e (Euler’s number))
log2(x) base 2 logarithm
log10(x) base 10 logarithm
exp(x) ex
sqrt(x) x
8
Some Functions in elfun
Function Description
sign(x) sign(x)
Returns:
1 1 if x>0
x 0 if x=0
-1 -1 if x<0

rem (a,b) both return the remainder from division a/b


mod (a,b)
>> rem(14,5)

ans = 4
• Note 1: The order of arguments in rem and mod does matter.
• Note 2: Don’t confuse the value e (Euler’s number) with the e used
in MATLAB to specify an exponent for scientific notation.
>> 10e3
ans = 9
10000
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

Operator name
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
~= not equal to

The relational operators in MATLAB 10


Relational Expressions: relational
operators

• Example: >> 10 < 18

ans =
1

• Example: >> 2 ~= 4

ans =

11
Relational Expressions: relational
operators

• Example: >> 10 < 18

ans =
1

>> ans + 3

ans =
4

• Although these are logical values, mathematical operations could be


performed on the resulting 1 or 0.

12
Relational Expressions: relational
operators

• Example: >> 9 > 8 > 7  Expressions are evaluated


from left to right
1 > 7

ans =
0

• Example: >> 9 > 8 > (7 < 6)

ans =

13
Review: Operator Precedence Rules

• Just like normal mathematical computations, some operators


have precedence over others in MATLAB.

operator name precedence


( ) Parentheses • Highest

^ Exponentiation
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction • Lowest

14
Operators Precedence Rules (Update 1):

operator name precedence


( ) Parentheses • Highest

^ Exponentiation or power
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction
<, <=, >, relational • Lowest
>=, ==, ~=

15
Relational Expressions: relational
operators
• Example: >> 9 > 8 > (7 < 6)

9 > 8 > 0

1 > 0
Note: Be careful about using
equality and inequality
ans = operators.
1
Occasionally roundoff errors
appear!
• Example: >> cos (pi/2) == 0
>> cos (pi/2)
ans =
0 ans =
6.1232e-17
16
Relational Expressions: logical operators
x
y
• The logical operators in MATLAB are:
||
Operator name
|| or Switch Representation
&& and of OR Function
x y
~ not

&&
• The “or” logical operator will output a true
value if either or both of the operands are true.
Switch Representation
• The “and” logical operator will output a true of AND Function
value only if both of the operands are true.
17
Relational Expressions: logical operators
x
y
• Summary: Truth Table for logical operators:
||

OR Function
x y ~x x || y x && y
True True False True True x y
True False False True False
False True True True False &&
False False True False False
AND Function

18
Relational Expressions: logical operators
• 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 .

• Example:
According to the truth table, if any
>> b = 0; operands of an AND expression are
false, the entire expression must be
>> (b ~= 0) && (a/d > 18)
false. So, if (b ~= 0) evaluates to false,
MATLAB assumes the entire
0 expression to be false and terminates
ans = its evaluation of the expression early.
0
19
Relational Expressions

• Example: >> 3 < 8 || 3 > 8

1 || ...

MATLAB doesn’t evaluate the rest


ans =

20
Operators Precedence Rules:

operator name precedence


( ) Parentheses • Highest

^ Exponentiation or power
-, ~ Negation, not
*, /, \ Multiplication and division
+, - Addition and subtraction
<, <=, >, relational
>=, ==, ~=
&& and
|| or • Lowest

21
Relational Expressions
• Example: operator
>> 3 == 3 + 3
( )
ans =
^
0
-, ~

• Example: >> 10 < 5 ^ 2 / 2 - 1 || 3 > 8 *, /, \


+, -
25 / 2 0
<, <=, >,
>=, ==, ~=
12.5 - 1
&&
10 < 11.5 ||

1 || 0
ans =
22
1
Relational Expressions
• Example:
What would be the value of this expression
>> -5 < x < 3

If the value of x is 0?
What if the value of x is -10?

>> -5 < x < 3


Reminder: Expressions are always
evaluated from left to right.
Either True (1) or False (0)

1 < 3 or 0 < 3

ans = Logical Error!


23
1
Relational Expressions

• Question:

if we want an expression that is logical true, which means x is in the


range from -5 to 3, how can we write this in MATLAB?

 5 x 3

>> -5 < x && x < 3

24
Relational Expressions

• Comparing characters (e.g. a, b, c) is also possible. Characters


are compared using their ASCII equivalent value

• Example: >> 'a' < 'd'

ans =

http://www.asciitable.com/

25
Common Errors
• Putting space in a variable name

• Confusing the format of an assignment statement (make sure that


the variable name is always on the left (x=6 , not 6=x)
• Forgetting to use parentheses to pass an argument to a function
(e.g., typing “fix 2.3” instead of “fix(2.3)”)
• Confusing the order of arguments passed to functions (e.g., using
rem (3,10) instead of rem (10,3) to find the remainder of 10/3)
• Using = instead of ==
• Putting a space in two-character operators (e.g. < = instead of <=)
26
27
Vectors and Matrices
• Vectors and matrices are used to store set of values.

• Vectors and matrices can be visualized as a table of values.

• The dimensions of a matrix are r × c, where r is the number of rows and c is the
number of columns.
3x2
1x1 1x4

28
Scalars
• A scalar in MATLAB is an array of size 1 x 1
1

• Example:
>> z = 6 a variable that stores a single value is an
example of a scalar
z =

29
Vectors
• A vector in MATLAB is equivalent to a 1-dimensional array where one of the
size of the dimensions is 1
• There are: row vectors (1×n) and column vectors (n×1)

row vector column vector


1x4
3x1

30
Row Vectors
• A row vector in MATLAB is a 1 x n array

• Example:
>> v = [1 2 3 4]

v =

1 2 3 4

>> v = [1, 2, 3, 4]

v =

1 2 3 4

31
Row Vectors
• MATLAB workspace after a row vector has been assigned:

32
Row Vectors: Colon Operator
• The colon operator (:) can be used to iterate values in a vector

• Format:
Vector_variable_name = initial value : step: final value

step size
• Example 1:

>> v = 1:2:7 Results in all of the integers


from 1 to 7 with the step of 2.

v =

1 3 5 7

33
Row Vectors: Colon Operator
• Example 2:
>> v = 1:6 If “step” is omitted, the default step size is 1

v =

1 2 3 4 5 6

34
Row Vectors: Colon Operator
• Example 3:
>> v = 1:3:12

v =

1 4 7 10 Adding 3 to 10 would go beyond 12,


so the vector stops at 10

35
Row Vectors: Colon Operator
• Example 4:

>> start = 20;


>> step = -3;
>> final = 6;
v = start:step:final

v =
Observe that the final value is not
20 17 14 11 8 guaranteed to be at the end of the
vector

36
Row Vectors: Linspace
• linspace(x,y,n) function creates a vector with n values in the inclusive
range from x to y. It creates a vector with n values that are linearly
spaced.

• If n is omitted, the default is 100 points


• If n = 1, linspace(x,y,1) returns y

• Example : >> v = linspace(1,10,4)


This creates a vector
with 4 values linearly
v =
spaced between 1 and
10, including 1 and 10.
1 4 7 10

37
Concatenating the vectors
• Vectors can be created by joining together existing vectors, or adding
elements to existing vectors

• Example :
>> v1 = 2:5;
>> v2 = 1:3;
>> v_total = [v1 v2]

v_total =

2 3 4 5 1 2 3

From v1 From v2

38

You might also like