Dr. Khaled Al-Qawasmi

Similar documents
SMS 3515: Scientific Computing. Sem /2015

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

Chapter 7: Programming in MATLAB

Selection Statements

Relational & Logical Operators, Selection Statements

7 Control Structures, Logical Statements

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

Introduction to MATLAB

Intro Chapter 1 Demo Help Lookfor Doc Quit exit Variable variablename = expression = assignment operator, initializing Incrementing Identifier names

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

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

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

Boolean Logic & Branching Lab Conditional Tests

Chapter 3: Programming with MATLAB

Programming 1. Script files. help cd Example:

Selection Statements

Files and File Management Scripts Logical Operations Conditional Statements

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Relational and Logical Statements

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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

Step by step set of instructions to accomplish a task or solve a problem

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

Structure Array 1 / 50

Introduction to MATLAB Programming

Branches, Conditional Statements

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

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

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

McTutorial: A MATLAB Tutorial

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

AN INTRODUCTION TO MATLAB

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Programming for Experimental Research. Flow Control

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

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

Arithmetic and Bitwise Operations on Binary Data

Question. Insight Through

An Introduction to MATLAB

RELATIONAL AND LOGICAL OPERATORS

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

Mechanical Engineering Department Second Year

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

COMPUTER SKILLS LESSON 12. Valeria Cardellini A.Y. 2015/16

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

Computational Finance

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

Prof. Navrati Saxena TA: Rochak Sachan

CS 221 Lecture. Tuesday, 11 October 2011

A control expression must evaluate to a value that can be interpreted as true or false.

CMSC201 Computer Science I for Majors

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Repetition Structures Chapter 9

Loop Statements and Vectorizing Code

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

FONDAMENTI DI INFORMATICA. Prof. Emiliano Casalicchio

Summary of the Lecture

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

Programming in MATLAB

Desktop Command window

These are reserved words of the C language. For example int, float, if, else, for, while etc.

BRANCHING if-else statements

Selections. CSE 114, Computer Science 1 Stony Brook University

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

Scientific Computing with MATLAB

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

CS227-Scientific Computing. Lecture 3-MATLAB Programming

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES EVEN SEMESTER FEB 2017

SECTION II: LANGUAGE BASICS

CS 221 Lecture. Tuesday, 13 September 2011

Stokes Modelling Workshop

Chap 6 - Introduction to HDL (b)

VARIABLES Storing numbers:

Chapter 2 Working with Data Types and Operators

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

Conditional Expressions and Decision Statements

egrapher Language Reference Manual

Lecture 1: Hello, MATLAB!

VBScript: Math Functions

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Logical and Function Constructions

Flow Control. Spring Flow Control Spring / 26

MATLAB Second Seminar

Dr Richard Greenaway

Lecture 5 Tao Wang 1

5. Selection: If and Switch Controls

Operators. Java operators are classified into three categories:

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Mini-Matlab Lesson 5: Functions and Loops

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Programming in MATLAB Part 2

Important Java terminology

Transcription:

Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351 Dr. Khaled Al-Qawasmi ١ Dr. Kahled Al-Qawasmi 2010-2011

Chapter 3 Selection Statements Dr. Kahled Al-Qawasmi 2010-2011 ٢

Relational expression If statements use expressions that are conceptually or logically either true or false Relational expressions sometimes are called Boolean or logical expression. Relational operator: >, <, >=, <=, ==, ~= In MATLAB logical true is represented by the integer 1, and logical false is represented by the integer 0. In the command window demonstrates the values of the expressions: >> 3<5 ans= 1 >>9<2 ans= 0 Dr. Kahled Al-Qawasmi 2010-2011 ٣

Cont. In the workspace the type of the expression result is logical Mathematical operation could be performed on the resulting 1 or 0. >> 5<7 ans=1 >>ans+3 ans=4 Dr. Kahled Al-Qawasmi 2010-2011 ٤

Cont. Comparing characters, for example a < c is also possible. Characters are compared using their ASCII >> a < c ans=1 The logical operator are: or && and ~ not Dr. Kahled Al-Qawasmi 2010-2011 ٥

Logical operator All logical operators operate on logical or Boolean operands The not operator is a unary operator The others are binary Not operator give the opposite value for example, ~(3<5) is false since (3<5) is true. In addition to these logical operators, MATLAB has a function xor, which is the exclusive or function. >>xor(3<5, a > c ) ans=1 >>xor(3<5, a < c ) ans=0 Dr. Kahled Al-Qawasmi 2010-2011 ٦

