SlideShare a Scribd company logo
Anonymous
and Inline
Functions
Shameer A Koyahttp://electricalenggtutorial.blogspot.com 1
• MATLAB's anonymous functions provide an easy way to
specify a function.
• An anonymous function is a function defined without
using a separate function file
• It is a MATLAB feature that lets you define a
mathematical expression of one or more inputs and
either assign that expression to a function.
• This method is good for relatively simple functions that
will not be used that often and that can be written in a
single expression.
http://electricalenggtutorial.blogspot.com 2
• An anonymous function of any number of variables can
be created by giving the @ symbol, followed by a
variable list, followed by the MATLAB expression.
• Anonymous function can be written in Command
Window, script file, or inside user-defined function.
• It is similar to an Inline Function with some significant
differences.
• Anonymous functions can only have one expression and
can only return a single variable
http://electricalenggtutorial.blogspot.com 3
• To give the anonymous function a name, simply put the
function's name on the left side of an equal sign and the
anonymous function on the right.
• NAME = @(ARGLIST)EXPRESSION
• NAME is name of the function. (using rules for names of user-
defined functions)
• @ - a function handle, an object that has information
about the function
• ARGLIST is the input arguments (a comma-separated
list).
• The body of the function, to the right of the parentheses,
is a single line MATLAB expression.
• Execute the function by calling it by means of the given
name same way as user-defined functions.
http://electricalenggtutorial.blogspot.com 4
>> z = @(x, y) x.^3-y.^3
z =
@(x, y) x.^3-y.^3
>> z( 3, 2)
ans = 19
>> triarea = @(a, h) 1/2*a*h
triarea =
@(a, h) 1/2*a*h
>> triarea(2,2)
ans = 2
>> x = 5;
>> y = 4;
>> area = triarea(x, y)
area = 10
http://electricalenggtutorial.blogspot.com 5
• An anonymous function expression can use a variable
that have been defined before the function is defined.
• Value of the variable when the function is defined will be
used for evaluating the expression.
• The changes made after the expression is defined will
not be reflected in evaluating the function.
• >> f = 5o;
• >> sinewave = @(t) sin( 2*pi*f.*t );
• >> x=[0:0.001:0.2];
• >> y=sinewave(x);
• >> plot(x,y)
http://electricalenggtutorial.blogspot.com 6
• Anonymous function will be unaffected by any variable change
after the function is defined
>> a = 2;
>> b = 3;
>> sample = @(x,y) a*x.^2 + b*y^2;
>> c = sample( 2, 1 )
c = 11
>> a = 5
a = 5
>> b = 0
b = 0
>> c = sample( 2, 1 )
c = 11
http://electricalenggtutorial.blogspot.com 7
• The inline command lets you create a function of any
number of variables by giving a string containing the
function followed by a series of strings denoting the order
of the input variables.
• This method is good for relatively simple functions that
will not be used that often and that can be written in a
single expression.
• It is similar to an Anonymous Function
http://electricalenggtutorial.blogspot.com 8
• Name = inline(‘expression‘)creates an inline
function object from the expression.
• The input arguments are automatically determined by searching the
expression for variable names.
• If no variable exists, 'x' is used.
• The expression to be evaluated is defined in single
quotes, followed in order by the variables of the function
also surrounded by single quotes.
• Name = inline(‘expression', 'arg1', ‘arg2')
http://electricalenggtutorial.blogspot.com 9
>> z = inline('x.^3-y.^3', ‘x', ‘y')
z =
Inline function:
z(x,y) = 'x.^3-y.^3
>> z( 3, 2)
ans = 19
>> sinewave=inline('sin(2*pi*f.*t)')
sinewave =
Inline function:
sinewave(f,t) = sin(2*pi*f.*t)
>>
sinewave=inline('sin(2*pi*f.*t)','f','t');
http://electricalenggtutorial.blogspot.com 10

More Related Content

What's hot

Matlab m files and scripts
Matlab m files and scriptsMatlab m files and scripts
Matlab m files and scripts
Ameen San
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
Rumman Ansari
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
Shiwani Gupta
 
Taylor series
Taylor seriesTaylor series
Taylor series
Milan Bhatiya
 
