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

PDF
ECM Repair Book is out now | Hi-Tech Khanna
PPTX
Lu Decomposition
PPTX
Jacobi iteration method
PPT
Space Vector Modulation(SVM) Technique for PWM Inverter
PDF
Liner algebra-vector space-1 introduction to vector space and subspace
PPTX
Relaxation method
PPTX
MATLAB - Arrays and Matrices
PDF
Power System Modeling and Simulation lab manual
ECM Repair Book is out now | Hi-Tech Khanna
Lu Decomposition
Jacobi iteration method
Space Vector Modulation(SVM) Technique for PWM Inverter
Liner algebra-vector space-1 introduction to vector space and subspace
Relaxation method
MATLAB - Arrays and Matrices
Power System Modeling and Simulation lab manual

What's hot (20)

PPT
Unit 1 graph theory
PPTX
Power system voltage stability
PDF
Cauchy riemann equations
PDF
internship report of 220/132 kV substation
PPT
Slide Mode Control (S.M.C.)
PPTX
ROOT OF NON-LINEAR EQUATIONS
PPT
Power series convergence ,taylor & laurent's theorem
DOC
NUMERICAL METHODS MULTIPLE CHOICE QUESTIONS
PPTX
Numerical analysis (Bisectional method) application
PPT
Synthesis of unsymmetrical phasors from their symmetrical components
PDF
Real time implementation of unscented kalman filter for target tracking
PPTX
Three phase voltage source inverter
PDF
A method for solving quadratic programming problems having linearly factoriz...
PDF
Fadal Power On-Off Operator Manual.pdf
PDF
Wireless Communication Networks and Systems 1st Edition Beard Solutions Manual
PPTX
Mv system neutral grounding
PDF
Multi Pulse Rectifier Using Different Phase Shifting Transformers and its THD...
PDF
Unsymmetrical Fault
PDF
Initial Value Problems
Unit 1 graph theory
Power system voltage stability
Cauchy riemann equations
internship report of 220/132 kV substation
Slide Mode Control (S.M.C.)
ROOT OF NON-LINEAR EQUATIONS
Power series convergence ,taylor & laurent's theorem
NUMERICAL METHODS MULTIPLE CHOICE QUESTIONS
Numerical analysis (Bisectional method) application
Synthesis of unsymmetrical phasors from their symmetrical components
Real time implementation of unscented kalman filter for target tracking
Three phase voltage source inverter
A method for solving quadratic programming problems having linearly factoriz...
Fadal Power On-Off Operator Manual.pdf
Wireless Communication Networks and Systems 1st Edition Beard Solutions Manual
Mv system neutral grounding
Multi Pulse Rectifier Using Different Phase Shifting Transformers and its THD...
Unsymmetrical Fault
Initial Value Problems
Ad

Viewers also liked (20)

PPTX
User Defined Functions in MATLAB Part-4
PPTX
Matlab Script - Loop Control
PPTX
MATLAB Programming - Loop Control Part 2
PPTX
User defined Functions in MATLAB Part 1
PPTX
User Defined Functions in MATLAB part 2
PPTX
MATLAB Scripts - Examples
PDF
Learning C# (Vietnamese)
PPTX
MATLAB programming tips 2 - Input and Output Commands
PPTX
Mat lab workshop
PDF
Matlab lec1
DOC
Jumping statements
DOCX
Matlab time series example
PPTX
Conditional Control in MATLAB Scripts
PPTX
Introduction to Matlab Scripts
PPT
Loops in matlab
PDF
Circuit analysis i with matlab computing and simulink sim powersystems modeling
PPTX
Do While and While Loop
PPTX
Cruise control simulation using matlab
PDF
Reduction of multiple subsystem [compatibility mode]
PPTX
Polynomials and Curve Fitting in MATLAB
User Defined Functions in MATLAB Part-4
Matlab Script - Loop Control
MATLAB Programming - Loop Control Part 2
User defined Functions in MATLAB Part 1
User Defined Functions in MATLAB part 2
MATLAB Scripts - Examples
Learning C# (Vietnamese)
MATLAB programming tips 2 - Input and Output Commands
Mat lab workshop
Matlab lec1
Jumping statements
Matlab time series example
Conditional Control in MATLAB Scripts
Introduction to Matlab Scripts
Loops in matlab
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Do While and While Loop
Cruise control simulation using matlab
Reduction of multiple subsystem [compatibility mode]
Polynomials and Curve Fitting in MATLAB
Ad

Similar to Anonymous and Inline Functions in MATLAB (20)

PDF
User defined functions in matlab
PDF
Matlab functions
PDF
Section-6-User-Defined-Functions.pdf
PPTX
matlab presentation fro engninering students
PDF
PPT
Week 3-Using functions Week 3-Using functions
PPTX
1.1Introduction to matlab.pptx
PDF
MATLAB Programming
PPTX
Matlab Functions
PDF
Lecture 01 variables scripts and operations
PPTX
PDF
Matlab for beginners, Introduction, signal processing
PPT
MatlabIntro (1).ppt
PPT
Matlab
DOCX
MATLAB sessions Laboratory 1MAT 275 Laboratory 1Introdu.docx
PPTX
An Introduction to MATLAB for beginners
PDF
Programming withmatlab
PDF
Malab tutorial
PDF
Matlab tutorial 4
PPT
matlabchapter1.ppt
User defined functions in matlab
Matlab functions
Section-6-User-Defined-Functions.pdf
matlab presentation fro engninering students
Week 3-Using functions Week 3-Using functions
1.1Introduction to matlab.pptx
MATLAB Programming
Matlab Functions
Lecture 01 variables scripts and operations
Matlab for beginners, Introduction, signal processing
MatlabIntro (1).ppt
Matlab
MATLAB sessions Laboratory 1MAT 275 Laboratory 1Introdu.docx
An Introduction to MATLAB for beginners
Programming withmatlab
Malab tutorial
Matlab tutorial 4
matlabchapter1.ppt

Recently uploaded (20)

PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
English Language Teaching from Post-.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Business Ethics Teaching Materials for college
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
English Language Teaching from Post-.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Business Ethics Teaching Materials for college
01-Introduction-to-Information-Management.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf

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