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

Matlab Chapter7 PDF

Here is the MATLAB code to calculate the sum of the first n terms of the given series: sum = 0; for k = 1:n sum = sum + ((-1)^k)*k^2; end b) Calculate the sum of the first n terms of the series: n k  k 1 2 k

Uploaded by

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

Matlab Chapter7 PDF

Here is the MATLAB code to calculate the sum of the first n terms of the given series: sum = 0; for k = 1:n sum = sum + ((-1)^k)*k^2; end b) Calculate the sum of the first n terms of the series: n k  k 1 2 k

Uploaded by

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

The Islamic University of Gaza

Faculty of Engineering
Civil Engineering Department

Computer Programming (ECIV 2302)


Chapter 7: Programming in MATLAB

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.

• If two scalars are compared, the result is a scalar 1 or 0.

• If two arrays (same size) are compared, the comparison is done


element by element and the result is a logical array of the same size
with 1’s and 0’s according to the outcome of the comparison.

• If a scalar is compared with an array, the scalar is compared with


every element of the array, and the result is a logical array of the
same size with 1’s and 0’s according to the outcome of the
comparison of each element.

3
4
Logical Vector s

Vector t consists of elements of r in


positions where s has 1’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.

Order of precedence in a mathematical expression that


includes relational and mathematical operation:
• The arithmetic operation (+,-,*,/,\) have precedence over
relational operations.
• The relational operators themselves have equal
precedence and are evaluated from left to right.

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:

• As arithmetic operators within a mathematical


expression.

• In addressing arrays.

• To control the flow of the program.

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.

• MATLAB has other logical built-in functions. See the next


table.

13
Function Description Example

xor(a, b) True (1) if one operand is >> xor (7,0)


true and the other is false. ans =
1
>> xor (7,-5)
ans =
0
all(A) • True (1) if all elements in a >> A =[6 2 15 9 7 11];
vector A are true (nonzero) >> all(A)
ans =
 False (0) if one or more 1
elements are false (zero) >> B=[6 2 15 9 0 11];
>> all(B)
 If A is a matrix, treats ans =
columns of A as vectors, 0
returns a vector with 1’s and
0’s.
14
Function Description Example

any(A)  True (1) if any element in a >> A =[6 0 15 0 0 11];


vector A is true (nonzero). >> any(A)
 False (o) if all elements are ans =
false (zero). 1
>> B =[0 0 0 0 0 0];
>> any(B)
ans =
0
find(A)  If A is a vector, returns the >> A =[0 9 4 3 7 0 0 1 8];
indices of the nonzero elements. >> find(A)
ans =
find(A>d)  If A is a vector, returns the 2 3 4 5 8 9
address of the elements that are >> find(A>4)
larger than d (any relational ans =
operator can be used). 2 5 9
15
• See sample problem 7-1 p 199

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

• See sample problem 7-2 p 201

18
The if-else-end structure

………..
……….. MATLAB program
if conditional statement
…………
MATLAB commands 1
…………
else
…………
MATLAB commands 2
…………
end
………….
MATLAB program
………….

• See sample problem 7-3 p 203


19
The if-elseif-else-end structure

………..
……….. 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.

2. There are one or several case commands, each has a value


(scalar or string)

3. How does the switch case statement work?


• The value of the switch expression in the switch command is
compared with values that are next to the case statement.
• Only one group of commands is executed, which follow the match
case statement.
• If there is more than one match, only the first matching case is
executed.

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.

• In each pass, all variables that are defined within the


loop are assigned new values.

28
7.4.1 for-end loops

Value of k in the fist pass


Loop index variable Increment in k after each pass

Value of k in the last pass


for k = f : s : t
…………
…………
…………
end
………….
………….

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

1. The break command


• Inside for or while loop, the break command terminates
the execution of the whole loop.
(MATLAB jumps to the end command and continue with
the next command).
• Inside a nested loop, only the nested loop is terminated.
• Outside a loop in a script or function file, it terminates
the execution of file.
2. The continue command
• Used inside the loop to stop the present pass and start
the next pass in the loop.

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

You might also like