MATLAB MATLAB - Lecture # 8. Programming in MATLAB / Chapter 7. if end if-else end if-elseif-else end

Similar documents
MATLAB - Lecture # 4

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

Programming for Experimental Research. Flow Control

Mechanical Engineering Department Second Year

ENGR 1181 MATLAB 09: For Loops 2

Chapter 4 Branching Statements & Program Design

Math 3820 Project. 1 Typeset or handwritten? Guidelines

Mental Math. Grade 9 Mathematics (10F) General Questions. 2. What is the volume of a pool that measures 7 m by 3 m by 2 m? 42 m 3

Branches, Conditional Statements

Chapter 7: Programming in MATLAB

Answer Key a reference section in the back of the Student Reference Book (SRB) that gives answers to sample problems

Programming in MATLAB

Summary of the Lecture

Programming in MATLAB Part 2

Chapter 4: Programming with MATLAB

Structure Array 1 / 50

CS 111X - Fall Test 1

Linear Modeling Elementary Education 4

Chapter 2 Working with Data Types and Operators

SOFTWARE ENGINEERING Prof.N.L.Sarda Computer Science & Engineering IIT Bombay. Lecture #10 Process Modelling DFD, Function Decomp (Part 2)

Fundamentals of Programming (Python) Getting Started with Programming

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

Computer Science Grade 10 Sample -Term 2 Date: February 2018 Time: TBC Duration: 45 minutes

BIL101E: Introduction to Computers and Information systems Lecture 8

CSE111 Introduction to Computer Applications

Outline. Database Management Systems (DBMS) Database Management and Organization. IT420: Database Management and Organization

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

Making Decisions In Python

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.

CONDITIONAL STATEMENTS AND FLOW CONTROL


BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Lesson 10. Student Outcomes. Lesson Notes

5. Selection: If and Switch Controls

ENGR 1181 MATLAB 05: Input and Output

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Boat Louisiana Online Louisiana Boater Safety Certification. Online Boater Safety User Guide

APPM 2460: Week Three For, While and If s

Instructions for Using the e-learning Module on Service-Learning for Students

VARIABLES & ASSIGNMENTS

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

MATLAB Basics 8 conditionals if statements

Polytechnic e-learning: LMS Manual - LECTURER

How to Access If Rubrics does not appear on your course navbar, click Edit Course, Tools, Rubrics to activate..

EASTERN ARIZONA COLLEGE Finite Mathematics

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

CS 115 Lecture 8. Selection: the if statement. Neil Moore

2.2 Creating & Initializing Variables in MATLAB

Introduction to Boolean Algebra

Lecture (03) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Spring 2018, HUM107 Introduction to Engineering

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

7 Control Structures, Logical Statements

CS 221 Lecture. Tuesday, 11 October 2011

Slide Copyright 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION. Chapter 13. Statistics Sampling Techniques

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Dr. Khaled Al-Qawasmi

Chapter 4 - Notes Control Structures I (Selection)

Introduction to Boolean Algebra

Dropbox. Dropbox includes a number of functions for managing the submission of assignments including:

Outline. Program development cycle. Algorithms development and representation. Examples.

Lab - 8 Awk Programming

function [A,V,a] = spherical cap(r,h) % Returns the area, volume and radius of a spherical cap % cut from a sphere of radius r, and cap height h

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Week 6 Lesson 1: Control-Flow Statements (Continued)

Transformations Reflections, and Rotations

Model 4.2 Faculty member + student Course syllabus for Advanced programming language - CS313D

Previous Lecture (and Discussion): Branching (if, elseif, else, end) Relational operators (<, >=, ==, ~=,, etc.) Logical operators (&&,, ~)

Dropbox. Dropbox includes a number of functions for managing the submission of assignments including:

Ministry of Higher Education and Scientific research

Topic 1. Mrs. Daniel Algebra 1

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Microsoft Office Outlook 2007: Intermediate Course 01 Customizing Outlook

