More Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Matlab
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
MATLAB Functions – Part II Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013.
Visualization Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Ordinary Differential Equations. Outline Announcements: –Homework II: Solutions on web –Homework III: due Wed. by 5, by Homework II Differential.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
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.
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
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.
Eigen Values Andras Zakupszki Nuttapon Pichetpongsa Inderjeet Singh Surat Wanamkang.
Today’s class Boundary Value Problems Eigenvalue Problems
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.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Lecture 1 - Introduction June 3, 2002 CVEN 302. Lecture’s Goals General Introduction to CVEN Computer Applications in Engineering and Construction.
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.
Functions CS 103 March 3, Review A function is a set of code we can execute on command to perform a specific task A function is a set of code we.
Functions CS 103. Review A function is a set of code we can execute on command to perform a specific task When we call a function, we can pass arguments.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
CY3A2 System identification Assignment: The assignment has three parts, all relating.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Solution of a Partial Differential Equations using the Method of Lines
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Working with Arrays in MATLAB
Homogeneous Linear Systems with Constant Coefficients Solutions of Systems of ODEs.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
What is the determinant of What is the determinant of
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
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 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Introduction to Matlab
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 27.
Fall 2006AE6382 Design Computing1 Advanced Matrix Applications in Matlab Now we’ll show a few relatively simple examples to illustrate some of the powerful.
Signal & Weight Vector Spaces
Section 1.7 Linear Independence and Nonsingular Matrices
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
1/15 Fun Matrix Facts and Tricks Ben G. Fitzpatrick Department of Mathematics Loyola Marymount University One LMU Drive.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Review of Eigenvectors and Eigenvalues from CliffsNotes Online mining-the-Eigenvectors-of-a- Matrix.topicArticleId-20807,articleId-
More File Input and Output
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
User Defined Functions
Use of Mathematics using Technology (Maltlab)
Navya Thum February 13, 2013 Day 7: MICROSOFT EXCEL Navya Thum February 13, 2013.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
funCTIONs and Data Import/Export
MATLAB Programming Basics Copyright © Software Carpentry 2011
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.
Announcements.
Presentation transcript:

More Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information. MATLAB Programming

MATLABMore Flow Control MATLAB is a programming language Flow control: if – else for while Functions

MATLABMore Flow Control MATLAB is a programming language Flow control: if – else for while Functions encapsulation logical independence

MATLABMore Flow Control Program is a set of tasks: Load data Perform calculation Display result

MATLABMore Flow Control Program is a set of tasks: Load data Get file name Read Test for correct format Perform calculation Prepare for computation Perform several independent subtasks Combine into a result …

MATLABMore Flow Control Main Workspace Variables X = 2 data = [2 3; 4 5]; Z = ‘file.txt’ … function cleanData = cleanMyData(data) Variables data = [2 3; …]; size = [4 2]; … cleanMyData(data); cleanData

MATLABMore Flow Control Example: The eigenvectors and eigenvalues of a square matrix are the set of vectors and associated scalars that satisfy the following equation: M * v = λ * v Matrix M times vector v

MATLABMore Flow Control Example: The eigenvectors and eigenvalues of a square matrix are the set of vectors and associated scalars that satisfy the following equation: M * v = λ * v Scalar λ times vector v

MATLABMore Flow Control Eigenvectors in MATLAB: A = [1 2; 3 4]; U = eig(A) U =

MATLABMore Flow Control Eigenvalues and eigenvectors: [U V] = eig(A) U V Is this a good idea?

MATLABMore Flow Control Two special functions: nargin() # How many parameters were passed? nargout() # How many return values are expected?

MATLABMore Flow Control Main Workspace Variables X = 2 data = [2 3; 4 5]; Z = ‘file.txt’ … function cleanData = cleanMyData(data) Variables data = [2 3; …]; size = [4 2]; … cleanMyData(data); cleanData nargout = 1

MATLABMore Flow Control Interpolation example: Time series data... With some bad measurements... That are 0.0.

MATLABMore Flow Control Interpolation example: Time series data... With some bad measurements... That are either 0.0 or (optionally) user specified… … and that could be greater than an optional max. Report the number of values that are replaced. Report the locations of all values that are replaced.

MATLABMore Flow Control MATLAB programming convention: The first return value should be the result of the calculation Subsequent, optional return values status variables. Example: max/min - return the actual max or min and the optional index. ode45 (differential equation solver) - return the time points and the actual solution then optional trajectory information.

MATLABMore Flow Control function clean_data = interpolate(data) function [clean_data, num_replaced, replace_locs] = interpolate(data, min, max) result, status1statusN Increasing detail…

MATLABMore Flow Control function [clean_data, num_replaced, replace_locs] = interpolate(data, min, max) if nargin() < 2 min = 0; end [I, J] = find( data < min );

MATLABMore Flow Control function [clean_data, num_replaced, replace_locs] = interpolate(data, min, max) if nargin() < 2 min = 0; end [I, J] = find( data < min ); if nargin() == 3 [Imax, Jmax] = find(data > max); I = [I ; Imax]; J = [J ; Jmax]; end

MATLABMore Flow Control ( Clean the data and put it in clean_data) if nargout() >= 2 num_replaced = length(I); end if nargout() == 3 replaced_locations = [I J]; end clean_data = data;

MATLABMore Flow Control Functions encapsulate things: Separate logical unit from the rest of the code Separate variable names to avoid confusion Multiple parameters and returns. nargin() nargout()

February 2011 created by Richard T. Guy Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information.