CH6: Programming in MATLAB

Similar documents
Chapter 7: Programming in MATLAB

Relational and Logical Statements

Relational and Logical Operators

Mechanical Engineering Department Second Year

Compact Matlab Course

Programming for Experimental Research. Flow Control

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif

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

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

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

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

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

Programming in MATLAB

Files and File Management Scripts Logical Operations Conditional Statements

ECE 102 Engineering Computation

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Programming in MATLAB Part 2

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

ENGR 1181 MATLAB 09: For Loops 2

CS227-Scientific Computing. Lecture 3-MATLAB Programming

COMP MATLAB.

An Introduction to MATLAB

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

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Linear algebra & Numerical Analysis

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

ECE 202 LAB 3 ADVANCED MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

ECE Lesson Plan - Class #3 Script M-files, Program Control, and Complex Numbers

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Structure Array 1 / 50

Lecture 1: Hello, MATLAB!

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

Dr. Iyad Jafar. Adapted from the publisher slides

Unit 3: Multiplication and Division Reference Guide pages x 7 = 392 factors: 56, 7 product 392

Relational & Logical Operators

Introduction to MATLAB

Introduction to MATLAB

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

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

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

Flow Control. CSC215 Lecture

AN INTRODUCTION TO MATLAB

4.0 Programming with MATLAB

1) Generate a vector of the even numbers between 5 and 50.

1 Overview of the standard Matlab syntax

Digital Image Analysis and Processing CPE

How to Do Word Problems. Study of Integers

Functions of Two Variables

Any Integer Can Be Written as a Fraction

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

Chapter 3: Programming with MATLAB

Linear Algebra and MATLAB

Computer Programming CS F111

Flow Control. Spring Flow Control Spring / 26

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Workshop of MATLAB. Section 3

Binghamton University. CS-211 Fall Control Flow

Dr. Khaled Al-Qawasmi

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

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

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Coding Tutorial. Derrick Hasterok. February 1, 2005

Constants and Variables

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

An Introduction to MATLAB

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Repetition Structures Chapter 9

COMP 208 Computers in Engineering

Introduction to programming in MATLAB

Branches, Conditional Statements

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Programming in MATLAB

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Boolean Logic & Branching Lab Conditional Tests

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 2. MATLAB Fundamentals

Arithmetic operations

McTutorial: A MATLAB Tutorial

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program

Lecture 4 Advanced Selection. Nested if-else, if-elseif, operators precedence

Programming 1. Script files. help cd Example:

MATLAB Lecture 4. Programming in MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

MATLAB Examples. Flow Control and Loops. Hans-Petter Halvorsen, M.Sc.

XQ: An XML Query Language Language Reference Manual

Scientific Computing with MATLAB

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Selections. Zheng-Liang Lu 91 / 120

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

CONDITIONAL STATEMENTS AND FLOW CONTROL

Simple if Simple if-else Nested if - else Notes for if-else statement Logical operators

Transcription:

CH6: Programming in MATLAB 1- Relational and Logical operators: Relational operators: Examples Result >> 5>8 0 >> a= 6~=2 a=1 >> b= (3>2)+(5*2==10*1)*(32>=128/4) b=2 >> c= 8/(2<5)*3+(1>9) c=?? Order of precedence: 1- Parentheses 2- Exponentiation 3- Multiplication, division 4- Addition, subtraction 5- Relational operators (>, <, >=, <=, = =, ~=) 1

Examples Result >> c=[5 12 50 21 1 4]; d=12; >> e= c>=d e=[0 1 1 1 0 0] >> f=[3 8 15 6 7 12]; >> h= f < c h=[1 1 1 1 0 0] >> a=[2 9 4; -3 5 2; 6 7-1];b=2; >> b= a<=b b= 1 0 0 1 0 1 0 0 1 >> r=[8 12 9 4 23 19 10]; >> s=r<=11 s=[1 0 1 1 0 0 1] >> t=r(s) >> w=r(r<=9) t= [8 9 4 10] w=?? 2

Logical operators: &, and(a,b) Gives 1 if A and B are nonzero, or(a,b) Gives 1 if A or B are nonzero ~, not(a) Gives 1 if A are zero xor(a,b) Gives 1 if just one is zero all(a) Gives 1 if all elements are nonzero any(a) find(a) Gives 1 any element are nonzero Gives indices of nonzero elements Examples Result >> 5&8 >> 3 0 >> ~50 >> m = 30*(2*(5*6 3*0)+(5&0*~0)) >> t = (-5<2<-1)*(~(5>7)) >> x= ~(5>=8) (2<-1) ans=1 ans=1 ans=1 m=60 t=?? x=?? 3