Repetition Structures

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

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

Chapter 11. Worked-Out Solutions Explorations (p. 585) Chapter 11 Maintaining Mathematical Proficiency (p. 583)

Algebra 1 (ACP1) Summer Refresher Assignment

Select statement and the like. CIS 331: Introduction to Database Systems

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

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Moodle Quick Guide Teacher Create face-to-face sessions

REPETITIVE EXECUTION: LOOPS

NFPA Edition

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

y = f (x) f(x) MATLAB MATLAB - Lecture # 7 Functions and Function Files / Chapter Function File

Info Lit Tip 93 What do they look like? HTML result, PDF result, citation, abstract, and more

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

SIMPLE INPUT and OUTPUT:

Relational and Logical Operators

Introduction to CPLEX. Some very convenient solvers for most students include those with Excel and Matlab.

Department of Mathematics and Computer Science University of Southern Denmark, Odense. Exercises for Week 47 on. Online Algorithms

Section 4.5 Linear Inequalities in Two Variables

Advance Java Concepts Students Lab Manual

DIABLO VALLEY COLLEGE CATALOG

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CSC 015: FUNDAMENTALS OF COMPUTER SCIENCE I

Queries with Multiple Criteria (OR)

Practice Exam III - Answers

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

Transcription:

MATLAB - Lecture # 8 Programming in MATLAB / Chapter 7 Topics Covered:. Relational and Logical Operators 2. Conditional statements. if if- if-if- INTRODUCTION TO PROGRAMMING 63-64 In a simple program the commands are executed in the order they are typed. Many situations may require that: * * * Commands will not be executed in order. Different commands are executed in different runs. The execution of a group of commands is repeated many times. INTRODUCTION TO PROGRAMMING 63-64 MATLAB provide tools (commands) that can be used to control the flow of a program. Read Chapter 7 in the MATLAB book. In the class we will only cover if- conditional statements (this lecture) and for- loops (next lecture). Students can learn other tools from the book by themselves. MATLAB - 8

RELATIONAL OPERATORS 64-67 Relational operator Meaning < Less than. <= Less than or equal to. > Greater than. >= Greater then or equal to. == Equal to. ~= Not equal to. Relational operators compare two numbers in a comparison statement. If the statement is true, it is assigned a value of. If the statement is false, it is assigned a value of 0. RELATIONAL OPERATORS, EXAMPLES 65 >> 5>8 0 >> a=5<0 Since 5 is not larger than 8 the answer is 0. Checks if 5 is smaller than 0, and assigns the answer to a. a= Since 5 is smaller than 0 the number is assigned to a. >> y=(6<0) + (7>8) + (5*3= =60/4) y= = =0 = 2 LOGICAL OPERATORS Logical operators have numbers as operands. A nonzero number is true. A zero number is false. 67 Logical Operator Name Meaning & AND if both operands Example: A & B (A and B) are true. OR if either or both Example: A B operands (A and B) are true. ~ NOT if the operand (A) is false. Example: ~ A if the operand (A) is true. MATLAB - 8 2

LOGICAL OPERATORS, EXAMPLES 68-69 >> 3&7 3 AND 7. 3 and 7 are both true (nonzero), so the outcome is. >> a=5 0 5 OR 0 (assign to variable a). a= is assigned to a since at least one number is true (nonzero). >> x=-2; y=5; Define variables x and y. >> -5<x<- Mathematically correct. The answer is false since MATLAB executes from left to right. -5<x 0 is true (=) and then <- is false (0). >> -5<x & x<- The mathematically correct statement is obtained by using the logical operator &. The inequalities are executed first. Since both are true (), the answer is. CONDITIONAL STATEMENTS Conditional statements enable MATLAB to make decisions. The process is similar to the way we (humans) make decisions. A condition stated. If the condition is met, one set of actions is taken. If the condition is not met, either nothing is done, or a second set of actions is taken. Example: If I win the Lottery, I will quit college, buy a new car, and go fishing. If I do not win the Lottery, I will study harder so that I can get a better job. THE FORM OF A CONDITIONAL STATEMENT 72 If Conditional expression Examples: consisting of relational and/or logical operators if a < b if c >= 5 if a == b if a ~= 0 if (d<h)&(x>7) if (x~=3) (y<0) All variables must have assigned values. MATLAB - 8 3