block diagram reduction solved problems
block diagram reduction solved problemsblock diagram reduction solved problems
block diagram reduction solved problems
Ameya Nijasure
 
weddle's rule
weddle's ruleweddle's rule
weddle's rule
Effa Kiran
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Gaussian Elimination Method
Gaussian Elimination MethodGaussian Elimination Method
Gaussian Elimination Method
Andi Firdaus
 
Gauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial PivotingGauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial Pivoting
SM. Aurnob
 
matlab
matlabmatlab
matlab
Farhan Ahmed
 
Modular programming
Modular programmingModular programming
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
QAU ISLAMABAD,PAKISTAN
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Echelon and reduced echelon form & Filters
Echelon and reduced echelon form & FiltersEchelon and reduced echelon form & Filters
Echelon and reduced echelon form & Filters
Zahid Ali
 
Matlab code for crout method
Matlab code for crout methodMatlab code for crout method
Matlab code for crout method
Taimoor Muzaffar Gondal
 

What's hot (20)

Gauss jordan
Gauss jordanGauss jordan
Gauss jordan
 
Matlab m files and scripts
Matlab m files and scriptsMatlab m files and scripts
Matlab m files and scripts
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
 
Control chap5
Control chap5Control chap5
Control chap5
 
Taylor series
Taylor seriesTaylor series
Taylor series
 
block diagram reduction solved problems
block diagram reduction solved problemsblock diagram reduction solved problems
block diagram reduction solved problems
 
Function
FunctionFunction
Function
 
weddle's rule
weddle's ruleweddle's rule
weddle's rule
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Gaussian Elimination Method
Gaussian Elimination MethodGaussian Elimination Method
Gaussian Elimination Method
 
Gauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial PivotingGauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial Pivoting
 
matlab
matlabmatlab
matlab
 
Modular programming
Modular programmingModular programming
Modular programming
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Chapter 4 (maths 3)
Chapter 4 (maths 3)Chapter 4 (maths 3)
Chapter 4 (maths 3)
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Echelon and reduced echelon form & Filters
Echelon and reduced echelon form & FiltersEchelon and reduced echelon form & Filters
Echelon and reduced echelon form & Filters
 
Matlab code for crout method
Matlab code for crout methodMatlab code for crout method
Matlab code for crout method
 

Viewers also liked

User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
Shameer Ahmed Koya
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
Shameer Ahmed Koya
 
MATLAB Scripts - Examples
MATLAB Scripts - ExamplesMATLAB Scripts - Examples
MATLAB Scripts - Examples
Shameer Ahmed Koya
 
Learning C# (Vietnamese)
Learning C# (Vietnamese)Learning C# (Vietnamese)
Learning C# (Vietnamese)
Nguyen Hung
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
Shameer Ahmed Koya
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
Vinay Kumar
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
 
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
Shameer Ahmed Koya
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlabTUOS-Sam
 
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modelingCircuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Indra S Wahyudi
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
Hock Leng PUAH
 
Cruise control simulation using matlab
Cruise control simulation using matlabCruise control simulation using matlab
Cruise control simulation using matlab
Hira Shaukat
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]azroyyazid
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
 

Viewers also liked (20)

User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
 
MATLAB Scripts - Examples
MATLAB Scripts - ExamplesMATLAB Scripts - Examples
MATLAB Scripts - Examples
 
Learning C# (Vietnamese)
Learning C# (Vietnamese)Learning C# (Vietnamese)
Learning C# (Vietnamese)
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Matlab time series example
Matlab time series exampleMatlab time series example
Matlab time series example
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
 
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
 
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modelingCircuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modeling
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
 
Cruise control simulation using matlab
Cruise control simulation using matlabCruise control simulation using matlab
Cruise control simulation using matlab
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
 

Similar to Anonymous and Inline Functions in MATLAB

Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
Ahmet Bulut
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
Aniket Thakur
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Functions
FunctionsFunctions
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
AkashSingh728626
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ashim Lamichhane
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
Umer Azeem
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
ArraLafuente
 
Mbd dd
Mbd ddMbd dd
DataStructures.pptx
DataStructures.pptxDataStructures.pptx
DataStructures.pptx
Thanuj Pothula
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
arvdexamsection
 
Java8
Java8Java8
Matlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - IMatlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - I
Vijay Kumar Gupta
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 

