Matlab Chapter7 PDF
Matlab Chapter7 PDF
Faculty of Engineering
Civil Engineering Department
1
7.1 Relational and Logical Operators
• == Equal to
• ~= Not equal to
• < less than
• > greater than
• <= Smaller than or equal to
• >= Greater than equal to
2
Notes:
• If the statement is true, it is assigned a value to 1.
• if the statement is false, it is assigned a value to 0.
3
4
Logical Vector s
5
Logical vectors and arrays:
• Differ from numerical vectors and arrays in which they
can be used for addressing.
• They can be used in arithmetic operations. Once a
logical vector or array is used in arithmetic operation I is
changed to a numerical vector or array.
6
7
Logical Operators
Logical Name Description
operator
& AND A&B A and B
• If both are true (nonzero), the result is true
(1),
• otherwise the result is false (0).
| OR A|B A or B
• If either one, or both are true, the result is
true (1),
• otherwise (both are false) the result is false
(0)
~ NOT ~ A not A
• Operates in one operand. Gives the
opposite of the operand.
• True (1) if the operand is false and false
(0) if the operand is true. 8
Logical operators (like relational operators) are used:
• In addressing arrays.
9
10
Order of precedence
Precedence Operation
1 (highest) Parentheses
2 Exponentiation
3 Not (~)
4 Multiplication, Division
5 Addition, subtraction
6 Relational operators (>, <,<=, >=, ==, ~=)
7 and (&)
8(lowest) Or ( | )
11
12
Built-in logical functions
• MATLAB has built functions that are equivalent to the
logical operators.
• and(A,B) equivalent to A&B.
• or(A,B) equivalent to A|B.
• not(A) equivalent to ~A.
13
Function Description Example
16
7.2 Conditional statements
• Allow MATLAB to make a decision of whether to execute or skip the
commands follow the conditional statement.
If statement
• Conditional expression consists of relational and/or logical
operation.
• Must have and end statement.
Examples:
• If a < b
• If c >= 5
• If a == b
• If a=~ b
• If (d<h) & (x>7)
• If (x~=13) | (y<0)
17
The if-end structure
………..
……….. MATLAB program
if conditional statement
…………
MATLAB commands
…………
end
………….
…………. MATLAB program
18
The if-else-end structure
………..
……….. MATLAB program
if conditional statement
…………
MATLAB commands 1
…………
else
…………
MATLAB commands 2
…………
end
………….
MATLAB program
………….
………..
……….. MATLAB program
if conditional statement
…………
………… MATLAB commands 1
elseif conditional statement
…………
………… MATLAB commands 2
else
…………
MATLAB commands 3
…………
end
………….
MATLAB program
…………. 20
Example (Problem 7.9)
21
22
7.3 The switch-case statement
………..
MATLAB program
………..
switch switch expression
case value1
…………
MATLAB commands 1
…………
case value2
…………
MATLAB commands 2
…………
case value3
…………
………… MATLAB commands 3
otherwise
………..
MATLAB commands 4
………..
end
………….
MATLAB program
…………. 23
Notes:
1. The switch expression is a variable that is:
– a scalar,
– a string,
– or a mathematical expression that includes preassigned
variables and can be evaluated.
24
Example
25
26
27
7.4 Loops
• In a loop, the commands are repeated several times.
Every execution or round is called a pass.
28
7.4.1 for-end loops
29
Notes:
• The loop index variable can have any variable name.
• The increment can be negative ( i.e k=10:-2:4)
• If the increment value is omitted, the value is 1.
• If t = f, the loop is executed once.
• If f>t and s>0, the loop is not executed.
• If f<t and s<0, the loop is not executed.
• If the values of s, s, t are such k cannot be equal to t :
i.e k = 8:10:50 k = 8, 18, 28, 38, 48 (largest value that is smaller
than t)
k = 20:-5:3 k = 20, 15, 10, 5 (smallest value that is larger than t)
• The values of k cannot be predefined as a vector. For k = [2 5 7 9 8].
• Each for loop must have an end.
• When the loop ends, the loop index variable (k) has the value that was
last assigned to it.
30
31
Sample problem 7-5, p 210
a) Calculate the sum of the first n terms of the series:
n
(1) k k
k 1 2 k
32
2 k 1
(1) x
k
b) Calculate sin(x) by using the Taylor series: sin x
k 0 ( 2k 1)!
33
Sample problem 7-6, p 212
34
7.4.2 while – end loop
……….. The while – end loops are
……….. MATLAB program used when looping is
needed but the number of
while conditional statement
passes is not known.
…………
…………MATLAB commands The looping process
end continues until a stated
…………. condition is false.
…………. MATLAB program
35
Sample problem 7-7, p 215 n
x
• Calculate f(x) = ex represented in Taylor series: ex
n 0 n!
• Stop the loop when the absolute value of the term added is
smaller than 0.0001 or n>=30.
36
37
7.5 Nested loops and nested conditional
statements
Every time k
……….. increasing by 1,
……….. MATLAB commands 1 the nested loop
for k = 1:n executed m
for h = 1:m times.
…………
………… Group of
Nested Loop Overall, the group
………… commands Loop
…………
of commands are
end executed nxm
end times.
………….
…………. MATLAB program
38
Sample problem 7-8, p 217
39
40
7.6 the break and continue command
41
1. The continue command
2. Used inside the loop to stop the present
pass and start the next pass in the loop.
42
Problem 7.15
43