What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c)1 1 1 1 1 1 a) 1 2 3 1 1 1 d)1 2 3 1 1 1 b) 1 2 3 1 2 3.

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
CMPS 1371 Introduction to Computing for Engineers
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Program Design and Development
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Adding Automated Functionality to Office Applications.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
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.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
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.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
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 for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Matlab for Engineers Logical Functions and Control Structures Chapter 8.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Working with Arrays in MATLAB
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.,
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
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.
EEE 161 Applied Electromagnetics
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB: Structures and File I/O
User Defined Functions
Conditional Statements
When I want to execute the subroutine I just give the command Write()
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
T. Jumana Abu Shmais – AOU - Riyadh
Logical Operations In Matlab.
Loop Statements & Vectorizing Code
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.
A LESSON IN LOOPING What is a loop?
Loop Statements & Vectorizing Code
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)

What does D store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)

What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(B,A) c) a) d) b)

What does D store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(B,A) c) a) d) b)

LEARNING ABOUT FUNCTIONS IN MATLAB

A simple function (poly2) Save the file as poly2.m (same as the name of the function)

The function is available from the command window or from other M-file programs

Comments You should comment functions, just as you would any computer code The comment lines immediately after the first line are returned when you query the help function

Functions with Multiple Inputs A user defined function with multiple inputs

Functions with Multiple Outputs This function returns 3 output values If you don’t ask for all three results, the program just returns the first value

User Defined Input To this point we have “hard coded” the values of variables into our M-file programs The input function allows us to prompt the user to enter a value

The input function is used in an M-file program to prompt the user to enter a value The prompt is displayed in the command window

Input also can input in matrices and characters

Output Options Use the disp function Use the fprintf function – same as C except single quotes

disp The display (disp) function can be used to display the contents of a matrix without printing the matrix name

The disp function can also be used to display a string

You can combine disp functions to create meaningful output from an M-file program, but the result of each disp function is on a separate line.

Using find and logical operators in Matlab

Logical Operators & and ~ not | or

find The find command searches a matrix and identifies which elements in that matrix meet a given criteria.

index numbers element values

find used with a 2D matrix x =[1 2 3; ; ; 8 3 1] element = find(x > 9) [row, column] = find(x > 9) Returns a single element number Returns the row and column designation of an element

The rest of Chapter 5 shows how to use if/else if and for loops in Matlab – very similar to C

Homework – Chapter 5, problems 1 and 2 These are a little tougher – so good luck!

Simple if if comparison statements end For example…. if G<50 count = count +1; disp(G); end

If statements Easy to interpret for scalars What does an if statement mean if the comparison includes a matrix? The comparison is only true if it is true for every member of the array G=[30,55,10] if G<50 count = count +1; disp(G); end The code inside the if statement is not executed, because the comparison is not true!!

The if/else structure The simple if triggers the execution of a block of code if a condition is true If it is false that block of code is skipped, and the program continues without doing anything What if instead you want to execute an alternate set of code if the condition is false?

Use an if structure to calculate a natural log Check to see if the input is positive If it is, calculate the natural log If it isn’t, send an error message to the screen

The if/else/elseif structure Use the elseif for multiple selection criteria For example Write a program to determine if an applicant is eligible to drive

Repetition Structures - Loops Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for while

When to use loops In general loops are best used with scalars Many of the problems you may want to attempt with loops can be better solved by vectorizing your code or with MATLAB’s logical functions such as find

For Loops for index = [matrix] commands to be executed end The loop starts with a for statement, and ends with the word end. The first line in the loop defines the number of times the loops will repeat, using an index number. The loop is executed once for each element of the index matrix identified in the first line The index of a for loop must be a variable.

Here’s a simple example In this case k is the index – the loop is repeated once for each value of k the index can be defined using any of the techniques we’ve learned

Here’s a simple example In this case k is the index – the loop is repeated once for each value of k the index can be defined using any of the techniques we’ve learned

While Loops while criterion commands to be executed end While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met.

This loop creates the matrix a, one element at a time

Improving the Efficiency of Loops In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.

The amount of time it takes to run this code will depend on your computer Here’s an example. This code creates a 40,000 element matrix of ones, then multiplies each element in the matrix by pi These two lines of code start a timer to measure the elapsed time required to run the lines of MATLAB code between them

This code accomplishes the same thing with a for loop