19
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

Programming Environment

  • Upload
    maia

  • View
    74

  • Download
    1

Embed Size (px)

DESCRIPTION

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) - PowerPoint PPT Presentation

Citation preview

Page 1: Programming Environment

ProgrammingEnvironment

S. Awad, Ph.D.

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

E.C.E. Department

University of Michigan-Dearborn

Introduction to Matlab:

Control Flow

Page 2: Programming Environment

2

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

Control Flow Topics

For Loop While Loop If-Elseif-Else-End Switch

Page 3: Programming Environment

3

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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;

Page 4: Programming Environment

4

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

Nested For Loops Create an m x n Hilbert Matrix

1

1

1

1

33.05.0

5.01

2

1

1

1),(

21

nm

ji

m

ijijia

nj

Page 5: Programming Environment

5

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

Page 6: Programming Environment

6

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

For Loop Array Example

Matlab Code 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

Page 7: Programming Environment

7

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

While Loops

while expresion :

: commands

:

end

While Loop Syntax:

Page 8: Programming Environment

8

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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 The MacLaurin series expansion for log(1+x)

(natural log), where |x| < 1, is given by:

1

1)1()1log(k

kk

k

xx

Page 9: Programming Environment

9

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

Page 10: Programming Environment

10

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

Page 11: Programming Environment

11

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

Page 12: Programming Environment

12

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

One Alternative If - End

One Alternative Expression Syntax

One Alternative Example

if expression

:

:Commands

:

end

» if a > 1

b=0;

c=5;

end

Page 13: Programming Environment

13

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

If-Else-End Constructions Two Alternative

Expression Syntax

Two Alternative Example

if expression

:

:Command Set 1

:

else expression

:

:Command Set 2

:

end

» if a > 0

b=1;

else

b=-1;

end

Page 14: Programming Environment

14

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

If-Elseif-Else-End Constructions

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

Page 15: Programming Environment

15

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

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

Page 16: Programming Environment

16

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

Break eps Example 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

end» EPS

EPS =

2.2204e-016 NOTE: i=53

Page 17: Programming Environment

17

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

case test_expression_1

command Set 1

case test_expression_2

command set 2

otherwise

command set3

end

Scalar or Character string

Page 18: Programming Environment

18

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

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

Page 19: Programming Environment

19

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Control Flow

Switch Example 2» x=2.7; units='m';

» switch units

» y

y =

270

Given measurement x in meters, Convert x to centimeters

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