# Advanced use of functions Anonymous functions function handles subfunctions and nested functions.

## Presentation on theme: "Advanced use of functions Anonymous functions function handles subfunctions and nested functions."— Presentation transcript:

Advanced use of functions Anonymous functions function handles subfunctions and nested functions

Function handle Useful as a parameter to other functions Can be considered as an alternate name for a function – but with more capabilities Example: sine_handle = @sin sine_handle(x) has same values as sin(x) for all x

Three ways of plotting sin(x) x = [0 : 0.01 : 2*pi] ; y = sin( x ); plot(x,y) plot( x, sin(x) ) plot( [0 : 0.01 : 2*pi], sin( [0 : 0.01 : 2*pi] )) ; Last method has the advantage that no permanent storage is needed for x and/or y

Function handle continued In last example everything is on one line but it requires writing the interval twice It would be more convient to write gen_plot( function_handle, interval ) The first parameter has to be a function handle and not just the name of a function gen_plot( sin, [0 : 0.01 : 2*pi ] ) does not make sense to Matlab, but the following does gen_plot( sine_handle, [0 : 0.01 : 2*pi] )

Using a function handle When plotting lots of functions it may be useful to have a function with the name gen_plot available function [] = gen_plot( func_handle, interval ) ; plot( interval, func_handle(interval) ) ; The example shows how to pass functions as parameters. gen_plot( sine_handle, [0 : 0.01 : 2*pi] )

Anonymous functions Assume the user needs to work temporarily with the function x 3 +3*x – 1 Instead of writing the function function y = mypoly(x) ; y = x.^3+3*x-1 and storing it as mypoly.m in subdirectory work we can use an anonymous function with the function handle mypoly mypoly = @(x) x.^3+3*x-1

Using anonymous functions With a function handle an anonymous function can be used like any other gen_plot( mypoly, [-10 : 0.01 : 10] ) or try to find a zero near 1.5 fzero( mypoly, 1.5 ) Without the function handle the anonymous function can also be inserted directly as a parameter gen_plot( @(x) x.^3+3*x-1, [-10 : 0.01 : 10] )

More examples f1 = @(x) x + 2* exp(-x) -3 fzero( f1, 0 ) fzero( f1, 1 ) Assume f1 had been defined as a function and kept in f1.m then fzero( f1, 0 ) would be in error Matlab used an alternate method in the past. In order to be backward compatible it is still available, but the use is not recommended: fzero( 'f1', 0 ) fzero( 'sin', 0 ) fzero( 'x.^3', 0 ) need to use default variable name x Use function handles instead!

Commands of Matlab: clear, dir, which, cd, … they can be used with a parameter, i.e. clear functions dir C:\MATLAB_SV701\toolbox\matlab which clear cd E:\work all are builtin functions the parameter is interpreted as a character string. A blank terminates the character string equivalent calls clear('functions') dir('C:\MATLAB_SV701\toolbox\matlab') which('clear') cd(' e:\work')

Remark: Builtin functions can be called like a command median [1,2,100] instead of median([1,2,100]) Matlab gives no warning in the first case and returns 1 [1,2,100] is treated as a character string median('[1,2,100]') also returns 1

Subfunctions, example function [avg,med] = mystat(u) n = length(u) ; avg = mymean( u,n ) ; med = mymedian( u,n ) ; end % function mystat function a = mymean( v,n ) a = sum(v)/n; end % function mymean function m = mymedian( v,n ) ; w = sort(v) ; if rem(n,2) ==1 m = w((n+1)/2) else m = (w(n/2)+ w(n/2+1))/2 ; end end % mymedian

Subfunctions subfunctions are stored in the same file as the main function and can only be called in that file the scope of subfunctions is restricted to the file in which they are defined the example given is for illustration only the example uses modular design, but carries it to an extreme the overhead of calling a function outweighs any benefit in this case if a function mystat has to be written the following would be acceptable

Avoid unnecessary calculations function [avg,med] = mystat2(u) n = length(u) ; avg = sum(u)/n ; if nargout == 2 % only compute if requested w = sort(u) ; if rem(n,2) ==1 med = w((n+1)/2) ; else med = (w(n/2)+ w(n/2+1))/2 ; end end % mystat2

Nested functions main_function nested_function_1 … end % nested_function_1 nested_function_2 … end % nested_function_2 … end % main_function

Nested function When using nested functions all functions need a matching end statement! subfunction versus nested functions nested functions have access to all variables defined in the main function! avoids passing parameters or using global variables For a structured design use subfunctions. Avoid nested functions or use them sparingly