Sei sulla pagina 1di 10

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W.

Southern Avenue Mesa, Arizona 85202

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. function pay = payment(loan,term,rate)

n = term * 12; FLOW OF EXECUTION

I = rate/1200;

pay = loan * i/(1-(1+i)^-n;

Figure 1: Flow Chart Representation of M-file Function The 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

Page 1

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

chart representation of MATLABs IF statement and the syntax necessary to define it inside a MATLAB M-file function definition. Flow of Execution

If Condition

TRUE

FALSE

MATLAB Command Line 1 MATLAB Command Line 2

MATLAB Command Line 3

MATLAB Command Line 4

Figure 2: The MATLAB IF Statement Note 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.

Page 2

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

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. Operator < <= > >= == ~= Description Less Than Less Than or Equal To Greater Than 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 piecewisedefined 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 f(x) = x +6 for x > 2 for x < 2

In order to develop a M-file function to evaluate the piecewise-defined function f(x), our Mfile 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). .

Page 3

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

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.

Page 4

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

Flow of Execution

FALSE

If Condition

TRUE

MATLAB Command Line A

MATLAB Command Line 1

MATLAB Command Line B

MATLAB Command Line 2

MATLAB Command Line C

MATLAB Command Line 3

MATLAB Command Line D

MATLAB Command Line 4

Figure 4: The MATLAB IF/ELSE Statement MATLAB 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

Page 5

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

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 f(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 Statement Lets 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. for x < 2

Page 6

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

Piecewise-Defined Function x2 f(x) = x +6 for 2 < x < 10 Operator & | Arithmetic Description AND OR for 0 < x < 2

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 Definition function y = pwise(x) if 0 <= x & x < 2 y=x^2; end if 2 <= x & x <= 10 y = -x+6; end Figure 6: M-File Definition Using Logical Operators Note that in order to satisfy more than one condition, simply combine the conditions together using the & or | operator as shown above. To check if 2 < x < 10, use the conditional expression 2 <= x & x <= 10. Why was the IF/ELSE variation not used in this example? The reason is that we had to satisfy an additional condition in order for the second expression to be used in the piecewise function.

Page 7

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

Using Multiple Conditions Piecewise-Defined Function x2 f(x) = x3 + 9 for 10 < x < 15 x4 -16 for x > 15 The elseif statement is used for multiple conditions for the variable x. There is no limit to the number of elseif statements that can be used. Note that an else (without conditions) is used for the last statement since the conditions encompass the remainder of the number line. MATLAB M-file Definition function y = pwise(x) if x < 2 y=x^2; elseif 2 <= x & x <= 10 y = -x + 6; elseif 10 < x & x <= 15 y = x^3 + 9; else y = x^4 16; end Figure 7: M-File Definition Using Elseif Summary of IF Statement Use 1) IF/END Statement - Statements in TRUE leg only 2) IF/ELSE/END Statement Statements in both TRUE and FALSE legs. All numbers accounted fo on the number line. 3) IF/ELSEIF/ELSEIF//END Multiple conditions in a piecewise function for x < 2

x +6 for 2 < x < 10

Page 8

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

Assignment Complete each of the following exercises. For each exercise, print out a copy of each Mfile and each plot to turn in. 1) Develop a M-file function for the following piecewise-defined function. Plot the function for x ranging from -10 to +5. 3e sin x 3x f(x) = 6 0 for 2 < x < 5 for x > 5
x 5

for x < 0 for 0 < x < 2

2) Develop a M-file function for the following piecewise-defined function. Plot the function for x ranging from 0 to 15. -x3 + 16x2 -65x + 50 -19x + 134 f(x) = -37
37 2 296 x x + 555 9 3

for x < 6 for 6 < x < 9 for 9 < x < 12 for x > 12

3) Develop an M-file function implementation of the flow chart shown below for the bestDesign function that can be used to find the optimal design for the space shuttle insect container problem discussed in class.

Page 9

ECE 102 Engineering Analysis Tools and Techniques Instructor: Bruce Carlton Physical Sciences Department 1833 W. Southern Avenue Mesa, Arizona 85202

function [max,rmax,hmax]=bestdesign(L,W,H,start,stop)

r=start; max=0; FALSE TRUE

r<stop

hmax=1000/(pi*rmax^2);

num=insectContainers(L,W,H,r); TRUE num>max FALSE End for If goes here max=num;

End of your program End for While goes here

rmax=r;

r=r+0.0001;

When you have completed your M-file definition of the bestDesign function using the flow chart above, use it to verify your solution obtained previously by executing it at the command line using the following command. Print out a copy of the Command Window and your M-file to turn in for the following Cases 1, 2, and 3. Case 1: [max,rmax,hmax]=bestDesign(50,30,100,1,20) Case 2: [max,rmax,hmax]=bestDesign(100,30,50,1,20) Case 3: [max,rmax,hmax]=bestDesign(30,100,50,1,20)

Page 10

Potrebbero piacerti anche