Similar to Anonymous and Inline Functions in MATLAB (20)

Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Java 8
Java 8Java 8
Java 8
 
Functions
FunctionsFunctions
Functions
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
Mbd dd
Mbd ddMbd dd
Mbd dd
 
DataStructures.pptx
DataStructures.pptxDataStructures.pptx
DataStructures.pptx
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Java8
Java8Java8
Java8
 
Matlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - IMatlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - I
 
Python functions
Python functionsPython functions
Python functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 

Recently uploaded

Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 

Recently uploaded (20)

Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 

Anonymous and Inline Functions in MATLAB

  • 1. Anonymous and Inline Functions Shameer A Koyahttp://electricalenggtutorial.blogspot.com 1
  • 2. • MATLAB's anonymous functions provide an easy way to specify a function. • An anonymous function is a function defined without using a separate function file • It is a MATLAB feature that lets you define a mathematical expression of one or more inputs and either assign that expression to a function. • This method is good for relatively simple functions that will not be used that often and that can be written in a single expression. http://electricalenggtutorial.blogspot.com 2
  • 3. • An anonymous function of any number of variables can be created by giving the @ symbol, followed by a variable list, followed by the MATLAB expression. • Anonymous function can be written in Command Window, script file, or inside user-defined function. • It is similar to an Inline Function with some significant differences. • Anonymous functions can only have one expression and can only return a single variable http://electricalenggtutorial.blogspot.com 3
  • 4. • To give the anonymous function a name, simply put the function's name on the left side of an equal sign and the anonymous function on the right. • NAME = @(ARGLIST)EXPRESSION • NAME is name of the function. (using rules for names of user- defined functions) • @ - a function handle, an object that has information about the function • ARGLIST is the input arguments (a comma-separated list). • The body of the function, to the right of the parentheses, is a single line MATLAB expression. • Execute the function by calling it by means of the given name same way as user-defined functions. http://electricalenggtutorial.blogspot.com 4
  • 5. >> z = @(x, y) x.^3-y.^3 z = @(x, y) x.^3-y.^3 >> z( 3, 2) ans = 19 >> triarea = @(a, h) 1/2*a*h triarea = @(a, h) 1/2*a*h >> triarea(2,2) ans = 2 >> x = 5; >> y = 4; >> area = triarea(x, y) area = 10 http://electricalenggtutorial.blogspot.com 5
  • 6. • An anonymous function expression can use a variable that have been defined before the function is defined. • Value of the variable when the function is defined will be used for evaluating the expression. • The changes made after the expression is defined will not be reflected in evaluating the function. • >> f = 5o; • >> sinewave = @(t) sin( 2*pi*f.*t ); • >> x=[0:0.001:0.2]; • >> y=sinewave(x); • >> plot(x,y) http://electricalenggtutorial.blogspot.com 6
  • 7. • Anonymous function will be unaffected by any variable change after the function is defined >> a = 2; >> b = 3; >> sample = @(x,y) a*x.^2 + b*y^2; >> c = sample( 2, 1 ) c = 11 >> a = 5 a = 5 >> b = 0 b = 0 >> c = sample( 2, 1 ) c = 11 http://electricalenggtutorial.blogspot.com 7
  • 8. • The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. • This method is good for relatively simple functions that will not be used that often and that can be written in a single expression. • It is similar to an Anonymous Function http://electricalenggtutorial.blogspot.com 8
  • 9. • Name = inline(‘expression‘)creates an inline function object from the expression. • The input arguments are automatically determined by searching the expression for variable names. • If no variable exists, 'x' is used. • The expression to be evaluated is defined in single quotes, followed in order by the variables of the function also surrounded by single quotes. • Name = inline(‘expression', 'arg1', ‘arg2') http://electricalenggtutorial.blogspot.com 9
  • 10. >> z = inline('x.^3-y.^3', ‘x', ‘y') z = Inline function: z(x,y) = 'x.^3-y.^3 >> z( 3, 2) ans = 19 >> sinewave=inline('sin(2*pi*f.*t)') sinewave = Inline function: sinewave(f,t) = sin(2*pi*f.*t) >> sinewave=inline('sin(2*pi*f.*t)','f','t'); http://electricalenggtutorial.blogspot.com 10