Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Similar documents
MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

7 Control Structures, Logical Statements

Chapter 7: Programming in MATLAB

C/C++ Programming for Engineers: Matlab Branches and Loops

Programming for Experimental Research. Flow Control

function [s p] = sumprod (f, g)

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

ENGR 1181 MATLAB 09: For Loops 2

Relational and Logical Operators

Programming in MATLAB

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

MATLIP: MATLAB-Like Language for Image Processing

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Boolean Logic & Branching Lab Conditional Tests

Introduction to MATLAB

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

Practice Reading for Loops

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

APPM 2460: Week Three For, While and If s

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

MATLAB Operators, control flow and scripting. Edited by Péter Vass

Structure Array 1 / 50

Selection Statements. Chapter 4. Copyright 2013 Elsevier Inc. All rights reserved 1

CS 221 Lecture. Tuesday, 11 October 2011

Repetition Structures Chapter 9

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction. C provides two styles of flow control:

Dr. Khaled Al-Qawasmi

Flow Control. Spring Flow Control Spring / 26

Files and File Management Scripts Logical Operations Conditional Statements

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

RELATIONAL AND LOGICAL OPERATORS

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

MATLAB TUTORIAL WORKSHEET

Mini-Matlab Lesson 5: Functions and Loops

Selection Statements

McTutorial: A MATLAB Tutorial

CS302: Self Check Quiz 2

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Lecture 3 MATLAB programming (1) Dr.Qi Ying

The Many Lessons in Fractals

Selections. Zheng-Liang Lu 91 / 120

Chapter 3: Programming with MATLAB

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

EL2310 Scientific Programming

Introduction to Engineering gii

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Problem One: A Quick Algebra Review

Relational and Logical Statements

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

CS227-Scientific Computing. Lecture 3-MATLAB Programming


EL2310 Scientific Programming

REPETITIVE EXECUTION: LOOPS

Relational & Logical Operators, Selection Statements

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 4. Flow of Control

Digital Image Processing

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to MATLAB LAB 1

MATLAB Laboratory 09/23/10 Lecture. Chapters 5 and 9: Plotting

CSE200 Lecture 6: RECURSION

Dr. Iyad Jafar. Adapted from the publisher slides

YOLOP Language Reference Manual

ME 461 C review Session Fall 2009 S. Keres

Relational & Logical Operators

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

These are notes for the third lecture; if statements and loops.

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Programming in MATLAB Part 2

Introduction to C Programming

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

REPETITION CONTROL STRUCTURE LOGO

An Introduction to MATLAB

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Computational Physics - Fortran February 1997

3.4. FOR-LOOPS 65. for <v a r i a b l e > in < sequence >:

LECTURE 0: Introduction and Background

Software Development Techniques. December Sample Exam Marking Scheme

Intro. Scheme Basics. scm> 5 5. scm>

Numerical Methods Lecture 1

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Language Reference Manual

CEMTool Tutorial. Control statements

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Variables and Assignments

Part I Part 1 Expressions

Lecture 11: Logical Functions & Selection Structures CMPSC 200 Programming for Engineers with MATLAB

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Transcription:

Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE (0) Relational operators: Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu >> 1 < 2 1 < > <= <= == = >> 1 > 2 0 The relational operators produce matrices of 0s and 1s if one of the arguments is a matrix. L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 1 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 2 / 1 Logical Operators s &, && AND (eager version,short-circuit version), OR (eager version, short-circuit version) NOT xor Exclusive OR all TRUE if all elements of vector are nonzero any TRUE if any elements of vector are nonzero Eager version will evaluate both expressions no matter what. The Short-circuit versions are only good on scalars and will only evaluate the second expression if needed. Pros and cons to each version in complicated programs - search for Short-circuit evaluation on Wikipedia for examples. The eager version of AND, OR, and NOT logical operators produce matrices of 0s and 1s if one of the arguments is a matrix. >> x = [-1 1 1]; y = [1 2-3]; >> x>0 & y >0 0 1 0 >> x>0 y>0 1 1 1 >> xor(x>0,y>0) 1 0 1 >> any(x>0) 1 >> all(x>0) 0 >> if x>0 && sin(1/x)<0.5 % one use of short-circuit L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 3 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 4 / 1

Order of Precedence Flow Control More functions in book! Precedence Operation 1 Parentheses 2 Exponents 3 Logical NOT 4 Multiplication/division 5 Addition/subtraction 6 Relational operators 7 Logical AND (&) 8 Logical OR ( ) 1 if- 2 if-- 3 if-if... - 4 switch 5 for 6 while The first four are conditional statements The last two are loops L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 5 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 6 / 1 IF statement IF ELSE and IF ELSEIF... ELSE if b ~=0 && (a == 5 a==3) y=a/b; z=a+b; if x >= 0 absx = x; absx = -x;. L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 7 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 8 / 1

sgn.m function s = sgn(x) % SGN(X) computes the sign or signum of the number X % if x < 0 s = -1; if x > 0 s = 1; sgn2.m function s = sgn2(x) % SGN2(X) Computes the sign or signum of the number X. % First checks that X is a number. if length(x) ~= 1 error( Input of sgn2 function is not a number ) if x < 0 s = -1; if x > 0 s = 1; L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 9 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 10 / 1 Pseudocode and Testing SWITCH statement Pseudocode (from dictionary.com) a program code unrelated to the hardware of a particular computer and requiring conversion to the code used by the computer before the program can be used. Pseudocode is especially useful to plan out your program(s) on scratch paper before typing - what variables do you need? What conditional statements need to be done, and how? How to set up the loops, etc. Testing: try and think of easy accuracy checks, but also error checks. switch expression case value case value. otherwise switch mod(n,2) case 0 disp( n is even! ) case 1 disp( n is odd! ) otherwise disp( n is not an integer! ) This can be easier in some instances than an if-if- statement L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 11 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 12 / 1

FOR loop for indexvariable = a:i:b MATLAB command(s) indexvariable does not need to be declared before; it can be new (and probably should be new) a:i:b or a:b has the indexvariable start at a and at (or going no further than) b, with an increment of i. (remember, if the increment is left out, the increment is 1). indexvariable = vector also works (see forex2.m) There is no need to increment the indexvariable; that is automatically done geomseries.m function s = geomseries(r, n) % GEOMSERIES(N) computes the sum of the geometric sequence % of R from k=0 to N % for k=0:n s = s + r^k; GOOD FOR SERIES! L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 13 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 14 / 1 WHILE loop While PSEUDOCODE while something is true do something Statements are executed as long as expression is true. Nice commands: break and continue read about it (also can be used in FOR loops) Something must change so loop isn t infinite! Read Chapter 7 and the H-drive for more details, syntax and examples! whileex1a.m function y = whileex1a(n) % WHILEEX1A(N) adds the numbers from 1 to N % L Oberbroeckling y=0; k=1; while k <= n y = y+k; k = k+1; L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 15 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 16 / 1

Notes on Nesting Loops and conditional statements can be nested within themselves Keep organized by having nice spacing (USE TABS) nest1 nest2 nest3 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 17 / 1