7
User-defined User-defined Matlab functions Matlab functions

User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Embed Size (px)

Citation preview

Page 1: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

User-defined User-defined Matlab functionsMatlab functions

Page 2: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Creating function m-files with Creating function m-files with a plain text editora plain text editor

MATLAB m-files must be plain text MATLAB m-files must be plain text files. Most word-processors provide files. Most word-processors provide the option of saving the file as plain the option of saving the file as plain text, (look for a ``Save As...'' option text, (look for a ``Save As...'' option in the file menu). When you are in the file menu). When you are writing m-files you will usually want writing m-files you will usually want to have the text editor and MATLAB to have the text editor and MATLAB open at the same time. open at the same time.

Page 3: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Function DefinitionFunction Definition The first line of a function m-file must be of the following form.The first line of a function m-file must be of the following form.

functionfunction [output_parameter_list] = [output_parameter_list] = function_name function_name(input_parameter_list) (input_parameter_list)

The function_name is a character string that will be used to The function_name is a character string that will be used to call the function. The function_name call the function. The function_name must also bemust also be the same  the same as the file name (without the ``.m'') in which the function is as the file name (without the ``.m'') in which the function is stored. stored.

In other words the MATLAB function, ``foo'', must be stored In other words the MATLAB function, ``foo'', must be stored in the file, ``foo.m''. Following the file name is the (optional) in the file, ``foo.m''. Following the file name is the (optional) input_parameter_list.There can exactly be one MATLAB input_parameter_list.There can exactly be one MATLAB function per m-file.function per m-file.

Commenting: MATLAB comment statements begin Commenting: MATLAB comment statements begin with the percent character, %. All characters from with the percent character, %. All characters from the % to the end of the line are treated as a the % to the end of the line are treated as a comment. comment. 

Page 4: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Simple exampleSimple example Here is a trivial function, Here is a trivial function, addtwo.maddtwo.m

function f=addtwo(x,y) function f=addtwo(x,y) % addtwo(x,y) Adds two numbers, ( or vectors) % addtwo(x,y) Adds two numbers, ( or vectors) % whatever, and assign the result to an output variable f% whatever, and assign the result to an output variable f f = x+y; f = x+y; end end

After the file is saved in the current working directory, you After the file is saved in the current working directory, you can use the function, e.g., can use the function, e.g., >> addtwo(2,3) >> addtwo(2,3) ans = ans = 5 5>> addtwo(addtwo(1,2), addtwo(2,3))>> addtwo(addtwo(1,2), addtwo(2,3)) ans = ans = 8 8

Page 5: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Anonymous matlab Anonymous matlab functionsfunctions

We can use anonymous functions in We can use anonymous functions in Matlab to emulate the behavior of Matlab to emulate the behavior of mathematical functions like f(x) = 2x^2 -3mathematical functions like f(x) = 2x^2 -3

The general syntax of an anoymous The general syntax of an anoymous function isfunction is>> fhandle = @(arglist) expr>> fhandle = @(arglist) exprfhandle: function name; fhandle: function name; arglist: the list of the arguments (e.q., x, arglist: the list of the arguments (e.q., x, y..)y..)expr: the body of the function. expr: the body of the function.

Page 6: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Example 1Example 1 Create a function to emulate the behavior Create a function to emulate the behavior

of the mathematical function f(x) = 2x^2 of the mathematical function f(x) = 2x^2 − 3.− 3.

>> f = @(x) 2*x^2 - 3>> f = @(x) 2*x^2 - 3 f =f = @(x) 2*x^2 – 3@(x) 2*x^2 – 3

>> f(3) >> f(3) ans = ans = 15 15

Page 7: User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide

Example 2Example 2

Create a function to emulate the Create a function to emulate the mathematical behavior of the function mathematical behavior of the function defined by defined by f(x, y) = 9 − x^2 − y^2f(x, y) = 9 − x^2 − y^2

>> f = @(x,y) 9 – x^2 – y^2>> f = @(x,y) 9 – x^2 – y^2

>> f(2,3) >> f(2,3) ans = ans = -4 -4