User Defined Functions Domain Lecture 3 f Range f x (x1,x2) 8/22/10 Intr Matlab for FM, Fall 2010 f(x) y f = x1+x2 1 Creating Function M‐files • User defined functions are stored as M‐files – Same kind of naming as script m‐files – First line tells Matlab it is a function 8/22/10 Intr Matlab for FM, Fall 2010 2 User‐defined functions must have a function declaration as their first executable statement • The line contains… – The word function – A variable (or variables) that defines the function output – A function name – A variable (or variables) used for the input argument function output = poly(x) 8/22/10 Intr Matlab for FM, Fall 2010 3 Function name The name of the m‐file is the name of the function Style “rule” ‐‐> use the file name as the name you give the function in the ‘function’ line (matlab demo) 8/22/10 Intr Matlab for FM, Fall 2010 4 Workshop (part of a hw problem) We will do this together • Create a function to generate random numbers (uniform on 0 to 1), keeping a sum of them until they add up to a specified value. • Output the number of numbers generated. demo: countEm() 8/22/10 Intr Matlab for FM, Fall 2010 5 A simple function The function name must be the same as the file name 8/22/10 Intr Matlab for FM, Fall 2010 6 The function is available from the command window or from other M‐file programs 8/22/10 Intr Matlab for FM, Fall 2010 7 Style While you are creating a function it may be useful to allow intermediate calculations to print to the command window. However, once you complete your “debugging” make sure that all your output is suppressed. Show only what is needed to facilitate understanding to the user of the function 8/22/10 Intr Matlab for FM, Fall 2010 8 Comments • You should comment functions liberally, just as you would any computer code • The comment lines immediately after the first line are returned when you query the help function • You should describe the purpose of the function and any input and output 8/22/10 Intr Matlab for FM, Fall 2010 9 8/22/10 Intr Matlab for FM, Fall 2010 10 Functions can accept… • numeric values • variables • scalars • arrays 8/22/10 Intr Matlab for FM, Fall 2010 11 Functions with Multiple Inputs and Outputs • Recall the remainder function This function has two inputs 8/22/10 Intr Matlab for FM, Fall 2010 12 A user defined function with multiple inputs 8/22/10 Intr Matlab for FM, Fall 2010 13 Functions with Multiple Outputs • Recall the max function • It returns two results 8/22/10 Intr Matlab for FM, Fall 2010 14 This function return 3 output values If you don’t ask for all three results, the program just returns the first value 8/22/10 Intr Matlab for FM, Fall 2010 15 Recall the size function At first this function looks like it returns two values – but it really only returns a single array with two elements 8/22/10 Intr Matlab for FM, Fall 2010 16 Practice • 6.1, p 189 Work a few, e.g. 1, 2, 3, and 4 Call the solution to 3 function sinP. Look at HW 6 ‐‐ expP • 6.2, p 196 Work a few 8/22/10 Intr Matlab for FM, Fall 2010 17 Functions with no input or no output • It isn’t always necessary to define an output In math terms ‐‐ The range may be the empty set 8/22/10 Intr Matlab for FM, Fall 2010 18 No output is defined Just because a function does not return an output value doesn’t mean it doesn’t do anything. This function draws a star 8/22/10 Intr Matlab for FM, Fall 2010 When you try to set the star function equal to a variable, an error statement is returned 19 Determining the number of input and output arguments • nargin – discovers the number of input arguments • nargout – discovers the number of output arguments 8/22/10 Intr Matlab for FM, Fall 2010 20 You can use these functions in your programming to make your functions more versatile • For example the surf function accepts a variable number of arguments • surf(z) plots the 2‐D matrix z against the index numbers • surf(x,y,z) plots the 2‐D matrix z against the x and y coordinates 8/22/10 Intr Matlab for FM, Fall 2010 21 When a variable number of arguments is allowed… • nargin returns ‐1 8/22/10 Intr Matlab for FM, Fall 2010 22 Checks to see how many output values were requested If star1 is not set equal to a variable the star is drawn but no output is returned If star1 is set equal to a variable, the rhyme is returned 8/22/10 Intr Matlab for FM, Fall 2010 23 Workshop • Create a function to generate random numbers (uniform on 0 to 1), keeping a sum of them until they add up to a specified value. • If one value is requested, – output the number of numbers generated. • If two values are requested, – output the number of numbers generated and the end total Two possible syntax forms [Nnums] = countEm(inVal) [Nnums, total] = countEm(inVal) 8/22/10 Intr Matlab for FM, Fall 2010 24 Local Variables (Workspace) • Variables defined in an M‐file function, only have meaning inside that program • if I set x=1 in the command window, it is not equal to 1 in the function • If I set y=2 in a function, it is not equal to 2 in the workspace window • When a function is invoked, a new workspace is created. • When the function terminates, the function workspace goes away. 8/22/10 Intr Matlab for FM, Fall 2010 25 ans is the only variable created x, y, a, and output are local variables to the g function When the g function is executed, the only variable created is determined in the command window (or script M‐file used to execute a program) 8/22/10 Intr Matlab for FM, Fall 2010 26 Even though g is defined in the workspace, the function can’t access it 8/22/10 If you don’t define g in this function, it won’t work!! Intr Matlab for FM, Fall 2010 27 Global Variables • Although it is possible to define global variables • Changing the value of a global variable can happen anywhere ‐‐ very hard to debug mistakes ***Avoid using global variables*** Demo: adding count to die.m and using countEm() first, add just the one global variable, then make global in countEm 8/22/10 Intr Matlab for FM, Fall 2010 28 Persistent Variables Persistent variables retain their value when the function‐workspace disappears Demo: make count in die.m persistent 8/22/10 Intr Matlab for FM, Fall 2010 29 Toolbox of Functions • Directory/folder nmc is called a toolbox. • Usually functions in a toolbox are connected by a theme, such as all the functions needed for solving ordinary differential equations. • When you call a function MATLAB searches for it along a predetermined path – First it looks in the current directory – Then it follows a search path determined by your installation of the program – we have already used addpath. 8/22/10 Intr Matlab for FM, Fall 2010 30 Define anonymous functions in a script M‐file • Suppose you’d like to define a function for natural log called ln • ln=@(x)log(x) – The @ symbol alerts MATLAB that ln is a function – The function input is next, inside parentheses – Finally the function is defined 8/22/10 Intr Matlab for FM, Fall 2010 31 function definition The name of the function is called a function handle – in this case it is ln Notice that function handles are represented with the box symbol in the workspace window 8/22/10 Intr Matlab for FM, Fall 2010 32 Function Functions • Some functions accept other functions as input • An examples include fplot(), nargin, and nargout. • More on function functions later…. 8/22/10 Intr Matlab for FM, Fall 2010 33 Summary • MATLAB contains a wide variety of built in functions • It also allows you to define your own functions 8/22/10 Intr Matlab for FM, Fall 2010 34 Summary – Function M‐Files • Function M‐files must start with a definition line containing – the word function – a variable that defines the function output (if there is any output) – a function name – a variable used for the input argument (if there is input) 8/22/10 Intr Matlab for FM, Fall 2010 35 Summary – Function M‐files • Function M‐files must be stored in the current directory or in a user defined toolbox • The function name must also be the file name 8/22/10 Intr Matlab for FM, Fall 2010 36 Summary ‐ Comments • Functions should contain ample comments to document the code • The comments directly after the function definition are used by the help feature to describe the function 8/22/10 Intr Matlab for FM, Fall 2010 37 Summary – Anonymous Functions • Anonymous functions are defined in a MATLAB session or M‐file • They only exist during the current session • They are useful as input to function functions 8/22/10 Intr Matlab for FM, Fall 2010 38
© Copyright 2026 Paperzz