Docsity

MATLAB Looping Structures and Logical Decisions, Slides of Calculus for Engineers

Partial preview of the text

Download MATLAB Looping Structures and Logical Decisions and more Slides Calculus for Engineers in PDF only on Docsity!

Please HELP Rm 3906A Lab

• Please do NOT SAVE ANY Files to the

DESKTOP on the computers in Rm3906A

Lab

• Saving to the machine DeskTop Leads to

Clutter and Glitchy Computers

• Thank You

Learning Goals

• Write MATLAB Programs That can MAKE

“Logical” Decisions that Affect Program

Output

• Write Programs that Employ

LOOPing Processes

  • For → No. Loops know a priori
  • while → Loop Terminates

based on Logic Criteria

Repetition → LOOPs

  • A “LOOP” is a Program

Structure that

REPEATS Until some

CONDITION is MET

  • The NUMBER of Loops

may Be

  • Known a priori (ahead of time) - No. of Loops Determined by simple COUNTING - Determined Dynamically - No. of Loops Determined by a DECISION statement
  • The Loop

consists of

  • A Condition Test
  • A Repeated Statement- Block

Test vs Statement Locations

  • PreTest Loop

 The key feature →

we test to see

whether or not to

continue before

executing the body

of the loop.

  • i.e., The Loop May Not Execute at All

 Good if Potential

Zero Executions is

Desired

 a.k.a. “While DO”

Test vs Statement Locations

  • MidTest Loop (^)  The generalization

of both the pre-test

and the post-test

loops

  • Empty Block-1 → PreTest Loop
  • Empty Block-2 → PostTest Loop

for Loop Statement

  • A PreTested, COUNTED

Loop

Start

k ≤ n?

Statements-

end

Statements

True False

Set k = m

Increment k by s

  • No. Repetitions Known

 MATLAB Syntax

for Counter = Start : Increment: End

statements

end

For Loop Example

  • Construct a 23x

2D Array filled with

RANDOM integer

between

−99 and +

  • Game Plan:
    • Use Nested for Loops along with rand, round, & fix commands
    • Track the No. of Construction Steps - The MATLAB Code % Bruce Mayer, PE % ENGR25 * 27Feb % Build_Random_Array_by_FOR_120228.m % % Build 7x11 Array filled with Random Integers between % Keep Track of Each Construction Step - 99 to + Step = 0 for k = 1:23 % ROW Count for m = 1:11 Step = Step + 1 % COL Count % counts how many times in Calc-§ % A(k,m) = (Random A(k,m)= (-1)^(round(rand(1)))(fix(99.9rand(1)))-SIGN)*(Randon-VALUE; 0-99) end^ end % % check No. of Positives & Negatives q = A>0; r = sum(q); NoPOS = sum(r) u = A<0; v = sum(u); NoNEG = sum(v) Steps = Step

The continue Statement

  • The continue

statement passes

control to the next

iteration of the loop

in which it appears,

skipping any

remaining statements

in the body of the

loop.

  • The Following Code

Uses a continue

x = [10,1000,-10,100]; y = NaN*x; for k = 1:length(x) if x(k) < 0 continue end y(k) = log10(x(k)); end

statement to avoid

taking the log of a

negative number.

 The Result:

y = 1, 3, NaN, 2

Use of a Logical MASK

  • The use of loops and branching can often

be avoided, thus creating simpler and faster

programs by using a logical array as a mask that

selects elements of

another array.

  • Any elements not selected will remain unchanged.
  • The following session creates the logical array D

from the 3x3 numeric array B

Use of a Logical MASK cont

  • Logical Mask Session

B = [0, -1, 4; 9, -14, 25; -34, 49, 64] B = 0 -1 4 9 -14 25 -34 49 64

D = (B >= 0) D = 1 0 1 1 0 1 0 1 1

Mask Array → a Logical that “masks out” Negative numbers

>> x = [-7 0 8 5 -2]Logical Masking Subtlety

x = -7 0 8 5 -

nn = x>=0 % the logical mask nn = 0 1 1 1 0

y = x(nn) y = 0 8 5

sqrt1 = sqrt(x(nn)) sqrt1 = 0 2.8284 2.

sqrt2 = x % make starting copy of x sqrt2 = -7 0 8 5 -

sqrt2(nn) = sqrt(sqrt2(nn)) sqrt2 = -7.0000 0 2.8284 2.2361 -2.

ONLY the Three Sq-Roots

the Three Sq-Roots AND the two NON- Roots

while Loops

  • The while loop is used when the looping

process terminates because a specified condition

is satisfied, and thus the number of passes is not

known in advance.

  • A simple example of a while loop is

x = 5; while x < 25 disp(x) x = 2*x - 1; end

 Results from the

disp statement are

5, 9, and 17.

while Loop Statement

  • For the while loop to

function properly two

conditions must occur

Start

Logical Decision

Statements (MUST Increment Loop Variable)

end

Statements

True False

  1. The loop variable must have a value BEFORE the while statement is executed (initialize)
  2. The loop variable must be changed somehow by the statements INSIDE the Loop

Set Loop Var Initial value

while Loop Build Vector

  • A simple while loop x = 5;k = 0; while x < 25 k = k + 1 y(k) = 3x; x = 2x- end

 The Results

k = 1 x = 9 k = 2 x = 17 k = 3 x =

  • The loop variable x is initially assigned the value 33 5, and it keeps this value until the statement x = 2*x - 1 is encountered the first time. Its value then changes to 9. Before each pass through the loop, x is checked to see if its value is less than 25. If so, the pass is made. If not, the loop terminates

y

y =

15 27 51