While Loops ENGR 1181 MATLAB 10.

Slides:



Advertisements
Similar presentations
Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops.
Advertisements

Looping Structures: Do Loops
Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Introduction to Computing Science and Programming I
Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
Program Design ENGR 1181 MATLAB 11. MATLAB Program Design in Real Life Complex manufacturing processes require careful logic mapping to determine what.
CS0004: Introduction to Programming Repetition – Do Loops.
Lesson 1: Factors and Multiples of Whole Numbers
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
Take out homework and have it ready to be checked!!!
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Fundamentals of Python: From First Programs Through Data Structures
5 Minute Check Complete in your notebook.
Fundamentals of Python: First Programs
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
Functions 1 ENGR 1181 MATLAB 14.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
1.3 Console Input And Output academy.zariba.com 1.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Conditional Statements ENGR 1187 MATLAB 7. Conditional Statements in Real Life A Fluorescence Activated Cell Sorter (FACS) sorts cells based on predetermined.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Design of Bio-Medical Virtual Instrumentation Tutorial 2.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
2D Plots 2 ENGR 1181 MATLAB 13. Plotting in the Real World 2D plots generated by MATLAB can be used in a variety of fields, including the one shown here,
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Matlab Programming for Engineers
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Array Operations ENGR 1181 MATLAB 4.
Teamwork and Problem Solving: Activities ENGR 1181 Class 2 – Part 2.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
5 Minute Check Determine the missing digit to make the statement true. Complete on the back of your homework. 1. 6, ?59 is divisible by 3.   2.
TODAY IN ALGEBRA…  Formative Assessment: Ch  Learning Goal 1: 2.6 You will divide real numbers  Learning Goal 2: 2.7 You will find square roots.
GCSE Computing: Programming GCSE Programming Remembering Python.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Introduction to Complex Numbers Adding, Subtracting, Multiplying Complex Numbers.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
CSE123 - Lecture 4 Structured Programming- Loops.
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Repetition Structures Chapter 9
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Chapter 4 MATLAB Programming
CS1371 Introduction to Computing for Engineers
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Dr. Joe Anderson September 6, 2017
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Take out homework and have it ready to be checked!!!
COMPUTING.
Presentation transcript:

While Loops ENGR 1181 MATLAB 10

While Loops in Real Life Revisiting the previous example of the combustible engine from our first discussion of for loops (fixed number of runs), we can apply this example to while loops (indefinite number of runs). The fixed cycle of four states is the for loop, which runs indefinitely while the engine is on.

Today's Learning Objectives After today’s class, students will be able to: Explain the proper application of while loops. Use external inputs (e.g., tic, toc) for real time programming.

Reminders About While Loops While loops must have a conditional expression, and the conditional expression must have at least one variable. Conditional variables must be "initialized" before the loop The conditional variable must advance with each pass. (Otherwise the loop will continue indefinitely!) ‘Ctrl + C’ will terminate execution of an indefinite loop. Like for loops, must have an end

Example: Simple While Loop a=0 while a<10 a=a+1 if (a==5) continue end disp(a) end 1 2 3 4 6 7 8 9 10 Note: conditional variable is initialized variable advances each time one end corresponds to if one end corresponds to while

Function: rem() This function checks for and returns a remainder when dividing x by y. rem(x,y) Ex: rem(x,3)will check if the value of x is divisible by 3. Ex: rem1 = rem(23,5) rem1 = 3

Example: Complex While Loop Task Steps Write a program that will use a while loop to find the smallest number divisible by both 3 and 5, with the square of the number greater than 3325. Initialize variables: sq & n Loop while sq < 3325 Advance n by 3 in each pass Check if n is divisible by 5 If yes, square the n Repeat loop until sq >3325. Loop stops & n value will give us the smallest integer to meet conditions

Example: Complex While Loops n=0; sq=0; %initialize variables: # & square while sq<=3325 %loop until square >3325 n=n+3 %advance # by 3 each pass if rem(n,5)==0 %check if # is divisible by 5 sq=n^2; %if yes, square # end %end if statement end %end while loop disp(n) %display the # that meets criteria

Function: ‘tic’ and ‘toc’ tic and toc are timing functions that monitor elapsed time in your program tic starts a stopwatch that runs in the background toc returns the elapsed time since the last tic command The value of toc can be assigned to a variable Ex: time1 = toc;

Tic/Toc Example %obtain value for N N=input('\nPlease enter a large value for N: '); tic for i=1:N sqroot= sqrt(i); end SqrtTime=toc; fprintf('\n\nIt took MATLAB %f seconds to calculate %i square roots\n', SqrtTime, N) %obtain value for N %start stopwatch %loop to calculate all of the square roots %obtain elapsed time %statement reporting how long it took to calculate the sq roots

Important Takeaways While loops need a conditional statement and conditional variables must be initialized While loop will run indefinitely until condition is met ‘rem()’, ’tic’, and ‘toc’ are very useful tools

What’s Next? Review today’s Quiz #10 Open the in-class activity from the EEIC website and we will go through it together. Then, start working on MAT-10 homework.