Chapter 4 MATLAB Programming

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Flow Charts, Loop Structures
Selection (decision) control structure Learning objective
Lecture 13 Decision structures
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6 Finding the Roots of Equations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Agenda Basic Logic Purpose if statement if / else statement
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Decision making If.. else statement.
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
CNG 140 C Programming (Lecture set 3)
PowerPoint Presentation Materials Transportation Engineering
PROGRAM CONTROL STRUCTURE
Chapter 4 MATLAB Programming
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Introduction to Programming for Mechanical Engineers (ME 319)
Copyright © The McGraw-Hill Companies, Inc
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Algorithms Second Edition by
1) C program development 2) Selection structure
Chapter 2 Excel Fundamentals
Plotting Data with MATLAB
Chapter 4: Decision Structures and Boolean Logic
Chapter R A Review of Basic Concepts and Skills
Introduction to Algorithms Second Edition by
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Decision making If statement.
Introduction to Algorithms Second Edition by
Assignment Pages: 10 – 12 (Day 1) Questions over Assignment? # 1 – 4
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Conditional Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CS150 Introduction to Computer Science 1
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Copyright © The McGraw-Hill Companies, Inc
Introduction to Algorithms Second Edition by
CHAPTER 6 SKELETAL SYSTEM
CS150 Introduction to Computer Science 1
The Selection Structure
Introduction to Algorithms Second Edition by
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Decision Structures and Boolean Logic
Chapter 3 Introduction to Physical Design of Transportation Facilities.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Introduction to Algorithms Second Edition by
Presentation transcript:

Chapter 4 MATLAB Programming Logical Structures Chapter 4 MATLAB Programming Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1

Logical if Statements We previously examined if statements in Excel: IF(condition, value if true, value if false) Since there are different values corresponding to true and false conditions, this type of logic is termed if-else MATLAB has several types of if constructs, beginning with the most basic – a simple if statement Engineering Computation: An Introduction Using MATLAB and Excel 2

if Statement The simple if statement specifies that a command or group of commands will be executed only if a condition exists If the condition does not exist, then the command(s) are simply bypassed, and the execution of the program continues All if statements must have an end statement as well Engineering Computation: An Introduction Using MATLAB and Excel 3

Example Let’s consider the example of grades again With a if statement, we can mark a passing grade: Note that nothing is printed if the grade is not passing g = input( 'Enter numerical grade '); if g >= 60 grade = 'P' end Engineering Computation: An Introduction Using MATLAB and Excel 4

Flowchart Engineering Computation: An Introduction Using MATLAB and Excel 5

Example >> Grades Enter numerical grade 75 grade = P Engineering Computation: An Introduction Using MATLAB and Excel 6

if-else Statement This form is similar to the Excel IF statement in that two alternative paths are presented If the condition is true, then one set of commands is executed. If the condition is not true (else), then another set of commands is executed The same result can be achieved by “stacking” simple if statements, but this form is more compact Also, it guarantees that one of the alternative paths is followed Engineering Computation: An Introduction Using MATLAB and Excel 7

Example Adding to the previous commands, we can designate a grade as either passing or failing Note that one of the two options must be chosen g = input( 'Enter numerical grade '); if g >= 60 grade = 'P' else grade = 'F' end Engineering Computation: An Introduction Using MATLAB and Excel 8

Flowchart Note that one of the output paths must be followed Engineering Computation: An Introduction Using MATLAB and Excel 9

Example >> Grades Enter numerical grade 75 grade = P F >> Engineering Computation: An Introduction Using MATLAB and Excel 10

if-elseif Statement This structure allows for more than two paths to be considered More compact structure than nested if statements As soon as a condition is satisfied, then the flow of calculations proceeds to the end statement Engineering Computation: An Introduction Using MATLAB and Excel 11

Example Grades example modified to report letter grades: g = input( 'Enter numerical grade '); if g >= 90 grade = 'A' elseif g >= 80 grade = 'B' elseif g >= 70 grade = 'C' elseif g >= 60 grade = 'D' else grade = 'F' end Engineering Computation: An Introduction Using MATLAB and Excel 12

Flowchart Engineering Computation: An Introduction Using MATLAB and Excel 13

Example >> Grades Enter numerical grade 85 grade = B F Engineering Computation: An Introduction Using MATLAB and Excel 14

Summary if statement: execute a command or set of commands if a condition is true, otherwise skip the commands if-else statement: execute one set of commands if a condition is true, execute a different set of commands if the condition is false if-elseif-else: allows the checking of multiple conditions Engineering Computation: An Introduction Using MATLAB and Excel 15