1 / 15

Advanced use of functions

0. Advanced use of functions. Anonymous functions function handles subfunctions and nested functions. Function handle. 0. 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

makara
Télécharger la présentation

Advanced use of functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 0 Advanced use of functions Anonymous functions function handles subfunctions and nested functions

  2. Function handle 0 • 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

  3. Three ways of plotting sin(x) 0 • 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

  4. Function handle continued 0 • 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] )

  5. Using a function handle 0 • 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] )

  6. Anonymous functions 0 Assume the user needs to work temporarily with the function x3+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

  7. Using anonymous functions 0 • 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] )

  8. More examples 0 • 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!

  9. Commands of Matlab: clear, dir, which, cd, … 0 • 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')

  10. Remark: 0 • 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

  11. Subfunctions, example 0 • 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

  12. Subfunctions 0 • 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

  13. Avoid unnecessary calculations 0 • 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 • end % mystat2

  14. Nested functions 0 • main_function • nested_function_1 • … • end % nested_function_1 • nested_function_2 • … • end % nested_function_2 • … • … • end % main_function

  15. Nested function 0 • 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

More Related