You are on page 1of 21

# LECTURE 4

Calling functions

Function handles

Sub-functions
Functions can be called from within functions

## function z = fun1(x,y) function q = fun2(x,y,z)

w = [1:5];
u = sin(y);

r = fun2(x,u,w) q=

z=
Function handle
f1.m

## Given the function function y = f1(x)

y = x + 2*exp(-x) - 3;

## File f1.m must be stored in your current directory or in one of

the directories in you current path, i.e., when you type

>> f1(3)
ans =
0.0996

Function handle
f1.m

## Given the function function y = f1(x)

y = x + 2*exp(-x) - 3;

## @ symbol function name

random access memory

function f1

>> @f1
random access memory
x function f1
@f1

x is a handle of function f1

>> x = @f1
function y = f1(x)

y = x + 2*exp(-x) - 3;

## >> x = @f1 variable x holds the

x = handle of function f1
@f1

## >> x(3) variable x can now be

ans = used to call function f1

0.099574
How we use function handles
>> help fzero
Using fzero to find zeros of f1
f1.m

function y = f1(x)
Assume that the file f1.m
is stored in your current directory y = x + 2*exp(-x) - 3;

## Matlab syntax: handle to function f1

initial guess for search

>>[x,fval] = fzero(@f1,0)
x = two zeros
-0.5831
fval =
0
Using fzero to find zeros of f1
f1.m
Finding the zeros of
function y = f1(x)

y = x + 2*exp(-x) - 3;

Matlab syntax:
initial guess for search

>>[x,fval] = fzero(@f1,4)
x = two zeros
2.8887
fval =
-4.4409e-016
Function fminbnd
Function fminbnd syntax

## value of x function handle

where fhandle=@myFun
minimum
is found
bounds of the interval where
the search is performed
i.e., x0 x x1

= myFun(xmin)
Using fminbnd
file f1.m stored in current directory

function y = f1(x)

y = x + 2*exp(-x) - 3;

handle to function f1

## >> [xmin,fmin] = fminbnd(@f1,-1,5)

xmin =
0.6931 minimum

fmin =
-1.3069
Subfunctions
Functions can be called from within functions

## function z = fun1(x,y) function q = fun2(x,y,z)

w = [1:5];
u = sin(y);

r = fun2(x,u,w) q=

z=
and a single Matlab M-file can contained them

function z = fun1(x,y)

w = [1:5];
u = sin(y);

r = fun2(x,u,w)

z=
function q = fun2(x,y,z)

q=
Subfunctions
Inafunctiondefinitionmfile,itislegaltohavetwo(ormore)functions
Thefirstfunctionistheprimary function(functionname=filename)
Thesecond,third,etc.functionsaresubfunctions
Eachfunctionusesthesamesyntaxforthefunctiondeclarationline
Itisgoodpracticetostartasubfunction namewithlocal

Subfunctions areonlycallablebyotherfunctionsinsidethefile:
Theprimaryfunctioninthesamefile,and
Anyothersubfunction inthesamefile
However,subfunction handlescanbepassasoutputargumentsoftheprimary
function,thusmakingthemaccessibleifneeded

Allvariablesinasubfunction arelocal
Likefunctions,useinputandoutputargumentstopassvariablesinandout
ofthesubfunction
Example: Without Subfunctions
myfunction.m
function [A,B] = myfunction(C,D) myfolder
A = C + utility1(C,D);
B = [utility2(D) C]; myfunction.m
utility1.m utility1.m
function [out] = utility1(A1,A2) utility2.m
out = utility2(A1) + utility3(A2) utility3.m

utility2.m
function [out] = utility2(A)
% Code ...

utility3.m
function [out] = utility3(A)
% Code ...
Example: With Subfunctions
myfunction.m
function [A,B] = myfunction(C,D) myfolder
A = C + utility1(C,D);
B = [utility2(D) C]; myfunction.m

## function [out] = utility1(A1,A2)

out = utility2(A1) + utility3(A2)

## function [out] = utility2(A)

% Code ... subfunctions

## function [out] = utility3(A)

% Code ...
subex.m
subex returns: function [A,H] = subex(B,C)

primary
A = B + localF(B,C);
A (classdouble) H = @localF;
H (functionhandleto localF)

subfunction
function Y = localF(W,Q)
Y = sqrt(W*Q);

## >> [R,H_localF] = subex(9,4);

>> R
R =
15 H_localF is the handle to localF
>> H_localF(2,8)
ans =
Message: Subfunctions can be accessed
4 anywhere via their handles.
>> localF(2,4)
??? Undefined function or method localF' for input
arguments of type 'double'.
Functioncancallotherfunctions
Matlab functionscanbereferredtobyfunctionhandles
Thefunctionhandlemappingisretainedevenafterthe
functionisoutofscope
Matlab functionsoperateonotherfunctionsandhave
functionshandlesasarguments
Subfunctions:second,third,functionsinafunction
definitionm.file
onlycallablebyotherfunctionsinsidethefile
theirhandlecanbepassedasanoutputargumentoftheprimary
function