ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:

Slides:



Advertisements
Similar presentations
Flow Charts, Loop Structures
Advertisements

CMPS 1371 Introduction to Computing for Engineers
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
MatLab – Palm Chapter 4, Part 3 For and While Loops
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.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
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.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
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.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
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.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
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.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab Programming for Engineers
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Structured Programming EEN170 Programming in MATLAB.
CSE123 - Lecture 4 Structured Programming- Loops.
EE 201 1C10-2 Spring 2012 LOOPS For.  Achieve Comprehension LOL of using Loops in programming. Class Learning Objectives 2C10-2 Spring 2012.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Repetition Structures Chapter 9
Chapter 4 MATLAB Programming
Programming Fundamentals Lecture #6 Program Control
MATLAB – Basic For Loops
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Types of Flow of Control
LOOPS For EE 201 C10-1 SPRING 2012.
Presentation transcript:

ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered: 1.for–end loops

ENG  A loop is a group of commands in a computer program that are being repeated.  Each repetition of the loop is called a pass.  The number of passes can be set to be fixed, or the looping process is terminated when a specified condition is satisfied.  In each pass some or all of the variables that are defined in the loop obtain new values. DEFINITION OF A LOOP

ENG for–end LOOPS for–end loops are used when the number of passes is known (or can be calculated) in advance. A variable is used to control the looping process. The general structure of a for–end loop is: for k = f : s : t end The value of k in the last pass Loop index variable The value of k in the first pass The increment in k after each pass A group of MATLAB commands

ENG for–end LOOPS  In the first pass, k = f, and Matlab executes the commands between the for and the end  The computer goes back to the for command for the second pass. k obtains a new value equal to k = f+s, and the commands between the for and the end are executed with the new value of k. for k = f : s : t end  The process repeats itself until the last pass where k = t.  Example: k = 1: 2: 9 there are five passes through the loop. The values of k are:

ENG RULES REGARDING THE for COMMAND for k = f: s: t  The increment value s can be negative (i.e. k = 25: -5: 10 produces 4 loops with: k = 25, 20, 15, 10)  If s is omitted, the increment value is 1 (default)  If f equals to t, the loop is executed once  The value of k should not be redefined within the loop  The looping continues until the value of k exceeds the value of t (i.e. k = 8:10:50 produces 5 loops with: k = 8, 18, 28, 38, 48)

ENG EXAMPLES OF for–end LOOPS Type in the command window: >> for k = 1: 3: 10 x = k^2 end x = 1 x = 16 x = 49 x = 100 >> for k = 5:8 b = 2*k end b = 10 b = 12 b = 14 b = 16 >> for k = 10:2:20 a = k/3; end >> a a = >> k k = 20 Semicolon, so a is not printed after each pass. If a step value is not entered, the default is 1. a = because in the last pass k = 20.

ENG for–end LOOPS  For every for command a computer program MUST have an end command  for loops can be used in the command window and in script and function files  A semicolon is not needed after the for k = m:s:p command to suppress printing of k  To display the value of k in each pass (sometimes useful for debugging) type k as one of the commands in the loop  Loops can include conditional statements and any other MATLAB commands (functions, plots, etc.)

ENG EXAMPLE OF USING A for–end LOOP Suppose that you want to use calculated values after the loop is finished – they need to be stored! Script file: for k=1:4 b(k)=2*(k+4) end Command Window: b = 10 b = b = b = At the end of the loop, b contains all of the values calculated during the loop. This approach can be used when calculating values which could not be generated by a colon operator and dot operators!

ENG EXAMPLE OF USING A for–end LOOP Specific elements can be extracted from an existing array as well Script file: a=1:25; for k=1:5 ka=5*k; b(k)=a(ka) end Command Window: b = 5 b = 5 10 b = b = b = Elements of a have been extracted and stored in b. This illustrates why the “for loop” variable is called an index, because this is its most common usage.