Operator precedence rules (). and ^ - ~ * / \ + - : < <= > >= == ~= && = Dr. Kahled Al-Qawasmi 2010-2011 ٧

The if statement The general form is: if condition action For example >> num=-4 >>if num<0 num=abs(num) >> Dr. Kahled Al-Qawasmi 2010-2011 ٨

Cont. abs() can be used to make sure that the square root function is not used on a negative number: Sqrtexamp.m num= input( please enter a number: ); If num<0 disp( ok, we ll use the absolute value ) num=abs(num); fprintf( the sqrt of %.1f is %.1f\n,num,sqrt(num)); >>sqrtexamp Please enter a number:-4.2 The sqrt of 4.2 is 2.0 Dr. Kahled Al-Qawasmi 2010-2011 ٩

Quick quetion Write a function that creates a vector of increasing value from mymin to my max and return the result. Assume a=3, b=7; But if a=7, b=3, we need to make swap. createvec.m function outvec=createvec(mymin, mymax) if mymin>mymax temp=mymin; mymin=mymax; mymax=temp; End Outvrc=mymin:mymax; Dr. Kahled Al-Qawasmi 2010-2011 ١٠

Logical True and False The concept of false is represented by the integer value of 0 But the concept of true can be represented by any nonzero value (not just the integer 1) >>if 5 disp( yes, this is true ) yes this is true Dr. Kahled Al-Qawasmi 2010-2011 ١١

Cont. Simple mistake can be occurred Lets say that the user is prompted for a choice Y or N for a yes/no >>letter=input ( choice (Y/N):, s ) The script should allow the user to enter ( y or Y ) >>letter== y letter== Y Of by mistake written as: >>letter== y Y This expression always be true. Because Y is nonzero value, the first part ma be false but the second part is always true. Dr. Kahled Al-Qawasmi 2010-2011 ١٢

The if-else statement Choosing between two actions of from several action, is accomplished using if-else, nested if, and switch If-else statement form if condition action 1 else action 2 Dr. Kahled Al-Qawasmi 2010-2011 ١٣

Checkradius.m radius=input( enter radius: ); if radius<=0 fprintf( sorry;%.2f is not a valid radius\n,radius); else area=calarea(radius); fprintf( for a circle with radius of %.2f,radius); fprintf( the area is %.2f\n,area) Dr. Kahled Al-Qawasmi 2010-2011 ١٤

Nested if-else statements To choose from more than two statements Compute the following mathematical function y= f(x): Y=1 for x<-1 Y=x 2 for -1 <= x<=2 y= 4 for x>2 if x<-1 y=1; if x>=-1 && x<= 2 y=x^2; if x<2 y=4; Dr. Kahled Al-Qawasmi 2010-2011 ١٥

cont. the previous code is not efficient, but: if x<-1 y=1; else if x>=-1 && x<= 2 y=x^2; else y=4; not all conditions need to be tested Dr. Kahled Al-Qawasmi 2010-2011 ١٦

the Elseif Cluse MATLAB has another method of accomplishing neted if-else statements if x<-1 y=1; elseif x>=-1 && x<= 2 y=x^2; else y=4; write a function to determine whether an input argument is a scalar, a vector or a matrix (use elseif) Dr. Kahled Al-Qawasmi 2010-2011 ١٧

findtype.m function outtype=findtype(arg) [r c]=size(arg); if r==1 && c==1 outtype='scalar'; elseif r==1 c==1 outtype='vector'; else outtype='matrix'; Dr. Kahled Al-Qawasmi 2010-2011 ١٨

example write a function that receives an integer quiz grade, which should be in the range from 0 to 10. then the program returns a corresponding letter grade according to the following scheme: a 9 or 10 is an A, an 8 is a B, a 7 is a C, a 6 is a D, and anything below that is an F. writ a function that receive an integer then check if the number is odd or even. the mod function is the Modulus after division. mod(n1,n2). Dr. Kahled Al-Qawasmi 2010-2011 ١٩

letgrade.m function grade=letgrade(quiz) %first error check if quiz<0 quiz>10 grade='x'; elseif quiz==9 quiz==10 grade='a'; elseif quiz==8 grade='b'; elseif quiz==7 grade='c'; elseif quiz==6 grade='d'; else grade='f'; Dr. Kahled Al-Qawasmi 2010-2011 ٢٠

