Você está na página 1de 5

 

%This anonymous function calculates the area of a rectangle


area_rect=@(height,base) height.*base

area_rect = function_handle with value:


@(height,base)height.*base

%The dot operator makes it flexible to be able to work with a vector or scalars

anonymous function without inputs

%This anonymous only prints a random number


prtran = @() fprintf('%.2f\n' ,rand)

prtran = function_handle with value:


@()fprintf('%.2f\n',rand)

%using the save command


save anonymous area_rect prtran %at the command window
%helps to store simple functions in one file

%dealing with function handles


 
%this example shows that built-in functions can be used in anonymous functions
handle_factorial = @factorial

handle_factorial = function_handle with value:


@factorial

%calling the previous function


handle_factorial(5)

ans = 120

%@sin as input
function_functions(@sin)
%@cos as input
function_functions(@cos)

%@sqrt as input
function_functions(@sqrt)

%@owncumsum_reloaded as an input(
function_functions(@owncumsum_reloaded)

%converting a string into a function


function_functions('sin')

%usint the name of your function as an


function_functions('owncumsum_reloaded')

%@sin as an input
fplot(@sin, [-pi pi])
%defining a variables
fplot(@(x) sin(x - pi/5)) %default is [-5 to 5]
feval(@sin, 3.2)

ans = -0.0584

%our own function


feval(@owncumsum, [2 3 8])

ans =
2 5 13

variable number ofarguments

%using a function area_circle with one input


area_circle(50)

n = 1
ans = 7.8540e+03

 
%using a function area_circle with two input
area_circle(50,i)

n = 2
ans = 1.1310e+06

Você também pode gostar