Download User-Defined Functions and Function Handles in MATLAB and more Slides Computational Methods in PDF only on Docsity!
Chp3 MATLAB
Functions: Part
Functions (ReDeaux)
- MATLAB Has Two Types of Functions
1. Built-In Functions Provided by the
Softeware
2. User-Defined
Functions are .m-files
that can accept InPut
Arguments/Parameters and
Return OutPut Values
Function Handles
- You can create a function handle to any function by using the “at” sign, @, before the function name. You can then name the handle if you wish, and use the handle to reference the function. For example, to create a handle to the sine function, you type >> sine_handle = @sin;
where sine_handle is a user-selected name for the handle (or NickName).
Function Handles cont
- A common use of a function handle is to pass the function as an argument to another function. For example, plot sinx over 0 ≤ x ≤ 6 as follows:
>> plot([0:0.01:6], sine_handle([0:0.01:6]))
The Result
Function Handles cont
- Can Pass a Function with @ sign handle
>> gen_plot(@sech,0:.02:10)
Calling Functions
- There are four ways to invoke, or “call,” a function into action. These are:
- As a character string identifying the appropriate function .m-file
- As a function handle
- As an “inline” function object
- As a string expression.
- Examples of these ways follow for the fzero Built-in fcn which acts on the user-defined function parab, which computes y = x^2 − 4
Calling Functions cont
- As a character string identifying the appropriate function .m-file, which is
The function may be called as follows, to compute the zero over the range: 0 ≤ x ≤ 3 >> [x, value] = fzero('parab', [0 3]) x = 2 value = 0
Calling Functions cont
- As a function handle to an existing function .m-file: >> [z, val0] = fzero(@parab,[0, 3])
- As an “inline” function object:
**>> parab1 = 'x.^2-4';
parab_inline = inline(parab1); [w, val_0] = fzero(parab_inline,[0, 3]) w = 2 val_0 = 0**
Calling Functions cont
- The function handle method (method 2) is the fastest method, followed by method 1.
- In addition to speed improvement, another advantage of using a function handle is that it provides access to subfunctions , which are normally not visible outside of their defining .m- file.
- If we give a “SubFunction” a handle, or nickname, then we can “reach into” the function file to engage the useful SubFcn withOUT using the main function
Types of User Defined Fcns
- The PRIMARY function is the first function in a .m-file and typically contains the main program. Following the primary function in the same file can be any number of subfunctions , which can serve as subroutines that support the primary function.
- That is, .m-files can contain code for more than one function. Additional functions within the file are called subfunctions, and these are only visible to the primary function or to other subfunctions in the same file.
Types of User Defined Fcns
- ANONYMOUS functions enable creation of a simple function withOUT needing to write a separate a .m-file for it.
- You can construct an anonymous function either at the MATLAB command line or from within another function or script.
- Thus, anonymous functions provide a quick way of making a function from any MATLAB expression withOUT the need to create, name, and save a file.
- More on anonymous fcns in a few slides
Types of User Defined Fcns
- SUBFUNCTIONS are placed in the primary function and are called by the primary fcn
- You can use multiple functions within a single primary function m-file
- NESTED functions are functions defined within another function.
- They can help to improve the readability of your program and also give you more flexible access to variables in the .m-file. Produce Nesting “ Levels ”
- The difference between nested functions and subfunctions is that subfunctions normally cannot be accessed outside of their primary function file
Function Function
- The term function function is not a separate
function type but refers to any function that
accepts another function as an input
argument, such as the function fzero or
fminbnd
- You can pass a function to another function
using a function handle.
Anonymous Functions
- The syntax for creating an anonymous function from an expression is fhandle = @(arglist) expr
Where
- The Term arglist is a comma-separated list of input arguments to be passed to the function
- The Term expr is any single , valid MATLAB expression.