Advanced use of functions l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Advanced use of functions PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Advanced use of functions l.jpg

0

Advanced use of functions

Anonymous functions

function handles

subfunctions

and

nested functions


Function handle l.jpg

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


Three ways of plotting sin x l.jpg

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


Function handle continued l.jpg

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] )


Using a function handle l.jpg

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] )


Anonymous functions l.jpg

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


Using anonymous functions l.jpg

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] )


More examples l.jpg

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!


  • Commands of matlab clear dir which cd l.jpg

    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')


    Remark l.jpg

    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


    Subfunctions example l.jpg

    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


    Subfunctions l.jpg

    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


    Avoid unnecessary calculations l.jpg

    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


    Nested functions l.jpg

    Nested functions

    0

    • main_function

    • nested_function_1

    • end % nested_function_1

    • nested_function_2

    • end % nested_function_2

    • end % main_function


    Nested function l.jpg

    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


  • Login