chk.m function chk(n) if n>0 if mod(n,2)==0 fprintf('the number%3d is even\n',n); else fprintf('the number%3d is odd\n',n); else disp('the number is not valid'); Dr. Kahled Al-Qawasmi 2010-2011 ٢١

the switch ststement a switch statement can be used in place of a nested if-else or an if statement with many elseif clause general form switch switch_expression case caseexp1 action1 case caseexp2 action2 %etc: there can be many of these otherwise action n Dr. Kahled Al-Qawasmi 2010-2011 ٢٢

example (grade example) switchgrade.m function grade=switchgrade(quiz) %first error check if quiz<0 quiz>10 grade='x'; else switch quiz case 10 grade= A'; case 9 grade='a'; case 8 grade='b' case 7 grade='c' case 6 grade='d' otherwise grade='f' Dr. Kahled Al-Qawasmi 2010-2011 ٢٣

Note: since the same action of printing A is desired for more than one case, these can be combined as follows: switch quiz case {10,9} grade= A ; case 8 grade= B ; %etc. Dr. Kahled Al-Qawasmi 2010-2011 ٢٤

the Menu function MATLAB has a built-in Function called menu that display a figure window with push buttons for the choices. the first string passed to the menu function is the heading the rest are labels that appear on the push buttons. the function returns the number of the button that is passed Dr. Kahled Al-Qawasmi 2010-2011 ٢٥

cont. for example: >>mypick=menu( pick a pizza, cheese, shroom, sausage ); the following window will appear there are three buttons whose equivalent values are 1,2 and 3 if the user press the susage button mupick would have the value 3: >> mypick mypic= 3 Dr. Kahled Al-Qawasmi 2010-2011 ٢٦

pickpizza.m mypick=menu('pick a pizza','cheese','shroom','sausage'); switch mypick case 1 disp('order cheese pizza' ); case 2 disp('order Mushroom pizza' ); case 3 disp('order sausage pizza' ); otherwise disp('no Pizza for us today'); Dr. Kahled Al-Qawasmi 2010-2011 ٢٧

Cont. if the user clicks the red X on the top-right of the menu box the value returned from the function will be 0 which will cause the otherwise clause to be executed instead of using a switch statement in this script it can be use an if-else statement with elseif clauses Dr. Kahled Al-Qawasmi 2010-2011 ٢٨

pickpizzaifelse.m mypick=menu('pick a pizza','cheese','shroom','sausage'); if mypick==1 disp('order cheese pizza' ); elseif mypick==2 disp('order mushroom pizza' ); elseif mypick==3 disp('order sausage pizza' ); else disp('follow directions next time!!!'); Dr. Kahled Al-Qawasmi 2010-2011 ٢٩

The IS Functions In MATLAB there are a lot of function that are built into MATLAB that test whether or not something is true these function names are begin with word is. these functions are frequently used in if statements. for example the function isletter return logical 1 if the argument is a letter of the alphabet, or 0 if it is not Dr. Kahled Al-Qawasmi 2010-2011 ٣٠

cont. >> isletter( 4 ) ans =0 >>isletter( h ) ans=1 also you can do the same thing using comparing characters position within the character encoding for example: mychar>= a && mychar <= z but the efficient method is: isletter(mychar) Dr. Kahled Al-Qawasmi 2010-2011 ٣١

cont. the function isempty returns logical true if a variable is empty, and logical false if it has a value, or an error message if the variable does not exist so it can be used to determine whether a variable has a value yet or not. >>clear >>isempty(evec)??? undefined function or variable evec. >>evec=[ ]; >>isempty(evec) ans=1 Dr. Kahled Al-Qawasmi 2010-2011 ٣٢

cont. >>evec=[evec 11]; >>isempty(evec) ans =0 the isempty function will also determine whether or not a string variable is empty. for example to check whether the user entered a string in the input function: >>istr = input( please enter a string:, s ); please enter a string: >>isempty(istr) ans=1 Dr. Kahled Al-Qawasmi 2010-2011 ٣٣

cont. the function iskeyword will determine whether or not a name is a keyword in MATLAB. by itself (with no argument), it will return the list of all keywords. notice that the name of functions like sin are not keywords. Dr. Kahled Al-Qawasmi 2010-2011 ٣٤

examples >> iskeyword( sin ) ans= 0 >>iskeyword( switch ) ans = 1 >> iskeyword ans= break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif %etc. Dr. Kahled Al-Qawasmi 2010-2011 ٣٥