• 沒有找到結果。

PROGRAMMING IN MATLAB

在文檔中 This page intentionally left blank (頁 66-70)

where I is the identity matrix

2.16 PROGRAMMING IN MATLAB

One most significant feature of MATLAB is its extendibility through user-written pro-grams such as the M-files. M-files are ordinary ASCII text files written in MATLAB language.

A function file is a subprogram.

2.16.1 Relational and Logical Operators

A relational operator compares two numbers by finding whether a comparison state-ment is true or false. A logical operator examines true/false statestate-ments and produces a result which is true or false according to the specific operator. Relational and logical operators are used in mathematical expressions and also in combination with other commands, to make deci-sion that control the flow a computer program.

MATLAB has six relational operators as shown in Table 2.21.

Table 2.21 Relational operators Relational operator Interpretation

< Less than

<= Less than or equal

> Greater than

>= Greater than or equal

= = Equal

~ = Not equal

The logical operators in MATLAB are shown in Table 2.22.

Table 2.22 Logical operators

Logical operator Name Description

& AND Operates on two operands (A and B). If both are true, Example: A & B the result is true (1), otherwise the result is false (0).

| OR Operates on two operands (A and B). If either one, or Example: A | B both are true, the result is true (1), otherwise (both are

false) the result is false (0).

~ NOT Operates on one operand (A). Gives the opposite of the Example: ~ A operand. True (1) if the operand is false, and false (0) if

the operand is true.

2.16.2 Order of Precedence

The following Table 2.23 shows the order of precedence used by MATLAB.

Table 2.23

Precedence Operation

1 (highest) Parentheses (If nested parentheses exist, inner have precedence).

2 Exponentiation.

3 Logical NOT (~).

4 Multiplication, Division.

5 Addition, Subtraction.

6 Relational operators (>, <, >=, <=, = =, ~=).

7 Logical AND (&).

8 (lowest) Logical OR (|).

2.16.3 Built-in Logical Functions

The MATLAB built-in functions which are equivalent to the logical operators are:

and(A, B) Equivalent to A & B or(A, B) Equivalent to A | B not(A) Equivalent to ~ A

List the MATLAB logical built-in functions are described in Table 2.24.

Table 2.24 Additional logical built-in functions

Function Description Example

xor(a, b) Exclusive or. Returns true (1) if one >>xor(8, – 1) operand is true and the other is false ans =

0

>>xor(8, 0) ans =

1

all(A) Returns 1 (true) if all elements in a vector >>A = [5 3 11 7 8 15]

A are true (nonzero). Returns 0 (false) if >>all(A) one or more elements are false (zero). If ans = A is a matrix, treats columns of A as 1

vectors, returns a vector with 1’s and 0’s. >>B = [3 6 11 4 0 13]

>>all(B) ans =

0

Function Description Example any(A) Returns 1 (true) if any element in a vector A >>A = [5 0 14 0 0 13]

is true (nonzero). Returns 0 (false) if all >>any(A)

elements are false (zero). ans =

If A is a matrix, treats columns of A as vectors, 1

returns a vector with 1’s and 0’s. >>B = [0 0 0 0 0 0 ]

>>any(B) ans =

0

find(A) If A is a vector, returns the indices of the non- >>A = [0 7 4 2 8 0 0 3 9]

zero elements. >>find(A)

find(A > d) If A is a vector, returns the address of the ans =

elements that are larger than d (any relational 2 3 4 11 8 9 operator can be used). >>find(A > 4)

ans = 4 5 6

The truth table for the operation of the four logical operators, and, or, Xor, and not are summarized in Table 2.25.

Table 2.25 Truth table

INPUT OUTPUT

A B AND OR XOR NOT NOT

A & B A | B (A, B) ~ A ~ B

false false false false false true true

false true false true true true false

true false false true true false true

true true true true false false false

2.16.4 Conditional Statements

A conditional statement is a command that allows MATLAB to make a decision of whether to execute a group of commands that follow the conditional statement or to skip these com-mands.

If conditional expression consists of relational and/or logical operators if a < 30

count = count + 1 disp a

end

The general form of a simple if statement is as follows:

if logical expression statements end

If the logical expression is true, the statements between the if statement and the end statement are executed. If the logical expression is false, then it goes to the statements follow-ing the end statement.

2.16.5 Nested if Statements

Following is an example of nested if statements:

if a < 30

count = count + 1;

disp(a);

if b > a b = 0 ; end

end

2.16.6 else AND elseif Clauses

The else clause allows to execute one set of statements if a logical expression is true and a different set if the logical expression is false.

% variable name inc if inc < 1

x_inc = inc/10;

else

x_inc = 0.05;

end

When several levels of if-else statements are nested, it may be difficult to find which logical expressions must be true (or false) to execute each set of statements. In such cases, the elaseif clause is used to clarify the program logic.

2.16.7 MATLAB while Structures

There is a structure in MATLAB that combines the for loop with the features of the if block. This is called the while loop and has the form:

while logical expression

This set of statements is executed repeatedly as long as the logical expressions remain true (equals + 1) or if the expression is a matrix rather than a simple scalar variable, as long as all the elements of the matrix remain nonzero.

end

In addition to the normal termination of a loop by means of the end statement, there are additional MATLAB commands available to interrupt the calculations. These commands are listed in Table 2.26 below:

Table 2.26

Command Description

break Terminates the execution of MATLAB for and while loops. In nested loops, break will terminate only the innermost loop in which it is placed.

return Primarily used in MATLAB functions, return will cause a normal return from a function from the point at which the return statement is executed.

error (‘text’) Terminates execution and displays the message contained in text on the screen. Note, the text must be enclosed in single quotes.

The MATLAB functions used are summarized in Table 2.27 below:

Table 2.27

Function Description

Relational A MATLAB logical relation is a comparison between two variables x operators and y of the same size effected by one of the six operators, <, <=, >, >=,

= =, ~=. The comparison involves corresponding elements of x and y, and yields a matrix or scalar of the same size with values of “true” or

“false” for each of its elements. In MATLAB, the value of “false” is zero, and “true” has a value of one. Any nonzero quantity is interpreted as

“true”.

Combinatorial The operators & (AND) and | (OR) may be used to combine two logical operators expressions.

all, any If x is a vector, all(x) returns a value of one if all of the elements of x are nonzero, and a value of zero otherwise. When X is a matrix, all(X) returns a row vector of ones or zeros obtained by applying all to each of the columns of X. The function any operates similarly if any of the elements of x are nonzero.

find If x is a vector, i = find(x) returns the indices of those elements of x that are nonzero (i.e., true). Thus, replacing all the negative elements of x by zero could be accomplished by

i = find(x < 0);

x(i) = zeros(size(i));

If X is a matrix, [i, j] = find(X) operates similarly and returns the

在文檔中 This page intentionally left blank (頁 66-70)