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.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Cells and Structures.
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.
Programming with MATLAB
Numerical Operations S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Numerical Integration.
Numerical Operations S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Optimization Toolbox.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
MATLAB Loops and Branching.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Matlab Basics Introduction to Matlab: Matrix Operations.
UNIT II Decision Making And Branching Decision Making And Looping
Numerical Operations S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Polynomial Manipulations.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
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.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: User Input / Output Programming Environment.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment  Command window, Workspace, Path window, Editor window,and Figure window) Basic.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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 tutorial course Lesson 4: Writing your own functions: programming constructs
Matlab Programming for Engineers
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Working with Loops, Conditional Statements, and Arrays.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
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.
Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql.
ITEC 2600 Introduction to Analytical Programming
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Control Statements in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
ITM 352 Flow-Control: Loops
Introduction to MATLAB
FLOW OF CONTROL.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Matlab Basics.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

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

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 2 Control Flow Topics n For Loop For Loop n While Loop While Loop n If-Elseif-Else-End If-Elseif-Else-End n Switch Switch

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 3 For Loop n For Loop Syntax: for v = expression (or array) … commands … end n To Create Column Vector x = [ ]’ » x = zeros(4,1); » for i=1:4 x(i) = i*i; end;

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 4 Nested For Loops n Create an m x n Hilbert Matrix

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 5 Nested For Loops Code » m = 4;n = 5; » a = zeros(m,n); » for i=1:m, for j=1:n a(i,j)=1/(i+j-1); end end » a a =

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 6 For Loop Array Example n Matlab Code n Results » data = [ ; ]; » for n = data x = n(1) - n(2) end x = -4  First Column in data x = -7  Second Column in data x = 5  Third Column in data x = 0  Fourth Column in data

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 7 While Loops while expresion : : commands : end n While Loop Syntax:

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 8 Estimate log(1+x) for x=0.5 by summing the series until the term to be added next is less than eps (floating point relative accuracy constant) in absolute value While Loop Log Example n The MacLaurin series expansion for log(1+x) (natural log), where |x| < 1, is given by:

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 9 While Loop Log Code » v=0;x=0.5;k=1; » while abs((x^k)/k) >= eps v = v+(-1)^(k+1)*((x^k)/k); k = k+1; end » v,k=k-1 v = log(1.5) k = 46 Number of Iterations

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 10 While Loop Factorial Example n What is the first integer n for which n! (n factorial) is a 100 digit number? » n = 1; » while prod(1:n) < 1.e100 n = n + 1; end= » n n = 70

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 11 Factorial Verification n Verify the results » prod(1:n)% n! = 70! ans = e+100 » prod(1:n-1)%(n-1)! = 69! ans = e+098 n Therefore, n=70 is the first integer where n! is a 100 digit number

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 12 One Alternative If - End n One Alternative Expression Syntax n One Alternative Example if expression : :Commands : end » if a > 1 b=0; c=5; end

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 13 If-Else-End Constructions n Two Alternative Expression Syntax n Two Alternative Example if expression : :Command Set 1 : else expression : :Command Set 2 : end » if a > 0 b=1; else b=-1; end

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 14 If-Elseif-Else-End Constructions n Several Alternative Expression Syntax if expression Command Set 1 elseif expression Command Set 2 elseif expression Command Set 3 else expression Command Set 4 end

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 15 Breaking Out of a Loop It is possible to break out of for loops & while loops using the break command When the break statement is executed, MATLAB jumps from the current loop in which it appears n In the case of nested for or while loops, MATLAB jumps out of only the current loop

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 16 Break eps Example n Find the value of eps (floating point relative accuracy constant) » EPS=1; » for i=1:1000 EPS=EPS/2; if(1+EPS)<=1 EPS=EPS*2; break; end » EPS EPS = e-016 NOTE: i=53

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 17 Switch Case Constructions n Used when a sequence of commands must be conditionally evaluated based on repeated use of an equality test with one common argument switch expression case test_expression_1 command Set 1 case test_expression_2 command set 2 otherwise command set3 end Scalar or Character string

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 18 Switch Example 1 » var=3; Display a message based on the variable var 2 or 3 or 4 » switch var case 1 disp('1'); case {2,3,4} disp('2 or 3 or 4'); case 5 disp('5'); otherwise disp('something else'); end

MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Control Flow 19 Switch Example 2 » x=2.7; units='m'; » switch units » y y = 270 Given measurement x in meters, Convert x to centimeters n Results in centimeters: case {'feet','ft'} y=x*2.54*12; case {'inch','in'} y=x*2.54; case {'meter','m'} y=100*x; case {'centimeter','cm'} y=x; case {'millimeter','mm'} y=x/10; otherwise disp(['Unknown Units:' units]); y=nan; end