THREE FORMS OF THE if STATEMENT If conditional statement commands if conditional statement t t command group command group 2 if conditional statement command group if conditional statement 2 command group 2 command group 3 THE if STATEMENT 74 if MATLAB program. if conditional expression A group of MATLAB MATLAB program. EXAMPLE OF USING THE if STATEMENT % A script file that demonstrates the use of the if- statement. % The user is asked to enter three grades. % The program calculates the average of the grades. % If the average is less than 60, a massage: % The student did not pass the course. is printed. score = input('enter (as a vector) the scores of the three tests '); ave_grade = (score() + score(2) + score(3))/3; disp('') disp(ave_grade) if ave_grade < 60 disp('the student did not pass the course.') MATLAB - 8 4

EXAMPLE OF USING THE if STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example Enter (as a vector) the scores of the three tests [78 6 85] 74.6667 >> Lecture8Example Enter (as a vector) the scores of the three tests [60 38 55] 5 The student did not pass the course. THE if - STATEMENT 74-75 MATLAB program. if if conditional expression Group of MATLAB Group 2 of MATLAB MATLAB program. EXAMPLE OF USING THE if - STATEMENT % A script file that demonstrates the use of the if-- statement. % The user is asked to enter three grades. The program calculates % the average of the grades. If the average is less than 60, a % massage: The student did not pass the course. is printed. % Otherwise, a massage: The student passed the course. is printed. score = input('enter (as a vector) the scores of the three tests '); ave_grade = (score() + score(2) + score(3))/3; disp('') disp(ave_grade) if ave_grade < 60 disp('the student did not pass the course.') disp('the student passed the course.') MATLAB - 8 5

EXAMPLE OF USING THE if - STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example2 Enter (as a vector) the scores of the three tests [65 80 83] 76 The student passed the course. >> Lecture8Example2 Enter (as a vector) the scores of the three tests [60 40 55] 5.6667 The student did not pass the course. THE if if-- STATEMENT MATLAB program. 76- if if if conditional expression Group of MATLAB if conditional expression Group 2 of MATLAB Group 2 of MATLAB MATLAB program. EXAMPLE OF USING THE if if-- STATEMENT % A script file that demonstrates the use of the if-if-- % statement. % The program calculates the tip in a restaurant according to the % amount of the bill. % If the bill is less than 0$ the tip is $.80. % Between $0 and $60 the tip is 8% of the bill. % Above $60 the tip is 20% of the bill. format bank clear tip (The file continues on the next slide) MATLAB - 8 6

(Continuation from the previous slide) bill = input('enter the amount of the bill (in dollars): '); if bill <= 0) tip =.8; if (bill > 0) & (bill <= 60) tip = bill*0.8; tip = bill*0.2; disp('the tip is (in dollars):') disp(tip) EXECUTING THE SCRIPT FILE OF THE RESTAURAT TIP CALCULATION >> Lecture8Example3 Enter the amount of the bill (in dollars): 5 The tip is (in dollars): 2.70 >> Lecture8Example3 Enter the amount of the bill (in dollars): 6 The tip is (in dollars):.80 >> Lecture8Example3 Enter the amount of the bill (in dollars): 00 The tip is (in dollars): 20.00 COMMENTS ABOUT if STATEMENTS For every if command a computer program must have an command. A program can have many if.. statements following each other. A computer program can perform the same task using different combinations of if -, if, and if if statements. MATLAB - 8 7