Engineering Fundamentals: An Introduction to Engineering, 4th ed.c

(Steven Felgate) #1

15.2 Using MATLAB Built-in Functions 475


TABLE 15.10 MATLAB’s Logical Operators


Logical
Operator Symbol Description

and & TRUE, If both conditions are true
or | TRUE, If either or both conditions are true
not  FALSE, If the result of a condition is TRUE, and
TRUE, If the result of a condition is FALSE
exclusive or xor FALSE, If both conditions are TRUE or both
conditions are FALSE, and TRUE, If either
condition is true
any TRUE, If any element of vector is nonzero
all TRUE, If all elements of vector is nonzero

Note that in the preceding example, the index is the integer i and its start-value is 1, it is in-
cremented by a value of 1, and its end-value is 5.

The whileLoop Using the whileloop, you can execute a line or a block of code until a specified
condition is met. The syntax of a whileloop is

while controlling-expression
a line or a block of your computer code
end

With the whilecommand, as long as the controlling-expression is true, the line or a block of
code will be executed. For the preceding example, the MATLAB code using the whilecommand
becomes:

x = 22.0;
while x <= 24.00
y=x^2+10;
disp([x',y'])
x = x + 0.5;
end

In the preceding example the symbol denotes less than or equal to and is called a rela-
tional or comparison operator. We will explain MATLAB’s logical and relational operators next.

Using MATLAB Logical and Relational Operators


In this section, we will look at some of MATLAB’s logical operators. These are operators that
allow you to test various conditions. MATLAB’s logical operators and their descriptions are
shown in Table 15.10.

MATLAB also offers relational or comparison operators that allow for the testing of rela-
tive magnitude of various arguments. These relational operators are shown in Table 15.11.

The Conditional Statements — if, else


When writing a computer program, sometimes it becomes necessary to execute a line or a block
of code based on whether a condition or set of conditions is met (true). MATLAB provides if
and elsecommands for such situations.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀

Free download pdf