Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.

Slides:



Advertisements
Similar presentations
Selection (decision) control structure Learning objective
Advertisements

Programming with MATLAB
ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
JavaScript, Third Edition
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
CIS Computer Programming Logic
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ITEC113 Algorithms and Programming Techniques
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to Problem Solving and Control Statements.
JavaScript, Fourth Edition
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Sixth Edition
Introduction to Computing Systems and Programming Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
EEE 161 Applied Electromagnetics
Matlab Training Session 4: Control, Flow and Functions
Operator Precedence Operators Precedence Parentheses () unary
Scripts & Functions Scripts and functions are contained in .m-files
Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Structured Program
Logical Operations In Matlab.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational Operators 3.Logical Operators 4.Conditional Statements (if/then/else, case) 5.Loops (for, while) 6.Debugger Textbook Reading Assignments Practice Problems 1.Chapter 4 Problems: 17b, 20a, 21a, 23, 24a

Introduction to Matlab Module #4 Page 2 1) Programming Basics Programming in Matlab - Matlab is an interpreted language, which means it does not require compiling. Instead, when a program is executed, it is interpreted line-by-line on the fly. - All structured programs can be created with three main constructs: 1) Sequential Operations- individual tasks that are executed in order 2) Conditional Operations - decisions that are made in the code based on evaluation of variables. The flow of the code execution is altered depending on the evaluations 3) Iterative Operations - program loops that execute over and over or are terminated based on conditional operations. - Using functions for common tasks is a proven technique for making programs easier to develop and debug. - All code should be commented so that others can understand the functionality of your program. - Flow charts and pseudo-code are a good way to design a program.

Introduction to Matlab Module #4 Page 3 1) Programming Basics fprintf command - A more sophisticated technique for printing information is the fprintf command fprintf(‘text’, A, ….) - A set of codes can be used to insert special characters: \n% return \r% beginning of a new line \b% backspace \t% tab ‘’% Apostrophe \\ % Backslash

Introduction to Matlab Module #4 Page 4 1) Programming Basics fprintf command - Formatted numbers can be printed the expression using the syntax %var - Number formats consists of: # of digits in front of the decimal point # of digits behind the decimal point # type of number format - The available number formats are: eScientific format with lowercase e EScientific format with uppercase E fDecimal format g%e or %f, whichever is shorter - Example of a number format for x= : %6.3f → %2.2f → Variable names to be printed are listed in the fprintf statement after the ‘text’ statement and are comma delimited.

Introduction to Matlab Module #4 Page 5 1) Programming Basics fprintf command examples:

Introduction to Matlab Module #4 Page 6 1) Programming Basics Command line Inputs - Inputs from the command line can be assigned to variables using the input command response = input('\nEnter a whole number between 0 and 100: '); fprintf('\nYou just entered %3.0f \n', response); test_stdinput_1.m

Introduction to Matlab Module #4 Page 7 2) Relational Operators Relational Operators - relational operators compare two values and return a TRUE or FALSE interpretation based on the chosen operator. - Matlab has 6 relational operators OperatorDescription Greater than >=Greater than or equal to ==Equal to ~=Not equal to - A TRUE evaluation of these operators is returned as a ‘1’. A FLASE evaluation of these operators is returned as a ‘0’ - The 1’s and 0’s returned from these operators are of the data type logical. This is different from a numeric 1 or 0. - Arrays and Matrices are compared on an element-by-element basis

Introduction to Matlab Module #4 Page 8 2) Relational Operators Relational Operator Examples ScalarsVectors Less Than>> 4 > [1,2,3] > 6 < 4ans = 0 Less than or equal>> 4 > [1,2,3] > 4 > 4 <= 3 ans = 0 Greater than >> 4 > 6ans = 0>> [1,2,3] > [3,2,1] ans = >> 6 > 4ans = 1 Greater than or equal >> 4 >= 5ans = 0>> [1,2,3] >= [3,2,1] ans = >> 4 >= 4ans = 1 >> 4 >= 3 ans = 1 Equal to>> 4 == 4ans = 1>> [1,2,3] == [3,2,1] ans = >> 4 == 5ans = 0 Not Equal to>> 4 ~= 4ans = 0>> [1,2,3] ~= [3,2,1] ans = >> 4 ~= 5ans = 1

