Control Flow Statements

Embed Size (px)

DESCRIPTION

Kendali Aliran Program (Perulangan dan Pengujian)

Citation preview

Making Decisions with MATLAB

15 Points Possible

Very often, models that you develop as an engineer will not always be as trivial as the simple evaluation of a series of mathematical expressions. In each of the previous exercises from last week you were asked to develop M-file functions that evaluated a mathematical expression (or a series of mathematical expressions) and then asked to plot them using the fplot command. This week we will introduce the capability of making decisions with MATLAB in order to build more complex M-file models. Figure 1 shown below is a flow chart representation of the payment M-file function you defined last week to calculate the monthly payment on a loan given the loan amount, the annual interest rate, and the term of the loan as input data. If you remember that a M-file function is nothing more than a series of MATLAB commands that are executed in sequence when the M-file function is used in MATLABs command window, its easy to visualize the execution of each of these commands as a flow of execution from one statement to the next until there are no more statements in the M-file for MATLAB to execute.

FLOW OF

EXECUTION

Figure 1: Flow Chart Representation of M-file FunctionThe way MATLAB, and programming languages in general, makes decisions is by altering the flow of execution within a M-file function definition. If some condition is met, then the flow of execution is altered in order to include the execution of a set of MATLAB commands that would have otherwise been ignored (not executed by MATLAB) if the condition was not met. Simply put, IF some condition is true then do something. MATLAB provides a special statement that we can include in a M-file definition to make decisions based on some condition being true -- an IF statement. Figure 2, shown on the next page, shows the flow chart representation of MATLABs IF statement and the syntax necessary to define it inside a MATLAB M-file function definition.

Figure 2: The MATLAB IF StatementNote that the flow of execution travels along the TRUE path if the condition is true and each of the MATLAB commands 1 to N (there can be any number) are executed. If, however, the condition is false, the flow of execution travels along the FALSE path which bypasses the MATLAB commands that are part of the IF statement and they are ignored and not executed. In the MATLAB syntax, if condition is true then MATLAB executes all the statements that are between the if and end lines and resumes execution with the line following the end statement. If condition is false, the statements between the if and end lines are ignored and execution immediately proceeds with the statement following the end statement. The idea behind the MATLAB IF statement is really pretty simple. Do something (execute a series of MATLAB commands) if a condition is true, otherwise proceed on. The question that remains is how does MATLAB decide whether a condition is true or not? This is accomplished by conditional expressions.

Conditional Expressions

Conditional expressions are simply expressions that compare the values of variables or expressions using the set of relational operators identified in the table below. OperatorDescription

=Greater Than or Equal To

==Equal To

~=Not Equal To

Table 1: MATLAB Relational Operators

Notice that each operator shown in the table above represents the simple process of comparing two values by determining whether or not the values are less than, less than or equal to, greater than, greater than or equal to, equal to, or not equal to each other. The following M-file definition of a piecewise-defined function (remember what a piecewise-defined function is from algebra?) will demonstrate the use of the MATLAB IF statement and the relational operators shown in the table above.

Piecewise-Defined Function

x2 for x < 2f(x) = x +6 for x > 2

In order to develop a M-file function to evaluate the piecewise-defined function f(x), our M-file function must determine which expression to use based on the condition that x must be less than 2 to use x2 and x must be greater than or equal to 2 to use the expression x +6. The MATLAB IF statement is a perfect fit for this type of problem. The M-file definition for this piecewise-defined function is given below (there is a better way to write this function using a variation of the IF statement but well worry about improving the function later)..

MATLAB M-file Definition

function y = pwise(x)

if x < 2

y=x^2;

end

if x >= 2

y=-x+6;

end

Figure 3: M-File Definition of Piecewise-Defined Function

Note that the M-file definition shown in Figure 3 is very straightforward. If x is less than 2 then y = x2. If x is greater than or equal to 2, then y = x +6.

Now lets change the function definition to take advantage of a variation of MATLABs IF statement that can simplify the M-file implementation considerably. Notice that if the value of x is not less than 2, then the value of the piecewise-defined expression will always be calculated using the expression y = x +6 regardless of the value of x. In this situation, we can take advantage of the variation of the IF statement shown in Figure 4 on the following page. MATLABs IF/ELSE statement is a perfect fit for this situation. If x < 2 then y = x2, otherwise y = -x + 6 and no additional comparison of the value of x is necessary.

Figure 4: The MATLAB IF/ELSE StatementMATLAB Syntax for IF Statement with True and False

If condition

MATLAB Command 1;

MATLAB Command 2;

MATLAB Command 3;

MATLAB Command 4;

else MATLAB Command A;

MATLAB Command B;

MATLAB Command C;

MATLAB Command D;

end

Note that in Figure 4, the flow of execution travels the TRUE path if the condition is true, otherwise the flow of execution travels along the FALSE path as shown. Figure 5, shown below shows the MATLAB M-file using the IF/ELSE statement to define the same piecewise-defined function shown in Figure 3.

Note that to use the IF/ELSE statement, every point on the number line must be included!

Note also that we use semicolons on ALL MATLAB statements except the IF Condition, else, and end statements.

Piecewise-Defined Function

x2 for x < 2f(x) = x +6 for x > 2

MATLAB M-file Definition

function y = pwise(x)

if x < 2

y = x^2;

else

y = -x+6;

end

Figure 5: M-File Definition Using IF/ELSE StatementLets change the function definition to use a slightly different set of domains for each of the functions that define the piecewise-defined function f(x) as shown below. Now our M-file function must satisfy two conditions in order to use the appropriate expression when evaluating the piecewise-defined function f(x). In the first case, x must be greater than or equal to zero AND less than 2 in order for the expression to be used. In the second case, -x + 6 will be used if x is greater than or equal to 2 AND less than 10. In each case, a combination of conditions must be satisfied in order for the appropriate expression to be used. Fortunately, MATLAB provides this capability through the use of the logical operators shown in Table 2 below.

Piecewise-Defined Function

x2 for 0 < x < 2f(x) = x +6for 2 < x < 10OperatorArithmetic Description

&AND

|OR

Table 2: MATLAB Logical Operators

In order to satisfy two conditions in a MATLAB IF statement, simply use the AND operator (&) if both conditions must be satisfied and the OR operator (|) if either or both of the conditions must be satisfied as shown in the Figure 6 below. Note that the variable x is repeated for each condition.MATLAB M-file Definitionfunction y = pwise(x)

if 0