MATLAB Loops and Branching.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Project 6: Working with If Statements Essentials for Design JavaScript Level One Michael Brooks.
Project on VB Control Structures
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Control Structures Corresponds with Chapters 3 and 4.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
1 Pseudocode For Program Design. 22 Rules for Pseudocode Write only one statement per line Capitalise initial keyword Indent to show hierarchy and structures.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow.
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
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
C++ for Engineers and Scientists Third Edition
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 11/03/2003.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
While and If-Else Loops ROBOTC Software. While Loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Algorithms and Pseudocode
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 MATLAB Programming
Topics The if Statement The if-else Statement Comparing Strings
Scripts & Functions Scripts and functions are contained in .m-files
Program Design Introduction to Computer Programming By:
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Selection Statements Chapter 4 Attaway MATLAB 4E.
Three Special Structures – Case, Do While, and Do Until
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Introduction to MATLAB
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Introduction to MATLAB
CISC101 Reminders All assignments are now posted.
Selection Statements Chapter 3.
statement. Another decision statement, , creates branches for multi-
Selection Statements Chapter 4 Attaway MATLAB 5E.
Presentation transcript:

MATLAB Loops and Branching

Loop types Counted loops; called for loop Conditional loops; called while loop

Looping! Scripts and functions also allow the ability to loop using conventional for and while loops. Note that the interpreter also lets you do it, it is simply less easy to grasp

For Loops Common to other programming languages for variable = expression statement ... end

For Loops, con’t: 2 Example: (taken from MATLAB help) a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end

For Loops, con’t: 3 The looping variable is defined in much the same way that we defined arrays/vectors. Ex. m = 1:k Or m = 1:10

For Loops, con’t: 4 Loops are shown to end by the keyword “end” Curly braces are not present to subdivide packets of code Make use of adequate white-space and tabbing to improve code readability

While Loops Similar to while loops in other languages while condition statement … end

While Loops, con’t: 2 Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end

While Loops, con’t: 3 Same notes apply to while loops. Code is separated by the keyword “end”

Looping conclusion Some other aspects of looping exist Use help while and help for to see them

LOGICAL CONTROL PROGRAMMING CONSTRUCTS Syntax of the if statement: if logical expression statements end

EXAMPLE x = some given value if x >= 0 y = sqrt (x) end

EXAMPLE x = 10; y = 20; if x >= 0 & y >= 0 z = sqrt(x) + sqrt(y); w = log(x) – 3*log(y); end

LOGICAL PROGRAMMING CONTRUCTS Nested “if” statements: if logical expression 1 statement group 1 if logical expression 2 statement group 2 end Note the indentions Nested Statement

LOGICAL PROGRAMMING CONTRUCTS THE else STATEMENT: If two mutually exclusive actions can occur as a result of a decision, use the else statement. if logical expression statement group 1 else statement group 2 end

In-class Exercise (5 minutes) Suppose y = x1/2 for x >= 0 and y = ex – 1 for x < 0 Write a program (.m script file) to calculate y assuming that x already has a scalar value. Test your program for x = 3 and x = -2.

SOLUTION (Script File) % Solution to In-Class Exercise if x >= 0 y = sqrt (x); else y = exp (x) -1; end Did you indent properly?!

LOGICAL PROGRAMMING CONSTRUCTS The elseif statement: When three actions can occur as a result of a decision, the else and elseif statements are used along with the if statement. Remember: ONLY ONE ACTION WILL ACTUALY OCCUR!!!

LOGICAL PROGRAMMING CONSTRUCTS if logical expression 1 statement group1 elseif logical expression 2 statement group 2 else statement group 3 end

EXAMPLE Given: y = ln x for x > 10 y = x1/2 for x >= 0 and x <= 10 y = ex – 1 for x < 0 Compute y if x has been assigned a scalar value.

SOLUTION (Script file) % Solution to example if x > 10 y = log (x) elseif x >= 0 y = sqrt (x) else y = exp (x) -1 end Does the order that I check things matter? YES!

LOGICAL PROGRAMMING CONSTRUCTS Write the syntax for the if-elseif-else-end construct if there are more than three alternatives.

SOLUTION if logical expression1 Statements group1 elseif logical expression2 Statements group2 elseif logical expression3 Statements group3 elseif logical expression4 Statements group4 … else Statement if all other cases are false end