Introduction to Matlab Module #4 Page 9 3) Logical Operators Logical Operators - logical operators perform Boolean operations on an element-by-element basis. - Matlab has 5 logical operators OperatorNameDescription ~NOT% Inversion (0→1, 1→0), works on Matrices &AND% Logical AND of operands, works on Matrices |OR% Logical OR of operands, works on Matrices &&Short-Circuit AND% Short-circuit AND of operands, only works on Scalars ||Short-Circuit OR% Short-circuit OR of operands, only works on Scalars

Introduction to Matlab Module #4 Page 10 3) Logical Operators Logical Operator Examples NOT>> ~0ans = 1 >> ~1ans = 0 AND>> 0&0ans = 0 >> 0&1 ans = 0 >> 1&0 ans = 0 >> 1&1 ans = 1 OR>> 0&0ans = 0 >> 0&1 ans = 1 >> 1&0 ans = 1 >> 1&1 ans = 1

Introduction to Matlab Module #4 Page 11 3) Logical Operators The “find” function - the “find” function returns the indices of any non-zero elements within an array. Ex)>> y = [ ] >> find(y)→ [ ]this indicates that elements y(1), y(2), and y(4) are non-zero - this can be useful when using logical arrays Ex)You wish to find which entries are TRUE and which are false in z = [ ] >> z = [ ] >> find(z) → [1 5] this tells which are 1’s and implies which entries are 0’s

Introduction to Matlab Module #4 Page 12 4) Conditional Statements If statements - these statements allow your program to either execute or skip a set of operations based on the evaluation of a statement. - these statements evaluate a Boolean expression and based on whether it is TRUE or FALSE, will execute specific code segments - the syntax for an if statement is: if logical expression statements % execute this code if the logical expression evaluates to TRUE end - if the “logical expression” evaluates TRUE, then the statements code will be executed. - If the “logical expression” evaluates FALSE, then the statements code will be skipped. - each “if” requires a corresponding “end”.

Introduction to Matlab Module #4 Page 13 4) Conditional Statements If statement example if (x > 0) disp('x was larger than 0'); end x is initialized to 3. when the program is executed, the logical expression (x>0) evaluated TRUE so the subsequent code was executed. test_if_1.m x is initialized to -1. when the program is executed, the logical expression (x>0) evaluated FALSE so the subsequent code was skipped.

Introduction to Matlab Module #4 Page 14 4) Conditional Statements If-else statements - An “else” clause can be used in conjunction with an if-statement to allow your code to take an action if the first logical expression evaluates to FALSE. - the syntax for an if statement is: if logical expression statements % execute this code if the logical expression evaluates to TRUE else statements % execute this code if the logical expression evaluates to FALSE end - if the “logical expression” evaluates TRUE, then the if-statements code will be executed. - If the “logical expression” evaluates FALSE, then the else-statements code will be executed. - each “if” requires a corresponding “end”.

Introduction to Matlab Module #4 Page 15 4) Conditional Statements If-else statement example if (x > 0) disp('x was larger than 0'); else disp('x was smaller than or equal to 0'); end x is initialized to 3. when the program is executed, the logical expression (x>0) evaluated TRUE so the if-statement code was executed. test_if_else_1.m x is initialized to -1. when the program is executed, the logical expression (x>0) evaluated FALSE so the else-statement code was executed.

Introduction to Matlab Module #4 Page 16 4) Conditional Statements If-elseif statements - An “elseif” clause can also be included to give multiple logical evaluations within a single if-statement. - The if, elseif, … statements are evaluated in sequence. The first logical evaluation of TRUE will result in the corresponding code to be executed. All other code in the if-statement will be skipped. - the syntax for an if statement is: if logical expression statements % execute this code if the above logical expression evaluates to TRUE elseif logical expression statements % execute this code if the above logical expression evaluates to TRUE : : : else statements % execute this code if none of the above logical expressions evaluated to TRUE end

Introduction to Matlab Module #4 Page 17 4) Conditional Statements If-elseif statement example if (x > 0) disp('x was larger than 0'); elseif (x < 0) disp('x was smaller than 0'); else disp('x was smaller than or equal to 0'); end x is initialized to 3. when the program is executed, the logical expression (x>0) evaluated TRUE so the if-statement code was executed. test_if_elseif_1.m x is initialized to -1. when the program is executed, the logical expression (x<0) evaluated TRUE so the elseif-statement code was executed. x is initialized to 0. when the program is executed, neither the (x>0) or (x<0) logical expressions evaluated TRUE so the else-statement code was executed.

