MATLAB – Basic For Loops

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
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.
Python quick start guide
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
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.
CPS120 Introduction to Computer Science Iteration (Looping)
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
For loops in programming Assumes you have seen assignment statements and print statements.
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
CPS120 Introduction to Computer Science Iteration (Looping)
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
EE 201 1C10-2 Spring 2012 LOOPS For.  Achieve Comprehension LOL of using Loops in programming. Class Learning Objectives 2C10-2 Spring 2012.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
GoldSim Monthly Webinar Series. Goals  Learn the basics of simple scripts  Learn the basics of the GoldSim Script Element  Not a lesson on numerical.
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Chapter 4 MATLAB Programming
CS1371 Introduction to Computing for Engineers
Introduction to Matlab
Basic operations in Matlab
Topic 5 for Loops -Arthur Schopenhauer
Scripts & Functions Scripts and functions are contained in .m-files
( Iteration / Repetition / Looping )
Chapter 6: Conditional Statements and Loops
Arrays, For loop While loop Do while loop
One-Dimensional Array Introduction Lesson xx
Outline Altering flow of control Boolean expressions
CS100J 26 April. Matlab Use help button!!! Variables, values, types
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
Fundamentals of visual basic
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
EECE.2160 ECE Application Programming
Chapter 4: Repetition Structures: Looping
EECE.2160 ECE Application Programming
Creating a PowerPoint Presentation
Week 7: Computer Tools for Problem Solving and Critical Thinking
EECE.2160 ECE Application Programming
LOOPS For EE 201 C10-1 SPRING 2012.
Intro to Programming (in JavaScript)
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

MATLAB – Basic For Loops Topics Covered: Loops in MATLAB - To repeat code - To access elements of an array Instructor notes: This set of slides covers loops, in particular the for-end loop. Also covered is the break command, which allows a program to jump out of a loop prior to reaching the terminal value of the loop counter. Instructional objectives: Acquire advanced programming concepts including programming using loops Use loops (for-end) and break statements in MATLAB for programming MATLAB - 9

Loops in MatLab What is a Loop ? A loop allows a group of commands in a program to be repeated. Each repetition of the loop is called a pass. Either the number of passes can be fixed. Or the loop can be terminated after some condition is satisfied. Instructor notes: This slide is fairly straightforward. Looping causes a group of commands to be executed repeatedly before moving on to the commands that follow the loop. A loop has a beginning statement, a statement or group of statements that make up its body, and an ending statement. Each time program execution moves through the loop it is called a pass. The number of passes through a loop can be set to some fixed amount, or the looping can be set to continue until a specified condition is reached. It is very likely, and quite possible, that all of the variables used by a loop will reach new values for each pass. We’re talking about the first case today (simple use of a fixed number of passes). MATLAB - 9

Examples of basic for–end Loops for k = 1:3 disp(k) end X=[5, 10, 15, 20]; for i = 1: length(X) disp(X(i)) end Output: 1 2 3 Output: 5 10 15 20 Instructor Notes: These 2 small examples give the essence of the for-end command. The students should type these as programs and then run them to have a feel of how the commands work. MATLAB - 9

for–end loops with vectors Create the vector v = [2 4 6 8]. Calculate and print the square of each element. Output: vs = 4 4 16 4 16 36 4 16 36 64 Program: v = [2 4 6 8] ; for k = 1 : 4 vs(k) = v(k)^2 end Instructor notes: This example introduces the student of going through a vector element by element. Very important that the students understand the addressing of vectors using the for-end loop. Note how the output array is being created one element at a time by the computer. MATLAB - 9

Calculate the factorial of 5 What is a factorial? http://education-portal.com/academy/lesson/how-to-evaluate-factorials.htm l If n=5 and x=1, you continue up to 5 x=x * i 1*1=1 1*2=2 2*3=6 6*4=24 24*5=120 Now, let’s try to have Matlab calculate the factorial of 5.. Presentation Short Title

Additional basic for-end loop example clc clear %This script file calculates the factorial of a number %declare the number you wish to calculate the factorial for n=5; %calculate factorial using a for loop x=1; for i=1:n x=x*i; end fprintf('The factorial of %i is %i\n',n,x)

Matlab Tip of the Day – Dividing your code into Sections Helps focus on one section of the script file Reduces overall computation time Easy to identify errors Helps create awesome published documents  Simple to use All you need is %% (two comment characters or double percent)

Using %% - The Steps Start first code section with %% Write the code for the first section Use %% again to start the next section and so on To run only a particular section Select particular section (by clicking anywhere in the section) Press Ctrl+Enter To run overall code or to publish – same as before