Loops Efficient Repeats By the end of this

Loops: Efficient Repeats By the end of this Lab, you should be able to: • use basic for loop syntax • use a for loop to solve problems requiring filling a vector or matrix Copy: root. m from lab website to your working directory Laboratory Outcomes: C. 1 solve engineering problems using computer tools. C. 2 apply arrays and array manipulations. C. 3 use and explain text variables & ASCII text files. C. 4 write a function with multiple inputs and outputs at the command line. C. 5 write a function that results in a non‐numerical output. C. 6 write programs using logical expressions and conditional statements. C. 7 write programs using loop structures. C. 8 fit data that follows linear, exponential or power law forms. C. 9 properly communicate a solution based on computer calculation or program.

Technique Practice script (Review) Type of Program: Script � Function 1. Problem Statement: Create a script that will read in a vector interactively. It should id the third element of that vector, id the length of the vector and create a new vector of the integers from 1 to the length of the input vector. 2. Inputs: Variable Name X Description Vector of numbers Units or Values Any real number Input Source* Interactive (input function) Description Units or Values Output type* 3. Outputs Variable Name Third Count Solution Steps: The third element in X Any Real number Integers from 1 to length of X Display in command window 1. input: interactively input is a vector into the variable x 2. store third element of x in a variable named third 3. find length of the vector x (i. e. , how many elements does x have? ) 4. produce a vector of all integers from one to the length of x, 5. output: display third element, vector of integers

% % % Script Name or Function Call: Vector. Stuff Written by: S. Scott Moor Date Created: Oct. 2019 Purpose of Program: This program interactively inputs a vector. It determines and displays 1. The third element in the vector 2. A vector that counts from 1 to the length of the input vector Variable Input: x Output: Third Count = description [units] = any vector = The third element of x = A count from 1 to the length of x % Input Section x = input('What is the input vector (in [ ] ') % Calculations and Output: . disp('The third element of the Vector is: ') Third = x(3) disp('The equal‐length vector of integers is: ') n = length(x); Count=1: n

Introduction to Loops: Try this sample program % Program Loop. Test % This is a quick script file to look at what a simple for loop does. % This loop simply echo prints the loop index value for k=1: 5 k end Work through the first page of For Loop Handout (focus on 1 & 2)

For Loop Syntax & Flowchart for k=m: s: n statements (that vary with k) end where: k = loop variable m = start s = increment n = stop

Loop Logic: Filling a vector Example: an element‐by‐element conditional For a vector x of a given length, take the square root of each value if it is positive function y = root(x) % Lots ‘o’ Comments if x(k) >= 0 y(k) = x(k)^0. 5; else y(k) = Na. N; end Previous function • Download from Website • Run with scalar input (e. g. x = 2) • Run with a vector (e. g. , x =[2 0 ‐ 2]) What happens? • Convert to match 4. MATLAB Example on pg. 2 • Run this function with the same vector What happens?

Quick Follow the Leader Exercise Walk students through editing their own root. m function as you show them the steps 1. add a loop around the conditional that counts from 1 to the length of the input vector 2. add indices to the input variable in the conditional tests (to test elements one by one) 3. add indices to the input variable in the calculations (to calculate for just the one element) 4. add indices to the output variable in the calculations (to store the result separately for each element) for k=1: length(x) if x(k)>=0 y(k)=x(k)^0. 5 else y(k) = Na. N end 1 2 3&4 4 1

Example Function ‐ try it (note: semicolons have been left off on purpose) function y=root 2(x) % lots of comments for k=1: length(x) if x(k)>=0 y(k)=x(k)^0. 5 else y(k) = Na. N end Loop counts from 1 to the length of the vector If structure inside a for loop. Loop steps through each element in the vector.

Fill Problem: Square Root of the elements of a vector Notice how loop index is used function y = root 2(x) % lots of comments for k = 1: length(x) if x(k) >= 0 y(k) = x(k)^0. 5; else y(k) = Na. N; end The loop index is set to go from one to the length of the input vector The loop index is used as an array index to point to each element of the input vector (x). The loop index is also used to step through each location in the output vector

Intro to For Loops Lab (Handout) For Loops ( and Fill Logic for Element‐by‐Element vector conditionals) I. Background – loop index & overview Pg. 1: Focus on #s 1 & 2 Example loop (vector square root) Pg. 2: Review carefully, try e. g. II. Problem: Vectored loop day program Turn in today III. Aside: quiz program A good break IV. Homework Problems Get a good start before you leave V. Problem: Create a 2‐D array (optional) Ask me when you get here

Arrays in loops Often array address change as you repeat a loop • 1 D- Vectors: • 2 D - Arrays: . . . X(i) X(i, j) Remember use of : or end in addressing vectors • Can use indexing for either the source (RHS): y = X(i) or • for target arrays (LHS) X(i, j) = y • MATLAB indices must be positives whole numbers use rounding functions (round, ceil, floor) when needed

Assignment (start today) Complete a Program Development Worksheet (Setup, Coding & Validation) for each of the following: a. Write a program that takes an arbitrary vector of real numbers and returns a vector with negative numbers squared and positive numbers b. Convert Grading Program to handle vectors c. Fibonacci Sequence Program (see handout) d. Bonus: Prepare a program to return the Ordinal (Julian) Date (i. e. , the number of days since the beginning of the year) given the current date (name of month, date in month, year). This program can be done with or without loops (but will require conditionals). It must consider leap days (use previous leap day function).
- Slides: 12