Introduction to Matlab Module #4 Page 18 4) Conditional Statements Case statements (or switch statements) - A Case statement is a list of logical expressions. - Each expression is evaluated in turn for a given input variable. - If an expression evaluates TRUE, the corresponding code statements will be executed. - the syntax for a case statement is: switch input expression case value1 statements % execute this code if the input is equal to this case case value2 statements % execute this code if the input is equal to this case : : otherwise statements % execute this code if none of the above cases evaluate to TRUE end

Introduction to Matlab Module #4 Page 19 4) Conditional Statements Case statement example switch (angle) case (0) fprintf('The input angle was 0 degrees \n'); case (1) fprintf('The input angle was 1 degrees \n'); case (2) fprintf('The input angle was 2 degrees \n'); otherwise fprintf('The input is out of range\n'); end test_case_1.m

Introduction to Matlab Module #4 Page 20 5) Loops Program Loops - Program loops allow you to repeatedly execute a section of code. - There area different types of loops that have different functionality For Loop- executes a segment of code a predefined # of times While Loop- executes a segment of code while a logical expression is TRUE

Introduction to Matlab Module #4 Page 21 5) Loops For Loops - A for loop uses a loop variable to count the number of times it will execute. - You can specify the start, stop, and step size of the loop variable in addition to its name. - The loop variable will increment/decrement by the step-size each time the loop is executed. - The loop will continue to execute until the current loop variable reaches the stop value. Syntax: for loop_variable = m:s:n statements end - in this code: m = the initial value of the loop variable n = the final value of the loop variable s = the step size that the loop variable will increment by each time the loop is executed

Introduction to Matlab Module #4 Page 22 5) Loops For Loop example - let’s create a loop variable called ‘k’ - let’s create a for loop that will start k at 1, increment it by 1, and loop until k reaches 10 (m:s:n = 1:1:10) - each time through the loop, let’s use the fprintf statement to print the value of the loop variable. for (k = 1:1:10) fprintf('The current value of k is: %2.0f \n', k); end test_for_1.m

Introduction to Matlab Module #4 Page 23 5) Loops For Loops - For loops are good for addressing arrays. - For loops can be nested to create two loop variables that can be used as the index for an array Syntax: for loop_variable1 = m:s:n for loop_variable2 = m:s:n statements end end

Introduction to Matlab Module #4 Page 24 5) Loops Nested For Loop example - let’s use two nested For loops to write the contents of a 3x2 array - let’s call the row variable m - let’s call the column variable n A = [2,5; -3,4; -7,1]; for (m = 1:1:3) for (n = 1:1:2) fprintf('The value of a%1.0f%1.0f is: %2.0f \n', m, n, A(m,n) ); end test_for_nested_1.m n m

Introduction to Matlab Module #4 Page 25 5) Loops While Loops - A while loop will continually execute as long as a logical expression evaluates TRUE Syntax: while logical_expression statements end - if the logical_expression evaluates TRUE, the loop will execute. - if the logical_expression evaluates FALASE, the loop will NOT execute.

Introduction to Matlab Module #4 Page 26 5) Loops While loop example - we will initialize x =2; - in our while loop, we square x and print the new value - we will continue this until the new value of x is greater than 500. x = 2; while (x < 500) x = x^2; fprintf('The value of x after squaring is: %3.0f \n', x); end test_while_1.m

Introduction to Matlab Module #4 Page 27 6) Debugger Debugger - There is a built-in debugger in the Matlab text editor - A debugger allows you to set breakpoints and step through your code while you observe the contents of variables. The menu allows you to: - Set Breakpoints - Remove Breakpoints - Step - Step Into - Continue Current variable values are observed in the Workspace Indicates location of Breakpoints Indicates current location of code execution

Introduction to Matlab Module #4 Page 28 Lab 4 Exercise Problems -For each of these exercises, you will create a script file. Your script file will perform the calculations and then display the answers to the workspace. -Create a directory on your Z drive called “Z:\Matlab_Course\Lab04” -Change your pwd to “Z:\Matlab_Course\Lab04” (>> cd Z:\Matlab_Course\Lab04) -Perform the following exercises: Ch4: 17b, 20a, 21a, 23, 24a