Examples >> k=[5 8 0 3 12];l=[6 7 8 0 3]; >> x= k&l >> y= or(k,l) >> z= all(k) >> h= any(l) >> m= xor(k,l) >> find(k) >> or(k>7,l) >> 5 & (k<6) Result x=[1 1 0 0 1] Y=[1 1 1 1 1] Z=0 h=1 m=[0 0 1 1 0] ans=[1 2 4 5] ans=?? ans=?? 2- Conditional Statements: It has 3 structures If- if-else- if-elseif-else- 4

Examples in the script file x=input('enter the first value'); y=input('enter the second value'); z=x*2*y; if x>y z=x*3*y; z Result x=5,y=6 z=60 x=4,y=3 z=36 x=input('enter the first value'); y=input('enter the second value'); if x>=y z=x*3*y; else z=x*2*y; z x=5,y=5 z=75 x=5,y=6 z=60 x=input('enter the first value'); y=input('enter the second value'); if x>y z=x*3*y; elseif x<y z=x*2*y; else z=x*y; z x=5,y=5 z=25 x=5,y=6 z=60 x=6,y=5 z=90 THE switch-case STATEMENT Examples in the script file Result n = input('enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') n=-1 negative one n=5 other value 5

6

7

Loops: -In a loop, the execution of a command, or a group of commands, is repeated several times consecutively. Each round of execution is called a pass. for- Loops the number of passes is specified when the loop starts. The increment s can be negative, If the increment value s is omitted, the value is 1 (default) If f = t, the loop is executed once. The value of k should not be redefined within the loop. When the loop s, the loop index variable (k) has the value that was last assigned to it. Example: for k=1:3:10 x=3*k 3 12 21 30 In some situations the same result can be obtained by either using for- loops or using element-by-element operations. In general, element-by-element operations are faster than loops and are recommed when either method can be used. 8

while- Loops while- loops are used in situations when looping is needed but the number of passes is not known in advance. -The first line is a while statement that includes a conditional expression. -When the program reaches this line the conditional expression is checked. -If it is false (0), MATLAB skips to the statement and continues with the program. -If the conditional expression is true (1), MATLAB executes the group of commands that follow between the while and commands. -The conditional expression in the while command must include at least one variable. -The variables in the conditional expression must have assigned values when MATLAB executes the while command for the first time. At least one of the variables in the conditional expression must be assigned a new value in the commands that are between the while and the. Example: x=1; while x<12 x=x^2+1 2 5 26 the user can stop the execution of an indefinite loop by pressing the Ctrl + C 9

NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS There is no limit to the number of loops and conditional statements that can be nested. The break command: When inside a loop (for or while), the break command terminates the execution of the loop (the whole loop, not just the last pass). The break command is usually used within a conditional statement. The continue command: The continue command can be used inside a loop (for or while) to stop the present pass and start the next pass in the looping process. The continue command is usually a part of a conditional statement. Example: x=1; while x>0.5 if x<15 x=x^2+1 else break 2 5 26 10

من المهم جدا مراجعة أمثلة هذا الشبتر في الكتاب Q1: i=132^2; for n=1:2*i if (rem(n,2)~=0) & (rem(n,11)==0) & (sqrt(n)>132) break fprintf('the required number is: %i\n',n) Q2: x=[-3.5 5-6.2 11.1 0 7-9.5 2 15-1 3 2.5]; n=length(x); for i=1:n-1 A=x(i); for j=i+1:n if x(j)<a A=x(j); x(j)=x(i); x(i)=a; x 11

Q3: write a program that eliminates the elements that devisable to 3, 4, 5 from a vector that inserted by the user. x=input('enter the vector : '); n=length(x) m=0; for k=1:n if rem(x(k),3)~=0 & rem(x(k),4)~=0 & rem(x(k),5)~=0 m=m+1; y(m)=x(k); y 12

Q4: Vin=input('Enter the value of the volume to be converted:'); VinUnits=input('Enter the current units (m3, Liter, ft3, or USGallon): ','s'); VoutUnits=input('Enter the new units (m3, Liter, ft3, or USGallon): ','s'); error=0; switch VinUnits case 'm3' VL=Vin*1000; case 'Liter' VL=Vin; case 'ft3' VL=Vin*(12*0.254)^3; case 'USGallon' VL=Vin*3.78541178; otherwise error=1; switch VoutUnits case 'm3' Vout=VL/1000; case 'Liter' Vout=VL; case 'ft3' Vout=VL/(12*0.254)^3; case 'USGallon' Vout=VL/3.78541178; otherwise error=1; if error disp('error current or new units are typed incorrectly.') else fprintf('v= %g %s\n',vout,voutunits) 13