0% found this document useful (0 votes)
33 views5 pages

Anonymous Functions Examples

The document discusses anonymous functions in MATLAB. It shows examples of anonymous functions that: 1) Calculate the area of a rectangle using two inputs for height and base. 2) Prints a random number without inputs. 3) Uses the built-in factorial function within an anonymous function. 4) Passes different function handles like @sin, @cos, and @sqrt as inputs to another function. 5) Converts a string to a function handle and passes it as an input. 6) Plots the sine function over a range using an anonymous function.

Uploaded by

Zack Obega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Anonymous Functions Examples

The document discusses anonymous functions in MATLAB. It shows examples of anonymous functions that: 1) Calculate the area of a rectangle using two inputs for height and base. 2) Prints a random number without inputs. 3) Uses the built-in factorial function within an anonymous function. 4) Passes different function handles like @sin, @cos, and @sqrt as inputs to another function. 5) Converts a string to a function handle and passes it as an input. 6) Plots the sine function over a range using an anonymous function.

Uploaded by

Zack Obega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy