Download presentation
Presentation is loading. Please wait.
Published byMartin Hodge Modified over 9 years ago
2
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow
3
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 2 Control Flow Topics n For Loop For Loop n While Loop While Loop n If-Elseif-Else-End If-Elseif-Else-End n Switch Switch
4
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 3 For Loop n For Loop Syntax: for v = expression (or array) … commands … end n To Create Column Vector x = [1 2 2 2 3 2 4 2 ]’ » x = zeros(4,1); » for i=1:4 x(i) = i*i; end;
5
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 4 Nested For Loops n Create an m x n Hilbert Matrix
6
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 5 Nested For Loops Code » m = 4;n = 5; » a = zeros(m,n); » for i=1:m, for j=1:n a(i,j)=1/(i+j-1); end end » a a = 1.0000 0.5000 0.3333 0.2500 0.2000 0.5000 0.3333 0.2500 0.2000 0.1667 0.3333 0.2500 0.2000 0.1667 0.1429 0.2500 0.2000 0.1667 0.1429 0.1250
7
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 6 For Loop Array Example n Matlab Code n Results » data = [3 9 4 5; 7 16 -1 5]; » for n = data x = n(1) - n(2) end x = -4 First Column in data x = -7 Second Column in data x = 5 Third Column in data x = 0 Fourth Column in data
8
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 7 While Loops while expresion : : commands : end n While Loop Syntax:
9
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 8 Estimate log(1+x) for x=0.5 by summing the series until the term to be added next is less than eps (floating point relative accuracy constant) in absolute value While Loop Log Example n The MacLaurin series expansion for log(1+x) (natural log), where |x| < 1, is given by:
10
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 9 While Loop Log Code » v=0;x=0.5;k=1; » while abs((x^k)/k) >= eps v = v+(-1)^(k+1)*((x^k)/k); k = k+1; end » v,k=k-1 v = 0.4055 log(1.5) k = 46 Number of Iterations
11
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 10 While Loop Factorial Example n What is the first integer n for which n! (n factorial) is a 100 digit number? » n = 1; » while prod(1:n) < 1.e100 n = n + 1; end= » n n = 70
12
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 11 Factorial Verification n Verify the results » prod(1:n)% n! = 70! ans = 1.1979e+100 » prod(1:n-1)%(n-1)! = 69! ans = 1.7112e+098 n Therefore, n=70 is the first integer where n! is a 100 digit number
13
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 12 One Alternative If - End n One Alternative Expression Syntax n One Alternative Example if expression : :Commands : end » if a > 1 b=0; c=5; end
14
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 13 If-Else-End Constructions n Two Alternative Expression Syntax n Two Alternative Example if expression : :Command Set 1 : else expression : :Command Set 2 : end » if a > 0 b=1; else b=-1; end
15
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 14 If-Elseif-Else-End Constructions n Several Alternative Expression Syntax if expression Command Set 1 elseif expression Command Set 2 elseif expression Command Set 3 else expression Command Set 4 end
16
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 15 Breaking Out of a Loop It is possible to break out of for loops & while loops using the break command When the break statement is executed, MATLAB jumps from the current loop in which it appears n In the case of nested for or while loops, MATLAB jumps out of only the current loop
17
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 16 Break eps Example n Find the value of eps (floating point relative accuracy constant) » EPS=1; » for i=1:1000 EPS=EPS/2; if(1+EPS)<=1 EPS=EPS*2; break; end » EPS EPS = 2.2204e-016 NOTE: i=53
18
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 17 Switch Case Constructions n Used when a sequence of commands must be conditionally evaluated based on repeated use of an equality test with one common argument switch expression case test_expression_1 command Set 1 case test_expression_2 command set 2 otherwise command set3 end Scalar or Character string
19
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 18 Switch Example 1 » var=3; Display a message based on the variable var 2 or 3 or 4 » switch var case 1 disp('1'); case {2,3,4} disp('2 or 3 or 4'); case 5 disp('5'); otherwise disp('something else'); end
20
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 19 Switch Example 2 » x=2.7; units='m'; » switch units » y y = 270 Given measurement x in meters, Convert x to centimeters n Results in centimeters: case {'feet','ft'} y=x*2.54*12; case {'inch','in'} y=x*2.54; case {'meter','m'} y=100*x; case {'centimeter','cm'} y=x; case {'millimeter','mm'} y=x/10; otherwise disp(['Unknown Units:' units]); y=nan; end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.