Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:

Slides:



Advertisements
Similar presentations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Advertisements

Programming with MATLAB
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
JavaScript, Third Edition
Chapter 4 Making Decisions
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
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.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Chapter 3: Data Types and Operators JavaScript - Introductory.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
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.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Matlab Programming for Engineers
EEE 161 Applied Electromagnetics
Matlab Training Session 4: Control, Flow and Functions
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Other Kinds of Arrays Chapter 11
Chapter 4: Making Decisions.
JavaScript: Control Statements.
MATLAB: Structures and File I/O
Matlab review Matlab is a numerical analysis system
T. Jumana Abu Shmais – AOU - Riyadh
INTRODUCTION TO 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.
Matlab Basics.
Presentation transcript:

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to: additional operators branches to control operations loops to repeat operations Textbook chapter 7, pages , (sections 7.1 – 7.2.2, 7.4, 7.4.1)

Relational Operators – the Idea In formal English someone might ask you “Is your age greater than or equal to 21?” Answers include: – Yes, of course – Here’s my ID card – I’m 18 – I knew this would happen if I forgot my ID – No

Using mathematical notation, we test or compute the relation age ≥ 21 or age >= 21 And expect 1 of only 2 answers: – “Yes” or “True” – “No” or “False”

Relational Operators in M ATLAB A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays (match the sizes on arrays!) – Numeric or string Operators: >> == = << =~ = Result is true (1) or false (0) – perhaps an array

Note – value and class

More examples: expressionresult 5 < 71 [ 3, 5, 2 ] > = [ 1, 0, 12 ]1 1 0 max( 1:6 ) < = 7 1 [3, pi, -12 ] > 'Tom' = = 'Bob'0 1 0 'Tom' = = 'm'0 0 1 Note – arrays and strings need to be the same size

Notes: – Can compute using the result: e.g. “how many of a particular letter in a state name?

Don’t confuse = = and = Round off errors can impact ~ = sind(0) = = 01 sind(180) = = 00 instead, test for small values abs( sind(180) ) < = eps1

Matlab has Logical Operators as Well A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays, numeric or string A and B are interpreted as logical (binary): – Numeric 0 is interpreted as false – All else is interpreted as true (equal to 1) Result is true (1) or false (0) – perhaps an array

Basic operators: and & or | xor not ~ ABA&BA|BA|Bxor(A,B)~A~A “truth table”“unary” operator

Examples: “Are you between 25 and 30 years old?” (age>=25) & (age<=30) “Is it winter?” (month==12 & day>=22) | (month==1) | (month==2) | (month==3 & day<=21)

Array example: Score = [ 70, 55, 88, 98, 80, 73, 90 C = (Score > 70) & (Score < 81) C = [ ] Useful in counting how many entries satisfy a condition: B_grades = sum( Score 80 )

Text examples: 'Tom'= ='m' | 'Tom'= ='o' name = input('enter name','s'); name = = 'Tom' | name = = 'Bob' Rolling dice: roll = sum(ceil(6*rand(1,2))); roll = = 7 | roll = = 11

Other useful logical operators: – Extend | and & from binary to arrays: any(X)all(X) – To check array size, value, and data type isempty(A) isinf(A)isnan(A) ischar(A)isnumeric(A) – To find the locations of events: find( )

Operator Precedence (left to right) 1. Parentheses ( ) 2. Transpose(') and power(.^) 3. Negation (-) and logical negation (~) 4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-) 6. Colon operator (:) 7. Relational operators (, >=, = =, ~=) 8. Logical AND (&) 9. Logical OR (|)

Branches, Conditional Statements Commands to select and execute certain blocks of code, skipping other blocks. Three types in Matlab: – if/else – switch – try/catch this week

“If/Else” Use relational and logical operators to determine what commands to execute: if expression {commands if true } else {commands if false } end evaluate this use of blue in editor; also, auto indentation on commands

Example – output whether a variable x is positive or not: x = … { computed somehow }; if x > 0 disp('the value is positive') else disp('the value is negative or zero') end

Example – output a warning if the variable x is negative (note that there is no “else” portion in this example): x = … { computed somehow }; if x < 0 disp( ' Warning: negative value ' ) end the else component is not required

Example – ask if a plot should be drawn: x = input( ' Plot now? ', ' s ' ); if x = = ' yes ' | x = = ' YES ' plot( ….. ) end more complicated expression to evaluate

Example – Write a script to put 2 numbers in numerical order:

Loops Commands to repeatedly execute certain blocks of code Two types in Matlab: – for – while this week

The “for” Loop Used for a specific number of repetitions of a group of commands: for index = array { commands to be repeated go here } end Rules: One repetition per column of array index takes on the corresponding column’s values

Example – collect 7 digits of a telephone number and store in an array: 7 repetitions since the array is [ ] digit cycles through the 7 values to create the 1 by 7 array “number”

Example – calculating interest for 10 years: command num2str converts numerical variables to string variables for concatenating with other strings

Example – implement a count down timer (in seconds):

Example – a general vector for array:

Example – a matrix for array:

Example – even a string array:

Errors in Your Scripts Syntax errors: Note – red text = bad newsBut tells you where

Run-time errors: inf or NaN results Note – black text = okay, just a warning

Logical errors in your program – hard to find – Example: quadratic equation solver – But x 2 +2x+1 = (x+1) 2  x = – 1 Use the built-in debugger Missing parentheses around 2*a

Matlab Data Files (not in the text) Types: –.asv = auto save – ascii = regular text files –.mat = Matlab’s proprietary format (multiple variables)