Download presentation

Presentation is loading. Please wait.

Published byWilliam Hanchett Modified over 2 years ago

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

2
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

3
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

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

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

6
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

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

8
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!

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

10
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

11
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

12
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

13
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

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

15
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

Similar presentations

OK

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on biodegradable and non biodegradable examples Ppt on event driven programming Ppt on bond length definition Ppt on festivals of kerala Ppt on limits and continuity problems Ppt on underwater wireless communication Ppt on business plan of restaurant Ppt on pin diodes Ppt on meeting skills Ppt on second law of thermodynamics and entropy