18 - MATLAB Programming I

# 18 - MATLAB Programming I - Engineering 101 Quote of the...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Those who will not economize will have to agonize. - Confucius MATLAB Programming I Programming in MATLAB Just as in C++ we must have at our disposal: Sequence Iteration Selection Selection in MATLAB Three selection constructs: if switch/case try/catch To decide if a portion of code will be executed we need to determine a condition. This requires conditional operators Conditional Operators in MATLAB == ~= > >= < <= equal to not equal to greater than greater than or equal to less than less than or equal to Conditional Operators in MATLAB == ~= > >= < <= equal to not equal to greater than greater than or equal to less than less than or equal to These return 1 for true or 0 for false Be careful with the == operator. Since all numbers are doubles small rounding errors can make expressions not equal. 1 Conditional Operators in MATLAB == ~= > >= < <= equal to not equal to greater than greater than or equal to less than less than or equal to Conditional Operators Conditional operators can be used between arrays 1 5 > 3 1 3 -1 -1 2 For example, if 0 == sin (pi) returns 0 because sin(pi) returns 1.2246e1.2246e-016 Conditional Operators Conditional operators can be used between arrays 1 5 > 3 1 0 1 3 -1 -1 2 1 0 Conditional Operators Conditional operators can be used between arrays 1 5 > 3 1 0 1 3 -1 -1 2 1 0 They can also be used between a scalar and an array 5 > 8 2 10 -1 Conditional Operators Conditional operators can be used between arrays 1 5 > 3 1 0 1 3 -1 -1 2 1 0 They can also be used between a scalar and an array 5 > 8 2 0 1 10 -1 0 1 Logical Operators in MATLAB & | xor( ) ~ A 0 0 1 1 B 0 1 0 1 A&B 0 0 0 1 Logical AND Logical OR Logical Exclusive OR Logical NOT A|B 0 1 1 1 xor(A, B) 0 1 1 0 2 Other Logical Functions ischar(a) isempty(a) isinf(a) isnan(a) isnumeric(a) 1 if a is a character array 1 if a is an empty array 1 if the value of a is Inf (infinity) 1 if the value of a is NaN (not a number) 1 if a is a numeric array 1- 1 Exercise What's the result of: ([ 1 2; 4 5] < [2 5; 1 3]) | [1 0; 0 1] 0 0 2- 1 0 0 1 3- 1 1 0 1 4- 1 0 1 0 1 What is the result of: (zeros(2)==eye(2))*ones(2) 1- 0 0 0 2- 0 0 1 1 3- 1 0 0 0 4- 1 1 1 1 1 Exercise What does is output from the following: The If Construct in MATLAB The basic form of the if construct in MATLAB is: if control_exp1 statement1 statement2 etc. elseif control_exp2 statement 3 statement 4 etc. else statement 5 statement 6 end Every construct must have an if and an end. The other parts are optional. You can have multiple elseif clauses. 1 B = 3 0 0 0 7 10 0 0 -2 -2 0 1 -2 -2 0 0 2 B = 3 0 1 1 7 10 1 1 -2 0 0 1 0 -2 0 0 3 B = 3 0 1 0 7 10 1 1 -2 0 3 1 0 -2 7 10 Example of the If Construct x = input(`Enter a number'); if x > 0 disp(`The number is positive'); elseif x < 0 disp(`The number is negative'); else disp(`The number is zero'); end Nested If Constructs You can nest if constructs much as you could in C++ if ( pos(x+1, y) == 0) if lastmovex ~= -1 x=x+1; lastmovex = 1; end elseif (pos(x-1, y) == 0) (pos(xif lastmovex ~= 1; x=xx=x-1; lastmovex = -1; end end 3 The Switch Construct MATLAB also has a switch construct for cases in which you are choosing from a number of alternatives: switch (switch_expr) case case_expr_1 Statement 1 Statement 2 case case_expr_2 Statement 3 Statement 4 otherwise Statement 5 end The Switch Construct MATLAB also has a switch construct for cases in which you are choosing from a number of alternatives: switch (switch_expr) One case may case case_expr_1 multiple case Statement 1 expressions. Statement 2 case {case_expr_2, case expr_3, case_expr_4} Statement 3 Statement 4 otherwise Statement 5 end have After executing the particular case the code jumps to after the end. The otherwise part is optional. The Switch Construct switch (day) case {1, 7} disp(`Weekend'); case 2 disp(`Monday'); case 3 disp(`Tuesday'); case 4 disp(`Wednesday'); case 5 disp(`Thursday'); case 6 disp(`Friday'); otherwise disp(`out of range'); end The Try/Catch Construct There is a special construct in MATLAB for catching errors try Statement 1 Statement 2 catch Statement 3 Statement 4 end The try block is executed. If an error occurs it jumps to the catch block. trytest.m The Try/Catch Construct a = [2 5 7 10] try i = input(`Enter a subscript'); disp([`value at ` int2str( i ) ` = ` num2str(a(i))] ); catch disp( [`out of range:' int2str( i )] ); end Iteration in MATLAB There are two formal iteration constructs in MATLAB: while for But often the most efficient way to execute a loop in MATLAB is to vectorize the operation 4 While Loops in MATLAB Very similar to C++: while expression statement 1 statement 2 etc. end While Loops in MATLAB Very similar to C++: sum = 0; x = input(`Enter a number:'); while x>=0 sum = sum + x; x = input(`Enter a number:'); end disp( [`sum = ` num2str(sum)] ); For Loops in MATLAB ForFor-loops in MATLAB are a bit different than for-loops forin C++ for index = array Statement 1 Statement 2 etc. end index takes the value of each column in the array in turn. For Loops in MATLAB prod = 1; for n = 1:10 prod = prod * n; end prod = 10! For Loops in MATLAB prod = 1; for n = 1:2:10 prod = prod * n; end For Loops in MATLAB prod = 1; for n = [ 2 10 3 12 7 2 ] prod = prod * n; end prod = 1*3*5*7*9 prod = 2*10*3*12*7*-2 5 For Loops in MATLAB The control expression in a for-loop in forMATLAB can even be an array with more than one dimension. In this case the index takes on the value of an entire column one column at a time. Exercise What is the result in x after: a = [1 : 3; 2:2:6]; x = ones(2,1); for b = a x = x.*b; end 1- [ 3 48 ] 2- [ 3 12 ] 3- [ 6 48 ] 4- [ 6 12 ] break and continue There are two commands you can insert into a MATLAB loop to alter the flow of control. break will cause the program to immediately exit the loop continue will cause the program to skip to the end of the loop, but the loop will continue to iterate if it is not complete. break and continue for i = 1:2:11 if mod(i, 3)==0 continue; end display( i ); 1 end 5 7 11 for i = 1:2:11 if mod(i, 3) == 0 break; end display( i ); 1 end Next Lecture MATLAB Programming II 6 ...
View Full Document

## This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.

Ask a homework question - tutors are online