Introduction To MATLAB For Engineers, Third Edition
Introduction To MATLAB For Engineers, Third Edition
PowerPoint to accompany
Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
Chapter 4
4-2
Structured Programming
A technique for designing programs in which a hierarchy
of modules is used, each having a single entry and a
single exit point, and in which control is passed
downward through the structure without unconditional
branches to higher levels of the structure.
In MATLAB these modules can be built-in or userdefined functions.
4-3
4-4
4-5
4-7
4-8
4-9
4-10
4-11
Flowchart representation of
the if statement.
Figure 4.12, page 151
4-12
Finding Bugs
4-14
4-15
4-16
4-17
Relational operators
3. Testing of the first complete program (the alpha release
phase). This is usually done only in-house by technical
people closely involved with the program development.
There might be several alpha releases as bugs are
discovered and removed.
4-18
Less than.
Less than or equal to.
Greater than.
Greater than or equal to.
Equal to.
Not equal to.
4-19
4-20
Meaning
4-21
z = 5 > 2 + 7
x = (5 > 2)
is equivalent to
they create a logical variable, in this case, x.
z = 5 >(2+7)
Prior to MATLAB 6.5 logical was an attribute of any
numeric data type. Now logical is a first-class data
type and a MATLAB class, and so logical is now
equivalent to other first-class types such as
character and cell arrays.
4-24
4-23
4-25
Logical operators
Specifying array subscripts with logical arrays extracts the
elements that correspond to the true (1) elements in the
logical array.
Given A =[5,6,7;8,9,10;11,12,13] and B =
logical(eye(3)), we can extract the diagonal elements
of A by typing C = A(B) to obtain C = [5;9;13].
Name
Definition
NOT
&
AND
OR
4-26
4-27
Operator
Name
Definition
&&
Short-Circuit AND
||
Short-Circuit OR
4-28
4-29
First
Second
Third
Fourth
Logical AND.
Fifth
Logical OR.
Logical function
Definition
Logical function
all(x)
ischar(A)
all(A)
any(x)
any(A)
isfinite(A)
4-30
Definition
Returns a 1 if A is a character array
and 0 otherwise.
Returns a 1 if A is an empty matrix and
0 otherwise.
Returns an array of the same
dimension as A, with ones where
A has inf and zeros elsewhere.
Returns an array of the same
dimension as A with ones where
A has NaN and zeros elsewhere.
(NaN stands for not a
number, which means an undefined
result.)
isempty(A)
isinf(A)
isnan(A)
4-31
4-32
Returns a 1 if A is a numeric
array and 0 otherwise.
Returns a 1 if A has no
elements with imaginary parts
and 0 otherwise.
Converts the elements of the
array A into logical values.
Returns an array the same
dimension as A and B; the new
array has ones where either A
or B is nonzero, but not both,
and zeros where A and B are
either both nonzero or both
zero.
Computes an array
containing the indices of
the nonzero elements of
the array A.
[u,v,w] = find(A)
4-33
Note that the find function returns the indices, and not the
values.
Note that the find function returns the indices, and not the
values.
4-34
4-35
The if Statement
if logical expression
statements
end
if logical expression
statement group 1
else
statement group 2
end
4-36
4-37
4-39
4-38
x = [4,-9,25];
if x >= 0
y = sqrt(x)
else
disp(Some of the elements of x are
negative.)
end
x = [4,-9,25];
if x < 0
disp(Some of the elements of x are
negative.)
else
y = sqrt(x)
end
4-40
y =
2
0 + 3.000i
5
4-41
The statements
if logical expression 1
if logical expression 2
statements
end
end
4-42
if x > 10
y = log(x)
elseif x >= 0
y = sqrt(x)
else
y = exp(x) - 1
end
4-44
4-45
>>number = 123
number =
123
4-46
4-49
4-47
for Loops
for k = 5:10:35
x = k^2
end
Flowchart of a
for Loop.
Note the following rules when using for loops with the loop
variable expression k = m:s:n:
Figure 4.51,
page 172
4-51
4-52
We can often avoid the use of loops and branching and thus
create simpler and faster programs by using a logical array
as a mask that selects elements of another array. Any
elements not selected will remain unchanged.
The following session creates the logical array C from the
numeric array A given previously.
x = [10,1000,-10,100];
y = NaN*x;
for k = 1:length(x)
if x(k) < 0
continue
end
y(k) = log10(x(k));
end
y
4-53
4-54
1 0 1
1 0 1
0 1 1
4-55