Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.

Slides:



Advertisements
Similar presentations
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Advertisements

Lecture 5.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Introduction to C Programming
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.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Chapter 10 Review: Matrix Algebra
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
1 CS161 Introduction to Computer Science Topic #9.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Digital Image Processing Introduction to M-function Programming.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Matlab
Chapter 2 - Introduction to C Programming
Matlab Training Session 4: Control, Flow and Functions
Lecture: MATLAB Chapter 1 Introduction
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
User Defined Functions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions In Matlab.
Chapter 2 - Introduction to C Programming
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Chapter 2 - Introduction to C Programming
Matlab Basics Tutorial
Introduction to C Programming
Presentation transcript:

Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161

Functions Functions were introduced in Chapter 6 of our text. Remember that functions can be thought of as a black box that excepts some inputs, makes some calculations and returns results. MATLAB contains hundreds of built in functions, there are auxiliary toolboxes with specialized functions, and you the user have the ability to write your own functions and use them over and over again. In the next set of slides we review and discuss in more detail the rules governing functions and how to set up your own toolboxes.

Functions: Construction Rules The function M-file name and the function name that appears in the first line of the function must be identical. Function M-file names can have up to 63 characters but they must adhere to the rules governing MATLAB variable names. Function M-file names are case sensitive. Function names must begin with a letter, then any combination or letters, numbers or the underscore character. They must not contain spaces.

Functions: Construction Rules II The first line of a function M-file is called the function declaration line and must contain the word function followed by the calling syntax for that function. The input and output variables identified in the first line are variables local to the function. The input variables contain data passed to the function, the output variables, data passed back to the main program. A set of contiguous comment lines after the function declaration are the help text for the function.

Functions: Construction Rules III Comments lines should describe possible calling sequences, the function’s purpose, algorithms used, and some simple examples as appropriate. Function names appearing in the help text of a function are normally capitalized to give them some visual distinction. All statements following the first set of contiguous comment lines compose the body of the function. These statements operate on the input variables to produce output results.

Functions: Construction Rules IV A function M-file terminates after the last line in the file is executed or when a return statement is encountered. A function can abort operation and return control back to the Command Window by calling the error function, for example, if length (v) > 1; error (‘v must be a scalar’) end aborts the function when v is not a scalar.

Functions: Construction Rules V A function can report a warning and continue execution by calling the warning function. The syntax is the same as for the error function. Function M-files can contain calls to other script M- files. Multiple functions can appear in a single function M- file, but the sub-functions can not be called from outside the M-file. It is suggested that sub-functions in an M-file be given a name beginning with the word local like local_myfun to improve readability.

Functions: Input and Output MATLAB functions can have any number of input and output arguments. Function M-files can have zero input and zero output arguments. Function M-files may have one or more input arguments and one or more output arguments. The built in function nargin and nargout can be used to determine the number of input and output arguments a function has.

Functions: Input and Output II MATLAB provides the ability to pass a variable number of input and output arguments to-from a function by using the varargin and varargout functions. This is more advanced so for our purposes we’ll define our input and output arguments. In general a function declaration line will have the form, function [a, b, c] = function_name(w, x, y, z) where a,b and c are the output arguments, and w,x,y and z are the input arguments. They may be scalars, vectors, or matrices.

Function Workspaces Since functions are black boxes all the variables created in a function including the input and output arguments are local to the function and are not known to the outside calling program. Moreover each function when it executes has its own temporary workspace that is created when the function is called and cleared when the function completes. It is possible to share variables between functions and other functions by declaring the variable to be a global variable within each workspace where the variable will be referenced. This is discouraged, but it is possible to do it if needed. It is recommended to pass variables via the input and output arguments for consistency as we have discussed.

Creating your own toolbox It is common to organize a group of related function M-files into a subdirectory of your MyMatlabfiles folder that is on the MATLAB search path. When you have completed the functions you can create a toolbox subdirectory and include two additional script M-files in the folder, namely the Contents.m and the Readme.m script M-files, both containing only comments. Mytoolbox should be used as the name of the directory containing the group of M-file.

Creating your own toolbox The script file Readme.m contains comment lines that describe the features of the toolbox, any late breaking news items, undocumented features, etc. The script file Contents.m contains comment lines that list all the M-files in the toolbox. Additionally, the first two lines should be %Toolbox Description %Version xxx dd-mmm-yyyy %List all M-files in the toolbox by name.

Functions: An Example Let’s put this all together with an example. Suppose we want to create a function called lengthv that computes the length of a row vector. Remember the length of a vector is determined by computing the sqrt of the sum of the squares of the elements. | x | = sqrt ( x x x n 2 ) Remember length is the name of a built in function, so we will call our function lengthv. Our function has one input argument and one output argument.

Functions: An Example II We will use the built in length function to determine if the input variable is indeed a vector with length greater than 1. If it isn’t, our function will stop and return with an error message. Name of function will be lengthv Input argument is x Output argument is len Error message, ‘Error: Input is not a vector.’

Functions: An Example III Consider, function len = lengthv(x) if length(x) > 1; len = sqrt(sum(x.^2)); else error(‘Error: Input is not a vector.’) end

Functions: An Example V Here when the function is called, the input argument is tested to see if it is indeed a vector. length(x) > 1 indicates that x is a vector and its length can be computed. When length(x) = 1, then x is a scalar and the function returns an error message. To use this function in any main program, simply call the function as you do with any built in function, e.g., >>a = [ ]; >>result = lengthv(a) result = 4

Functions: An Example VI Consider, >>b = 3; >>result = lengthv(b) Error: Input is not a vector. Here, the input to the function lengthv when it is called is a scalar, the function returns the error message. Control returns to the Command Window and the program stops. Remember, when you save your function you must use the file name lengthv.

Functions: Final Thoughts When should one consider using a user defined function? When you find yourself repeating a set of statements several times in a larger program. Define and replace the set of statements as a user defined function and make your program shorter and more readable. When you are writing a number of programs in some field of study, consider developing a set of functions to facilitate your study. Breaking larger programs into a set of smaller ones utilizing functions may make the process of debugging easier.