MatLab - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

MatLab

Description:

The number of passes is NOT known a priori SYNTAX: while logical expression statements end See p. 223 for flowchart of while loop. while LOOP FLOW CHART while ... – PowerPoint PPT presentation

Number of Views:161
Avg rating:3.0/5.0
Slides: 29
Provided by: CivilEngi98
Category:
Tags: matlab | flowchart | loop

less

Transcript and Presenter's Notes

Title: MatLab


1
MatLab Palm Chapter 4, Part 3For and While
Loops
  • Class 11.1 Palm Ch 4.5

2
RAT 11.1
  • As in INDIVIDUAL you have 2 minutes to answer the
    following question.
  • What is the FINAL value assigned to y in the
    following segment of MATLAB code?
  • x 0
  • while x lt 5
  • y sqrt(x)
  • x x 1
  • end
  • When asked, you have 30-seconds to submit your
    paper.
  • Answer y 2

3
Learning Objectives
  • Students should be able to
  • Understand Matlab iteration structuresfor end
    Loops
  • while end Loops
  • Identify Implied loops
  • Recognize these structures as repetition (or
    iteration) structures.
  • Develop flow (logic) diagrams for combinations of
    branching and looping structures

4
LOOPS
  • A LOOP is a structure for repeating (iterating) a
    block of code.
  • MATLAB uses two types of loops.
  • for loop number of passes known a priori (p.
    210).
  • while loop loop terminates when a specified
    condition is satisfied (p. 221).

5
for LOOPS
  • SYNTAX
  • for k startstepstop
  • block
  • end
  • start initial value
  • step incremental value
  • stop terminating value
  • See p. 212 for flowchart of for loop.

Indentation Mandatory!
6
for LOOP FLOW CHART
Loop structure
7
EXAMPLE
  • for k 51035
  • x k2
  • end

8
for end Loops (p. 211)
  • for k 51035
  • x k2
  • end
  • Means
  • starting with k 5, execute the statements
    between for end
  • then increment k by 10 and execute the statements
    with k 15 etc. until k gt 35.
  • Then exit the loop (k? at this point)

9
for end Loops contd.
  • Step value may be negative (default is positive
    1)
  • For step gt 0, loop does not execute if start gt
    stop
  • For step lt 0, loop does not execute if start lt
    stop
  • If start stop, loop executes once
  • Non-integer step values give unpredictable
    results (dont use them!)

10
INDIVIDUAL EXERCISE (5 min.)
  • Evaluate xn sin (np/10) for n between 1 and 10
    (in increments of 1).
  • Write a Matlab PROGRAM.
  • (Plot x versus n)
  • Save file on R\ and name file after an
    historical figure.

11
SOLUTION
  • for n 110
  • x(n) sin(npi/10)
  • end
  • plot(110,x) or
  • plot(x)
  • title(In-class
  • example)
  • xlabel(n)
  • ylabel(x(n))

12
for Loops
  • for loops can be nested (one inside each other).
  • Each loop is closed with an end.
  • EXAMPLE Proper indention is a MUST!!
  • N 10
  • for I 1N
  • for J 1N
  • A(I,J) 1/(I J 1)
  • end
  • end
  • Why is I OK and i not OK?

13
for Loops
  • for loops can be nested (one inside each other).
  • Each loop is closed with an end.
  • EXAMPLE Proper indention is a MUST!!
  • N 10
  • for r 1N
  • for c 1N
  • A(r,c) 1/(r c 1)
  • end
  • end
  • Using r (for row) and c (for col) is MUCH
    better!!!

14
In-class Assignment 11.1 (20 min.)
  • Work through the nested loop and if example on
    page 212.
  • Write an program file to solve T4.5-1 (p. 213)
  • Save your solutions on the R\ drive
  • Name your files after an animal.

15
Solution
  • PROGRAM example1.m
  • Solution to T4.5-1 on p. 213 of the Palm
  • Matlab book.
  • S. Socolofsky
  • ENGR 111A 501-503
  • for r 14
  • for c 13
  • A(r,c) 4 (r-1)6 (c-1)4
  • end
  • end

16
The break Statement
  • You can EXIT out of any looping structure with a
    break (Both for and while)
  • y0
  • x1 -2 3 -4 5
  • for k 1length(x)
  • if x(k) lt 0
  • break
  • end
  • y y sqrt(x(k))
  • end
  • -- Jump to here on break

17
The continue Statement
  • You can skip bad values in any looping
    structure with a continue
  • y0
  • x1 -2 3 -4 5
  • for k 1length(x)
  • if x(k) lt 0
  • continue
  • end
  • y y sqrt(x(k))
  • end Jump to here on continue

18
Implied Loops (p. 216)
  • You can use built-in loops instead of for end
    loops in most cases. Sometimes it is better,
    sometimes not.
  • Note the matrix example with simple logic
  • Note the for end example with more complex
    logic
  • Note the find function example for a very normal
    situation that you once upon a time had to write
    code for every time.

19
Implied Loops (cont.)
  • Many MatLab commands contain implied loops.
  • Example
  • x 05100
  • y cos(x)
  • Example
  • y find(xgt0)
  • See p. 216 for equivalent loops for these.

20
while LOOPS
  • The while loop terminates because a specified
    condition is satisfied.
  • The number of passes is NOT known a priori
  • SYNTAX
  • while logical expression
  • statements
  • end
  • See p. 223 for flowchart of while loop.

21
while LOOP FLOW CHART
Loop structure
22
while end Loops
  • Look at the examples on page 221.
  • x 5
  • while x lt 25
  • disp(x)
  • x 2x-1 -make sure x changes
  • end
  • Means
  • starting with x 5, execute the statements
    between the while end this displays x and
    calculates a new value of x and loops back.
  • Then exit the loop when x exceeds or equals 25.

23
while end Loops (p.222)
  • THE LOOP VARIABLE MUST HAVE A VALUE BEFORE THE
    while STATEMENT IS EXECUTED
  • THE LOOP VARIABLE MUST BE CHANGED SOMEHOW BY THE
    STATEMENTS
  • Note the usual mistakes toward the bottom of the
    page.
  • Make sure that you save your code before testing
    any while end loop.

24
INDIVUDAL EXERCISE (5 min.)
  • Write an equivalent while loop for the implied
    loop shown
  • x 05100
  • y sin(x)
  • disp (y)
  • Save on the R\ drive
  • Name file after a city outside of Texas

25
SOLUTION
  • x 0
  • while x lt 100
  • y sin(x)
  • x x5
  • disp(y)
  • end

26
NESTED while LOOPS
  • while loops can also be nested.
  • Each loop must be closed with an end.
  • break statements may be used to jump out of loops.

27
EXAMPLE
  • r 1 N 10
  • while r lt N
  • c 1
  • while c lt N
  • A(r,c) 1/(r c -1)
  • c c 1
  • end
  • r r 1
  • end - note use of r,c instead of i,j.

28
Assignment 11.1
  • Individual assignment.
  • Due Nov 15, 2004
  • Palm MATLAB
  • Chapter 4 22 (use for-loop in part b), 28 (use
    a while-loop), T4.5-4 (p. 225)
  • Extra Problem (see assignments web page)!
  • Extra Credit (see assignments web page)!!
  • Read Palm section 4.7.
Write a Comment
User Comments (0)
About PowerShow.com