illustration of anonymous functionsdissimilar


Illustration of anonymous functions:

Dissimilar functions stored in the M-files, when no argument is passed to an anonymous function, the parentheses should still be in the function definition and in the function call. For illustration, here is an anonymous function which prints an arbitrary real number with two decimal positions, and also a call to this function:

 

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

>> prtran()

0.95

 

Just typing the name of the function handle will show its contents, that is the function definition.

>> prtran

prtran =

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

 

This is why to call a function, even though no arguments are passed, the parentheses should be used.

To save the anonymous function, it can be saved to a MAT-file, and then it can be loaded when required.

 

>> cirarea = @ (radius) pi * radius .^2;

>> save anonfns cirarea

>> clear

>> load anonfns

>> who

Your variables are:

cirarea

>> cirarea

cirarea =

@ (radius) pi * radius .^2

 

The other anonymous functions can be appended to this MAT-file. However, a benefit of anonymous functions is that they do not have to be saved in separate M-files; it is often helpful to save groups of associated anonymous functions in a MAT-file. If there are several anonymous functions which are often used, they can be saved in a MAT-file and then loaded from this MAT-file in every MATLAB Command Window.

Request for Solution File

Ask an Expert for Answer!!
Applications of MATLAB: illustration of anonymous functionsdissimilar
Reference No:- TGS0175142

Expected delivery within 24 Hours