# Programming Environment - PowerPoint PPT Presentation

1 / 19

Introduction to Matlab:. Programming Environment. Control Flow. S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn. Control Flow Topics. For Loop. While Loop. If-Elseif-Else-End. Switch. For Loop. For Loop Syntax:. for v = expression (or array)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Programming Environment

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

Introduction to Matlab:

## ProgrammingEnvironment

Control Flow

M. Corless, M.S.E.E.

E.C.E. Department

University of Michigan-Dearborn

### Control Flow Topics

• For Loop

• While Loop

• If-Elseif-Else-End

• Switch

### For Loop

• For Loop Syntax:

for v = expression (or array)

… commands …

end

• To Create Column Vector x = [12 22 32 42]’

» x = zeros(4,1);

» for i=1:4

x(i) = i*i;

end;

### Nested For Loops

• Create an m x n Hilbert Matrix

### 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

» aa = 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

Matlab Code

Results

### For Loop Array Example

» 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

### While Loops

• While Loop Syntax:

whileexpresion

:

: commands

:

end

### While Loop Log Example

• The MacLaurinseries expansion for log(1+x) (natural log), where |x| < 1, is given by:

• 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 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.4055log(1.5)

k =

46Number of Iterations

### While Loop Factorial Example

• 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

### Factorial Verification

• Verify the results

» prod(1:n)% n! = 70!

ans =

1.1979e+100

» prod(1:n-1)%(n-1)! = 69!

ans =

1.7112e+098

• Therefore, n=70 is the first integer where n! is a 100 digit number

One Alternative Expression Syntax

One Alternative Example

### One Alternative If - End

if expression

:

:Commands

:

end

» if a > 1

b=0;

c=5;

end

Two Alternative Expression Syntax

Two Alternative Example

### If-Else-End Constructions

if expression

:

:Command Set 1

:

else expression

:

:Command Set 2

:

end

» if a > 0

b=1;

else

b=-1;

end

Several Alternative Expression Syntax

### If-Elseif-Else-End Constructions

if expression

Command Set 1

elseif expression

Command Set 2

elseif expression

Command Set 3

else expression

Command Set 4

end

### Breaking Out of a Loop

• It is possible to break out of for loops & while loops using thebreak command

• When the break statement is executed, MATLAB jumps from the current loop in which it appears

• In the case of nested for or while loops, MATLAB jumps out of only the current loop

### Break eps Example

» EPS=1;

• Find the value of eps (floating point relative accuracy constant)

» for i=1:1000

EPS=EPS/2;

if(1+EPS)<=1

EPS=EPS*2;

break;

end

end

» EPS

EPS =

2.2204e-016NOTE: i=53

### Switch Case Constructions

• Used when a sequence of commands must be conditionally evaluated based on repeated use of an equality test with one common argument

switch expression

casetest_expression_1

command Set 1

casetest_expression_2

command set 2

otherwise

command set3

end

Scalar or Character string

### Switch Example 1

• Display a message based on the variable var

» var=3;

» 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

2 or 3 or 4

» x=2.7; units='m';

» switch units

» y

y =

270

### Switch Example 2

• Given measurement x in meters, Convert x to 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;

• Results in centimeters:

otherwise

disp(['Unknown Units:' units]);

y=nan;

end