Professional Documents
Culture Documents
2023-2-KMU134-L13
2023-2-KMU134-L13
Anonymous functions
Function functions
Global variables
sprintf command
Optional arguments (nargin, nargout, nargchk)
Logical functions
EdaCelik© 164
Anonymous functions
are simple one-line functions created without the need for
an M-file. It provides a means of calling a function indirectly,
and/or defining variables easily.
EdaCelik© 165
Examples
>> sqr = @(x) x.^2; >> b=[2,3,4];
>> a=sqr(5) >> sqr(b) %you can also use it with an array
a= ans =
25 4 9 16
fh = @try1;
fh(2.0) try1(2.0)
ans=
ans =
5.0 5.0
EdaCelik© 167
Example 2 -- Multiple Anonymous Functions
To find the equivalent anonymous function for this expression, step by step:
EdaCelik© 169
Ex*: 3 different ways to use a
function function
>> myint1=quad(@(x) (x.^2 + 2*x + 1), 0, 1)
myint1 = 2.3333
>> f2=@(x) x.^2+2*x+1;
>> myint2=quad(f2,0,1)
myint2 = 2.3333
>> myint3=quad(@f3,0,1) %use @ if it is an .m file
myint3 = 2.3333
function a=f3(x)
a=x .^ 2 + 2 * x + 1;
EdaCelik© 170
* We will learn about the other function functions in KMU231
* Function functions are stored in the funfun folder in the MATLAB folder
in C drive under: toolbox -> compiler -> mcr -> matlab -> funfun
EdaCelik© 171
Global Variables
If you want more than one function to share a single copy of a variable,
simply declare the variable as global in all the functions.
Do the same thing at the command line if you want the base workspace to
access the variable.
The global declaration must occur before the variable is actually used in a
function.
Although it is not required, using capital letters for the names of global
variables helps distinguish them from other variables.
function h = falling(t)
global GRAVITY
h = 1/2*GRAVITY*t.^2;
global GRAVITY
GRAVITY = 32;
y = falling((0:.5:5)');
EdaCelik© 172
KMU 134- Computer Programming
Optional/Variable
Arguments
Optional Arguments /Variable Argument Lists
Many MATLAB functions support optional input arguments and output
arguments.
Ex: plot(Y)
plot(X1,Y1,...)
plot(X1,Y1,LineSpec,...)
plot(...,'PropertyName',PropertyValue,...)
How do MATLAB functions know how many input and output arguments
are present, and how do they adjust their behavior accordingly?
EdaCelik© 174
error—Display error message and abort the function producing the error.
This function is used if the argument errors are fatal.
nargin—This function returns the number of actual input arguments that were
used to call the function.
nargout—This function returns the number of actual output arguments that were
used to call the function.
EdaCelik© 175
nargin/nargout
Number of function input / output arguments
EdaCelik© 176
nargchk
Check number of input arguments. Generates a string containing a
standard error message if a function is called with too few or too many
arguments. The syntax of this function is:
where,
min_args: the minimum number of arguments,
max_args: the maximum number of arguments,
num_args: the actual number of arguments.
>> foo(1,2,3,4)
??? Error using ==> foo
Too many input arguments.
function f = foo(x, y, z)
msg=nargchk(2, 3, nargin);
error(msg)
EdaCelik© 178
Example 5.3
function [mag, angle] = polar_value(x,y)
% POLAR_VALUE Converts (x,y) to (r,theta)
msg = nargchk(1,2,nargin);
error(msg);
if nargin < 2
y = 0;
end
if x == 0 & y == 0
msg = 'Both x any y are zero: angle is meaningless!';
warning(msg);
end
if nargout == 2
angle = atan2(y,x) * 180/pi;
end
angle =
-45
EdaCelik© 180
Example (part of a code)
EdaCelik© 181
Note that a MATLAB function may be declared to have more output
arguments than are actually used, and this is not an error. The function
does not actually have to check nargout to determine if an output
argument is present.
» a = junk(2,1)
a =
3
» [a b] = junk(2,1)
a =
3
b =
1
EdaCelik© 182
Example
function [out1,out2] = humps(x)
if nargin==0,
x = 0:.05:1;
end
if nargout==2,
out1 = x; out2 = y;
else
out1 = y;
end
EdaCelik© 183
fplot(@humps,[0,2]); * Because humps is a M-file,
using inside the function fplot,
q = quad(@humps,0.5,1);
we need the @ sign.
title(['Area = ',num2str(q)]);
* If the function was defined on
the command window, we
wouldn’t need the @ sign, we
would just write the name of the
anonymous function.
a=sprintf('Area= %3.2f',q);
title(a)
OR
title(sprintf('Area= %3.2f',q))
184
Ref: http://www.mathworks.com/ EdaCelik©